Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDO/Adapter: decribeColumns method fixes. #1562

Merged
merged 4 commits into from
Dec 19, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unit test corrected to check CONSTRAINT, sqlite adapter has been corr…
…ected
  • Loading branch information
lantian committed Nov 18, 2013
commit a1a61e354c75ac4d04ff763b1a7cd152830564c1
2 changes: 1 addition & 1 deletion ext/db/adapter/pdo/mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){
/**
* Timestamp as date
*/
if (phalcon_memnstr_str(column_type, SL("TIMESTAMP"))) {
if (phalcon_memnstr_str(column_type, SL("timestamp"))) {
phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE);
break;
}
Expand Down
26 changes: 19 additions & 7 deletions ext/db/adapter/pdo/sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){

PHALCON_INIT_VAR(old_column);

phalcon_is_iterable(describe, &ah0, &hp0, 0, 0);
phalcon_is_iterable(describe, &ah0, &hp0, 0, 0);

while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

Expand All @@ -175,25 +175,28 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){

PHALCON_OBS_NVAR(column_type);
phalcon_array_fetch_long(&column_type, field, 2, PH_NOISY);

/**
* Check the column type to get the correct Phalcon type
*/
while (1) {
while (1) {

/**
* Tinyint(1) is boolean
*/
if (phalcon_memnstr_str(column_type, SL("tinyint(1)"))) {
phalcon_array_update_string_long(&definition, SL("type"), 8, PH_SEPARATE);
phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_SEPARATE);
phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_SEPARATE);
ZVAL_STRING(column_type, "boolean", 1); // Change column type to skip size check.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory leak here

break;
}

/**
* Smallint/Bigint/Integers/Int are int
*/
if (phalcon_memnstr_str(column_type, SL("int"))) {
PHALCON_INIT_NVAR(pos);
phalcon_fast_stripos_str(pos, column_type, SL("int"));
if (PHALCON_IS_NOT_FALSE(pos)) {
phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE);
phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE);
phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE);
Expand Down Expand Up @@ -226,6 +229,14 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){
break;
}

/**
* Timestamp as date
*/
if (phalcon_memnstr_str(column_type, SL("timestamp"))) {
phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE);
break;
}

/**
* Decimals are floats
*/
Expand All @@ -245,7 +256,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){
}

/**
* Date/Datetime are varchars
* Special type for datetime
*/
if (phalcon_memnstr_str(column_type, SL("datetime"))) {
phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE);
Expand Down Expand Up @@ -277,11 +288,12 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){
phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE);
break;
}

/**
* By default is string
*/
phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE);
break;
break;
}

if (phalcon_memnstr_str(column_type, SL("("))) {
Expand Down
8 changes: 4 additions & 4 deletions unit-tests/DbDialectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ public function testMysqlDialect()
$references = $this->getReferences();

//Add Foreign Key
$this->assertEquals($dialect->addForeignKey('table', null, $references['fk1']), 'ALTER TABLE `table` ADD FOREIGN KEY `fk1`(`column1`) REFERENCES `ref_table`(`column2`)');
$this->assertEquals($dialect->addForeignKey('table', 'schema', $references['fk1']), 'ALTER TABLE `schema`.`table` ADD FOREIGN KEY `fk1`(`column1`) REFERENCES `ref_table`(`column2`)');
$this->assertEquals($dialect->addForeignKey('table', null, $references['fk2']), 'ALTER TABLE `table` ADD FOREIGN KEY `fk2`(`column3`, `column4`) REFERENCES `ref_table`(`column5`, `column6`)');
$this->assertEquals($dialect->addForeignKey('table', 'schema', $references['fk2']), 'ALTER TABLE `schema`.`table` ADD FOREIGN KEY `fk2`(`column3`, `column4`) REFERENCES `ref_table`(`column5`, `column6`)');
$this->assertEquals($dialect->addForeignKey('table', null, $references['fk1']), 'ALTER TABLE `table` ADD CONSTRAINT `fk1` FOREIGN KEY (`column1`) REFERENCES `ref_table`(`column2`)');
$this->assertEquals($dialect->addForeignKey('table', 'schema', $references['fk1']), 'ALTER TABLE `schema`.`table` ADD CONSTRAINT `fk1` FOREIGN KEY (`column1`) REFERENCES `ref_table`(`column2`)');
$this->assertEquals($dialect->addForeignKey('table', null, $references['fk2']), 'ALTER TABLE `table` ADD CONSTRAINT `fk2` FOREIGN KEY (`column3`, `column4`) REFERENCES `ref_table`(`column5`, `column6`)');
$this->assertEquals($dialect->addForeignKey('table', 'schema', $references['fk2']), 'ALTER TABLE `schema`.`table` ADD CONSTRAINT `fk2` FOREIGN KEY (`column3`, `column4`) REFERENCES `ref_table`(`column5`, `column6`)');

$this->assertEquals($dialect->dropForeignKey('table', null, 'fk1'), 'ALTER TABLE `table` DROP FOREIGN KEY `fk1`');
$this->assertEquals($dialect->dropForeignKey('table', 'schema', 'fk1'), 'ALTER TABLE `schema`.`table` DROP FOREIGN KEY `fk1`');
Expand Down