copy_from.phpt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. --TEST--
  2. PDO PgSQL pgsqlCopyFromArray and pgsqlCopyFromFile
  3. --EXTENSIONS--
  4. pdo
  5. pdo_pgsql
  6. --SKIPIF--
  7. <?php
  8. require __DIR__ . '/config.inc';
  9. require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
  10. PDOTest::skip();
  11. ?>
  12. --FILE--
  13. <?php
  14. require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
  15. $db = PDOTest::test_factory(__DIR__ . '/common.phpt');
  16. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
  18. $db->exec('CREATE TABLE test (a integer not null primary key, b text, c integer)');
  19. echo "Preparing test file and array for CopyFrom tests\n";
  20. $tableRows = array();
  21. $tableRowsWithDifferentNullValues = array();
  22. for($i=0;$i<3;$i++) {
  23. $firstParameter = $i;
  24. $secondParameter = "test insert {$i}";
  25. $tableRows[] = "{$firstParameter}\t{$secondParameter}\t\\N";
  26. $tableRowsWithDifferentNullValues[] = "{$firstParameter};{$secondParameter};NULL";
  27. $tableRowsWithDifferentNullValuesAndSelectedFields[] = "{$firstParameter};NULL";
  28. }
  29. $filename = 'test_pgsqlCopyFromFile.csv';
  30. $filenameWithDifferentNullValues = 'test_pgsqlCopyFromFileWithDifferentNullValues.csv';
  31. $filenameWithDifferentNullValuesAndSelectedFields = 'test_pgsqlCopyFromFileWithDifferentNullValuesAndSelectedFields.csv';
  32. file_put_contents($filename, implode("\n",$tableRows));
  33. file_put_contents($filenameWithDifferentNullValues, implode("\n",$tableRowsWithDifferentNullValues));
  34. file_put_contents($filenameWithDifferentNullValuesAndSelectedFields, implode("\n",$tableRowsWithDifferentNullValuesAndSelectedFields));
  35. echo "Testing pgsqlCopyFromArray() with default parameters\n";
  36. $db->beginTransaction();
  37. var_dump($db->pgsqlCopyFromArray('test',$tableRows));
  38. $stmt = $db->query("select * from test");
  39. foreach($stmt as $r) {
  40. var_dump($r);
  41. }
  42. $db->rollback();
  43. echo "Testing pgsqlCopyFromArray() with different field separator and not null indicator\n";
  44. $db->beginTransaction();
  45. var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValues,";","NULL"));
  46. $stmt = $db->query("select * from test");
  47. foreach($stmt as $r) {
  48. var_dump($r);
  49. }
  50. $db->rollback();
  51. echo "Testing pgsqlCopyFromArray() with only selected fields\n";
  52. $db->beginTransaction();
  53. var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
  54. $stmt = $db->query("select * from test");
  55. foreach($stmt as $r) {
  56. var_dump($r);
  57. }
  58. $db->rollback();
  59. echo "Testing pgsqlCopyFromArray() with error\n";
  60. $db->beginTransaction();
  61. try {
  62. var_dump($db->pgsqlCopyFromArray('test_error',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
  63. } catch (Exception $e) {
  64. echo "Exception: {$e->getMessage()}\n";
  65. }
  66. $db->rollback();
  67. echo "Testing pgsqlCopyFromFile() with default parameters\n";
  68. $db->beginTransaction();
  69. var_dump($db->pgsqlCopyFromFile('test',$filename));
  70. $stmt = $db->query("select * from test");
  71. foreach($stmt as $r) {
  72. var_dump($r);
  73. }
  74. $db->rollback();
  75. echo "Testing pgsqlCopyFromFile() with different field separator and not null indicator\n";
  76. $db->beginTransaction();
  77. var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValues,";","NULL"));
  78. $stmt = $db->query("select * from test");
  79. foreach($stmt as $r) {
  80. var_dump($r);
  81. }
  82. $db->rollback();
  83. echo "Testing pgsqlCopyFromFile() with only selected fields\n";
  84. $db->beginTransaction();
  85. var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
  86. $stmt = $db->query("select * from test");
  87. foreach($stmt as $r) {
  88. var_dump($r);
  89. }
  90. $db->rollback();
  91. echo "Testing pgsqlCopyFromFile() with error\n";
  92. $db->beginTransaction();
  93. try {
  94. var_dump($db->pgsqlCopyFromFile('test_error',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
  95. } catch (Exception $e) {
  96. echo "Exception: {$e->getMessage()}\n";
  97. }
  98. $db->rollback();
  99. echo "Testing pgsqlCopyFromFile() with non existing file\n";
  100. $db->beginTransaction();
  101. try {
  102. var_dump($db->pgsqlCopyFromFile('test',"nonexisting/foo.csv",";","NULL",'a,c'));
  103. } catch (Exception $e) {
  104. echo "Exception: {$e->getMessage()}\n";
  105. }
  106. $db->rollback();
  107. // Clean up
  108. foreach (array($filename, $filenameWithDifferentNullValues, $filenameWithDifferentNullValuesAndSelectedFields) as $f) {
  109. @unlink($f);
  110. }
  111. ?>
  112. --EXPECTF--
  113. Preparing test file and array for CopyFrom tests
  114. Testing pgsqlCopyFromArray() with default parameters
  115. bool(true)
  116. array(6) {
  117. ["a"]=>
  118. int(0)
  119. [0]=>
  120. int(0)
  121. ["b"]=>
  122. string(13) "test insert 0"
  123. [1]=>
  124. string(13) "test insert 0"
  125. ["c"]=>
  126. NULL
  127. [2]=>
  128. NULL
  129. }
  130. array(6) {
  131. ["a"]=>
  132. int(1)
  133. [0]=>
  134. int(1)
  135. ["b"]=>
  136. string(13) "test insert 1"
  137. [1]=>
  138. string(13) "test insert 1"
  139. ["c"]=>
  140. NULL
  141. [2]=>
  142. NULL
  143. }
  144. array(6) {
  145. ["a"]=>
  146. int(2)
  147. [0]=>
  148. int(2)
  149. ["b"]=>
  150. string(13) "test insert 2"
  151. [1]=>
  152. string(13) "test insert 2"
  153. ["c"]=>
  154. NULL
  155. [2]=>
  156. NULL
  157. }
  158. Testing pgsqlCopyFromArray() with different field separator and not null indicator
  159. bool(true)
  160. array(6) {
  161. ["a"]=>
  162. int(0)
  163. [0]=>
  164. int(0)
  165. ["b"]=>
  166. string(13) "test insert 0"
  167. [1]=>
  168. string(13) "test insert 0"
  169. ["c"]=>
  170. NULL
  171. [2]=>
  172. NULL
  173. }
  174. array(6) {
  175. ["a"]=>
  176. int(1)
  177. [0]=>
  178. int(1)
  179. ["b"]=>
  180. string(13) "test insert 1"
  181. [1]=>
  182. string(13) "test insert 1"
  183. ["c"]=>
  184. NULL
  185. [2]=>
  186. NULL
  187. }
  188. array(6) {
  189. ["a"]=>
  190. int(2)
  191. [0]=>
  192. int(2)
  193. ["b"]=>
  194. string(13) "test insert 2"
  195. [1]=>
  196. string(13) "test insert 2"
  197. ["c"]=>
  198. NULL
  199. [2]=>
  200. NULL
  201. }
  202. Testing pgsqlCopyFromArray() with only selected fields
  203. bool(true)
  204. array(6) {
  205. ["a"]=>
  206. int(0)
  207. [0]=>
  208. int(0)
  209. ["b"]=>
  210. NULL
  211. [1]=>
  212. NULL
  213. ["c"]=>
  214. NULL
  215. [2]=>
  216. NULL
  217. }
  218. array(6) {
  219. ["a"]=>
  220. int(1)
  221. [0]=>
  222. int(1)
  223. ["b"]=>
  224. NULL
  225. [1]=>
  226. NULL
  227. ["c"]=>
  228. NULL
  229. [2]=>
  230. NULL
  231. }
  232. array(6) {
  233. ["a"]=>
  234. int(2)
  235. [0]=>
  236. int(2)
  237. ["b"]=>
  238. NULL
  239. [1]=>
  240. NULL
  241. ["c"]=>
  242. NULL
  243. [2]=>
  244. NULL
  245. }
  246. Testing pgsqlCopyFromArray() with error
  247. Exception: SQLSTATE[42P01]: Undefined table: 7 %s: %stest_error%s
  248. Testing pgsqlCopyFromFile() with default parameters
  249. bool(true)
  250. array(6) {
  251. ["a"]=>
  252. int(0)
  253. [0]=>
  254. int(0)
  255. ["b"]=>
  256. string(13) "test insert 0"
  257. [1]=>
  258. string(13) "test insert 0"
  259. ["c"]=>
  260. NULL
  261. [2]=>
  262. NULL
  263. }
  264. array(6) {
  265. ["a"]=>
  266. int(1)
  267. [0]=>
  268. int(1)
  269. ["b"]=>
  270. string(13) "test insert 1"
  271. [1]=>
  272. string(13) "test insert 1"
  273. ["c"]=>
  274. NULL
  275. [2]=>
  276. NULL
  277. }
  278. array(6) {
  279. ["a"]=>
  280. int(2)
  281. [0]=>
  282. int(2)
  283. ["b"]=>
  284. string(13) "test insert 2"
  285. [1]=>
  286. string(13) "test insert 2"
  287. ["c"]=>
  288. NULL
  289. [2]=>
  290. NULL
  291. }
  292. Testing pgsqlCopyFromFile() with different field separator and not null indicator
  293. bool(true)
  294. array(6) {
  295. ["a"]=>
  296. int(0)
  297. [0]=>
  298. int(0)
  299. ["b"]=>
  300. string(13) "test insert 0"
  301. [1]=>
  302. string(13) "test insert 0"
  303. ["c"]=>
  304. NULL
  305. [2]=>
  306. NULL
  307. }
  308. array(6) {
  309. ["a"]=>
  310. int(1)
  311. [0]=>
  312. int(1)
  313. ["b"]=>
  314. string(13) "test insert 1"
  315. [1]=>
  316. string(13) "test insert 1"
  317. ["c"]=>
  318. NULL
  319. [2]=>
  320. NULL
  321. }
  322. array(6) {
  323. ["a"]=>
  324. int(2)
  325. [0]=>
  326. int(2)
  327. ["b"]=>
  328. string(13) "test insert 2"
  329. [1]=>
  330. string(13) "test insert 2"
  331. ["c"]=>
  332. NULL
  333. [2]=>
  334. NULL
  335. }
  336. Testing pgsqlCopyFromFile() with only selected fields
  337. bool(true)
  338. array(6) {
  339. ["a"]=>
  340. int(0)
  341. [0]=>
  342. int(0)
  343. ["b"]=>
  344. NULL
  345. [1]=>
  346. NULL
  347. ["c"]=>
  348. NULL
  349. [2]=>
  350. NULL
  351. }
  352. array(6) {
  353. ["a"]=>
  354. int(1)
  355. [0]=>
  356. int(1)
  357. ["b"]=>
  358. NULL
  359. [1]=>
  360. NULL
  361. ["c"]=>
  362. NULL
  363. [2]=>
  364. NULL
  365. }
  366. array(6) {
  367. ["a"]=>
  368. int(2)
  369. [0]=>
  370. int(2)
  371. ["b"]=>
  372. NULL
  373. [1]=>
  374. NULL
  375. ["c"]=>
  376. NULL
  377. [2]=>
  378. NULL
  379. }
  380. Testing pgsqlCopyFromFile() with error
  381. Exception: SQLSTATE[42P01]: Undefined table: 7 %s: %stest_error%s
  382. Testing pgsqlCopyFromFile() with non existing file
  383. Exception: SQLSTATE[HY000]: General error: 7 Unable to open the file