diff --git a/src/Pluf/DB/Schema.php b/src/Pluf/DB/Schema.php index 424c2f0..d24ff2b 100644 --- a/src/Pluf/DB/Schema.php +++ b/src/Pluf/DB/Schema.php @@ -96,4 +96,27 @@ class Pluf_DB_Schema } return true; } + + /** + * Given a column name or a string with column names in the format + * "column1, column2, column3", returns the escaped correctly + * quoted column names. This is good for index creation. + * + * @param string Column + * @param Pluf_DB DB handler + * @return string Quoted for the DB column(s) + */ + public static function quoteColumn($col, $db) + { + if (false !== strpos($col, ',')) { + $cols = explode(',', $col); + } else { + $cols = array($col); + } + $res = array(); + foreach ($cols as $col) { + $res[] = $db->qn(trim($col)); + } + return implode(', ', $res); + } } diff --git a/src/Pluf/DB/Schema/PostgreSQL.php b/src/Pluf/DB/Schema/PostgreSQL.php index eedff82..9ab02e0 100644 --- a/src/Pluf/DB/Schema/PostgreSQL.php +++ b/src/Pluf/DB/Schema/PostgreSQL.php @@ -158,11 +158,13 @@ class Pluf_DB_Schema_PostgreSQL } else { $unique = ''; } + $index[$this->con->pfx.$model->_a['table'].'_'.$idx] = sprintf('CREATE '.$unique.'INDEX %s ON %s (%s);', $this->con->pfx.$model->_a['table'].'_'.$idx, $this->con->pfx.$model->_a['table'], - $val['col']); + Pluf_DB_Schema::quoteColumn($val['col'], $this->con) + ); } foreach ($model->_a['cols'] as $col => $val) { $field = new $val['type'](); @@ -171,7 +173,8 @@ class Pluf_DB_Schema_PostgreSQL sprintf('CREATE UNIQUE INDEX %s ON %s (%s);', $this->con->pfx.$model->_a['table'].'_'.$col.'_unique_idx', $this->con->pfx.$model->_a['table'], - $col); + Pluf_DB_Schema::quoteColumn($col, $this->con) + ); } } return $index; diff --git a/src/Pluf/DB/Schema/SQLite.php b/src/Pluf/DB/Schema/SQLite.php index 5676992..a2c71e7 100644 --- a/src/Pluf/DB/Schema/SQLite.php +++ b/src/Pluf/DB/Schema/SQLite.php @@ -158,20 +158,14 @@ class Pluf_DB_Schema_SQLite if (!isset($val['col'])) { $val['col'] = $idx; } - if (false !== strpos($val['col'], ',')) { - $out = array(); - foreach (explode(',', $val['col']) as $col) { - $out[] = $this->con->qn(trim($col)); - } - $val['col'] = implode(', ', $out); - } else { - $val['col'] = $this->con->qn($val['col']); - } + $unique = (isset($val['type']) && ($val['type'] == 'unique')) ? 'UNIQUE ' : ''; $index[$this->con->pfx.$model->_a['table'].'_'.$idx] = - sprintf('CREATE INDEX %s ON %s (%s);', + sprintf('CREATE %sINDEX %s ON %s (%s);', + $unique, $this->con->pfx.$model->_a['table'].'_'.$idx, $this->con->pfx.$model->_a['table'], - $val['col']); + Pluf_DB_Schema::quoteColumn($val['col'], $this->con) + ); } foreach ($model->_a['cols'] as $col => $val) { $field = new $val['type'](); @@ -180,14 +174,15 @@ class Pluf_DB_Schema_SQLite sprintf('CREATE INDEX %s ON %s (%s);', $this->con->pfx.$model->_a['table'].'_'.$col.'_foreignkey_idx', $this->con->pfx.$model->_a['table'], - $this->con->qn($col)); + Pluf_DB_Schema::quoteColumn($col, $this->con)); } if (isset($val['unique']) and $val['unique'] == true) { $index[$this->con->pfx.$model->_a['table'].'_'.$col.'_unique'] = sprintf('CREATE UNIQUE INDEX %s ON %s (%s);', $this->con->pfx.$model->_a['table'].'_'.$col.'_unique_idx', $this->con->pfx.$model->_a['table'], - $this->con->qn($col)); + Pluf_DB_Schema::quoteColumn($col, $this->con) + ); } } return $index; diff --git a/src/Pluf/Tests/Model/Schema.php b/src/Pluf/Tests/Model/Schema.php new file mode 100644 index 0000000..ee949bc --- /dev/null +++ b/src/Pluf/Tests/Model/Schema.php @@ -0,0 +1,97 @@ +_a['verbose'] = 'compressed'; + $this->_a['table'] = 'compressed'; + $this->_a['model'] = __CLASS__; + $this->_a['cols'] = array( + // It is mandatory to have an "id" column. + 'id' => + array( + 'type' => 'Pluf_DB_Field_Sequence', + //It is automatically added. + 'blank' => true, + ), + 'column1' => + array( + 'type' => 'Pluf_DB_Field_Varchar', + ), + 'column2' => + array( + 'type' => 'Pluf_DB_Field_Varchar', + ), + 'column3' => + array( + 'type' => 'Pluf_DB_Field_Varchar', + ), + ); + $this->_a['idx'] = array( + 'test_idx' => + array( + 'col' => 'column1, column2, column3', + 'type' => 'unique', + ), + ); + + } +} + + +class Pluf_Tests_Model_Schema extends UnitTestCase { + + function __construct() + { + parent::__construct('Test the compressed field.'); + } + + function testCreate() + { + $db = Pluf::db(); + $schema = new Pluf_DB_Schema($db); + $m = new Pluf_Tests_Model_Schema_Model(); + $schema->model = $m; + $schema->createTables(); + $m->column1 = 'Youplaboum'; + $m->column2 = 'Youplaboum'; + $m->column3 = 'Youplaboum'; + $m->create(); + $this->assertEqual(1, $m->id); + $m = new Pluf_Tests_Model_Schema_Model(); + $m->column1 = 'Youplaboum'; + $m->column2 = 'Youplaboum'; + $m->column3 = 'Youplaboum'; + try { + $m->create(); + $this->assertNotEqual(2, $m->id, 'Should not be able to create.'); + } catch (Exception $e) { + // do nothing + } + $schema->dropTables(); + } +} \ No newline at end of file diff --git a/src/Pluf/conf/pluf.test.php b/src/Pluf/conf/pluf.test.php index 87a7ad1..b04d7a1 100644 --- a/src/Pluf/conf/pluf.test.php +++ b/src/Pluf/conf/pluf.test.php @@ -53,7 +53,7 @@ $cfg['app_views'] = dirname(__FILE__).'/../app-views.php'; $cfg['db_login'] = 'testpluf'; $cfg['db_password'] = 'testpluf'; $cfg['db_server'] = 'localhost'; -$cfg['db_database'] = $cfg['tmp_folder'].'/pluf.tmp.sqlite.db'; +$cfg['db_database'] = ':memory:'; $cfg['app_base'] = '/testapp'; $cfg['url_format'] = 'simple';