UNSIGNED is only relevant to MySQL. Adjust accordingly. 598 passing tests.
authorChuck Hagenbuch <chuck@horde.org>
Tue, 12 Jan 2010 05:14:11 +0000 (00:14 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 12 Jan 2010 05:14:53 +0000 (00:14 -0500)
framework/Db/lib/Horde/Db/Adapter/Base/Schema.php
framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php
framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php
framework/Db/test/Horde/Db/Adapter/MysqliTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php
framework/Db/test/Horde/Db/Adapter/Postgresql/ColumnDefinitionTest.php
framework/Db/test/Horde/Db/Adapter/Postgresql/ColumnTest.php
framework/Db/test/Horde/Db/Adapter/Sqlite/ColumnDefinitionTest.php
framework/Db/test/Horde/Db/Adapter/Sqlite/ColumnTest.php

index 14358c6..7bb5ba1 100644 (file)
@@ -691,10 +691,6 @@ abstract class Horde_Db_Adapter_Base_Schema
             }
         }
 
-        if (!empty($unsigned)) {
-            $sql .= ' UNSIGNED';
-        }
-
         return $sql;
     }
 
index d0f3ec0..55977cc 100644 (file)
@@ -400,6 +400,36 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Base_Schema
     }
 
     /**
+     * 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
index 65e6083..c825a92 100644 (file)
@@ -450,10 +450,9 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
         $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;
@@ -473,12 +472,11 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
         $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.
@@ -503,9 +501,9 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
                 $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);
@@ -566,23 +564,21 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
      */
     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.");
         }
index 7719aa2..5c622a5 100644 (file)
@@ -462,6 +462,7 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $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());
@@ -798,6 +799,26 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $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();
@@ -1063,10 +1084,16 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
 
     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');
index d6134d3..c6a55f9 100644 (file)
@@ -482,6 +482,7 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $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());
@@ -818,6 +819,26 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $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();
@@ -1083,10 +1104,16 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
 
     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');
index bb59fe7..cbb2cbc 100644 (file)
@@ -971,8 +971,8 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
 
     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()
index 262483f..0478b1e 100644 (file)
@@ -77,21 +77,6 @@ class Horde_Db_Adapter_Postgresql_ColumnDefinitionTest extends PHPUnit_Framework
         $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(
index 5886725..9981a71 100644 (file)
@@ -87,13 +87,6 @@ class Horde_Db_Adapter_Postgresql_ColumnTest extends PHPUnit_Framework_TestCase
     {
         $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()
index d1f8f98..a782106 100644 (file)
@@ -84,21 +84,6 @@ class Horde_Db_Adapter_Sqlite_ColumnDefinitionTest extends PHPUnit_Framework_Tes
         $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(
index 2d4d139..23eec8e 100644 (file)
@@ -87,13 +87,6 @@ class Horde_Db_Adapter_Sqlite_ColumnTest extends PHPUnit_Framework_TestCase
     {
         $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()