PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Horde/Db/Adapter/Mysql/Schema.php

http://github.com/maintainable/framework
PHP | 354 lines | 177 code | 41 blank | 136 comment | 15 complexity | f20415d233b807a7cb8930255e5511dd MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2007 Maintainable Software, LLC
  4. * Copyright 2008 The Horde Project (http://www.horde.org/)
  5. *
  6. * @author Mike Naberezny <mike@maintainable.com>
  7. * @author Derek DeVries <derek@maintainable.com>
  8. * @author Chuck Hagenbuch <chuck@horde.org>
  9. * @license http://opensource.org/licenses/bsd-license.php
  10. * @category Horde
  11. * @package Horde_Db
  12. * @subpackage Adapter
  13. */
  14. /**
  15. * @author Mike Naberezny <mike@maintainable.com>
  16. * @author Derek DeVries <derek@maintainable.com>
  17. * @author Chuck Hagenbuch <chuck@horde.org>
  18. * @license http://opensource.org/licenses/bsd-license.php
  19. * @category Horde
  20. * @package Horde_Db
  21. * @subpackage Adapter
  22. */
  23. class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Abstract_Schema
  24. {
  25. /*##########################################################################
  26. # Quoting
  27. ##########################################################################*/
  28. /**
  29. * @return string
  30. */
  31. public function quoteColumnName($name)
  32. {
  33. return '`' . str_replace('`', '``', $name) . '`';
  34. }
  35. /**
  36. * @return string
  37. */
  38. public function quoteTableName($name)
  39. {
  40. return str_replace('.', '`.`', $this->quoteColumnName($name));
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function quoteTrue()
  46. {
  47. return '1';
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function quoteFalse()
  53. {
  54. return '0';
  55. }
  56. /*##########################################################################
  57. # Schema Statements
  58. ##########################################################################*/
  59. /**
  60. * The db column types for this adapter
  61. *
  62. * @return array
  63. */
  64. public function nativeDatabaseTypes()
  65. {
  66. return array(
  67. 'primaryKey' => 'int(11) DEFAULT NULL auto_increment PRIMARY KEY',
  68. 'string' => array('name' => 'varchar', 'limit' => 255),
  69. 'text' => array('name' => 'text', 'limit' => null),
  70. 'integer' => array('name' => 'int', 'limit' => 11),
  71. 'float' => array('name' => 'float', 'limit' => null),
  72. 'decimal' => array('name' => 'decimal', 'limit' => null),
  73. 'datetime' => array('name' => 'datetime', 'limit' => null),
  74. 'timestamp' => array('name' => 'datetime', 'limit' => null),
  75. 'time' => array('name' => 'time', 'limit' => null),
  76. 'date' => array('name' => 'date', 'limit' => null),
  77. 'binary' => array('name' => 'blob', 'limit' => null),
  78. 'boolean' => array('name' => 'tinyint', 'limit' => 1),
  79. );
  80. }
  81. /**
  82. * Dump entire schema structure or specific table
  83. *
  84. * @param string $table
  85. * @return string
  86. */
  87. public function structureDump($table=null)
  88. {
  89. foreach ($this->selectAll('SHOW TABLES') as $row) {
  90. if ($table && $table != current($row)) { continue; }
  91. $dump = $this->selectOne('SHOW CREATE TABLE ' . $this->quoteTableName(current($row)));
  92. $creates[] = $dump['Create Table'] . ';';
  93. }
  94. return isset($creates) ? implode("\n\n", $creates) : null;
  95. }
  96. /**
  97. * Create the given db
  98. *
  99. * @param string $name
  100. */
  101. public function createDatabase($name)
  102. {
  103. return $this->execute("CREATE DATABASE `$name`");
  104. }
  105. /**
  106. * Drop the given db
  107. *
  108. * @param string $name
  109. */
  110. public function dropDatabase($name)
  111. {
  112. return $this->execute("DROP DATABASE IF EXISTS `$name`");
  113. }
  114. /**
  115. * Get the name of the current db
  116. *
  117. * @return string
  118. */
  119. public function currentDatabase()
  120. {
  121. return $this->selectValue('SELECT DATABASE() AS db');
  122. }
  123. /**
  124. * Returns the database character set
  125. *
  126. * @return string
  127. */
  128. public function getCharset()
  129. {
  130. return $this->showVariable('character_set_database');
  131. }
  132. /**
  133. * Returns the database collation strategy
  134. *
  135. * @return string
  136. */
  137. public function getCollation()
  138. {
  139. return $this->showVariable('collation_database');
  140. }
  141. /**
  142. * List of tables for the db
  143. *
  144. * @param string $name
  145. */
  146. public function tables($name=null)
  147. {
  148. return $this->selectValues('SHOW TABLES');
  149. }
  150. /**
  151. * List of indexes for the given table
  152. *
  153. * @param string $tableName
  154. * @param string $name
  155. */
  156. public function indexes($tableName, $name=null)
  157. {
  158. $indexes = array();
  159. $currentIndex = null;
  160. foreach ($this->select('SHOW KEYS FROM ' . $this->quoteTableName($tableName)) as $row) {
  161. if ($currentIndex != $row[2]) {
  162. if ($row[2] == 'PRIMARY') continue;
  163. $currentIndex = $row[2];
  164. $indexes[] = (object)array('table' => $row[0],
  165. 'name' => $row[2],
  166. 'unique' => $row[1] == '0',
  167. 'columns' => array());
  168. }
  169. $indexes[sizeof($indexes)-1]->columns[] = $row[4];
  170. }
  171. return $indexes;
  172. }
  173. /**
  174. * @param string $tableName
  175. * @param string $name
  176. */
  177. public function columns($tableName, $name=null)
  178. {
  179. // check cache
  180. $rows = @unserialize($this->_cache->get("tables/$tableName"));
  181. // query to build rows
  182. if (!$rows) {
  183. $rows = $this->selectAll('SHOW FIELDS FROM ' . $this->quoteTableName($tableName), $name);
  184. // write cache
  185. $this->_cache->set("tables/$tableName", serialize($rows));
  186. }
  187. // create columns from rows
  188. $columns = array();
  189. foreach ($rows as $row) {
  190. $columns[] = new Horde_Db_Adapter_Mysql_Column(
  191. $row[0], $row[4], $row[1], $row[2] == 'YES');
  192. }
  193. return $columns;
  194. }
  195. /**
  196. * Override createTable to return a Mysql Table Definition
  197. * param string $name
  198. * param array $options
  199. */
  200. public function createTable($name, $options=array())
  201. {
  202. $pk = isset($options['primaryKey']) && $options['primaryKey'] === false ? false : 'id';
  203. $tableDefinition =
  204. new Horde_Db_Adapter_Mysql_TableDefinition($name, $this, $options);
  205. if ($pk != false) {
  206. $tableDefinition->primaryKey($pk);
  207. }
  208. return $tableDefinition;
  209. }
  210. /**
  211. * @param string $name
  212. * @param array $options
  213. */
  214. public function endTable($name, $options=array())
  215. {
  216. $inno = array('options' => 'ENGINE=InnoDB');
  217. return parent::endTable($name, array_merge($inno, $options));
  218. }
  219. /**
  220. * @param string $name
  221. * @param string $newName
  222. */
  223. public function renameTable($name, $newName)
  224. {
  225. $this->_clearTableCache($name);
  226. return $this->execute('RENAME TABLE '.$this->quoteTableName($name).' TO '.$this->quoteTableName($newName));
  227. }
  228. /**
  229. * @param string $tableName
  230. * @param string $columnName
  231. * @param string $default
  232. */
  233. public function changeColumnDefault($tableName, $columnName, $default)
  234. {
  235. $this->_clearTableCache($tableName);
  236. $quotedTableName = $this->quoteTableName($tableName);
  237. $quotedColumnName = $this->quoteColumnName($columnName);
  238. $sql = "SHOW COLUMNS FROM $quotedTableName LIKE ".$this->quoteString($columnName);
  239. $res = $this->selectOne($sql);
  240. $currentType = $res['Type'];
  241. $default = $this->quote($default);
  242. $sql = "ALTER TABLE $quotedTableName CHANGE $quotedColumnName $quotedColumnName
  243. $currentType DEFAULT $default";
  244. return $this->execute($sql);
  245. }
  246. /**
  247. * @param string $tableName
  248. * @param string $columnName
  249. * @param string $type
  250. * @param array $options
  251. */
  252. public function changeColumn($tableName, $columnName, $type, $options=array())
  253. {
  254. $this->_clearTableCache($tableName);
  255. $quotedTableName = $this->quoteTableName($tableName);
  256. $quotedColumnName = $this->quoteColumnName($columnName);
  257. if (!array_key_exists('default', $options)) {
  258. $row = $this->selectOne("SHOW COLUMNS FROM $quotedTableName LIKE ".$this->quoteString($columnName));
  259. $options['default'] = $row['Default'];
  260. }
  261. $limit = !empty($options['limit']) ? $options['limit'] : null;
  262. $precision = !empty($options['precision']) ? $options['precision'] : null;
  263. $scale = !empty($options['scale']) ? $options['scale'] : null;
  264. $typeSql = $this->typeToSql($type, $limit, $precision, $scale);
  265. $sql = "ALTER TABLE $quotedTableName CHANGE $quotedColumnName $quotedColumnName $typeSql";
  266. $sql = $this->addColumnOptions($sql, $options);
  267. $this->execute($sql);
  268. }
  269. /**
  270. * @param string $tableName
  271. * @param string $columnName
  272. * @param string $newColumnName
  273. */
  274. public function renameColumn($tableName, $columnName, $newColumnName)
  275. {
  276. $this->_clearTableCache($tableName);
  277. $quotedTableName = $this->quoteTableName($tableName);
  278. $quotedColumnName = $this->quoteColumnName($columnName);
  279. $sql = "SHOW COLUMNS FROM $quotedTableName LIKE ".$this->quoteString($columnName);
  280. $res = $this->selectOne($sql);
  281. $currentType = $res["Type"];
  282. $sql = "ALTER TABLE $quotedTableName CHANGE ".
  283. $quotedColumnName.' '.
  284. $this->quoteColumnName($newColumnName)." ".
  285. $currentType;
  286. return $this->execute($sql);
  287. }
  288. /**
  289. * SHOW VARIABLES LIKE 'name'
  290. *
  291. * @param string $name
  292. * @return string
  293. */
  294. public function showVariable($name)
  295. {
  296. return $this->selectValue('SHOW VARIABLES LIKE '.$this->quoteString($name));
  297. }
  298. /**
  299. * Add AFTER option
  300. *
  301. * @param string $sql
  302. * @param array $options
  303. * @return string
  304. */
  305. public function addColumnOptions($sql, $options)
  306. {
  307. $sql = parent::addColumnOptions($sql, $options);
  308. if (isset($options['after'])) {
  309. $sql .= " AFTER ".$this->quoteColumnName($options['after']);
  310. }
  311. return $sql;
  312. }
  313. }