Mysql.php 21 KB

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