DbiDummy.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Fake database driver for testing purposes
  5. *
  6. * It has hardcoded results for given queries what makes easy to use it
  7. * in testsuite. Feel free to include other queries which your test will
  8. * need.
  9. *
  10. * @package PhpMyAdmin-DBI
  11. * @subpackage Dummy
  12. */
  13. namespace PhpMyAdmin\Dbi;
  14. require_once 'libraries/dbi/dbi_dummy.inc.php';
  15. /**
  16. * Fake database driver for testing purposes
  17. *
  18. * It has hardcoded results for given queries what makes easy to use it
  19. * in testsuite. Feel free to include other queries which your test will
  20. * need.
  21. *
  22. * @package PhpMyAdmin-DBI
  23. * @subpackage Dummy
  24. */
  25. class DbiDummy implements DbiExtension
  26. {
  27. private $_queries = array();
  28. const OFFSET_GLOBAL = 1000;
  29. /**
  30. * connects to the database server
  31. *
  32. * @param string $user mysql user name
  33. * @param string $password mysql user password
  34. * @param array $server host/port/socket/persistent
  35. *
  36. * @return mixed false on error or a mysqli object on success
  37. */
  38. public function connect(
  39. $user,
  40. $password,
  41. array $server = []
  42. ) {
  43. return true;
  44. }
  45. /**
  46. * selects given database
  47. *
  48. * @param string $dbname name of db to select
  49. * @param resource $link mysql link resource
  50. *
  51. * @return bool
  52. */
  53. public function selectDb($dbname, $link)
  54. {
  55. $GLOBALS['dummy_db'] = $dbname;
  56. return true;
  57. }
  58. /**
  59. * runs a query and returns the result
  60. *
  61. * @param string $query query to run
  62. * @param resource $link mysql link resource
  63. * @param int $options query options
  64. *
  65. * @return mixed
  66. */
  67. public function realQuery($query, $link = null, $options = 0)
  68. {
  69. $query = trim(preg_replace('/ */', ' ', str_replace("\n", ' ', $query)));
  70. for ($i = 0, $nb = count($this->_queries); $i < $nb; $i++) {
  71. if ($this->_queries[$i]['query'] != $query) {
  72. continue;
  73. }
  74. $this->_queries[$i]['pos'] = 0;
  75. if (!is_array($this->_queries[$i]['result'])) {
  76. return false;
  77. }
  78. return $i;
  79. }
  80. for ($i = 0, $nb = count($GLOBALS['dummy_queries']); $i < $nb; $i++) {
  81. if ($GLOBALS['dummy_queries'][$i]['query'] != $query) {
  82. continue;
  83. }
  84. $GLOBALS['dummy_queries'][$i]['pos'] = 0;
  85. if (!is_array($GLOBALS['dummy_queries'][$i]['result'])) {
  86. return false;
  87. }
  88. return $i + self::OFFSET_GLOBAL;
  89. }
  90. echo "Not supported query: $query\n";
  91. return false;
  92. }
  93. /**
  94. * Run the multi query and output the results
  95. *
  96. * @param resource $link connection object
  97. * @param string $query multi query statement to execute
  98. *
  99. * @return array|bool
  100. */
  101. public function realMultiQuery($link, $query)
  102. {
  103. return false;
  104. }
  105. /**
  106. * returns result data from $result
  107. *
  108. * @param object $result MySQL result
  109. *
  110. * @return array
  111. */
  112. public function fetchAny($result)
  113. {
  114. $query_data = &$this->getQueryData($result);
  115. if ($query_data['pos'] >= count($query_data['result'])) {
  116. return false;
  117. }
  118. $ret = $query_data['result'][$query_data['pos']];
  119. $query_data['pos'] += 1;
  120. return $ret;
  121. }
  122. /**
  123. * returns array of rows with associative and numeric keys from $result
  124. *
  125. * @param object $result result MySQL result
  126. *
  127. * @return array
  128. */
  129. public function fetchArray($result)
  130. {
  131. $query_data = &$this->getQueryData($result);
  132. $data = $this->fetchAny($result);
  133. if (!is_array($data)
  134. || !isset($query_data['columns'])
  135. ) {
  136. return $data;
  137. }
  138. foreach ($data as $key => $val) {
  139. $data[$query_data['columns'][$key]] = $val;
  140. }
  141. return $data;
  142. }
  143. /**
  144. * returns array of rows with associative keys from $result
  145. *
  146. * @param object $result MySQL result
  147. *
  148. * @return array
  149. */
  150. public function fetchAssoc($result)
  151. {
  152. $data = $this->fetchAny($result);
  153. $query_data = &$this->getQueryData($result);
  154. if (!is_array($data) || !isset($query_data['columns'])) {
  155. return $data;
  156. }
  157. $ret = array();
  158. foreach ($data as $key => $val) {
  159. $ret[$query_data['columns'][$key]] = $val;
  160. }
  161. return $ret;
  162. }
  163. /**
  164. * returns array of rows with numeric keys from $result
  165. *
  166. * @param object $result MySQL result
  167. *
  168. * @return array
  169. */
  170. public function fetchRow($result)
  171. {
  172. $data = $this->fetchAny($result);
  173. return $data;
  174. }
  175. /**
  176. * Adjusts the result pointer to an arbitrary row in the result
  177. *
  178. * @param object $result database result
  179. * @param integer $offset offset to seek
  180. *
  181. * @return bool true on success, false on failure
  182. */
  183. public function dataSeek($result, $offset)
  184. {
  185. $query_data = &$this->getQueryData($result);
  186. if ($offset > count($query_data['result'])) {
  187. return false;
  188. }
  189. $query_data['pos'] = $offset;
  190. return true;
  191. }
  192. /**
  193. * Frees memory associated with the result
  194. *
  195. * @param object $result database result
  196. *
  197. * @return void
  198. */
  199. public function freeResult($result)
  200. {
  201. return;
  202. }
  203. /**
  204. * Check if there are any more query results from a multi query
  205. *
  206. * @param resource $link the connection object
  207. *
  208. * @return bool false
  209. */
  210. public function moreResults($link)
  211. {
  212. return false;
  213. }
  214. /**
  215. * Prepare next result from multi_query
  216. *
  217. * @param resource $link the connection object
  218. *
  219. * @return boolean false
  220. */
  221. public function nextResult($link)
  222. {
  223. return false;
  224. }
  225. /**
  226. * Store the result returned from multi query
  227. *
  228. * @param resource $link the connection object
  229. *
  230. * @return mixed false when empty results / result set when not empty
  231. */
  232. public function storeResult($link)
  233. {
  234. return false;
  235. }
  236. /**
  237. * Returns a string representing the type of connection used
  238. *
  239. * @param resource $link mysql link
  240. *
  241. * @return string type of connection used
  242. */
  243. public function getHostInfo($link)
  244. {
  245. return '';
  246. }
  247. /**
  248. * Returns the version of the MySQL protocol used
  249. *
  250. * @param resource $link mysql link
  251. *
  252. * @return integer version of the MySQL protocol used
  253. */
  254. public function getProtoInfo($link)
  255. {
  256. return -1;
  257. }
  258. /**
  259. * returns a string that represents the client library version
  260. *
  261. * @return string MySQL client library version
  262. */
  263. public function getClientInfo()
  264. {
  265. return '';
  266. }
  267. /**
  268. * returns last error message or false if no errors occurred
  269. *
  270. * @param resource $link connection link
  271. *
  272. * @return string|bool $error or false
  273. */
  274. public function getError($link)
  275. {
  276. return false;
  277. }
  278. /**
  279. * returns the number of rows returned by last query
  280. *
  281. * @param object $result MySQL result
  282. *
  283. * @return string|int
  284. */
  285. public function numRows($result)
  286. {
  287. if (is_bool($result)) {
  288. return 0;
  289. }
  290. $query_data = &$this->getQueryData($result);
  291. return count($query_data['result']);
  292. }
  293. /**
  294. * returns the number of rows affected by last query
  295. *
  296. * @param resource $link the mysql object
  297. * @param bool $get_from_cache whether to retrieve from cache
  298. *
  299. * @return string|int
  300. */
  301. public function affectedRows($link = null, $get_from_cache = true)
  302. {
  303. return 0;
  304. }
  305. /**
  306. * returns metainfo for fields in $result
  307. *
  308. * @param object $result result set identifier
  309. *
  310. * @return array meta info for fields in $result
  311. */
  312. public function getFieldsMeta($result)
  313. {
  314. return array();
  315. }
  316. /**
  317. * return number of fields in given $result
  318. *
  319. * @param object $result MySQL result
  320. *
  321. * @return int field count
  322. */
  323. public function numFields($result)
  324. {
  325. $query_data = &$this->getQueryData($result);
  326. if (!isset($query_data['columns'])) {
  327. return 0;
  328. }
  329. return count($query_data['columns']);
  330. }
  331. /**
  332. * returns the length of the given field $i in $result
  333. *
  334. * @param object $result result set identifier
  335. * @param int $i field
  336. *
  337. * @return int length of field
  338. */
  339. public function fieldLen($result, $i)
  340. {
  341. return -1;
  342. }
  343. /**
  344. * returns name of $i. field in $result
  345. *
  346. * @param object $result result set identifier
  347. * @param int $i field
  348. *
  349. * @return string name of $i. field in $result
  350. */
  351. public function fieldName($result, $i)
  352. {
  353. return '';
  354. }
  355. /**
  356. * returns concatenated string of human readable field flags
  357. *
  358. * @param object $result result set identifier
  359. * @param int $i field
  360. *
  361. * @return string field flags
  362. */
  363. public function fieldFlags($result, $i)
  364. {
  365. return '';
  366. }
  367. /**
  368. * returns properly escaped string for use in MySQL queries
  369. *
  370. * @param mixed $link database link
  371. * @param string $str string to be escaped
  372. *
  373. * @return string a MySQL escaped string
  374. */
  375. public function escapeString($link, $str)
  376. {
  377. return $str;
  378. }
  379. /**
  380. * Adds query result for testing
  381. *
  382. * @param string $query SQL
  383. * @param array $result Expected result
  384. *
  385. * @return void
  386. */
  387. public function setResult($query, $result)
  388. {
  389. $this->_queries[] = array(
  390. 'query' => $query,
  391. 'result' => $result,
  392. );
  393. }
  394. /**
  395. * Return query data for ID
  396. *
  397. * @param object $result result set identifier
  398. *
  399. * @return array
  400. */
  401. private function &getQueryData($result)
  402. {
  403. if ($result >= self::OFFSET_GLOBAL) {
  404. return $GLOBALS['dummy_queries'][$result - self::OFFSET_GLOBAL];
  405. } else {
  406. return $this->_queries[$result];
  407. }
  408. }
  409. }