Skip to content

Commit

Permalink
Merge pull request yiisoft#2349 from resurtm/fixes-2336
Browse files Browse the repository at this point in the history
Fixes yiisoft#2336: PostgreSQL: CDbCommandBuilder uses `NULL` instead of `DEFAULT` as default value for the primary keys of serial type.
  • Loading branch information
resurtm committed May 28, 2013
2 parents f08356d + 2fb846b commit 2ea6127
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Version 1.1.14 work in progress
- Bug #2311: Fixed SQlite default value for timestamp CURRENT_TIMESTAMP (zeeke)
- Bug #2321: CGettextPoFile is now able to parse multiline msgid and msgstr declarations (resurtm)
- Bug #2325: Fixed UTF-8 troubles in CDateTimeParser (error in parsing chinese and thai dates) (s-larionov)
- Bug #2336: PostgreSQL: CDbCommandBuilder used `NULL` instead of `DEFAULT` as default value for the primary keys of serial type (resurtm)
- Bug #2368: Reset error CSS for ':input', which includes SELECT elements (blueyed)
- Bug #2398: Fixed 'Undefined variable: results' E_NOTICE at CProfileLogRoute (klimov-paul)
- Bug #2402: Fixed clientValidation incorrectly rendered as HTML attribute, when used in CActiveForm::error() (mdomba)
Expand Down
1 change: 1 addition & 0 deletions framework/YiiBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ public static function registerAutoloader($callback, $append=false)
'COciSchema' => '/db/schema/oci/COciSchema.php',
'COciTableSchema' => '/db/schema/oci/COciTableSchema.php',
'CPgsqlColumnSchema' => '/db/schema/pgsql/CPgsqlColumnSchema.php',
'CPgsqlCommandBuilder' => '/db/schema/pgsql/CPgsqlCommandBuilder.php',
'CPgsqlSchema' => '/db/schema/pgsql/CPgsqlSchema.php',
'CPgsqlTableSchema' => '/db/schema/pgsql/CPgsqlTableSchema.php',
'CSqliteColumnSchema' => '/db/schema/sqlite/CSqliteColumnSchema.php',
Expand Down
15 changes: 13 additions & 2 deletions framework/db/schema/CDbCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function createInsertCommand($table,$data)
foreach($pks as $pk)
{
$fields[]=$table->getColumn($pk)->rawName;
$placeholders[]='NULL';
$placeholders[]=$this->getIntegerPrimaryKeyDefaultValue();
}
}
$sql="INSERT INTO {$table->rawName} (".implode(', ',$fields).') VALUES ('.implode(', ',$placeholders).')';
Expand Down Expand Up @@ -859,4 +859,15 @@ protected function ensureTable(&$table)
throw new CDbException(Yii::t('yii','Table "{table}" does not exist.',
array('{table}'=>$tableName)));
}
}

/**
* Returns default value of the integer/serial primary key. Default value means that the next
* autoincrement/sequence value would be used.
* @return string default value of the integer/serial primary key.
* @since 1.1.14
*/
protected function getIntegerPrimaryKeyDefaultValue()
{
return 'NULL';
}
}
30 changes: 30 additions & 0 deletions framework/db/schema/pgsql/CPgsqlCommandBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* CPgsqlCommandBuilder class file.
*
* @author Timur Ruziev <resurtm@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* CPgsqlCommandBuilder provides basic methods to create query commands for tables.
*
* @author Timur Ruziev <resurtm@gmail.com>
* @package system.db.schema.pgsql
* @since 1.1.14
*/
class CPgsqlCommandBuilder extends CDbCommandBuilder
{
/**
* Returns default value of the integer/serial primary key. Default value means that the next
* autoincrement/sequence value would be used.
* @return string default value of the integer/serial primary key.
* @since 1.1.14
*/
protected function getIntegerPrimaryKeyDefaultValue()
{
return 'DEFAULT';
}
}
10 changes: 10 additions & 0 deletions framework/db/schema/pgsql/CPgsqlSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,14 @@ public function dropIndex($name, $table)
{
return 'DROP INDEX '.$this->quoteTableName($name);
}

/**
* Creates a command builder for the database.
* This method may be overridden by child classes to create a DBMS-specific command builder.
* @return CPgsqlCommandBuilder command builder instance.
*/
protected function createCommandBuilder()
{
return new CPgsqlCommandBuilder($this);
}
}
21 changes: 20 additions & 1 deletion tests/framework/db/ar/CActiveRecord2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class CActiveRecord2Test extends CTestCase
{
/**
* @var CDbConnection
*/
private $db;

public function setUp()
Expand Down Expand Up @@ -604,4 +607,20 @@ public function testIssue2122()
$this->assertEquals('post 2',$user->postsWithParam[0]->title);
$this->assertEquals('post 3',$user->postsWithParam[1]->title);
}
}

/**
* https://github.com/yiisoft/yii/issues/2336
*/
public function testEmptyModel()
{
$post=new NullablePost2();
$post->insert();

$post=new NullablePost2();
$post->title='dummy';
$post->insert();

$this->assertEquals(2,$this->db->createCommand('SELECT COUNT(*) FROM "test"."nullable_posts"')->queryScalar());
$this->assertEquals(1,$this->db->createCommand('SELECT COUNT(*) FROM "test"."nullable_posts" WHERE LENGTH("title") > 0')->queryScalar());
}
}
34 changes: 28 additions & 6 deletions tests/framework/db/data/models2.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public function tableName()

/**
* @property integer $id
* @property string $first_name
* @property string $last_name
* @property integer $user_id
* @property string $title
* @property string $create_time
* @property integer $author_id
* @property string $content
*/
class Post2 extends CActiveRecord
{
Expand Down Expand Up @@ -81,9 +82,30 @@ public function tableName()

/**
* @property integer $id
* @property string $first_name
* @property string $last_name
* @property integer $user_id
* @property string $title
* @property string $create_time
* @property integer $author_id
* @property string $content
*/
class NullablePost2 extends CActiveRecord
{
public static function model($class=__CLASS__)
{
return parent::model($class);
}

public function tableName()
{
return 'test.nullable_posts';
}
}

/**
* @property integer $id
* @property string $title
* @property string $create_time
* @property integer $author_id
* @property string $content
*/
class PostExt2 extends CActiveRecord
{
Expand Down
11 changes: 11 additions & 0 deletions tests/framework/db/data/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ INSERT INTO test.posts (title, create_time, author_id, content) VALUES ('post 3'
INSERT INTO test.posts (title, create_time, author_id, content) VALUES ('post 4',TIMESTAMP '2004-10-19 10:23:54',2,'content 4');
INSERT INTO test.posts (title, create_time, author_id, content) VALUES ('post 5',TIMESTAMP '2004-10-19 10:23:54',3,'content 5');

CREATE TABLE test.nullable_posts
(
id SERIAL NOT NULL PRIMARY KEY,
title VARCHAR(128) NULL,
create_time TIMESTAMP NULL,
author_id INTEGER NULL,
content TEXT NULL,
CONSTRAINT FK_post_author FOREIGN KEY (author_id)
REFERENCES test.users (id) ON DELETE CASCADE ON UPDATE RESTRICT
);

CREATE TABLE test.comments
(
id SERIAL NOT NULL PRIMARY KEY,
Expand Down

0 comments on commit 2ea6127

Please sign in to comment.