/usr/share/php/Horde/Db/Adapter/Mysql/Schema.php is in php-horde-db 2.3.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | <?php
/**
* Copyright 2007 Maintainable Software, LLC
* Copyright 2008-2016 Horde LLC (http://www.horde.org/)
*
* @author Mike Naberezny <mike@maintainable.com>
* @author Derek DeVries <derek@maintainable.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
* @subpackage Adapter
*/
/**
* Class for MySQL-specific managing of database schemes and handling of SQL
* dialects and quoting.
*
* @author Mike Naberezny <mike@maintainable.com>
* @author Derek DeVries <derek@maintainable.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
* @subpackage Adapter
*/
class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Base_Schema
{
/*##########################################################################
# Object factories
##########################################################################*/
/**
* Factory for Column objects.
*
* @param string $name The column's name, such as "supplier_id" in
* "supplier_id int(11)".
* @param string $default The type-casted default value, such as "new" in
* "sales_stage varchar(20) default 'new'".
* @param string $sqlType Used to extract the column's type, length and
* signed status, if necessary. For example
* "varchar" and "60" in "company_name varchar(60)"
* or "unsigned => true" in "int(10) UNSIGNED".
* @param boolean $null Whether this column allows NULL values.
*
* @return Horde_Db_Adapter_Mysql_Column A column object.
*/
public function makeColumn($name, $default, $sqlType = null, $null = true)
{
return new Horde_Db_Adapter_Mysql_Column($name, $default, $sqlType, $null);
}
/*##########################################################################
# Quoting
##########################################################################*/
/**
* Returns a quoted form of the column name.
*
* @param string $name A column name.
*
* @return string The quoted column name.
*/
public function quoteColumnName($name)
{
return '`' . str_replace('`', '``', $name) . '`';
}
/**
* Returns a quoted form of the table name.
*
* Defaults to column name quoting.
*
* @param string $name A table name.
*
* @return string The quoted table name.
*/
public function quoteTableName($name)
{
return str_replace('.', '`.`', $this->quoteColumnName($name));
}
/*##########################################################################
# Schema Statements
##########################################################################*/
/**
* Returns a hash of mappings from the abstract data types to the native
* database types.
*
* See TableDefinition::column() for details on the recognized abstract
* data types.
*
* @see TableDefinition::column()
*
* @return array A database type map.
*/
public function nativeDatabaseTypes()
{
return array(
'autoincrementKey' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
'string' => array('name' => 'varchar', 'limit' => 255),
'text' => array('name' => 'text', 'limit' => null),
'mediumtext' => array('name' => 'mediumtext', 'limit' => null),
'longtext' => array('name' => 'longtext', 'limit' => null),
'integer' => array('name' => 'int', 'limit' => 11),
'float' => array('name' => 'float', 'limit' => null),
'decimal' => array('name' => 'decimal', 'limit' => null),
'datetime' => array('name' => 'datetime', 'limit' => null),
'timestamp' => array('name' => 'datetime', 'limit' => null),
'time' => array('name' => 'time', 'limit' => null),
'date' => array('name' => 'date', 'limit' => null),
'binary' => array('name' => 'longblob', 'limit' => null),
'boolean' => array('name' => 'tinyint', 'limit' => 1),
);
}
/**
* Returns a list of all tables of the current database.
*
* @return array A table list.
*/
public function tables()
{
return $this->selectValues('SHOW TABLES');
}
/**
* Returns a table's primary key.
*
* @param string $tableName A table name.
* @param string $name (can be removed?)
*
* @return Horde_Db_Adapter_Base_Index The primary key index object.
*/
public function primaryKey($tableName, $name = null)
{
// Share the column cache with the columns() method
$rows = @unserialize($this->cacheRead("tables/columns/$tableName"));
if (!$rows) {
$rows = $this->selectAll(
'SHOW FIELDS FROM ' . $this->quoteTableName($tableName),
$name);
$this->cacheWrite("tables/columns/$tableName", serialize($rows));
}
$pk = $this->makeIndex($tableName, 'PRIMARY', true, true, array());
foreach ($rows as $row) {
if ($row['Key'] == 'PRI') {
$pk->columns[] = $row['Field'];
}
}
return $pk;
}
/**
* Returns a list of tables indexes.
*
* @param string $tableName A table name.
* @param string $name (can be removed?)
*
* @return array A list of Horde_Db_Adapter_Base_Index objects.
*/
public function indexes($tableName, $name=null)
{
$indexes = @unserialize($this->cacheRead("tables/indexes/$tableName"));
if (!$indexes) {
$indexes = array();
$currentIndex = null;
foreach ($this->select('SHOW KEYS FROM ' . $this->quoteTableName($tableName)) as $row) {
if ($currentIndex != $row['Key_name']) {
if ($row['Key_name'] == 'PRIMARY') {
continue;
}
$currentIndex = $row['Key_name'];
$indexes[] = $this->makeIndex(
$tableName, $row['Key_name'], false, $row['Non_unique'] == '0', array());
}
$indexes[count($indexes) - 1]->columns[] = $row['Column_name'];
}
$this->cacheWrite("tables/indexes/$tableName", serialize($indexes));
}
return $indexes;
}
/**
* Returns a list of table columns.
*
* @param string $tableName A table name.
* @param string $name (can be removed?)
*
* @return array A list of Horde_Db_Adapter_Base_Column objects.
*/
public function columns($tableName, $name=null)
{
$rows = @unserialize($this->cacheRead("tables/columns/$tableName"));
if (!$rows) {
$rows = $this->selectAll('SHOW FIELDS FROM ' . $this->quoteTableName($tableName), $name);
$this->cacheWrite("tables/columns/$tableName", serialize($rows));
}
// Create columns from rows.
$columns = array();
foreach ($rows as $row) {
$columns[$row['Field']] = $this->makeColumn(
$row['Field'], $row['Default'], $row['Type'], $row['Null'] == 'YES');
}
return $columns;
}
/**
* Finishes and executes table creation.
*
* @param string|Horde_Db_Adapter_Base_TableDefinition $name
* A table name or object.
* @param array $options
* A list of options. See createTable().
*/
public function endTable($name, $options = array())
{
if ($name instanceof Horde_Db_Adapter_Base_TableDefinition) {
$options = array_merge($name->getOptions(), $options);
}
if (isset($options['options'])) {
$opts = $options['options'];
} else {
if (empty($options['charset'])) {
$options['charset'] = $this->getCharset();
}
$opts = 'ENGINE=InnoDB DEFAULT CHARSET=' . $options['charset'];
}
return parent::endTable($name, array_merge(array('options' => $opts), $options));
}
/**
* Renames a table.
*
* @param string $name A table name.
* @param string $newName The new table name.
*/
public function renameTable($name, $newName)
{
$this->_clearTableCache($name);
$sql = sprintf('ALTER TABLE %s RENAME %s',
$this->quoteTableName($name),
$this->quoteTableName($newName));
return $this->execute($sql);
}
/**
* Changes an existing column's definition.
*
* @param string $tableName A table name.
* @param string $columnName A column name.
* @param string $type A data type.
* @param array $options Column options. See
* Horde_Db_Adapter_Base_TableDefinition#column()
* for details.
*/
public function changeColumn($tableName, $columnName, $type,
$options = array())
{
$this->_clearTableCache($tableName);
$quotedTableName = $this->quoteTableName($tableName);
$quotedColumnName = $this->quoteColumnName($columnName);
$options = array_merge(
array('limit' => null,
'precision' => null,
'scale' => null,
'unsigned' => null),
$options);
$sql = sprintf('SHOW COLUMNS FROM %s LIKE %s',
$quotedTableName,
$this->quoteString($columnName));
$row = $this->selectOne($sql);
if (!array_key_exists('default', $options)) {
$options['default'] = $row['Default'];
$options['column'] = $this->makeColumn($columnName,
$row['Default'],
$row['Type'],
$row['Null'] == 'YES');
}
$typeSql = $this->typeToSql($type, $options['limit'],
$options['precision'], $options['scale'],
$options['unsigned']);
$dropPk = ($type == 'autoincrementKey' && $row['Key'] == 'PRI')
? 'DROP PRIMARY KEY,'
: '';
$sql = sprintf('ALTER TABLE %s %s CHANGE %s %s %s',
$quotedTableName,
$dropPk,
$quotedColumnName,
$quotedColumnName,
$typeSql);
if ($type != 'autoincrementKey') {
$sql = $this->addColumnOptions($sql, $options);
}
$this->execute($sql);
}
/**
* Sets a new default value for a column.
*
* If you want to set the default value to NULL, you are out of luck. You
* need to execute the apppropriate SQL statement yourself.
*
* @param string $tableName A table name.
* @param string $columnName A column name.
* @param mixed $default The new default value.
*/
public function changeColumnDefault($tableName, $columnName, $default)
{
$this->_clearTableCache($tableName);
$quotedTableName = $this->quoteTableName($tableName);
$quotedColumnName = $this->quoteColumnName($columnName);
$sql = sprintf('SHOW COLUMNS FROM %s LIKE %s',
$quotedTableName,
$this->quoteString($columnName));
$res = $this->selectOne($sql);
$column = $this->makeColumn($columnName, $res['Default'], $res['Type'], $res['Null'] == 'YES');
$default = $this->quote($default, $column);
$sql = sprintf('ALTER TABLE %s CHANGE %s %s %s DEFAULT %s',
$quotedTableName,
$quotedColumnName,
$quotedColumnName,
$res['Type'],
$default);
return $this->execute($sql);
}
/**
* Renames a column.
*
* @param string $tableName A table name.
* @param string $columnName A column name.
* @param string $newColumnName The new column name.
*/
public function renameColumn($tableName, $columnName, $newColumnName)
{
$this->_clearTableCache($tableName);
$quotedTableName = $this->quoteTableName($tableName);
$quotedColumnName = $this->quoteColumnName($columnName);
$sql = sprintf('SHOW COLUMNS FROM %s LIKE %s',
$quotedTableName,
$this->quoteString($columnName));
$res = $this->selectOne($sql);
$currentType = $res['Type'];
$sql = sprintf('ALTER TABLE %s CHANGE %s %s %s',
$quotedTableName,
$quotedColumnName,
$this->quoteColumnName($newColumnName),
$currentType);
return $this->execute($sql);
}
/**
* Removes a primary key from a table.
*
* @param string $tableName A table name.
*
* @throws Horde_Db_Exception
*/
public function removePrimaryKey($tableName)
{
$this->_clearTableCache($tableName);
$sql = sprintf('ALTER TABLE %s DROP PRIMARY KEY',
$this->quoteTableName($tableName));
return $this->execute($sql);
}
/**
* Builds the name for an index.
*
* Cuts the index name to the maximum length of 64 characters limited by
* MySQL.
*
* @param string $tableName A table name.
* @param string|array $options Either a column name or index options:
* - column: (string|array) column name(s).
* - name: (string) the index name to fall
* back to if no column names specified.
*/
public function indexName($tableName, $options = array())
{
return substr(parent::indexName($tableName, $options), 0, 64);
}
/**
* Creates a database.
*
* @param string $name A database name.
* @param array $options Database options.
*/
public function createDatabase($name, $options = array())
{
return $this->execute("CREATE DATABASE `$name`");
}
/**
* Drops a database.
*
* @param string $name A database name.
*/
public function dropDatabase($name)
{
return $this->execute("DROP DATABASE IF EXISTS `$name`");
}
/**
* Returns the name of the currently selected database.
*
* @return string The database name.
*/
public function currentDatabase()
{
return $this->selectValue('SELECT DATABASE() AS db');
}
/**
* Generates the SQL definition for a column type.
*
* @param string $type A column type.
* @param integer $limit Maximum column length (non decimal type only)
* @param integer $precision The number precision (decimal type only).
* @param integer $scale The number scaling (decimal columns only).
* @param boolean $unsigned Whether the column is an unsigned number
* (non decimal columns only).
*
* @return string The SQL definition. If $type is not one of the
* internally supported types, $type is returned unchanged.
*/
public function typeToSql($type, $limit = null, $precision = null,
$scale = null, $unsigned = null)
{
// If there is no explicit limit, adjust $nativeLimit for unsigned
// integers.
if ($type == 'integer' && !empty($unsigned) && empty($limit)) {
$natives = $this->nativeDatabaseTypes();
$native = isset($natives[$type]) ? $natives[$type] : null;
if (empty($native)) {
return $type;
}
$nativeLimit = is_array($native) ? $native['limit'] : null;
if (is_integer($nativeLimit)) {
$limit = $nativeLimit - 1;
}
}
$sql = parent::typeToSql($type, $limit, $precision, $scale, $unsigned);
if (!empty($unsigned)) {
$sql .= ' UNSIGNED';
}
return $sql;
}
/**
* Adds default/null options to column SQL definitions.
*
* @param string $sql Existing SQL definition for a column.
* @param array $options Column options:
* - null: (boolean) Whether to allow NULL values.
* - default: (mixed) Default column value.
* - autoincrement: (boolean) Whether the column is
* an autoincrement column. Driver depedendent.
* - after: (string) Insert column after this one.
* MySQL specific.
*
* @return string The manipulated SQL definition.
*/
public function addColumnOptions($sql, $options)
{
$sql = parent::addColumnOptions($sql, $options);
if (isset($options['after'])) {
$sql .= ' AFTER ' . $this->quoteColumnName($options['after']);
}
if (!empty($options['autoincrement'])) {
$sql .= ' AUTO_INCREMENT';
}
return $sql;
}
/**
* Returns an expression using the specified operator.
*
* @param string $lhs The column or expression to test.
* @param string $op The operator.
* @param string $rhs The comparison value.
* @param boolean $bind If true, the method returns the query and a list
* of values suitable for binding as an array.
* @param array $params Any additional parameters for the operator.
*
* @return string|array The SQL test fragment, or an array containing the
* query and a list of values if $bind is true.
*/
public function buildClause($lhs, $op, $rhs, $bind = false,
$params = array())
{
switch ($op) {
case '~':
if ($bind) {
return array($lhs . ' REGEXP ?', array($rhs));
} else {
return $lhs . ' REGEXP ' . $rhs;
}
}
return parent::buildClause($lhs, $op, $rhs, $bind, $params);
}
/*##########################################################################
# MySQL specific methods
##########################################################################*/
/**
* Returns the character set of query results.
*
* @return string The result's charset.
*/
public function getCharset()
{
return $this->showVariable('character_set_results');
}
/**
* Sets the client and result charset.
*
* @param string $charset The character set to use for client queries and
* results.
*/
public function setCharset($charset)
{
$charset = $this->_mysqlCharsetName($charset);
$this->execute('SET NAMES ' . $this->quoteString($charset));
}
/**
* Returns the MySQL name of a character set.
*
* @param string $charset A charset name.
*
* @return string MySQL-normalized charset.
*/
public function _mysqlCharsetName($charset)
{
$charset = preg_replace(array('/[^a-z0-9]/', '/iso8859(\d)/'),
array('', 'latin$1'),
Horde_String::lower($charset));
$validCharsets = $this->selectValues('SHOW CHARACTER SET');
if (!in_array($charset, $validCharsets)) {
throw new Horde_Db_Exception($charset . ' is not supported by MySQL (' . implode(', ', $validCharsets) . ')');
}
return $charset;
}
/**
* Returns the database collation strategy.
*
* @return string Database collation.
*/
public function getCollation()
{
return $this->showVariable('collation_database');
}
/**
* Returns a database variable.
*
* Convenience wrapper around "SHOW VARIABLES LIKE 'name'".
*
* @param string $name A variable name.
*
* @return string The variable value.
* @throws Horde_Db_Exception
*/
public function showVariable($name)
{
$value = $this->selectOne('SHOW VARIABLES LIKE ' . $this->quoteString($name));
if ($value['Variable_name'] == $name) {
return $value['Value'];
} else {
throw new Horde_Db_Exception($name . ' is not a recognized variable');
}
}
/**
*/
public function caseSensitiveEqualityOperator()
{
return '= BINARY';
}
/**
*/
public function limitedUpdateConditions($whereSql, $quotedTableName,
$quotedPrimaryKey)
{
return $whereSql;
}
}
|