}
}
- if (!empty($unsigned)) {
- $sql .= ' UNSIGNED';
- }
-
return $sql;
}
}
/**
+ * The sql for this column type
+ *
+ * @param string $type
+ * @param string $limit
+ */
+ 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;
+ }
+
+ /**
* Add AFTER option
*
* @param string $sql
$limit = isset($options['limit']) ? $options['limit'] : null;
$precision = isset($options['precision']) ? $options['precision'] : null;
$scale = isset($options['scale']) ? $options['scale'] : null;
- $unsigned = isset($options['unsigned']) ? $options['unsigned'] : null;
// Add the column.
- $this->execute('ALTER TABLE '.$this->quoteTableName($tableName).' ADD COLUMN '.$this->quoteColumnName($columnName).' '.$this->typeToSql($type, $limit, $precision, $scale, $unsigned));
+ $this->execute('ALTER TABLE '.$this->quoteTableName($tableName).' ADD COLUMN '.$this->quoteColumnName($columnName).' '.$this->typeToSql($type, $limit, $precision, $scale));
$default = isset($options['default']) ? $options['default'] : null;
$notnull = isset($options['null']) && $options['null'] === false;
$limit = isset($options['limit']) ? $options['limit'] : null;
$precision = isset($options['precision']) ? $options['precision'] : null;
$scale = isset($options['scale']) ? $options['scale'] : null;
- $unsigned = isset($options['unsigned']) ? $options['unsigned'] : null;
$quotedTableName = $this->quoteTableName($tableName);
try {
- $this->execute('ALTER TABLE '.$quotedTableName.' ALTER COLUMN '.$this->quoteColumnName($columnName).' TYPE '.$this->typeToSql($type, $limit, $precision, $scale, $unsigned));
+ $this->execute('ALTER TABLE '.$quotedTableName.' ALTER COLUMN '.$this->quoteColumnName($columnName).' TYPE '.$this->typeToSql($type, $limit, $precision, $scale));
} 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, $unsigned).')');
+ $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).')');
} else {
- $this->execute('UPDATE '.$quotedTableName.' SET '.$this->quoteColumnName($tmpColumnName).' = CAST('.$this->quoteColumnName($columnName).' AS '.$this->typeToSql($type, $limit, $precision, $scale, $unsigned).')');
+ $this->execute('UPDATE '.$quotedTableName.' SET '.$this->quoteColumnName($tmpColumnName).' = CAST('.$this->quoteColumnName($columnName).' AS '.$this->typeToSql($type, $limit, $precision, $scale).')');
}
$this->removeColumn($tableName, $columnName);
*/
public function typeToSql($type, $limit = null, $precision = null, $scale = null, $unsigned = null)
{
- if ($type != 'integer') return parent::typeToSql($type, $limit, $precision, $scale, $unsigned);
-
- $unsigned = !empty($unsigned) ? ' UNSIGNED' : '';
+ if ($type != 'integer') return parent::typeToSql($type, $limit, $precision, $scale);
switch ($limit) {
case 1:
case 2:
- return 'smallint' . $unsigned;
+ return 'smallint';
case 3:
case 4:
case null:
- return 'integer' . $unsigned;
+ return 'integer';
case 5:
case 6:
case 7:
case 8:
- return 'bigint' . $unsigned;
+ return 'bigint';
default:
throw new Horde_Db_Exception("No integer type has byte size $limit. Use a numeric with precision 0 instead.");
}
$this->assertEquals('integer', $col->getType());
$this->assertEquals(false, $col->isNull());
$this->assertEquals(10, $col->getLimit());
+ $this->assertEquals(true, $col->isUnsigned());
$this->assertEquals('', $col->getDefault());
$this->assertEquals('int(10) unsigned', $col->getSqlType());
$this->assertEquals(false, $col->isText());
$this->assertEquals('decimal(5,2)', $afterChange->getSqlType());
}
+ public function testChangeColumnUnsigned()
+ {
+ $table = $this->_conn->createTable('testings');
+ $table->column('foo', 'integer');
+ $table->end();
+
+ $beforeChange = $this->_getColumn('testings', 'foo');
+ $this->assertFalse($beforeChange->isUnsigned());
+
+ $this->_conn->execute("INSERT INTO testings (id, foo) VALUES (1, -1)");
+
+ $this->_conn->changeColumn('testings', 'foo', 'integer', array('unsigned' => true));
+
+ $afterChange = $this->_getColumn('testings', 'foo');
+ $this->assertTrue($afterChange->isUnsigned());
+
+ $row = (object)$this->_conn->selectOne('SELECT * FROM testings');
+ $this->assertEquals(0, $row->foo);
+ }
+
public function testRenameColumn()
{
$this->_createTestUsersTable();
public function testTypeToSqlInt()
{
- $result = $this->_conn->typeToSql('integer', '11');
+ $result = $this->_conn->typeToSql('integer');
$this->assertEquals('int(11)', $result);
}
+ public function testTypeToSqlIntUnsigned()
+ {
+ $result = $this->_conn->typeToSql('integer', null, null, null, true);
+ $this->assertEquals('int(10) UNSIGNED', $result);
+ }
+
public function testTypeToSqlIntLimit()
{
$result = $this->_conn->typeToSql('integer', '1');
$this->assertEquals('integer', $col->getType());
$this->assertEquals(false, $col->isNull());
$this->assertEquals(10, $col->getLimit());
+ $this->assertEquals(true, $col->isUnsigned());
$this->assertEquals('', $col->getDefault());
$this->assertEquals('int(10) unsigned', $col->getSqlType());
$this->assertEquals(false, $col->isText());
$this->assertEquals('decimal(5,2)', $afterChange->getSqlType());
}
+ public function testChangeColumnUnsigned()
+ {
+ $table = $this->_conn->createTable('testings');
+ $table->column('foo', 'integer');
+ $table->end();
+
+ $beforeChange = $this->_getColumn('testings', 'foo');
+ $this->assertFalse($beforeChange->isUnsigned());
+
+ $this->_conn->execute("INSERT INTO testings (id, foo) VALUES (1, -1)");
+
+ $this->_conn->changeColumn('testings', 'foo', 'integer', array('unsigned' => true));
+
+ $afterChange = $this->_getColumn('testings', 'foo');
+ $this->assertTrue($afterChange->isUnsigned());
+
+ $row = (object)$this->_conn->selectOne('SELECT * FROM testings');
+ $this->assertEquals(0, $row->foo);
+ }
+
public function testRenameColumn()
{
$this->_createTestUsersTable();
public function testTypeToSqlInt()
{
- $result = $this->_conn->typeToSql('integer', '11');
+ $result = $this->_conn->typeToSql('integer');
$this->assertEquals('int(11)', $result);
}
+ public function testTypeToSqlIntUnsigned()
+ {
+ $result = $this->_conn->typeToSql('integer', null, null, null, true);
+ $this->assertEquals('int(10) UNSIGNED', $result);
+ }
+
public function testTypeToSqlIntLimit()
{
$result = $this->_conn->typeToSql('integer', '1');
public function testTypeToSqlInt()
{
- $result = $this->_conn->typeToSql('integer', '11');
- $this->assertEquals('int(11)', $result);
+ $result = $this->_conn->typeToSql('integer');
+ $this->assertEquals('int', $result);
}
public function testTypeToSqlIntLimit()
$this->assertEquals('"col_name" decimal(5, 2)', $col->toSql());
}
- public function testToSqlUnsigned()
- {
- $col = new Horde_Db_Adapter_Base_ColumnDefinition(
- $this->_conn, 'col_name', 'integer', null, null, null, true
- );
- $this->assertEquals('"col_name" integer UNSIGNED', $col->toSql());
-
- // set attribute instead
- $col = new Horde_Db_Adapter_Base_ColumnDefinition(
- $this->_conn, 'col_name', 'integer'
- );
- $col->setUnsigned(true);
- $this->assertEquals('"col_name" integer UNSIGNED', $col->toSql());
- }
-
public function testToSqlNotNull()
{
$col = new Horde_Db_Adapter_Base_ColumnDefinition(
{
$col = new Horde_Db_Adapter_Postgresql_Column('age', 'NULL', 'int(11)');
$this->assertEquals('integer', $col->getType());
- $this->assertFalse($col->isUnsigned());
- }
-
- public function testTypeIntegerUnsigned()
- {
- $col = new Horde_Db_Adapter_Postgresql_Column('age', 'NULL', 'integer UNSIGNED');
- $this->assertTrue($col->isUnsigned());
}
public function testTypeFloat()
$this->assertEquals('"col_name" decimal(5, 2)', $col->toSql());
}
- public function testToSqlUnsigned()
- {
- $col = new Horde_Db_Adapter_Base_ColumnDefinition(
- $this->_conn, 'col_name', 'integer', null, null, null, true
- );
- $this->assertEquals('"col_name" int UNSIGNED', $col->toSql());
-
- // set attribute instead
- $col = new Horde_Db_Adapter_Base_ColumnDefinition(
- $this->_conn, 'col_name', 'integer'
- );
- $col->setUnsigned(true);
- $this->assertEquals('"col_name" int UNSIGNED', $col->toSql());
- }
-
public function testToSqlNotNull()
{
$col = new Horde_Db_Adapter_Base_ColumnDefinition(
{
$col = new Horde_Db_Adapter_Sqlite_Column('age', 'NULL', 'int(11)');
$this->assertEquals('integer', $col->getType());
- $this->assertFalse($col->isUnsigned());
- }
-
- public function testTypeIntegerUnsigned()
- {
- $col = new Horde_Db_Adapter_Sqlite_Column('age', 'NULL', 'int UNSIGNED');
- $this->assertTrue($col->isUnsigned());
}
public function testTypeFloat()