copy_from.phpt 7.5 KB

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