pdo_011.phpt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. --TEST--
  2. PDO Common: PDO::FETCH_FUNC and statement overloading
  3. --SKIPIF--
  4. <?php # vim:ft=php
  5. if (!extension_loaded('pdo')) die('skip');
  6. $dir = getenv('REDIR_TEST_DIR');
  7. if (false == $dir) die('skip no driver');
  8. require_once $dir . 'pdo_test.inc';
  9. PDOTest::skip();
  10. ?>
  11. --FILE--
  12. <?php
  13. if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
  14. require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
  15. $db = PDOTest::factory();
  16. $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val VARCHAR(10), grp VARCHAR(10))');
  17. $db->exec('INSERT INTO test VALUES(1, \'A\', \'Group1\')');
  18. $db->exec('INSERT INTO test VALUES(2, \'B\', \'Group1\')');
  19. $db->exec('INSERT INTO test VALUES(3, \'C\', \'Group2\')');
  20. $db->exec('INSERT INTO test VALUES(4, \'D\', \'Group2\')');
  21. class DerivedStatement extends PDOStatement
  22. {
  23. private function __construct($name, $db)
  24. {
  25. $this->name = $name;
  26. echo __METHOD__ . "($name)\n";
  27. }
  28. function reTrieve($id, $val) {
  29. echo __METHOD__ . "($id,$val)\n";
  30. return array($id=>$val);
  31. }
  32. }
  33. $select1 = $db->prepare('SELECT grp, id FROM test');
  34. $select2 = $db->prepare('SELECT id, val FROM test');
  35. $derived = $db->prepare('SELECT id, val FROM test', array(PDO::ATTR_STATEMENT_CLASS=>array('DerivedStatement', array('Overloaded', $db))));
  36. class Test1
  37. {
  38. public function __construct($id, $val)
  39. {
  40. echo __METHOD__ . "($id,$val)\n";
  41. $this->id = $id;
  42. $this->val = $val;
  43. }
  44. static public function factory($id, $val)
  45. {
  46. echo __METHOD__ . "($id,$val)\n";
  47. return new self($id, $val);
  48. }
  49. }
  50. function test($id,$val='N/A')
  51. {
  52. echo __METHOD__ . "($id,$val)\n";
  53. return array($id=>$val);
  54. }
  55. $f = new Test1(0,0);
  56. $select1->execute();
  57. var_dump($select1->fetchAll(PDO::FETCH_FUNC|PDO::FETCH_GROUP, 'test'));
  58. $select2->execute();
  59. var_dump($select2->fetchAll(PDO::FETCH_FUNC, 'test'));
  60. $select2->execute();
  61. var_dump($select2->fetchAll(PDO::FETCH_FUNC, array('Test1','factory')));
  62. $select2->execute();
  63. var_dump($select2->fetchAll(PDO::FETCH_FUNC, array($f, 'factory')));
  64. var_dump(get_class($derived));
  65. $derived->execute();
  66. var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'retrieve')));
  67. $derived->execute();
  68. var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'reTrieve')));
  69. $derived->execute();
  70. var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'RETRIEVE')));
  71. ?>
  72. --EXPECTF--
  73. DerivedStatement::__construct(Overloaded)
  74. Test1::__construct(0,0)
  75. test(1,N/A)
  76. test(2,N/A)
  77. test(3,N/A)
  78. test(4,N/A)
  79. array(2) {
  80. ["Group1"]=>
  81. array(2) {
  82. [0]=>
  83. array(1) {
  84. [1]=>
  85. string(3) "N/A"
  86. }
  87. [1]=>
  88. array(1) {
  89. [2]=>
  90. string(3) "N/A"
  91. }
  92. }
  93. ["Group2"]=>
  94. array(2) {
  95. [0]=>
  96. array(1) {
  97. [3]=>
  98. string(3) "N/A"
  99. }
  100. [1]=>
  101. array(1) {
  102. [4]=>
  103. string(3) "N/A"
  104. }
  105. }
  106. }
  107. test(1,A)
  108. test(2,B)
  109. test(3,C)
  110. test(4,D)
  111. array(4) {
  112. [0]=>
  113. array(1) {
  114. [1]=>
  115. string(1) "A"
  116. }
  117. [1]=>
  118. array(1) {
  119. [2]=>
  120. string(1) "B"
  121. }
  122. [2]=>
  123. array(1) {
  124. [3]=>
  125. string(1) "C"
  126. }
  127. [3]=>
  128. array(1) {
  129. [4]=>
  130. string(1) "D"
  131. }
  132. }
  133. Test1::factory(1,A)
  134. Test1::__construct(1,A)
  135. Test1::factory(2,B)
  136. Test1::__construct(2,B)
  137. Test1::factory(3,C)
  138. Test1::__construct(3,C)
  139. Test1::factory(4,D)
  140. Test1::__construct(4,D)
  141. array(4) {
  142. [0]=>
  143. object(Test1)#%d (2) {
  144. ["id"]=>
  145. string(1) "1"
  146. ["val"]=>
  147. string(1) "A"
  148. }
  149. [1]=>
  150. object(Test1)#%d (2) {
  151. ["id"]=>
  152. string(1) "2"
  153. ["val"]=>
  154. string(1) "B"
  155. }
  156. [2]=>
  157. object(Test1)#%d (2) {
  158. ["id"]=>
  159. string(1) "3"
  160. ["val"]=>
  161. string(1) "C"
  162. }
  163. [3]=>
  164. object(Test1)#%d (2) {
  165. ["id"]=>
  166. string(1) "4"
  167. ["val"]=>
  168. string(1) "D"
  169. }
  170. }
  171. Test1::factory(1,A)
  172. Test1::__construct(1,A)
  173. Test1::factory(2,B)
  174. Test1::__construct(2,B)
  175. Test1::factory(3,C)
  176. Test1::__construct(3,C)
  177. Test1::factory(4,D)
  178. Test1::__construct(4,D)
  179. array(4) {
  180. [0]=>
  181. object(Test1)#%d (2) {
  182. ["id"]=>
  183. string(1) "1"
  184. ["val"]=>
  185. string(1) "A"
  186. }
  187. [1]=>
  188. object(Test1)#%d (2) {
  189. ["id"]=>
  190. string(1) "2"
  191. ["val"]=>
  192. string(1) "B"
  193. }
  194. [2]=>
  195. object(Test1)#%d (2) {
  196. ["id"]=>
  197. string(1) "3"
  198. ["val"]=>
  199. string(1) "C"
  200. }
  201. [3]=>
  202. object(Test1)#%d (2) {
  203. ["id"]=>
  204. string(1) "4"
  205. ["val"]=>
  206. string(1) "D"
  207. }
  208. }
  209. string(16) "DerivedStatement"
  210. DerivedStatement::reTrieve(1,A)
  211. DerivedStatement::reTrieve(2,B)
  212. DerivedStatement::reTrieve(3,C)
  213. DerivedStatement::reTrieve(4,D)
  214. array(4) {
  215. [0]=>
  216. array(1) {
  217. [1]=>
  218. string(1) "A"
  219. }
  220. [1]=>
  221. array(1) {
  222. [2]=>
  223. string(1) "B"
  224. }
  225. [2]=>
  226. array(1) {
  227. [3]=>
  228. string(1) "C"
  229. }
  230. [3]=>
  231. array(1) {
  232. [4]=>
  233. string(1) "D"
  234. }
  235. }
  236. DerivedStatement::reTrieve(1,A)
  237. DerivedStatement::reTrieve(2,B)
  238. DerivedStatement::reTrieve(3,C)
  239. DerivedStatement::reTrieve(4,D)
  240. array(4) {
  241. [0]=>
  242. array(1) {
  243. [1]=>
  244. string(1) "A"
  245. }
  246. [1]=>
  247. array(1) {
  248. [2]=>
  249. string(1) "B"
  250. }
  251. [2]=>
  252. array(1) {
  253. [3]=>
  254. string(1) "C"
  255. }
  256. [3]=>
  257. array(1) {
  258. [4]=>
  259. string(1) "D"
  260. }
  261. }
  262. DerivedStatement::reTrieve(1,A)
  263. DerivedStatement::reTrieve(2,B)
  264. DerivedStatement::reTrieve(3,C)
  265. DerivedStatement::reTrieve(4,D)
  266. array(4) {
  267. [0]=>
  268. array(1) {
  269. [1]=>
  270. string(1) "A"
  271. }
  272. [1]=>
  273. array(1) {
  274. [2]=>
  275. string(1) "B"
  276. }
  277. [2]=>
  278. array(1) {
  279. [3]=>
  280. string(1) "C"
  281. }
  282. [3]=>
  283. array(1) {
  284. [4]=>
  285. string(1) "D"
  286. }
  287. }