PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Model/Datasource/Database/Mysql.php

https://bitbucket.org/daveschwan/ronin-group
PHP | 791 lines | 509 code | 59 blank | 223 comment | 112 complexity | 614827eeb2336d09ffea023e3e2e0171 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * MySQL layer for DBO
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Model.Datasource.Database
  15. * @since CakePHP(tm) v 0.10.5.1790
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('DboSource', 'Model/Datasource');
  19. /**
  20. * MySQL DBO driver object
  21. *
  22. * Provides connection and SQL generation for MySQL RDMS
  23. *
  24. * @package Cake.Model.Datasource.Database
  25. */
  26. class Mysql extends DboSource {
  27. /**
  28. * Datasource description
  29. *
  30. * @var string
  31. */
  32. public $description = "MySQL DBO Driver";
  33. /**
  34. * Base configuration settings for MySQL driver
  35. *
  36. * @var array
  37. */
  38. protected $_baseConfig = array(
  39. 'persistent' => true,
  40. 'host' => 'localhost',
  41. 'login' => 'root',
  42. 'password' => '',
  43. 'database' => 'cake',
  44. 'port' => '3306'
  45. );
  46. /**
  47. * Reference to the PDO object connection
  48. *
  49. * @var PDO $_connection
  50. */
  51. protected $_connection = null;
  52. /**
  53. * Start quote
  54. *
  55. * @var string
  56. */
  57. public $startQuote = "`";
  58. /**
  59. * End quote
  60. *
  61. * @var string
  62. */
  63. public $endQuote = "`";
  64. /**
  65. * use alias for update and delete. Set to true if version >= 4.1
  66. *
  67. * @var boolean
  68. */
  69. protected $_useAlias = true;
  70. /**
  71. * List of engine specific additional field parameters used on table creating
  72. *
  73. * @var array
  74. */
  75. public $fieldParameters = array(
  76. 'charset' => array('value' => 'CHARACTER SET', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault'),
  77. 'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => ' ', 'column' => 'Collation', 'position' => 'beforeDefault'),
  78. 'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault')
  79. );
  80. /**
  81. * List of table engine specific parameters used on table creating
  82. *
  83. * @var array
  84. */
  85. public $tableParameters = array(
  86. 'charset' => array('value' => 'DEFAULT CHARSET', 'quote' => false, 'join' => '=', 'column' => 'charset'),
  87. 'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => '=', 'column' => 'Collation'),
  88. 'engine' => array('value' => 'ENGINE', 'quote' => false, 'join' => '=', 'column' => 'Engine')
  89. );
  90. /**
  91. * MySQL column definition
  92. *
  93. * @var array
  94. */
  95. public $columns = array(
  96. 'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'),
  97. 'string' => array('name' => 'varchar', 'limit' => '255'),
  98. 'text' => array('name' => 'text'),
  99. 'biginteger' => array('name' => 'bigint', 'limit' => '20'),
  100. 'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
  101. 'float' => array('name' => 'float', 'formatter' => 'floatval'),
  102. 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
  103. 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
  104. 'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
  105. 'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
  106. 'binary' => array('name' => 'blob'),
  107. 'boolean' => array('name' => 'tinyint', 'limit' => '1')
  108. );
  109. /**
  110. * Mapping of collation names to character set names
  111. *
  112. * @var array
  113. */
  114. protected $_charsets = array();
  115. /**
  116. * Connects to the database using options in the given configuration array.
  117. *
  118. * MySQL supports a few additional options that other drivers do not:
  119. *
  120. * - `unix_socket` Set to the path of the MySQL sock file. Can be used in place
  121. * of host + port.
  122. * - `ssl_key` SSL key file for connecting via SSL. Must be combined with `ssl_cert`.
  123. * - `ssl_cert` The SSL certificate to use when connecting via SSL. Must be
  124. * combined with `ssl_key`.
  125. * - `ssl_ca` The certificate authority for SSL connections.
  126. *
  127. * @return boolean True if the database could be connected, else false
  128. * @throws MissingConnectionException
  129. */
  130. public function connect() {
  131. $config = $this->config;
  132. $this->connected = false;
  133. $flags = array(
  134. PDO::ATTR_PERSISTENT => $config['persistent'],
  135. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  136. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  137. );
  138. if (!empty($config['encoding'])) {
  139. $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding'];
  140. }
  141. if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
  142. $flags[PDO::MYSQL_ATTR_SSL_KEY] = $config['ssl_key'];
  143. $flags[PDO::MYSQL_ATTR_SSL_CERT] = $config['ssl_cert'];
  144. }
  145. if (!empty($config['ssl_ca'])) {
  146. $flags[PDO::MYSQL_ATTR_SSL_CA] = $config['ssl_ca'];
  147. }
  148. if (empty($config['unix_socket'])) {
  149. $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
  150. } else {
  151. $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
  152. }
  153. try {
  154. $this->_connection = new PDO(
  155. $dsn,
  156. $config['login'],
  157. $config['password'],
  158. $flags
  159. );
  160. $this->connected = true;
  161. if (!empty($config['settings'])) {
  162. foreach ($config['settings'] as $key => $value) {
  163. $this->_execute("SET $key=$value");
  164. }
  165. }
  166. } catch (PDOException $e) {
  167. throw new MissingConnectionException(array(
  168. 'class' => get_class($this),
  169. 'message' => $e->getMessage()
  170. ));
  171. }
  172. $this->_charsets = array();
  173. $this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");
  174. return $this->connected;
  175. }
  176. /**
  177. * Check whether the MySQL extension is installed/loaded
  178. *
  179. * @return boolean
  180. */
  181. public function enabled() {
  182. return in_array('mysql', PDO::getAvailableDrivers());
  183. }
  184. /**
  185. * Returns an array of sources (tables) in the database.
  186. *
  187. * @param mixed $data
  188. * @return array Array of table names in the database
  189. */
  190. public function listSources($data = null) {
  191. $cache = parent::listSources();
  192. if ($cache) {
  193. return $cache;
  194. }
  195. $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']));
  196. if (!$result) {
  197. $result->closeCursor();
  198. return array();
  199. }
  200. $tables = array();
  201. while ($line = $result->fetch(PDO::FETCH_NUM)) {
  202. $tables[] = $line[0];
  203. }
  204. $result->closeCursor();
  205. parent::listSources($tables);
  206. return $tables;
  207. }
  208. /**
  209. * Builds a map of the columns contained in a result
  210. *
  211. * @param PDOStatement $results
  212. * @return void
  213. */
  214. public function resultSet($results) {
  215. $this->map = array();
  216. $numFields = $results->columnCount();
  217. $index = 0;
  218. while ($numFields-- > 0) {
  219. $column = $results->getColumnMeta($index);
  220. if ($column['len'] === 1 && (empty($column['native_type']) || $column['native_type'] === 'TINY')) {
  221. $type = 'boolean';
  222. } else {
  223. $type = empty($column['native_type']) ? 'string' : $column['native_type'];
  224. }
  225. if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) {
  226. $this->map[$index++] = array($column['table'], $column['name'], $type);
  227. } else {
  228. $this->map[$index++] = array(0, $column['name'], $type);
  229. }
  230. }
  231. }
  232. /**
  233. * Fetches the next row from the current result set
  234. *
  235. * @return mixed array with results fetched and mapped to column names or false if there is no results left to fetch
  236. */
  237. public function fetchResult() {
  238. if ($row = $this->_result->fetch(PDO::FETCH_NUM)) {
  239. $resultRow = array();
  240. foreach ($this->map as $col => $meta) {
  241. list($table, $column, $type) = $meta;
  242. $resultRow[$table][$column] = $row[$col];
  243. if ($type === 'boolean' && $row[$col] !== null) {
  244. $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
  245. }
  246. }
  247. return $resultRow;
  248. }
  249. $this->_result->closeCursor();
  250. return false;
  251. }
  252. /**
  253. * Gets the database encoding
  254. *
  255. * @return string The database encoding
  256. */
  257. public function getEncoding() {
  258. return $this->_execute('SHOW VARIABLES LIKE ?', array('character_set_client'))->fetchObject()->Value;
  259. }
  260. /**
  261. * Query charset by collation
  262. *
  263. * @param string $name Collation name
  264. * @return string Character set name
  265. */
  266. public function getCharsetName($name) {
  267. if ((bool)version_compare($this->getVersion(), "5", "<")) {
  268. return false;
  269. }
  270. if (isset($this->_charsets[$name])) {
  271. return $this->_charsets[$name];
  272. }
  273. $r = $this->_execute(
  274. 'SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?',
  275. array($name)
  276. );
  277. $cols = $r->fetch(PDO::FETCH_ASSOC);
  278. if (isset($cols['CHARACTER_SET_NAME'])) {
  279. $this->_charsets[$name] = $cols['CHARACTER_SET_NAME'];
  280. } else {
  281. $this->_charsets[$name] = false;
  282. }
  283. return $this->_charsets[$name];
  284. }
  285. /**
  286. * Returns an array of the fields in given table name.
  287. *
  288. * @param Model|string $model Name of database table to inspect or model instance
  289. * @return array Fields in table. Keys are name and type
  290. * @throws CakeException
  291. */
  292. public function describe($model) {
  293. $key = $this->fullTableName($model, false);
  294. $cache = parent::describe($key);
  295. if ($cache) {
  296. return $cache;
  297. }
  298. $table = $this->fullTableName($model);
  299. $fields = false;
  300. $cols = $this->_execute('SHOW FULL COLUMNS FROM ' . $table);
  301. if (!$cols) {
  302. throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $table));
  303. }
  304. while ($column = $cols->fetch(PDO::FETCH_OBJ)) {
  305. $fields[$column->Field] = array(
  306. 'type' => $this->column($column->Type),
  307. 'null' => ($column->Null === 'YES' ? true : false),
  308. 'default' => $column->Default,
  309. 'length' => $this->length($column->Type),
  310. );
  311. if (!empty($column->Key) && isset($this->index[$column->Key])) {
  312. $fields[$column->Field]['key'] = $this->index[$column->Key];
  313. }
  314. foreach ($this->fieldParameters as $name => $value) {
  315. if (!empty($column->{$value['column']})) {
  316. $fields[$column->Field][$name] = $column->{$value['column']};
  317. }
  318. }
  319. if (isset($fields[$column->Field]['collate'])) {
  320. $charset = $this->getCharsetName($fields[$column->Field]['collate']);
  321. if ($charset) {
  322. $fields[$column->Field]['charset'] = $charset;
  323. }
  324. }
  325. }
  326. $this->_cacheDescription($key, $fields);
  327. $cols->closeCursor();
  328. return $fields;
  329. }
  330. /**
  331. * Generates and executes an SQL UPDATE statement for given model, fields, and values.
  332. *
  333. * @param Model $model
  334. * @param array $fields
  335. * @param array $values
  336. * @param mixed $conditions
  337. * @return array
  338. */
  339. public function update(Model $model, $fields = array(), $values = null, $conditions = null) {
  340. if (!$this->_useAlias) {
  341. return parent::update($model, $fields, $values, $conditions);
  342. }
  343. if (!$values) {
  344. $combined = $fields;
  345. } else {
  346. $combined = array_combine($fields, $values);
  347. }
  348. $alias = $joins = false;
  349. $fields = $this->_prepareUpdateFields($model, $combined, empty($conditions), !empty($conditions));
  350. $fields = implode(', ', $fields);
  351. $table = $this->fullTableName($model);
  352. if (!empty($conditions)) {
  353. $alias = $this->name($model->alias);
  354. if ($model->name == $model->alias) {
  355. $joins = implode(' ', $this->_getJoins($model));
  356. }
  357. }
  358. $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
  359. if ($conditions === false) {
  360. return false;
  361. }
  362. if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) {
  363. $model->onError();
  364. return false;
  365. }
  366. return true;
  367. }
  368. /**
  369. * Generates and executes an SQL DELETE statement for given id/conditions on given model.
  370. *
  371. * @param Model $model
  372. * @param mixed $conditions
  373. * @return boolean Success
  374. */
  375. public function delete(Model $model, $conditions = null) {
  376. if (!$this->_useAlias) {
  377. return parent::delete($model, $conditions);
  378. }
  379. $alias = $this->name($model->alias);
  380. $table = $this->fullTableName($model);
  381. $joins = implode(' ', $this->_getJoins($model));
  382. if (empty($conditions)) {
  383. $alias = $joins = false;
  384. }
  385. $complexConditions = false;
  386. foreach ((array)$conditions as $key => $value) {
  387. if (strpos($key, $model->alias) === false) {
  388. $complexConditions = true;
  389. break;
  390. }
  391. }
  392. if (!$complexConditions) {
  393. $joins = false;
  394. }
  395. $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
  396. if ($conditions === false) {
  397. return false;
  398. }
  399. if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
  400. $model->onError();
  401. return false;
  402. }
  403. return true;
  404. }
  405. /**
  406. * Sets the database encoding
  407. *
  408. * @param string $enc Database encoding
  409. * @return boolean
  410. */
  411. public function setEncoding($enc) {
  412. return $this->_execute('SET NAMES ' . $enc) !== false;
  413. }
  414. /**
  415. * Returns an array of the indexes in given datasource name.
  416. *
  417. * @param string $model Name of model to inspect
  418. * @return array Fields in table. Keys are column and unique
  419. */
  420. public function index($model) {
  421. $index = array();
  422. $table = $this->fullTableName($model);
  423. $old = version_compare($this->getVersion(), '4.1', '<=');
  424. if ($table) {
  425. $indexes = $this->_execute('SHOW INDEX FROM ' . $table);
  426. // @codingStandardsIgnoreStart
  427. // MySQL columns don't match the cakephp conventions.
  428. while ($idx = $indexes->fetch(PDO::FETCH_OBJ)) {
  429. if ($old) {
  430. $idx = (object)current((array)$idx);
  431. }
  432. if (!isset($index[$idx->Key_name]['column'])) {
  433. $col = array();
  434. $index[$idx->Key_name]['column'] = $idx->Column_name;
  435. if ($idx->Index_type === 'FULLTEXT') {
  436. $index[$idx->Key_name]['type'] = strtolower($idx->Index_type);
  437. } else {
  438. $index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0);
  439. }
  440. } else {
  441. if (!empty($index[$idx->Key_name]['column']) && !is_array($index[$idx->Key_name]['column'])) {
  442. $col[] = $index[$idx->Key_name]['column'];
  443. }
  444. $col[] = $idx->Column_name;
  445. $index[$idx->Key_name]['column'] = $col;
  446. }
  447. if (!empty($idx->Sub_part)) {
  448. if (!isset($index[$idx->Key_name]['length'])) {
  449. $index[$idx->Key_name]['length'] = array();
  450. }
  451. $index[$idx->Key_name]['length'][$idx->Column_name] = $idx->Sub_part;
  452. }
  453. }
  454. // @codingStandardsIgnoreEnd
  455. $indexes->closeCursor();
  456. }
  457. return $index;
  458. }
  459. /**
  460. * Generate a MySQL Alter Table syntax for the given Schema comparison
  461. *
  462. * @param array $compare Result of a CakeSchema::compare()
  463. * @param string $table
  464. * @return array Array of alter statements to make.
  465. */
  466. public function alterSchema($compare, $table = null) {
  467. if (!is_array($compare)) {
  468. return false;
  469. }
  470. $out = '';
  471. $colList = array();
  472. foreach ($compare as $curTable => $types) {
  473. $indexes = $tableParameters = $colList = array();
  474. if (!$table || $table == $curTable) {
  475. $out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
  476. foreach ($types as $type => $column) {
  477. if (isset($column['indexes'])) {
  478. $indexes[$type] = $column['indexes'];
  479. unset($column['indexes']);
  480. }
  481. if (isset($column['tableParameters'])) {
  482. $tableParameters[$type] = $column['tableParameters'];
  483. unset($column['tableParameters']);
  484. }
  485. switch ($type) {
  486. case 'add':
  487. foreach ($column as $field => $col) {
  488. $col['name'] = $field;
  489. $alter = 'ADD ' . $this->buildColumn($col);
  490. if (isset($col['after'])) {
  491. $alter .= ' AFTER ' . $this->name($col['after']);
  492. }
  493. $colList[] = $alter;
  494. }
  495. break;
  496. case 'drop':
  497. foreach ($column as $field => $col) {
  498. $col['name'] = $field;
  499. $colList[] = 'DROP ' . $this->name($field);
  500. }
  501. break;
  502. case 'change':
  503. foreach ($column as $field => $col) {
  504. if (!isset($col['name'])) {
  505. $col['name'] = $field;
  506. }
  507. $colList[] = 'CHANGE ' . $this->name($field) . ' ' . $this->buildColumn($col);
  508. }
  509. break;
  510. }
  511. }
  512. $colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes));
  513. $colList = array_merge($colList, $this->_alterTableParameters($curTable, $tableParameters));
  514. $out .= "\t" . implode(",\n\t", $colList) . ";\n\n";
  515. }
  516. }
  517. return $out;
  518. }
  519. /**
  520. * Generate a "drop table" statement for the given table
  521. *
  522. * @param type $table Name of the table to drop
  523. * @return string Drop table SQL statement
  524. */
  525. protected function _dropTable($table) {
  526. return 'DROP TABLE IF EXISTS ' . $this->fullTableName($table) . ";";
  527. }
  528. /**
  529. * Generate MySQL table parameter alteration statements for a table.
  530. *
  531. * @param string $table Table to alter parameters for.
  532. * @param array $parameters Parameters to add & drop.
  533. * @return array Array of table property alteration statements.
  534. */
  535. protected function _alterTableParameters($table, $parameters) {
  536. if (isset($parameters['change'])) {
  537. return $this->buildTableParameters($parameters['change']);
  538. }
  539. return array();
  540. }
  541. /**
  542. * Format indexes for create table
  543. *
  544. * @param array $indexes An array of indexes to generate SQL from
  545. * @param string $table Optional table name, not used
  546. * @return array An array of SQL statements for indexes
  547. * @see DboSource::buildIndex()
  548. */
  549. public function buildIndex($indexes, $table = null) {
  550. $join = array();
  551. foreach ($indexes as $name => $value) {
  552. $out = '';
  553. if ($name === 'PRIMARY') {
  554. $out .= 'PRIMARY ';
  555. $name = null;
  556. } else {
  557. if (!empty($value['unique'])) {
  558. $out .= 'UNIQUE ';
  559. }
  560. $name = $this->startQuote . $name . $this->endQuote;
  561. }
  562. if (isset($value['type']) && strtolower($value['type']) === 'fulltext') {
  563. $out .= 'FULLTEXT ';
  564. }
  565. $out .= 'KEY ' . $name . ' (';
  566. if (is_array($value['column'])) {
  567. if (isset($value['length'])) {
  568. $vals = array();
  569. foreach ($value['column'] as $column) {
  570. $name = $this->name($column);
  571. if (isset($value['length'])) {
  572. $name .= $this->_buildIndexSubPart($value['length'], $column);
  573. }
  574. $vals[] = $name;
  575. }
  576. $out .= implode(', ', $vals);
  577. } else {
  578. $out .= implode(', ', array_map(array(&$this, 'name'), $value['column']));
  579. }
  580. } else {
  581. $out .= $this->name($value['column']);
  582. if (isset($value['length'])) {
  583. $out .= $this->_buildIndexSubPart($value['length'], $value['column']);
  584. }
  585. }
  586. $out .= ')';
  587. $join[] = $out;
  588. }
  589. return $join;
  590. }
  591. /**
  592. * Generate MySQL index alteration statements for a table.
  593. *
  594. * @param string $table Table to alter indexes for
  595. * @param array $indexes Indexes to add and drop
  596. * @return array Index alteration statements
  597. */
  598. protected function _alterIndexes($table, $indexes) {
  599. $alter = array();
  600. if (isset($indexes['drop'])) {
  601. foreach ($indexes['drop'] as $name => $value) {
  602. $out = 'DROP ';
  603. if ($name === 'PRIMARY') {
  604. $out .= 'PRIMARY KEY';
  605. } else {
  606. $out .= 'KEY ' . $this->startQuote . $name . $this->endQuote;
  607. }
  608. $alter[] = $out;
  609. }
  610. }
  611. if (isset($indexes['add'])) {
  612. $add = $this->buildIndex($indexes['add']);
  613. foreach ($add as $index) {
  614. $alter[] = 'ADD ' . $index;
  615. }
  616. }
  617. return $alter;
  618. }
  619. /**
  620. * Format length for text indexes
  621. *
  622. * @param array $lengths An array of lengths for a single index
  623. * @param string $column The column for which to generate the index length
  624. * @return string Formatted length part of an index field
  625. */
  626. protected function _buildIndexSubPart($lengths, $column) {
  627. if ($lengths === null) {
  628. return '';
  629. }
  630. if (!isset($lengths[$column])) {
  631. return '';
  632. }
  633. return '(' . $lengths[$column] . ')';
  634. }
  635. /**
  636. * Returns an detailed array of sources (tables) in the database.
  637. *
  638. * @param string $name Table name to get parameters
  639. * @return array Array of table names in the database
  640. */
  641. public function listDetailedSources($name = null) {
  642. $condition = '';
  643. if (is_string($name)) {
  644. $condition = ' WHERE name = ' . $this->value($name);
  645. }
  646. $result = $this->_connection->query('SHOW TABLE STATUS ' . $condition, PDO::FETCH_ASSOC);
  647. if (!$result) {
  648. $result->closeCursor();
  649. return array();
  650. }
  651. $tables = array();
  652. foreach ($result as $row) {
  653. $tables[$row['Name']] = (array)$row;
  654. unset($tables[$row['Name']]['queryString']);
  655. if (!empty($row['Collation'])) {
  656. $charset = $this->getCharsetName($row['Collation']);
  657. if ($charset) {
  658. $tables[$row['Name']]['charset'] = $charset;
  659. }
  660. }
  661. }
  662. $result->closeCursor();
  663. if (is_string($name) && isset($tables[$name])) {
  664. return $tables[$name];
  665. }
  666. return $tables;
  667. }
  668. /**
  669. * Converts database-layer column types to basic types
  670. *
  671. * @param string $real Real database-layer column type (i.e. "varchar(255)")
  672. * @return string Abstract column type (i.e. "string")
  673. */
  674. public function column($real) {
  675. if (is_array($real)) {
  676. $col = $real['name'];
  677. if (isset($real['limit'])) {
  678. $col .= '(' . $real['limit'] . ')';
  679. }
  680. return $col;
  681. }
  682. $col = str_replace(')', '', $real);
  683. $limit = $this->length($real);
  684. if (strpos($col, '(') !== false) {
  685. list($col, $vals) = explode('(', $col);
  686. }
  687. if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
  688. return $col;
  689. }
  690. if (($col === 'tinyint' && $limit === 1) || $col === 'boolean') {
  691. return 'boolean';
  692. }
  693. if (strpos($col, 'bigint') !== false || $col === 'bigint') {
  694. return 'biginteger';
  695. }
  696. if (strpos($col, 'int') !== false) {
  697. return 'integer';
  698. }
  699. if (strpos($col, 'char') !== false || $col === 'tinytext') {
  700. return 'string';
  701. }
  702. if (strpos($col, 'text') !== false) {
  703. return 'text';
  704. }
  705. if (strpos($col, 'blob') !== false || $col === 'binary') {
  706. return 'binary';
  707. }
  708. if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) {
  709. return 'float';
  710. }
  711. if (strpos($col, 'enum') !== false) {
  712. return "enum($vals)";
  713. }
  714. return 'text';
  715. }
  716. /**
  717. * Gets the schema name
  718. *
  719. * @return string The schema name
  720. */
  721. public function getSchemaName() {
  722. return $this->config['database'];
  723. }
  724. /**
  725. * Check if the server support nested transactions
  726. *
  727. * @return boolean
  728. */
  729. public function nestedTransactionSupported() {
  730. return $this->useNestedTransactions && version_compare($this->getVersion(), '4.1', '>=');
  731. }
  732. }