*/
public function quoteString($string)
{
- return "'".str_replace(array('\\', '\''), array('\\\\', '\\\''), $string)."'";
+ return "'" . str_replace(array('\\', '\''), array('\\\\', '\\\''), $string) . "'";
}
/**
$temp = !empty($options['temporary']) ? 'TEMPORARY' : null;
$opts = !empty($options['options']) ? $options['options'] : null;
- $sql = "CREATE $temp TABLE ".$this->quoteTableName($tableDefinition->getName())." (\n".
- $tableDefinition->toSql()."\n".
- ") $opts";
-
+ $sql = sprintf("CREATE %s TABLE %s (\n%s\n) %s",
+ $temp,
+ $this->quoteTableName($tableDefinition->getName()),
+ $tableDefinition->toSql(),
+ $opts);
return $this->execute($sql);
}
$scale = isset($options['scale']) ? $options['scale'] : null;
$unsigned = isset($options['unsigned']) ? $options['unsigned'] : null;
- $sql = 'ALTER TABLE ' . $this->quoteTableName($tableName) .
- ' ADD '.$this->quoteColumnName($columnName) .
- ' '.$this->typeToSql($type, $limit, $precision, $scale, $unsigned);
+ $sql = sprintf('ALTER TABLE %s ADD %s %s',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName),
+ $this->typeToSql($type, $limit, $precision, $scale, $unsigned));
$sql = $this->addColumnOptions($sql, $options);
return $this->execute($sql);
}
{
$this->_clearTableCache($tableName);
- $sql = 'ALTER TABLE ' . $this->quoteTableName($tableName).' DROP '.$this->quoteColumnName($columnName);
+ $sql = sprintf('ALTER TABLE %s DROP %s',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName));
return $this->execute($sql);
}
* @param string $type
* @param array $options
*/
- abstract public function changeColumn($tableName, $columnName, $type, $options=array());
+ abstract public function changeColumn($tableName, $columnName, $type, $options = array());
/**
* Sets a new default value for a column. If you want to set the default
$columnNames = (array)($columnName);
$indexName = $this->indexName($tableName, array('column' => $columnNames));
- $indexType = !empty($options['unique']) ? "UNIQUE" : null;
+ $indexType = !empty($options['unique']) ? 'UNIQUE' : null;
$indexName = !empty($options['name']) ? $options['name'] : $indexName;
foreach ($columnNames as $colName) {
$quotedCols[] = $this->quoteColumnName($colName);
}
$quotedColumnNames = implode(', ', $quotedCols);
- $sql = "CREATE $indexType INDEX ".$this->quoteColumnName($indexName).
- 'ON '.$this->quoteTableName($tableName) . " ($quotedColumnNames)";
+ $sql = sprintf('CREATE %s INDEX %s ON %s (%s)',
+ $indexType,
+ $this->quoteColumnName($indexName),
+ $this->quoteTableName($tableName),
+ $quotedColumnNames);
return $this->execute($sql);
}
$this->_clearTableCache($tableName);
$index = $this->indexName($tableName, $options);
- $sql = "DROP INDEX ".$this->quoteColumnName($index).' ON ' . $this->quoteTableName($tableName);
+ $sql = sprintf('DROP INDEX %s ON %s',
+ $this->quoteColumnName($index),
+ $this->quoteTableName($tableName));
return $this->execute($sql);
}
{
$natives = $this->nativeDatabaseTypes();
$native = isset($natives[$type]) ? $natives[$type] : null;
- if (empty($native)) { return $type; }
+ if (empty($native)) {
+ return $type;
+ }
$sql = is_array($native) ? $native['name'] : $native;
if ($type == 'decimal') {
if (isset($options['default'])) {
$default = $options['default'];
$column = isset($options['column']) ? $options['column'] : null;
- $sql .= ' DEFAULT '.$this->quote($default, $column);
+ $sql .= ' DEFAULT ' . $this->quote($default, $column);
}
return $sql;
*/
public function distinct($columns, $orderBy=null)
{
- return "DISTINCT $columns";
+ return 'DISTINCT ' . $columns;
}
/**
*/
public function addOrderByForAssocLimiting($sql, $options)
{
- return $sql.'ORDER BY '.$options['order'];
+ return $sql . 'ORDER BY ' . $options['order'];
}
*/
protected function _clearTableCache($tableName)
{
- $this->_cache->set("tables/columns/$tableName", '');
- $this->_cache->set("tables/indexes/$tableName", '');
+ $this->_cache->set('tables/columns/' . $tableName, '');
+ $this->_cache->set('tables/indexes/' . $tableName, '');
}
}
{
$opts = 'ENGINE=InnoDB';
if (!empty($options['charset'])) {
- $opts .=' DEFAULT CHARSET=' . $options['charset'];
+ $opts .= ' DEFAULT CHARSET=' . $options['charset'];
}
return parent::endTable($name, array_merge(array('options' => $opts), $options));
}
public function renameTable($name, $newName)
{
$this->_clearTableCache($name);
-
- return $this->execute('ALTER TABLE '.$this->quoteTableName($name).' RENAME '.$this->quoteTableName($newName));
+ $sql = sprintf('ALTER TABLE %s RENAME %s',
+ $this->quoteTableName($name),
+ $this->quoteTableName($newName));
+ return $this->execute($sql);
}
/**
$quotedTableName = $this->quoteTableName($tableName);
$quotedColumnName = $this->quoteColumnName($columnName);
- $sql = "SHOW COLUMNS FROM $quotedTableName LIKE ".$this->quoteString($columnName);
+ $sql = sprintf('SHOW COLUMNS FROM %s LIKE %s',
+ $quotedTableName,
+ $this->quoteString($columnName));
$res = $this->selectOne($sql);
$currentType = $res['Type'];
$default = $this->quote($default);
- $sql = "ALTER TABLE $quotedTableName CHANGE $quotedColumnName $quotedColumnName
- $currentType DEFAULT $default";
+ $sql = sprintf('ALTER TABLE %s CHANGE %s %s %s DEFAULT %s',
+ $quotedTableName,
+ $quotedColumnName,
+ $quotedColumnName,
+ $currentType,
+ $default);
return $this->execute($sql);
}
$quotedColumnName = $this->quoteColumnName($columnName);
if (!array_key_exists('default', $options)) {
- $row = $this->selectOne("SHOW COLUMNS FROM $quotedTableName LIKE ".$this->quoteString($columnName));
+ $sql = sprintf('SHOW COLUMNS FROM %s LIKE %s',
+ $quotedTableName,
+ $this->quoteString($columnName));
+ $row = $this->selectOne($sql);
$options['default'] = $row['Default'];
}
$typeSql = $this->typeToSql($type, $limit, $precision, $scale, $unsigned);
- $sql = "ALTER TABLE $quotedTableName CHANGE $quotedColumnName $quotedColumnName $typeSql";
+ $sql = sprintf('ALTER TABLE %s CHANGE %s %s %s',
+ $quotedTableName,
+ $quotedColumnName,
+ $quotedColumnName,
+ $typeSql);
$sql = $this->addColumnOptions($sql, $options);
$this->execute($sql);
}
$quotedTableName = $this->quoteTableName($tableName);
$quotedColumnName = $this->quoteColumnName($columnName);
- $sql = "SHOW COLUMNS FROM $quotedTableName LIKE ".$this->quoteString($columnName);
+ $sql = sprintf('SHOW COLUMNS FROM %s LIKE %s',
+ $quotedTableName,
+ $this->quoteString($columnName));
$res = $this->selectOne($sql);
- $currentType = $res["Type"];
+ $currentType = $res['Type'];
- $sql = "ALTER TABLE $quotedTableName CHANGE ".
- $quotedColumnName.' '.
- $this->quoteColumnName($newColumnName)." ".
- $currentType;
+ $sql = sprintf('ALTER TABLE %s CHANGE %s %s %s',
+ $quotedTableName,
+ $quotedColumnName,
+ $this->quoteColumnName($newColumnName),
+ $currentType);
return $this->execute($sql);
}
*/
public function showVariable($name)
{
- $value = $this->selectOne('SHOW VARIABLES LIKE '.$this->quoteString($name));
+ $value = $this->selectOne('SHOW VARIABLES LIKE ' . $this->quoteString($name));
if ($value['Variable_name'] == $name) {
return $value['Value'];
} else {
if ($type == 'integer' && !empty($unsigned) && empty($limit)) {
$natives = $this->nativeDatabaseTypes();
$native = isset($natives[$type]) ? $natives[$type] : null;
- if (empty($native)) { return $type; }
+ if (empty($native)) {
+ return $type;
+ }
$nativeLimit = is_array($native) ? $native['limit'] : null;
if (is_integer($nativeLimit)) {
{
$sql = parent::addColumnOptions($sql, $options);
if (isset($options['after'])) {
- $sql .= " AFTER ".$this->quoteColumnName($options['after']);
+ $sql .= ' AFTER ' . $this->quoteColumnName($options['after']);
}
if (!empty($options['autoincrement'])) {
$sql .= ' AUTO_INCREMENT';
}
return $sql;
}
-
}
*/
public function quote($value, $column = null)
{
- if (!$column)
+ if (!$column) {
return parent::quote($value, $column);
+ }
- if (is_string($value) && ($column->getType() == 'binary') && method_exists($column, 'stringToBinary')) {
+ if (is_string($value) &&
+ $column->getType() == 'binary' &&
+ method_exists($column, 'stringToBinary')) {
/*@TODO test blobs/bytea fields with postgres/pdo and figure out how
this should work */
return $this->quotedStringPrefix() . "'" . $column->stringToBinary($value) . "'";
} elseif (is_numeric($value) && $column->getSqlType() == 'money') {
// Not truly string input, so doesn't require (or allow) escape string syntax.
return "'" . $value . "'";
- } elseif (is_string($value) && substr($column->getSqlType(), 0, 3) == 'bit') {
+ } elseif (is_string($value) &&
+ substr($column->getSqlType(), 0, 3) == 'bit') {
if (preg_match('/^[01]*$/', $value)) {
// Bit-string notation
return "B'" . $value . "'";
{
return array(
'primaryKey' => 'serial primary key',
- 'string' => array('name' => 'character varying', 'limit' => 255),
- 'text' => array('name' => 'text', 'limit' => null),
- 'integer' => array('name' => 'integer', 'limit' => null),
- 'float' => array('name' => 'float', 'limit' => null),
- 'decimal' => array('name' => 'decimal', 'limit' => null),
- 'datetime' => array('name' => 'timestamp', 'limit' => null),
- 'timestamp' => array('name' => 'timestamp', 'limit' => null),
- 'time' => array('name' => 'time', 'limit' => null),
- 'date' => array('name' => 'date', 'limit' => null),
- 'binary' => array('name' => 'bytea', 'limit' => null),
- 'boolean' => array('name' => 'boolean', 'limit' => null),
+ 'string' => array('name' => 'character varying', 'limit' => 255),
+ 'text' => array('name' => 'text', 'limit' => null),
+ 'integer' => array('name' => 'integer', 'limit' => null),
+ 'float' => array('name' => 'float', 'limit' => null),
+ 'decimal' => array('name' => 'decimal', 'limit' => null),
+ 'datetime' => array('name' => 'timestamp', 'limit' => null),
+ 'timestamp' => array('name' => 'timestamp', 'limit' => null),
+ 'time' => array('name' => 'time', 'limit' => null),
+ 'date' => array('name' => 'date', 'limit' => null),
+ 'binary' => array('name' => 'bytea', 'limit' => null),
+ 'boolean' => array('name' => 'boolean', 'limit' => null),
);
}
*/
public function primaryKey($tableName, $name = null)
{
- $sql = 'SELECT column_name
- FROM information_schema.constraint_column_usage
- WHERE table_name = ' . $this->quoteString($tableName) .
- ' AND constraint_name = ' . $this->quoteString($tableName . '_pkey');
+ $sql = sprintf('SELECT column_name FROM information_schema.constraint_column_usage WHERE table_name = %s AND constraint_name = %s',
+ $this->quoteString($tableName),
+ $this->quoteString($tableName . '_pkey'));
$pk = $this->selectValues($sql, $name);
return $this->makeIndex($tableName, 'PRIMARY', true, true, $pk);
$sql = "
SELECT distinct i.relname, d.indisunique, a.attname
FROM pg_class t, pg_class i, pg_index d, pg_attribute a
- WHERE i.relkind = 'i'
+ WHERE i.relkind = 'i'
AND d.indexrelid = i.oid
AND d.indisprimary = 'f'
AND t.oid = d.indrelid
public function setSchemaSearchPath($schemaCsv)
{
if ($schemaCsv) {
- $this->execute("SET search_path TO $schemaCsv");
+ $this->execute('SET search_path TO ' . $schemaCsv);
$this->_schemaSearchPath = $schemaCsv;
}
}
{
if (!($pk && $sequence)) {
list($defaultPk, $efaultSequence) = $this->pkAndSequenceFor($table);
- if (!$pk) $pk = $defaultPk;
- if (!$sequence) $sequence = $defaultSequence;
+ if (!$pk) {
+ $pk = $defaultPk;
+ }
+ if (!$sequence) {
+ $sequence = $defaultSequence;
+ }
}
if ($pk) {
$quotedTable = $this->quoteTableName($table);
$quotedPk = $this->quoteColumnName($pk);
- $sql = "SELECT setval($quotedSequence, (SELECT COALESCE(MAX($quotedPk)+(SELECT increment_by FROM $quotedSequence), (SELECT min_value FROM $quotedSequence)) FROM $quotedTable), false)";
+ $sql = sprintf('SELECT setval(%s, (SELECT COALESCE(MAX(%s) + (SELECT increment_by FROM %s), (SELECT min_value FROM %s)) FROM %s), false)',
+ $quotedSequence,
+ $quotedPk,
+ $quotedSequence,
+ $quotedSequence,
+ $quotedTable);
$this->selectValue($sql, 'Reset sequence');
} else {
- if ($this->_logger) { $this->_logger->warn("$table has primary key $pk with no default sequence"); }
+ if ($this->_logger) {
+ $this->_logger->warn(sprintf('%s has primary key %s with no default sequence', $table, $pk));
+ }
}
}
}
pg_depend dep,
pg_namespace name,
pg_constraint cons
- WHERE seq.oid = dep.objid
- AND seq.relkind = 'S'
- AND attr.attrelid = dep.refobjid
- AND attr.attnum = dep.refobjsubid
- AND attr.attrelid = cons.conrelid
- AND attr.attnum = cons.conkey[1]
- AND cons.contype = 'p'
- AND dep.refobjid = '$table'::regclass";
+ WHERE seq.oid = dep.objid
+ AND seq.relkind = 'S'
+ AND attr.attrelid = dep.refobjid
+ AND attr.attnum = dep.refobjsubid
+ AND attr.attrelid = cons.conrelid
+ AND attr.attnum = cons.conkey[1]
+ AND cons.contype = 'p'
+ AND dep.refobjid = '$table'::regclass";
$result = $this->selectOne($sql, 'PK and serial sequence');
if (!$result) {
{
$this->_clearTableCache($name);
- return $this->execute('ALTER TABLE ' . $this->quoteTableName($name) . ' RENAME TO ' . $this->quoteTableName($newName));
+ return $this->execute(sprintf('ALTER TABLE %s RENAME TO %s', $this->quoteTableName($name), $this->quoteTableName($newName)));
}
/**
$this->_clearTableCache($tableName);
$autoincrement = isset($options['autoincrement']) ? $options['autoincrement'] : null;
- $limit = isset($options['limit']) ? $options['limit'] : null;
- $precision = isset($options['precision']) ? $options['precision'] : null;
- $scale = isset($options['scale']) ? $options['scale'] : null;
+ $limit = isset($options['limit']) ? $options['limit'] : null;
+ $precision = isset($options['precision']) ? $options['precision'] : null;
+ $scale = isset($options['scale']) ? $options['scale'] : null;
$sqltype = $this->typeToSql($type, $limit, $precision, $scale);
}
// Add the column.
- $this->execute('ALTER TABLE ' . $this->quoteTableName($tableName) . ' ADD COLUMN ' . $this->quoteColumnName($columnName) . ' ' . $sqltype);
+ $sql = sprintf('ALTER TABLE %s ADD COLUMN %s %s',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName),
+ $sqltype);
+ $this->execute($sql);
$default = isset($options['default']) ? $options['default'] : null;
$notnull = isset($options['null']) && $options['null'] === false;
$this->_clearTableCache($tableName);
$autoincrement = isset($options['autoincrement']) ? $options['autoincrement'] : null;
- $limit = isset($options['limit']) ? $options['limit'] : null;
- $precision = isset($options['precision']) ? $options['precision'] : null;
- $scale = isset($options['scale']) ? $options['scale'] : null;
+ $limit = isset($options['limit']) ? $options['limit'] : null;
+ $precision = isset($options['precision']) ? $options['precision'] : null;
+ $scale = isset($options['scale']) ? $options['scale'] : null;
$quotedTableName = $this->quoteTableName($tableName);
try {
- $this->execute('ALTER TABLE '.$quotedTableName.' ALTER COLUMN '.$this->quoteColumnName($columnName).' TYPE '.$this->typeToSql($type, $limit, $precision, $scale));
+ $sql = sprintf('ALTER TABLE %s ALTER COLUMN %s TYPE %s',
+ $quotedTableName,
+ $this->quoteColumnName($columnName),
+ $this->typeToSql($type, $limit, $precision, $scale));
+ $this->execute($sql);
} catch (Horde_Db_Exception $e) {
// This is PostgreSQL 7.x, or the old type could not be coerced to
// the new type, so we have to use a more arcane way of doing it.
$this->addColumn($tableName, $tmpColumnName, $type, $options);
if ($oldType == 'boolean') {
- $this->execute('UPDATE '.$quotedTableName.' SET '.$this->quoteColumnName($tmpColumnName).' = CAST(CASE WHEN '.$this->quoteColumnName($columnName).' IS TRUE THEN 1 ELSE 0 END AS '.$this->typeToSql($type, $limit, $precision, $scale).')');
+ $sql = sprintf('UPDATE %s SET %s = CAST(CASE WHEN %s IS TRUE THEN 1 ELSE 0 END AS %s)',
+ $quotedTableName,
+ $this->quoteColumnName($tmpColumnName),
+ $this->quoteColumnName($columnName),
+ $this->typeToSql($type, $limit, $precision, $scale));
} else {
- $this->execute('UPDATE '.$quotedTableName.' SET '.$this->quoteColumnName($tmpColumnName).' = CAST('.$this->quoteColumnName($columnName).' AS '.$this->typeToSql($type, $limit, $precision, $scale).')');
+ $sql = sprintf('UPDATE %s SET %s = CAST(%s AS %s)',
+ $quotedTableName,
+ $this->quoteColumnName($tmpColumnName),
+ $this->quoteColumnName($columnName),
+ $this->typeToSql($type, $limit, $precision, $scale));
}
-
+ $this->execute($sql);
$this->removeColumn($tableName, $columnName);
$this->renameColumn($tableName, $tmpColumnName, $columnName);
* default value (NEXTVAL is a postgres keyword, not a text
* value). */
$this->_clearTableCache($tableName);
- $this->execute('ALTER TABLE ' . $this->quoteTableName($tableName) . ' ALTER COLUMN ' . $this->quoteColumnName($columnName) . ' SET DEFAULT NEXTVAL(' . $this->quoteSequenceName($seq_name) . ')');
+ $sql = sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT NEXTVAL(%s)',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName),
+ $this->quoteSequenceName($seq_name));
+ $this->execute($sql);
} elseif (array_key_exists('default', $options)) {
$this->changeColumnDefault($tableName, $columnName, $default);
}
public function changeColumnDefault($tableName, $columnName, $default)
{
$this->_clearTableCache($tableName);
- return $this->execute('ALTER TABLE '.$this->quoteTableName($tableName). ' ALTER COLUMN '.$this->quoteColumnName($columnName).' SET DEFAULT '.$this->quote($default));
+ $sql = sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName),
+ $this->quote($default));
+ return $this->execute($sql);
}
public function changeColumnNull($tableName, $columnName, $null, $default = null)
{
$this->_clearTableCache($tableName);
if (!($null || is_null($default))) {
- $this->execute('UPDATE '.$this->quoteTableName($tableName).' SET '.$this->quoteColumnName($columnName).' = '.$this->quote($default).' WHERE '.$this->quoteColumnName($columnName).' IS NULL');
+ $sql = sprintf('UPDATE %s SET %s = %s WHERE %s IS NULL',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName),
+ $this->quote($default),
+ $this->quoteColumnName($columnName));
+ $this->execute($sql);
}
- return $this->execute('ALTER TABLE '.$this->quoteTableName($tableName).' ALTER '.$this->quoteColumnName($columnName).' '.($null ? 'DROP' : 'SET').' NOT NULL');
+ $sql = sprintf('ALTER TABLE %s ALTER %s %s NOT NULL',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName),
+ $null ? 'DROP' : 'SET');
+ return $this->execute($sql);
}
/**
public function renameColumn($tableName, $columnName, $newColumnName)
{
$this->_clearTableCache($tableName);
- return $this->execute('ALTER TABLE '.$this->quoteTableName($tableName).' RENAME COLUMN '.$this->quoteColumnName($columnName).' TO '.$this->quoteColumnName($newColumnName));
+ $sql = sprintf('ALTER TABLE %s RENAME COLUMN %s TO %s',
+ $this->quoteTableName($tableName),
+ $this->quoteColumnName($columnName),
+ $this->quoteColumnName($newColumnName));
+ return $this->execute($sql);
}
/**
public function removeIndex($tableName, $options = array())
{
$this->_clearTableCache($tableName);
- return $this->execute('DROP INDEX '.$this->indexName($tableName, $options));
+ return $this->execute('DROP INDEX ' . $this->indexName($tableName, $options));
}
/**
public function distinct($columns, $orderBy = null)
{
if (empty($orderBy)) {
- return "DISTINCT $columns";
+ return 'DISTINCT ' . $columns;
}
// Construct a clean list of column names from the ORDER BY clause, removing
// Return a DISTINCT ON() clause that's distinct on the columns we want but includes
// all the required columns for the ORDER BY to work properly.
- return 'DISTINCT ON ('.$colummns.') '.$columns.', '.implode(', ', $orderColumns);
+ return sprintf('DISTINCT ON (%s) %s, %s',
+ $colummns, $columns, implode(', ', $orderColumns));
}
/**
*/
public function addOrderByForAssociationLimiting($sql, $options)
{
- if (empty($options['order'])) return $sql;
+ if (empty($options['order'])) {
+ return $sql;
+ }
$order = array();
foreach (preg_split('/\s*,\s*/', $options['order'], -1, PREG_SPLIT_NO_EMPTY) as $s) {
}
$order = implode(', ', $order);
- return "SELECT * FROM ($sql) AS id_list ORDER BY $order";
+ return sprintf('SELECT * FROM (%s) AS id_list ORDER BY %s',
+ $sql, $order);
}
/**
SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
- WHERE a.attrelid = '.$this->quote($tableName).'::regclass
+ WHERE a.attrelid = ' . $this->quote($tableName) . '::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum', $name);
}
return 0;
}
-
}
public function renameTable($name, $newName)
{
$this->_clearTableCache($name);
-
- return $this->execute('ALTER TABLE ' . $this->quoteTableName($name) . ' RENAME TO ' . $this->quoteTableName($newName));
+ $sql = sprintf('ALTER TABLE %s RENAME TO %s',
+ $this->quoteTableName($name),
+ $this->quoteTableName($newName));
+ return $this->execute($sql);
}
/**
$this->_clearTableCache($tableName);
return $this->_alterTable($tableName, array(),
- create_function('$definition', 'unset($definition["'.$columnName.'"]);'));
+ create_function('$definition', 'unset($definition["' . $columnName . '"]);'));
}
/**
{
$this->_clearTableCache($tableName);
- $defs = array('$definition["'.$columnName.'"]->setType("'.$type.'");');
- if (isset($options['limit'])) { $defs[] = '$definition["'.$columnName.'"]->setLimit("'.$options['limit'].'");'; }
- if (isset($options['null'])) { $defs[] = '$definition["'.$columnName.'"]->setNull("'.$options['null'].'");'; }
- if (isset($options['precision'])) { $defs[] = '$definition["'.$columnName.'"]->setPrecision("'.$options['precision'].'");'; }
- if (isset($options['scale'])) { $defs[] = '$definition["'.$columnName.'"]->setScale("'.$options['scale'].'");'; }
+ $defs = array(sprintf('$definition["%s"]->setType("%s");', $columnName, $type));
+ if (isset($options['limit'])) {
+ $defs[] = sprintf('$definition["%s"]->setLimit("%s");', $columnName, $options['limit']);
+ }
+ if (isset($options['null'])) {
+ $defs[] = sprintf('$definition["%s"]->setNull("%s");', $columnName, $options['null']);
+ }
+ if (isset($options['precision'])) {
+ $defs[] = sprintf('$definition["%s"]->setPrecision("%s");', $columnName, $options['precision']);
+ }
+ if (isset($options['scale'])) {
+ $defs[] = sprintf('$definition["%s"]->setScale("%s");', $columnName, $options['scale']);
+ }
if (array_key_exists('default', $options)) {
if ($options['default'] === true) {
} else {
$default = '"' . $options['default'] . '"';
}
- $defs[] = '$definition["'.$columnName.'"]->setDefault('.$default.');'; }
+ $defs[] = sprintf('$definition["%s"]->setDefault(%s);', $columnName, $default);
+ }
return $this->_alterTable($tableName, array(),
create_function('$definition', implode("\n", $defs)));
$default = is_null($default) ? 'null' : '"' . $default . '"';
return $this->_alterTable($tableName, array(),
- create_function('$definition', '$definition["'.$columnName.'"]->setDefault('.$default.');'));
+ create_function('$definition', sprintf('$definition["%s"]->setDefault(%s);', $columnName, $default)));
}
/**
$this->_clearTableCache($tableName);
$index = $this->indexName($tableName, $options);
- $sql = 'DROP INDEX '.$this->quoteColumnName($index);
+ $sql = 'DROP INDEX ' . $this->quoteColumnName($index);
return $this->execute($sql);
}
{
$this->beginDbTransaction();
- $alteredTableName = "altered_$tableName";
+ $alteredTableName = 'altered_' . $tableName;
$this->_moveTable($tableName, $alteredTableName, array_merge($options, array('temporary' => true)));
$this->_moveTable($alteredTableName, $tableName, array(), $callback);
$definition = $this->createTable($to, $options);
foreach ($fromColumns as $column) {
- $columnName = isset($options['rename'][$column->getName()]) ? $options['rename'][$column->getName()] : $column->getName();
+ $columnName = isset($options['rename'][$column->getName()])
+ ? $options['rename'][$column->getName()]
+ : $column->getName();
$definition->column($columnName, $column->getType(), array(
'limit' => $column->getLimit(),
}
$primaryKey = $this->primaryKey($from);
- if ($primaryKey) { $definition->primaryKey($primaryKey); }
+ if ($primaryKey) {
+ $definition->primaryKey($primaryKey);
+ }
if (is_callable($callback)) {
call_user_func($callback, $definition);
foreach ($this->indexes($from) as $index) {
$name = $index->getName();
- if ($to == "altered_$from") {
- $name = "temp_$name";
- } elseif ($from == "altered_$to") {
+ if ($to == 'altered_' . $from) {
+ $name = 'temp_' . $name;
+ } elseif ($from == 'altered_' . $to) {
$name = substr($name, 5);
}
if (!empty($columns)) {
// Index name can't be the same
- $opts = array('name' => str_replace("_$from_", "_$to_", $name));
- if ($index->unique) { $opts['unique'] = true; }
+ $opts = array('name' => str_replace('_' . $from . '_', '_' . $to . '_', $name));
+ if ($index->unique) {
+ $opts['unique'] = true;
+ }
$this->addIndex($to, $columns, $opts);
}
}
$quotedFrom = $this->quoteTableName($from);
$quotedFromColumns = implode(', ', array_map(array($this, 'quoteColumnName'), $fromColumns));
- $this->execute("INSERT INTO $quotedTo ($quotedToColumns) SELECT $quotedFromColumns FROM $quotedFrom");
+ $sql = sprintf('INSERT INTO %s (%s) SELECT %s FROM %s',
+ $quotedTo,
+ $quotedToColumns,
+ $quotedFromColumns,
+ $quotedFrom);
+ $this->execute($sql);
}
}