Mysql.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. if (!empty($config['settings'])) {
  148. foreach ($config['settings'] as $key => $value) {
  149. $this->_execute("SET $key=$value");
  150. }
  151. }
  152. } catch (PDOException $e) {
  153. throw new MissingConnectionException(array(
  154. 'class' => get_class($this),
  155. 'message' => $e->getMessage()
  156. ));
  157. }
  158. $this->_charsets = array();
  159. $this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");
  160. return $this->connected;
  161. }
  162. /**
  163. * Check whether the MySQL extension is installed/loaded
  164. *
  165. * @return boolean
  166. */
  167. public function enabled() {
  168. return in_array('mysql', PDO::getAvailableDrivers());
  169. }
  170. /**
  171. * Returns an array of sources (tables) in the database.
  172. *
  173. * @param mixed $data
  174. * @return array Array of table names in the database
  175. */
  176. public function listSources($data = null) {
  177. $cache = parent::listSources();
  178. if ($cache) {
  179. return $cache;
  180. }
  181. $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']));
  182. if (!$result) {
  183. $result->closeCursor();
  184. return array();
  185. } else {
  186. $tables = array();
  187. while ($line = $result->fetch(PDO::FETCH_NUM)) {
  188. $tables[] = $line[0];
  189. }
  190. $result->closeCursor();
  191. parent::listSources($tables);
  192. return $tables;
  193. }
  194. }
  195. /**
  196. * Builds a map of the columns contained in a result
  197. *
  198. * @param PDOStatement $results
  199. * @return void
  200. */
  201. public function resultSet($results) {
  202. $this->map = array();
  203. $numFields = $results->columnCount();
  204. $index = 0;
  205. while ($numFields-- > 0) {
  206. $column = $results->getColumnMeta($index);
  207. if (empty($column['native_type'])) {
  208. $type = ($column['len'] == 1) ? 'boolean' : 'string';
  209. } else {
  210. $type = $column['native_type'];
  211. }
  212. if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) {
  213. $this->map[$index++] = array($column['table'], $column['name'], $type);
  214. } else {
  215. $this->map[$index++] = array(0, $column['name'], $type);
  216. }
  217. }
  218. }
  219. /**
  220. * Fetches the next row from the current result set
  221. *
  222. * @return mixed array with results fetched and mapped to column names or false if there is no results left to fetch
  223. */
  224. public function fetchResult() {
  225. if ($row = $this->_result->fetch(PDO::FETCH_NUM)) {
  226. $resultRow = array();
  227. foreach ($this->map as $col => $meta) {
  228. list($table, $column, $type) = $meta;
  229. $resultRow[$table][$column] = $row[$col];
  230. if ($type === 'boolean' && $row[$col] !== null) {
  231. $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
  232. }
  233. }
  234. return $resultRow;
  235. }
  236. $this->_result->closeCursor();
  237. return false;
  238. }
  239. /**
  240. * Gets the database encoding
  241. *
  242. * @return string The database encoding
  243. */
  244. public function getEncoding() {
  245. return $this->_execute('SHOW VARIABLES LIKE ?', array('character_set_client'))->fetchObject()->Value;
  246. }
  247. /**
  248. * Query charset by collation
  249. *
  250. * @param string $name Collation name
  251. * @return string Character set name
  252. */
  253. public function getCharsetName($name) {
  254. if ((bool)version_compare($this->getVersion(), "5", "<")) {
  255. return false;
  256. }
  257. if (isset($this->_charsets[$name])) {
  258. return $this->_charsets[$name];
  259. }
  260. $r = $this->_execute(
  261. 'SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?',
  262. array($name)
  263. );
  264. $cols = $r->fetch(PDO::FETCH_ASSOC);
  265. if (isset($cols['CHARACTER_SET_NAME'])) {
  266. $this->_charsets[$name] = $cols['CHARACTER_SET_NAME'];
  267. } else {
  268. $this->_charsets[$name] = false;
  269. }
  270. return $this->_charsets[$name];
  271. }
  272. /**
  273. * Returns an array of the fields in given table name.
  274. *
  275. * @param Model|string $model Name of database table to inspect or model instance
  276. * @return array Fields in table. Keys are name and type
  277. * @throws CakeException
  278. */
  279. public function describe($model) {
  280. $key = $this->fullTableName($model, false);
  281. $cache = parent::describe($key);
  282. if ($cache) {
  283. return $cache;
  284. }
  285. $table = $this->fullTableName($model);
  286. $fields = false;
  287. $cols = $this->_execute('SHOW FULL COLUMNS FROM ' . $table);
  288. if (!$cols) {
  289. throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $table));
  290. }
  291. while ($column = $cols->fetch(PDO::FETCH_OBJ)) {
  292. $fields[$column->Field] = array(
  293. 'type' => $this->column($column->Type),
  294. 'null' => ($column->Null === 'YES' ? true : false),
  295. 'default' => $column->Default,
  296. 'length' => $this->length($column->Type),
  297. );
  298. if (!empty($column->Key) && isset($this->index[$column->Key])) {
  299. $fields[$column->Field]['key'] = $this->index[$column->Key];
  300. }
  301. foreach ($this->fieldParameters as $name => $value) {
  302. if (!empty($column->{$value['column']})) {
  303. $fields[$column->Field][$name] = $column->{$value['column']};
  304. }
  305. }
  306. if (isset($fields[$column->Field]['collate'])) {
  307. $charset = $this->getCharsetName($fields[$column->Field]['collate']);
  308. if ($charset) {
  309. $fields[$column->Field]['charset'] = $charset;
  310. }
  311. }
  312. }
  313. $this->_cacheDescription($key, $fields);
  314. $cols->closeCursor();
  315. return $fields;
  316. }
  317. /**
  318. * Generates and executes an SQL UPDATE statement for given model, fields, and values.
  319. *
  320. * @param Model $model
  321. * @param array $fields
  322. * @param array $values
  323. * @param mixed $conditions
  324. * @return array
  325. */
  326. public function update(Model $model, $fields = array(), $values = null, $conditions = null) {
  327. if (!$this->_useAlias) {
  328. return parent::update($model, $fields, $values, $conditions);
  329. }
  330. if (!$values) {
  331. $combined = $fields;
  332. } else {
  333. $combined = array_combine($fields, $values);
  334. }
  335. $alias = $joins = false;
  336. $fields = $this->_prepareUpdateFields($model, $combined, empty($conditions), !empty($conditions));
  337. $fields = implode(', ', $fields);
  338. $table = $this->fullTableName($model);
  339. if (!empty($conditions)) {
  340. $alias = $this->name($model->alias);
  341. if ($model->name == $model->alias) {
  342. $joins = implode(' ', $this->_getJoins($model));
  343. }
  344. }
  345. $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
  346. if ($conditions === false) {
  347. return false;
  348. }
  349. if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) {
  350. $model->onError();
  351. return false;
  352. }
  353. return true;
  354. }
  355. /**
  356. * Generates and executes an SQL DELETE statement for given id/conditions on given model.
  357. *
  358. * @param Model $model
  359. * @param mixed $conditions
  360. * @return boolean Success
  361. */
  362. public function delete(Model $model, $conditions = null) {
  363. if (!$this->_useAlias) {
  364. return parent::delete($model, $conditions);
  365. }
  366. $alias = $this->name($model->alias);
  367. $table = $this->fullTableName($model);
  368. $joins = implode(' ', $this->_getJoins($model));
  369. if (empty($conditions)) {
  370. $alias = $joins = false;
  371. }
  372. $complexConditions = false;
  373. foreach ((array)$conditions as $key => $value) {
  374. if (strpos($key, $model->alias) === false) {
  375. $complexConditions = true;
  376. break;
  377. }
  378. }
  379. if (!$complexConditions) {
  380. $joins = false;
  381. }
  382. $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
  383. if ($conditions === false) {
  384. return false;
  385. }
  386. if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
  387. $model->onError();
  388. return false;
  389. }
  390. return true;
  391. }
  392. /**
  393. * Sets the database encoding
  394. *
  395. * @param string $enc Database encoding
  396. * @return boolean
  397. */
  398. public function setEncoding($enc) {
  399. return $this->_execute('SET NAMES ' . $enc) !== false;
  400. }
  401. /**
  402. * Returns an array of the indexes in given datasource name.
  403. *
  404. * @param string $model Name of model to inspect
  405. * @return array Fields in table. Keys are column and unique
  406. */
  407. public function index($model) {
  408. $index = array();
  409. $table = $this->fullTableName($model);
  410. $old = version_compare($this->getVersion(), '4.1', '<=');
  411. if ($table) {
  412. $indexes = $this->_execute('SHOW INDEX FROM ' . $table);
  413. // @codingStandardsIgnoreStart
  414. // MySQL columns don't match the cakephp conventions.
  415. while ($idx = $indexes->fetch(PDO::FETCH_OBJ)) {
  416. if ($old) {
  417. $idx = (object)current((array)$idx);
  418. }
  419. if (!isset($index[$idx->Key_name]['column'])) {
  420. $col = array();
  421. $index[$idx->Key_name]['column'] = $idx->Column_name;
  422. if ($idx->Index_type === 'FULLTEXT') {
  423. $index[$idx->Key_name]['type'] = strtolower($idx->Index_type);
  424. } else {
  425. $index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0);
  426. }
  427. } else {
  428. if (!empty($index[$idx->Key_name]['column']) && !is_array($index[$idx->Key_name]['column'])) {
  429. $col[] = $index[$idx->Key_name]['column'];
  430. }
  431. $col[] = $idx->Column_name;
  432. $index[$idx->Key_name]['column'] = $col;
  433. }
  434. if (!empty($idx->Sub_part)) {
  435. if (!isset($index[$idx->Key_name]['length'])) {
  436. $index[$idx->Key_name]['length'] = array();
  437. }
  438. $index[$idx->Key_name]['length'][$idx->Column_name] = $idx->Sub_part;
  439. }
  440. }
  441. // @codingStandardsIgnoreEnd
  442. $indexes->closeCursor();
  443. }
  444. return $index;
  445. }
  446. /**
  447. * Generate a MySQL Alter Table syntax for the given Schema comparison
  448. *
  449. * @param array $compare Result of a CakeSchema::compare()
  450. * @param string $table
  451. * @return array Array of alter statements to make.
  452. */
  453. public function alterSchema($compare, $table = null) {
  454. if (!is_array($compare)) {
  455. return false;
  456. }
  457. $out = '';
  458. $colList = array();
  459. foreach ($compare as $curTable => $types) {
  460. $indexes = $tableParameters = $colList = array();
  461. if (!$table || $table == $curTable) {
  462. $out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
  463. foreach ($types as $type => $column) {
  464. if (isset($column['indexes'])) {
  465. $indexes[$type] = $column['indexes'];
  466. unset($column['indexes']);
  467. }
  468. if (isset($column['tableParameters'])) {
  469. $tableParameters[$type] = $column['tableParameters'];
  470. unset($column['tableParameters']);
  471. }
  472. switch ($type) {
  473. case 'add':
  474. foreach ($column as $field => $col) {
  475. $col['name'] = $field;
  476. $alter = 'ADD ' . $this->buildColumn($col);
  477. if (isset($col['after'])) {
  478. $alter .= ' AFTER ' . $this->name($col['after']);
  479. }
  480. $colList[] = $alter;
  481. }
  482. break;
  483. case 'drop':
  484. foreach ($column as $field => $col) {
  485. $col['name'] = $field;
  486. $colList[] = 'DROP ' . $this->name($field);
  487. }
  488. break;
  489. case 'change':
  490. foreach ($column as $field => $col) {
  491. if (!isset($col['name'])) {
  492. $col['name'] = $field;
  493. }
  494. $colList[] = 'CHANGE ' . $this->name($field) . ' ' . $this->buildColumn($col);
  495. }
  496. break;
  497. }
  498. }
  499. $colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes));
  500. $colList = array_merge($colList, $this->_alterTableParameters($curTable, $tableParameters));
  501. $out .= "\t" . implode(",\n\t", $colList) . ";\n\n";
  502. }
  503. }
  504. return $out;
  505. }
  506. /**
  507. * Generate a "drop table" statement for the given table
  508. *
  509. * @param type $table Name of the table to drop
  510. * @return string Drop table SQL statement
  511. */
  512. protected function _dropTable($table) {
  513. return 'DROP TABLE IF EXISTS ' . $this->fullTableName($table) . ";";
  514. }
  515. /**
  516. * Generate MySQL table parameter alteration statements for a table.
  517. *
  518. * @param string $table Table to alter parameters for.
  519. * @param array $parameters Parameters to add & drop.
  520. * @return array Array of table property alteration statements.
  521. */
  522. protected function _alterTableParameters($table, $parameters) {
  523. if (isset($parameters['change'])) {
  524. return $this->buildTableParameters($parameters['change']);
  525. }
  526. return array();
  527. }
  528. /**
  529. * Format indexes for create table
  530. *
  531. * @param array $indexes An array of indexes to generate SQL from
  532. * @param string $table Optional table name, not used
  533. * @return array An array of SQL statements for indexes
  534. * @see DboSource::buildIndex()
  535. */
  536. public function buildIndex($indexes, $table = null) {
  537. $join = array();
  538. foreach ($indexes as $name => $value) {
  539. $out = '';
  540. if ($name === 'PRIMARY') {
  541. $out .= 'PRIMARY ';
  542. $name = null;
  543. } else {
  544. if (!empty($value['unique'])) {
  545. $out .= 'UNIQUE ';
  546. }
  547. $name = $this->startQuote . $name . $this->endQuote;
  548. }
  549. if (isset($value['type']) && strtolower($value['type']) === 'fulltext') {
  550. $out .= 'FULLTEXT ';
  551. }
  552. $out .= 'KEY ' . $name . ' (';
  553. if (is_array($value['column'])) {
  554. if (isset($value['length'])) {
  555. $vals = array();
  556. foreach ($value['column'] as $column) {
  557. $name = $this->name($column);
  558. if (isset($value['length'])) {
  559. $name .= $this->_buildIndexSubPart($value['length'], $column);
  560. }
  561. $vals[] = $name;
  562. }
  563. $out .= implode(', ', $vals);
  564. } else {
  565. $out .= implode(', ', array_map(array(&$this, 'name'), $value['column']));
  566. }
  567. } else {
  568. $out .= $this->name($value['column']);
  569. if (isset($value['length'])) {
  570. $out .= $this->_buildIndexSubPart($value['length'], $value['column']);
  571. }
  572. }
  573. $out .= ')';
  574. $join[] = $out;
  575. }
  576. return $join;
  577. }
  578. /**
  579. * Generate MySQL index alteration statements for a table.
  580. *
  581. * @param string $table Table to alter indexes for
  582. * @param array $indexes Indexes to add and drop
  583. * @return array Index alteration statements
  584. */
  585. protected function _alterIndexes($table, $indexes) {
  586. $alter = array();
  587. if (isset($indexes['drop'])) {
  588. foreach ($indexes['drop'] as $name => $value) {
  589. $out = 'DROP ';
  590. if ($name === 'PRIMARY') {
  591. $out .= 'PRIMARY KEY';
  592. } else {
  593. $out .= 'KEY ' . $this->startQuote . $name . $this->endQuote;
  594. }
  595. $alter[] = $out;
  596. }
  597. }
  598. if (isset($indexes['add'])) {
  599. $add = $this->buildIndex($indexes['add']);
  600. foreach ($add as $index) {
  601. $alter[] = 'ADD ' . $index;
  602. }
  603. }
  604. return $alter;
  605. }
  606. /**
  607. * Format length for text indexes
  608. *
  609. * @param array $lengths An array of lengths for a single index
  610. * @param string $column The column for which to generate the index length
  611. * @return string Formatted length part of an index field
  612. */
  613. protected function _buildIndexSubPart($lengths, $column) {
  614. if (is_null($lengths)) {
  615. return '';
  616. }
  617. if (!isset($lengths[$column])) {
  618. return '';
  619. }
  620. return '(' . $lengths[$column] . ')';
  621. }
  622. /**
  623. * Returns an detailed array of sources (tables) in the database.
  624. *
  625. * @param string $name Table name to get parameters
  626. * @return array Array of table names in the database
  627. */
  628. public function listDetailedSources($name = null) {
  629. $condition = '';
  630. if (is_string($name)) {
  631. $condition = ' WHERE name = ' . $this->value($name);
  632. }
  633. $result = $this->_connection->query('SHOW TABLE STATUS ' . $condition, PDO::FETCH_ASSOC);
  634. if (!$result) {
  635. $result->closeCursor();
  636. return array();
  637. } else {
  638. $tables = array();
  639. foreach ($result as $row) {
  640. $tables[$row['Name']] = (array)$row;
  641. unset($tables[$row['Name']]['queryString']);
  642. if (!empty($row['Collation'])) {
  643. $charset = $this->getCharsetName($row['Collation']);
  644. if ($charset) {
  645. $tables[$row['Name']]['charset'] = $charset;
  646. }
  647. }
  648. }
  649. $result->closeCursor();
  650. if (is_string($name) && isset($tables[$name])) {
  651. return $tables[$name];
  652. }
  653. return $tables;
  654. }
  655. }
  656. /**
  657. * Converts database-layer column types to basic types
  658. *
  659. * @param string $real Real database-layer column type (i.e. "varchar(255)")
  660. * @return string Abstract column type (i.e. "string")
  661. */
  662. public function column($real) {
  663. if (is_array($real)) {
  664. $col = $real['name'];
  665. if (isset($real['limit'])) {
  666. $col .= '(' . $real['limit'] . ')';
  667. }
  668. return $col;
  669. }
  670. $col = str_replace(')', '', $real);
  671. $limit = $this->length($real);
  672. if (strpos($col, '(') !== false) {
  673. list($col, $vals) = explode('(', $col);
  674. }
  675. if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
  676. return $col;
  677. }
  678. if (($col === 'tinyint' && $limit === 1) || $col === 'boolean') {
  679. return 'boolean';
  680. }
  681. if (strpos($col, 'bigint') !== false || $col === 'bigint') {
  682. return 'biginteger';
  683. }
  684. if (strpos($col, 'int') !== false) {
  685. return 'integer';
  686. }
  687. if (strpos($col, 'char') !== false || $col === 'tinytext') {
  688. return 'string';
  689. }
  690. if (strpos($col, 'text') !== false) {
  691. return 'text';
  692. }
  693. if (strpos($col, 'blob') !== false || $col === 'binary') {
  694. return 'binary';
  695. }
  696. if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) {
  697. return 'float';
  698. }
  699. if (strpos($col, 'enum') !== false) {
  700. return "enum($vals)";
  701. }
  702. return 'text';
  703. }
  704. /**
  705. * Gets the schema name
  706. *
  707. * @return string The schema name
  708. */
  709. public function getSchemaName() {
  710. return $this->config['database'];
  711. }
  712. /**
  713. * Check if the server support nested transactions
  714. *
  715. * @return boolean
  716. */
  717. public function nestedTransactionSupported() {
  718. return $this->useNestedTransactions && version_compare($this->getVersion(), '4.1', '>=');
  719. }
  720. }