Mysql.php 21 KB

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