pdo_011.phpt 5.3 KB

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