DbalInterface.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Dbal;
  4. use mysqli_result;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\SystemDatabase;
  7. use PhpMyAdmin\Table;
  8. /**
  9. * Main interface for database interactions
  10. */
  11. interface DbalInterface
  12. {
  13. /**
  14. * runs a query
  15. *
  16. * @param string $query SQL query to execute
  17. * @param mixed $link optional database link to use
  18. * @param int $options optional query options
  19. * @param bool $cache_affected_rows whether to cache affected rows
  20. *
  21. * @return mixed
  22. */
  23. public function query(
  24. string $query,
  25. $link = DatabaseInterface::CONNECT_USER,
  26. int $options = 0,
  27. bool $cache_affected_rows = true
  28. );
  29. /**
  30. * runs a query and returns the result
  31. *
  32. * @param string $query query to run
  33. * @param mixed $link link type
  34. * @param int $options query options
  35. * @param bool $cache_affected_rows whether to cache affected row
  36. *
  37. * @return mixed
  38. */
  39. public function tryQuery(
  40. string $query,
  41. $link = DatabaseInterface::CONNECT_USER,
  42. int $options = 0,
  43. bool $cache_affected_rows = true
  44. );
  45. /**
  46. * Run multi query statement and return results
  47. *
  48. * @param string $multiQuery multi query statement to execute
  49. * @param int $linkIndex index of the opened database link
  50. *
  51. * @return mysqli_result[]|bool (false)
  52. */
  53. public function tryMultiQuery(string $multiQuery = '', $linkIndex = DatabaseInterface::CONNECT_USER);
  54. /**
  55. * returns array with table names for given db
  56. *
  57. * @param string $database name of database
  58. * @param mixed $link mysql link resource|object
  59. *
  60. * @return array tables names
  61. */
  62. public function getTables(string $database, $link = DatabaseInterface::CONNECT_USER): array;
  63. /**
  64. * returns
  65. *
  66. * @param string $database name of database
  67. * @param array $tables list of tables to search for for relations
  68. * @param int $link mysql link resource|object
  69. *
  70. * @return array array of found foreign keys
  71. */
  72. public function getForeignKeyConstrains(
  73. string $database,
  74. array $tables,
  75. $link = DatabaseInterface::CONNECT_USER
  76. ): array;
  77. /**
  78. * returns array of all tables in given db or dbs
  79. * this function expects unquoted names:
  80. * RIGHT: my_database
  81. * WRONG: `my_database`
  82. * WRONG: my\_database
  83. * if $tbl_is_group is true, $table is used as filter for table names
  84. *
  85. * <code>
  86. * $dbi->getTablesFull('my_database');
  87. * $dbi->getTablesFull('my_database', 'my_table'));
  88. * $dbi->getTablesFull('my_database', 'my_tables_', true));
  89. * </code>
  90. *
  91. * @param string $database database
  92. * @param string|array $table table name(s)
  93. * @param bool $tbl_is_group $table is a table group
  94. * @param int $limit_offset zero-based offset for the count
  95. * @param bool|int $limit_count number of tables to return
  96. * @param string $sort_by table attribute to sort by
  97. * @param string $sort_order direction to sort (ASC or DESC)
  98. * @param string $table_type whether table or view
  99. * @param mixed $link link type
  100. *
  101. * @return array list of tables in given db(s)
  102. *
  103. * @todo move into Table
  104. */
  105. public function getTablesFull(
  106. string $database,
  107. $table = '',
  108. bool $tbl_is_group = false,
  109. int $limit_offset = 0,
  110. $limit_count = false,
  111. string $sort_by = 'Name',
  112. string $sort_order = 'ASC',
  113. ?string $table_type = null,
  114. $link = DatabaseInterface::CONNECT_USER
  115. ): array;
  116. /**
  117. * Get VIEWs in a particular database
  118. *
  119. * @param string $db Database name to look in
  120. *
  121. * @return array Set of VIEWs inside the database
  122. */
  123. public function getVirtualTables(string $db): array;
  124. /**
  125. * returns array with databases containing extended infos about them
  126. *
  127. * @param string $database database
  128. * @param bool $force_stats retrieve stats also for MySQL < 5
  129. * @param int $link link type
  130. * @param string $sort_by column to order by
  131. * @param string $sort_order ASC or DESC
  132. * @param int $limit_offset starting offset for LIMIT
  133. * @param bool|int $limit_count row count for LIMIT or true
  134. * for
  135. * $GLOBALS['cfg']['MaxDbList']
  136. *
  137. * @return array
  138. *
  139. * @todo move into ListDatabase?
  140. */
  141. public function getDatabasesFull(
  142. ?string $database = null,
  143. bool $force_stats = false,
  144. $link = DatabaseInterface::CONNECT_USER,
  145. string $sort_by = 'SCHEMA_NAME',
  146. string $sort_order = 'ASC',
  147. int $limit_offset = 0,
  148. $limit_count = false
  149. ): array;
  150. /**
  151. * returns detailed array with all columns for sql
  152. *
  153. * @param string $sql_query target SQL query to get columns
  154. * @param array $view_columns alias for columns
  155. *
  156. * @return array
  157. */
  158. public function getColumnMapFromSql(string $sql_query, array $view_columns = []): array;
  159. /**
  160. * returns detailed array with all columns for given table in database,
  161. * or all tables/databases
  162. *
  163. * @param string $database name of database
  164. * @param string $table name of table to retrieve columns from
  165. * @param string $column name of specific column
  166. * @param mixed $link mysql link resource
  167. *
  168. * @return array
  169. */
  170. public function getColumnsFull(
  171. ?string $database = null,
  172. ?string $table = null,
  173. ?string $column = null,
  174. $link = DatabaseInterface::CONNECT_USER
  175. ): array;
  176. /**
  177. * Returns descriptions of columns in given table (all or given by $column)
  178. *
  179. * @param string $database name of database
  180. * @param string $table name of table to retrieve columns from
  181. * @param string $column name of column, null to show all columns
  182. * @param bool $full whether to return full info or only column names
  183. * @param int $link link type
  184. *
  185. * @return array array indexed by column names or,
  186. * if $column is given, flat array description
  187. */
  188. public function getColumns(
  189. string $database,
  190. string $table,
  191. ?string $column = null,
  192. bool $full = false,
  193. $link = DatabaseInterface::CONNECT_USER
  194. ): array;
  195. /**
  196. * Returns all column names in given table
  197. *
  198. * @param string $database name of database
  199. * @param string $table name of table to retrieve columns from
  200. * @param mixed $link mysql link resource
  201. *
  202. * @return array|null
  203. */
  204. public function getColumnNames(
  205. string $database,
  206. string $table,
  207. $link = DatabaseInterface::CONNECT_USER
  208. ): ?array;
  209. /**
  210. * Returns indexes of a table
  211. *
  212. * @param string $database name of database
  213. * @param string $table name of the table whose indexes are to be retrieved
  214. * @param mixed $link mysql link resource
  215. *
  216. * @return array
  217. */
  218. public function getTableIndexes(
  219. string $database,
  220. string $table,
  221. $link = DatabaseInterface::CONNECT_USER
  222. ): array;
  223. /**
  224. * returns value of given mysql server variable
  225. *
  226. * @param string $var mysql server variable name
  227. * @param int $type DatabaseInterface::GETVAR_SESSION |
  228. * DatabaseInterface::GETVAR_GLOBAL
  229. * @param mixed $link mysql link resource|object
  230. *
  231. * @return mixed value for mysql server variable
  232. */
  233. public function getVariable(
  234. string $var,
  235. int $type = DatabaseInterface::GETVAR_SESSION,
  236. $link = DatabaseInterface::CONNECT_USER
  237. );
  238. /**
  239. * Sets new value for a variable if it is different from the current value
  240. *
  241. * @param string $var variable name
  242. * @param string $value value to set
  243. * @param mixed $link mysql link resource|object
  244. *
  245. * @return bool whether query was a successful
  246. */
  247. public function setVariable(string $var, string $value, $link = DatabaseInterface::CONNECT_USER): bool;
  248. /**
  249. * Function called just after a connection to the MySQL database server has
  250. * been established. It sets the connection collation, and determines the
  251. * version of MySQL which is running.
  252. */
  253. public function postConnect(): void;
  254. /**
  255. * Sets collation connection for user link
  256. *
  257. * @param string $collation collation to set
  258. */
  259. public function setCollation(string $collation): void;
  260. /**
  261. * Function called just after a connection to the MySQL database server has
  262. * been established. It sets the connection collation, and determines the
  263. * version of MySQL which is running.
  264. */
  265. public function postConnectControl(): void;
  266. /**
  267. * returns a single value from the given result or query,
  268. * if the query or the result has more than one row or field
  269. * the first field of the first row is returned
  270. *
  271. * <code>
  272. * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
  273. * $user_name = $dbi->fetchValue($sql);
  274. * // produces
  275. * // $user_name = 'John Doe'
  276. * </code>
  277. *
  278. * @param string $query The query to execute
  279. * @param int $row_number row to fetch the value from,
  280. * starting at 0, with 0 being
  281. * default
  282. * @param int|string $field field to fetch the value from,
  283. * starting at 0, with 0 being
  284. * default
  285. * @param int $link link type
  286. *
  287. * @return mixed value of first field in first row from result
  288. * or false if not found
  289. */
  290. public function fetchValue(
  291. string $query,
  292. int $row_number = 0,
  293. $field = 0,
  294. $link = DatabaseInterface::CONNECT_USER
  295. );
  296. /**
  297. * Returns only the first row from the result or null if result is empty.
  298. *
  299. * <code>
  300. * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
  301. * $user = $dbi->fetchSingleRow($sql);
  302. * // produces
  303. * // $user = array('id' => 123, 'name' => 'John Doe')
  304. * </code>
  305. *
  306. * @param string $query The query to execute
  307. * @param string $type NUM|ASSOC|BOTH returned array should either numeric
  308. * associative or both
  309. * @param int $link link type
  310. */
  311. public function fetchSingleRow(
  312. string $query,
  313. string $type = 'ASSOC',
  314. $link = DatabaseInterface::CONNECT_USER
  315. ): ?array;
  316. /**
  317. * returns all rows in the resultset in one array
  318. *
  319. * <code>
  320. * $sql = 'SELECT * FROM `user`';
  321. * $users = $dbi->fetchResult($sql);
  322. * // produces
  323. * // $users[] = array('id' => 123, 'name' => 'John Doe')
  324. *
  325. * $sql = 'SELECT `id`, `name` FROM `user`';
  326. * $users = $dbi->fetchResult($sql, 'id');
  327. * // produces
  328. * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
  329. *
  330. * $sql = 'SELECT `id`, `name` FROM `user`';
  331. * $users = $dbi->fetchResult($sql, 0);
  332. * // produces
  333. * // $users['123'] = array(0 => 123, 1 => 'John Doe')
  334. *
  335. * $sql = 'SELECT `id`, `name` FROM `user`';
  336. * $users = $dbi->fetchResult($sql, 'id', 'name');
  337. * // or
  338. * $users = $dbi->fetchResult($sql, 0, 1);
  339. * // produces
  340. * // $users['123'] = 'John Doe'
  341. *
  342. * $sql = 'SELECT `name` FROM `user`';
  343. * $users = $dbi->fetchResult($sql);
  344. * // produces
  345. * // $users[] = 'John Doe'
  346. *
  347. * $sql = 'SELECT `group`, `name` FROM `user`'
  348. * $users = $dbi->fetchResult($sql, array('group', null), 'name');
  349. * // produces
  350. * // $users['admin'][] = 'John Doe'
  351. *
  352. * $sql = 'SELECT `group`, `name` FROM `user`'
  353. * $users = $dbi->fetchResult($sql, array('group', 'name'), 'id');
  354. * // produces
  355. * // $users['admin']['John Doe'] = '123'
  356. * </code>
  357. *
  358. * @param string $query query to execute
  359. * @param string|int|array $key field-name or offset
  360. * used as key for
  361. * array or array of
  362. * those
  363. * @param string|int $value value-name or offset
  364. * used as value for
  365. * array
  366. * @param int $link link type
  367. * @param int $options query options
  368. *
  369. * @return array resultrows or values indexed by $key
  370. */
  371. public function fetchResult(
  372. string $query,
  373. $key = null,
  374. $value = null,
  375. $link = DatabaseInterface::CONNECT_USER,
  376. int $options = 0
  377. );
  378. /**
  379. * Get supported SQL compatibility modes
  380. *
  381. * @return array supported SQL compatibility modes
  382. */
  383. public function getCompatibilities(): array;
  384. /**
  385. * returns warnings for last query
  386. *
  387. * @param int $link link type
  388. *
  389. * @return array warnings
  390. */
  391. public function getWarnings($link = DatabaseInterface::CONNECT_USER): array;
  392. /**
  393. * returns an array of PROCEDURE or FUNCTION names for a db
  394. *
  395. * @param string $db db name
  396. * @param string $which PROCEDURE | FUNCTION
  397. * @param int $link link type
  398. *
  399. * @return array the procedure names or function names
  400. */
  401. public function getProceduresOrFunctions(
  402. string $db,
  403. string $which,
  404. $link = DatabaseInterface::CONNECT_USER
  405. ): array;
  406. /**
  407. * returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW
  408. *
  409. * @param string $db db name
  410. * @param string $which PROCEDURE | FUNCTION | EVENT | VIEW
  411. * @param string $name the procedure|function|event|view name
  412. * @param int $link link type
  413. *
  414. * @return string|null the definition
  415. */
  416. public function getDefinition(
  417. string $db,
  418. string $which,
  419. string $name,
  420. $link = DatabaseInterface::CONNECT_USER
  421. ): ?string;
  422. /**
  423. * returns details about the PROCEDUREs or FUNCTIONs for a specific database
  424. * or details about a specific routine
  425. *
  426. * @param string $db db name
  427. * @param string $which PROCEDURE | FUNCTION or null for both
  428. * @param string $name name of the routine (to fetch a specific routine)
  429. *
  430. * @return array information about ROCEDUREs or FUNCTIONs
  431. */
  432. public function getRoutines(string $db, ?string $which = null, string $name = ''): array;
  433. /**
  434. * returns details about the EVENTs for a specific database
  435. *
  436. * @param string $db db name
  437. * @param string $name event name
  438. *
  439. * @return array information about EVENTs
  440. */
  441. public function getEvents(string $db, string $name = ''): array;
  442. /**
  443. * returns details about the TRIGGERs for a specific table or database
  444. *
  445. * @param string $db db name
  446. * @param string $table table name
  447. * @param string $delimiter the delimiter to use (may be empty)
  448. *
  449. * @return array information about triggers (may be empty)
  450. */
  451. public function getTriggers(string $db, string $table = '', $delimiter = '//');
  452. /**
  453. * gets the current user with host
  454. *
  455. * @return string the current user i.e. user@host
  456. */
  457. public function getCurrentUser(): string;
  458. /**
  459. * Checks if current user is superuser
  460. *
  461. * @return bool Whether user is a superuser
  462. */
  463. public function isSuperUser(): bool;
  464. public function isGrantUser(): bool;
  465. public function isCreateUser(): bool;
  466. public function isConnected(): bool;
  467. /**
  468. * Get the current user and host
  469. *
  470. * @return array array of username and hostname
  471. */
  472. public function getCurrentUserAndHost(): array;
  473. /**
  474. * Returns value for lower_case_table_names variable
  475. *
  476. * @return string|bool
  477. */
  478. public function getLowerCaseNames();
  479. /**
  480. * connects to the database server
  481. *
  482. * @param int $mode Connection mode on of CONNECT_USER, CONNECT_CONTROL
  483. * or CONNECT_AUXILIARY.
  484. * @param array|null $server Server information like host/port/socket/persistent
  485. * @param int $target How to store connection link, defaults to $mode
  486. *
  487. * @return mixed false on error or a connection object on success
  488. */
  489. public function connect(int $mode, ?array $server = null, ?int $target = null);
  490. /**
  491. * selects given database
  492. *
  493. * @param string $dbname database name to select
  494. * @param int $link link type
  495. */
  496. public function selectDb(string $dbname, $link = DatabaseInterface::CONNECT_USER): bool;
  497. /**
  498. * returns array of rows with associative and numeric keys from $result
  499. *
  500. * @param object $result result set identifier
  501. */
  502. public function fetchArray($result): ?array;
  503. /**
  504. * returns array of rows with associative keys from $result
  505. *
  506. * @param object $result result set identifier
  507. */
  508. public function fetchAssoc($result): ?array;
  509. /**
  510. * returns array of rows with numeric keys from $result
  511. *
  512. * @param object $result result set identifier
  513. */
  514. public function fetchRow($result): ?array;
  515. /**
  516. * Adjusts the result pointer to an arbitrary row in the result
  517. *
  518. * @param object $result database result
  519. * @param int $offset offset to seek
  520. *
  521. * @return bool true on success, false on failure
  522. */
  523. public function dataSeek($result, int $offset): bool;
  524. /**
  525. * Frees memory associated with the result
  526. *
  527. * @param object $result database result
  528. */
  529. public function freeResult($result): void;
  530. /**
  531. * Check if there are any more query results from a multi query
  532. *
  533. * @param int $link link type
  534. *
  535. * @return bool true or false
  536. */
  537. public function moreResults($link = DatabaseInterface::CONNECT_USER): bool;
  538. /**
  539. * Prepare next result from multi_query
  540. *
  541. * @param int $link link type
  542. *
  543. * @return bool true or false
  544. */
  545. public function nextResult($link = DatabaseInterface::CONNECT_USER): bool;
  546. /**
  547. * Store the result returned from multi query
  548. *
  549. * @param int $link link type
  550. *
  551. * @return mixed false when empty results / result set when not empty
  552. */
  553. public function storeResult($link = DatabaseInterface::CONNECT_USER);
  554. /**
  555. * Returns a string representing the type of connection used
  556. *
  557. * @param int $link link type
  558. *
  559. * @return string|bool type of connection used
  560. */
  561. public function getHostInfo($link = DatabaseInterface::CONNECT_USER);
  562. /**
  563. * Returns the version of the MySQL protocol used
  564. *
  565. * @param int $link link type
  566. *
  567. * @return int|bool version of the MySQL protocol used
  568. */
  569. public function getProtoInfo($link = DatabaseInterface::CONNECT_USER);
  570. /**
  571. * returns a string that represents the client library version
  572. *
  573. * @param int $link link type
  574. *
  575. * @return string MySQL client library version
  576. */
  577. public function getClientInfo($link = DatabaseInterface::CONNECT_USER): string;
  578. /**
  579. * returns last error message or false if no errors occurred
  580. *
  581. * @param int $link link type
  582. *
  583. * @return string|bool error or false
  584. */
  585. public function getError($link = DatabaseInterface::CONNECT_USER);
  586. /**
  587. * returns the number of rows returned by last query
  588. *
  589. * @param object $result result set identifier
  590. *
  591. * @return string|int
  592. */
  593. public function numRows($result);
  594. /**
  595. * returns last inserted auto_increment id for given $link
  596. * or $GLOBALS['userlink']
  597. *
  598. * @param int $link link type
  599. *
  600. * @return int|bool
  601. */
  602. public function insertId($link = DatabaseInterface::CONNECT_USER);
  603. /**
  604. * returns the number of rows affected by last query
  605. *
  606. * @param int $link link type
  607. * @param bool $get_from_cache whether to retrieve from cache
  608. *
  609. * @return int|bool
  610. */
  611. public function affectedRows($link = DatabaseInterface::CONNECT_USER, bool $get_from_cache = true);
  612. /**
  613. * returns metainfo for fields in $result
  614. *
  615. * @param object $result result set identifier
  616. *
  617. * @return mixed meta info for fields in $result
  618. */
  619. public function getFieldsMeta($result);
  620. /**
  621. * return number of fields in given $result
  622. *
  623. * @param object $result result set identifier
  624. *
  625. * @return int field count
  626. */
  627. public function numFields($result): int;
  628. /**
  629. * returns the length of the given field $i in $result
  630. *
  631. * @param object $result result set identifier
  632. * @param int $i field
  633. *
  634. * @return int|bool length of field
  635. */
  636. public function fieldLen($result, int $i);
  637. /**
  638. * returns name of $i. field in $result
  639. *
  640. * @param object $result result set identifier
  641. * @param int $i field
  642. *
  643. * @return string name of $i. field in $result
  644. */
  645. public function fieldName($result, int $i): string;
  646. /**
  647. * returns concatenated string of human readable field flags
  648. *
  649. * @param object $result result set identifier
  650. * @param int $i field
  651. *
  652. * @return string field flags
  653. */
  654. public function fieldFlags($result, $i): string;
  655. /**
  656. * returns properly escaped string for use in MySQL queries
  657. *
  658. * @param string $str string to be escaped
  659. * @param mixed $link optional database link to use
  660. *
  661. * @return string a MySQL escaped string
  662. */
  663. public function escapeString(string $str, $link = DatabaseInterface::CONNECT_USER);
  664. /**
  665. * Checks if this database server is running on Amazon RDS.
  666. */
  667. public function isAmazonRds(): bool;
  668. /**
  669. * Gets SQL for killing a process.
  670. *
  671. * @param int $process Process ID
  672. */
  673. public function getKillQuery(int $process): string;
  674. /**
  675. * Get the phpmyadmin database manager
  676. */
  677. public function getSystemDatabase(): SystemDatabase;
  678. /**
  679. * Get a table with database name and table name
  680. *
  681. * @param string $db_name DB name
  682. * @param string $table_name Table name
  683. */
  684. public function getTable(string $db_name, string $table_name): Table;
  685. /**
  686. * returns collation of given db
  687. *
  688. * @param string $db name of db
  689. *
  690. * @return string collation of $db
  691. */
  692. public function getDbCollation(string $db): string;
  693. /**
  694. * returns default server collation from show variables
  695. */
  696. public function getServerCollation(): string;
  697. /**
  698. * Server version as number
  699. */
  700. public function getVersion(): int;
  701. /**
  702. * Server version
  703. */
  704. public function getVersionString(): string;
  705. /**
  706. * Server version comment
  707. */
  708. public function getVersionComment(): string;
  709. /**
  710. * Whether connection is MariaDB
  711. */
  712. public function isMariaDB(): bool;
  713. /**
  714. * Whether connection is Percona
  715. */
  716. public function isPercona(): bool;
  717. /**
  718. * Prepare an SQL statement for execution.
  719. *
  720. * @param string $query The query, as a string.
  721. * @param int $link Link type.
  722. *
  723. * @return object|false A statement object or false.
  724. */
  725. public function prepare(string $query, $link = DatabaseInterface::CONNECT_USER);
  726. }