Skip to content
This repository
Browse code

Enhancements to the #2090.

  • Loading branch information...
commit f6bd5a492bc3184e5f1baf2860dbc31a0b007512 1 parent 0c71699
resurtm resurtm authored
4 framework/db/schema/CDbSchema.php
@@ -565,7 +565,8 @@ public function dropIndex($name, $table)
565 565 * Builds a SQL statement for adding a primary key constraint to an existing table.
566 566 * @param string $name the name of the primary key constraint.
567 567 * @param string $table the table that the primary key constraint will be added to.
568   - * @param mixed $columns comma separated string or array of columns that the primary key will consist of.
  568 + * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
  569 + * Array value can be passed since 1.1.14.
569 570 * @return string the SQL statement for adding a primary key constraint to an existing table.
570 571 * @since 1.1.13
571 572 */
@@ -592,5 +593,4 @@ public function dropPrimaryKey($name,$table)
592 593 return 'ALTER TABLE ' . $this->quoteTableName($table) . ' DROP CONSTRAINT '
593 594 . $this->quoteColumnName($name);
594 595 }
595   -
596 596 }
10 framework/db/schema/mysql/CMysqlSchema.php
@@ -335,16 +335,16 @@ public function dropPrimaryKey($name,$table)
335 335 }
336 336
337 337 /**
338   - * Builds a SQL statement for adding a primary key constraint too a table.
339   - * @param string $name Not used in the MySql syntax, the primary key is always called PRIMARY and is reserved.
  338 + * Builds a SQL statement for adding a primary key constraint to a table.
  339 + * @param string $name not used in the MySQL syntax, the primary key is always called PRIMARY and is reserved.
340 340 * @param string $table the table that the primary key constraint will be added to.
341   - * @param mixed $columns comma separated string or array of columns that the primary key will consist of.
  341 + * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
342 342 * @return string the SQL statement for adding a primary key constraint to an existing table.
343 343 * @since 1.1.14
344 344 */
345   - public function addPrimaryKey($name, $table, $columns)
  345 + public function addPrimaryKey($name,$table,$columns)
346 346 {
347   - if (is_string($columns))
  347 + if(is_string($columns))
348 348 $columns=preg_split('/\s*,\s*/',$columns,-1,PREG_SPLIT_NO_EMPTY);
349 349 foreach($columns as $i=>$col)
350 350 $columns[$i]=$this->quoteColumnName($col);
4 framework/db/schema/sqlite/CSqliteSchema.php
@@ -300,10 +300,10 @@ public function dropIndex($name, $table)
300 300
301 301 /**
302 302 * Builds a SQL statement for adding a primary key constraint to an existing table.
303   - * Because SQLite does not support adding a primary key on an existing table this method will throw an exception
  303 + * Because SQLite does not support adding a primary key on an existing table this method will throw an exception.
304 304 * @param string $name the name of the primary key constraint.
305 305 * @param string $table the table that the primary key constraint will be added to.
306   - * @param string $columns the name of the column to that the constraint will be added on.
  306 + * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
307 307 * @return string the SQL statement for adding a primary key constraint to an existing table.
308 308 * @since 1.1.13
309 309 */
14 tests/framework/db/schema/CMysql2Test.php
@@ -6,6 +6,9 @@
6 6
7 7 class CMysql2Test extends CTestCase
8 8 {
  9 + /**
  10 + * @var CDbConnection
  11 + */
9 12 private $db;
10 13
11 14 public function setUp()
@@ -136,4 +139,15 @@ public function testDropIndex()
136 139 $expect='DROP INDEX `id_pk` ON `test`';
137 140 $this->assertEquals($expect, $sql);
138 141 }
  142 +
  143 + public function testAddPrimaryKey()
  144 + {
  145 + $sql=$this->db->schema->addPrimaryKey('this-string-is-ignored','table','id');
  146 + $expect='ALTER TABLE `table` ADD PRIMARY KEY (`id` )';
  147 + $this->assertEquals($expect, $sql);
  148 +
  149 + $sql=$this->db->schema->addPrimaryKey('this-string-is-ignored','table',array('id1','id2'));
  150 + $expect='ALTER TABLE `table` ADD PRIMARY KEY (`id1`, `id2` )';
  151 + $this->assertEquals($expect, $sql);
  152 + }
139 153 }

0 comments on commit f6bd5a4

Please sign in to comment.
Something went wrong with that request. Please try again.