Skip to content

Commit

Permalink
Merge pull request #940 from dreamsxin/decimal_scale_1.3.0
Browse files Browse the repository at this point in the history
Add Decimal scale 1.3.0
  • Loading branch information
Phalcon committed Jul 29, 2013
2 parents 852ffd9 + 110e6b5 commit 7a0c019
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
9 changes: 7 additions & 2 deletions ext/db/adapter/pdo/mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){
zval *table, *schema = NULL, *dialect, *ztrue, *sql, *fetch_num;
zval *describe, *old_column = NULL, *size_pattern, *columns;
zval *field = NULL, *definition = NULL, *column_type = NULL, *matches = NULL;
zval *pos = NULL, *match_one = NULL, *attribute = NULL, *column_name = NULL;
zval *pos = NULL, *match_one = NULL, *match_two = NULL, *attribute = NULL, *column_name = NULL;
zval *column = NULL;
HashTable *ah0;
HashPosition hp0;
Expand Down Expand Up @@ -170,7 +170,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){
PHALCON_INIT_VAR(old_column);

PHALCON_INIT_VAR(size_pattern);
ZVAL_STRING(size_pattern, "#\\(([0-9]+)(,[0-9]+)*\\)#", 1);
ZVAL_STRING(size_pattern, "#\\(([0-9]++)(?:,\\s*([0-9]++))?\\)#", 1);

PHALCON_INIT_VAR(columns);
array_init(columns);
Expand Down Expand Up @@ -300,6 +300,11 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){
phalcon_array_fetch_long(&match_one, matches, 1, PH_NOISY);
phalcon_array_update_string(&definition, SL("size"), &match_one, PH_COPY | PH_SEPARATE);
}
if (phalcon_array_isset_long(matches, 2)) {
PHALCON_OBS_NVAR(match_two);
phalcon_array_fetch_long(&match_two, matches, 2, PH_NOISY);
phalcon_array_update_string(&definition, SL("scale"), &match_two, PH_COPY | PH_SEPARATE);
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions ext/db/adapter/pdo/postgresql.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){

zval *table, *schema = NULL, *columns, *dialect, *sql, *fetch_num;
zval *describe, *old_column = NULL, *field = NULL, *definition = NULL;
zval *char_size = NULL, *numeric_size = NULL, *column_type = NULL;
zval *char_size = NULL, *numeric_size = NULL, *numeric_scale = NULL, *column_type = NULL;
zval *attribute = NULL, *column_name = NULL, *column = NULL;
HashTable *ah0;
HashPosition hp0;
Expand Down Expand Up @@ -188,6 +188,9 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){

PHALCON_OBS_NVAR(numeric_size);
phalcon_array_fetch_long(&numeric_size, field, 3, PH_NOISY);

PHALCON_OBS_NVAR(numeric_scale);
phalcon_array_fetch_long(&numeric_scale, field, 4, PH_NOISY);

PHALCON_OBS_NVAR(column_type);
phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY);
Expand All @@ -213,6 +216,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){
phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE);
phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE);
phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&definition, SL("scale"), &numeric_scale, PH_COPY | PH_SEPARATE);
phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE);
} else {
if (phalcon_memnstr_str(column_type, SL("char"))) {
Expand Down Expand Up @@ -269,7 +273,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){
* Check if the field is primary key
*/
PHALCON_OBS_NVAR(attribute);
phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY);
phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY);
if (PHALCON_IS_STRING(attribute, "PRI")) {
phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE);
}
Expand All @@ -278,7 +282,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){
* Check if the column allows null values
*/
PHALCON_OBS_NVAR(attribute);
phalcon_array_fetch_long(&attribute, field, 4, PH_NOISY);
phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY);
if (PHALCON_IS_STRING(attribute, "NO")) {
phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE);
}
Expand All @@ -287,7 +291,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){
* Check if the column is auto increment
*/
PHALCON_OBS_NVAR(attribute);
phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY);
phalcon_array_fetch_long(&attribute, field, 7, PH_NOISY);
if (PHALCON_IS_STRING(attribute, "auto_increment")) {
phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_SEPARATE);
}
Expand Down
9 changes: 7 additions & 2 deletions ext/db/adapter/pdo/sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){
zval *table, *schema = NULL, *columns, *dialect, *size_pattern;
zval *sql, *fetch_num, *describe, *old_column = NULL, *field = NULL;
zval *definition = NULL, *column_type = NULL, *pos = NULL, *attribute = NULL;
zval *matches = NULL, *match_one = NULL, *column_name = NULL, *column = NULL;
zval *matches = NULL, *match_one = NULL, *match_two = NULL, *column_name = NULL, *column = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
Expand All @@ -148,7 +148,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){
phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);

PHALCON_INIT_VAR(size_pattern);
ZVAL_STRING(size_pattern, "#\\(([0-9]+)(,[0-9]+)*\\)#", 1);
ZVAL_STRING(size_pattern, "#\\(([0-9]++)(?:,\\s*([0-9]++))?\\)#", 1);

PHALCON_INIT_VAR(sql);
phalcon_call_method_p2(sql, dialect, "describecolumns", table, schema);
Expand Down Expand Up @@ -246,6 +246,11 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){
phalcon_array_fetch_long(&match_one, matches, 1, PH_NOISY);
phalcon_array_update_string(&definition, SL("size"), &match_one, PH_COPY | PH_SEPARATE);
}
if (phalcon_array_isset_long(matches, 2)) {
PHALCON_OBS_NVAR(match_two);
phalcon_array_fetch_long(&match_two, matches, 2, PH_NOISY);
phalcon_array_update_string(&definition, SL("scale"), &match_two, PH_COPY | PH_SEPARATE);
}
}
}

Expand Down
23 changes: 21 additions & 2 deletions ext/db/column.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ PHP_METHOD(Phalcon_Db_Column, getBindType){
PHP_METHOD(Phalcon_Db_Column, __set_state){

zval *data, *definition, *column_name, *column_type;
zval *not_null, *primary, *size, *dunsigned, *after;
zval *not_null, *primary, *size, *scale, *dunsigned, *after;
zval *is_numeric, *first, *bind_type;
zval *t0 = NULL, *t1 = NULL;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -444,6 +445,24 @@ PHP_METHOD(Phalcon_Db_Column, __set_state){
phalcon_array_update_string(&definition, SL("size"), &size, PH_COPY | PH_SEPARATE);
}

if (phalcon_array_isset_string(data, SS("_scale"))) {
PHALCON_INIT_VAR(t0);
ZVAL_LONG(t0, 3);
PHALCON_INIT_VAR(is_numeric);
is_equal_function(is_numeric, column_type, t0 TSRMLS_CC);
if (PHALCON_IS_NOT_TRUE(is_numeric)) {
PHALCON_INIT_VAR(t1);
ZVAL_LONG(t1, 7);
is_equal_function(is_numeric, column_type, t1 TSRMLS_CC);
}

if (PHALCON_IS_TRUE(is_numeric)) {
PHALCON_OBS_VAR(scale);
phalcon_array_fetch_string(&scale, data, SL("_scale"), PH_NOISY);
phalcon_array_update_string(&definition, SL("scale"), &scale, PH_COPY | PH_SEPARATE);
}
}

if (phalcon_array_isset_string(data, SS("_unsigned"))) {
PHALCON_OBS_VAR(dunsigned);
phalcon_array_fetch_string(&dunsigned, data, SL("_unsigned"), PH_NOISY);
Expand All @@ -457,7 +476,7 @@ PHP_METHOD(Phalcon_Db_Column, __set_state){
}

if (phalcon_array_isset_string(data, SS("_isNumeric"))) {
PHALCON_OBS_VAR(is_numeric);
PHALCON_OBS_NVAR(is_numeric);
phalcon_array_fetch_string(&is_numeric, data, SL("_isNumeric"), PH_NOISY);
phalcon_array_update_string(&definition, SL("isNumeric"), &is_numeric, PH_COPY | PH_SEPARATE);
}
Expand Down
4 changes: 2 additions & 2 deletions ext/db/dialect/postgresql.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,10 @@ PHP_METHOD(Phalcon_Db_Dialect_Postgresql, describeColumns){

if (zend_is_true(schema)) {
PHALCON_INIT_VAR(sql);
PHALCON_CONCAT_SVSVS(sql, "SELECT DISTINCT c.column_name AS Field, c.data_type AS Type, c.character_maximum_length AS Size, c.numeric_precision AS NumericSize, c.is_nullable AS Null, CASE WHEN pkc.column_name NOTNULL THEN 'PRI' ELSE '' END AS Key, CASE WHEN c.data_type LIKE '%int%' AND c.column_default LIKE '%nextval%' THEN 'auto_increment' ELSE '' END AS Extra, c.ordinal_position AS Position FROM information_schema.columns c LEFT JOIN ( SELECT kcu.column_name, kcu.table_name, kcu.table_schema FROM information_schema.table_constraints tc INNER JOIN information_schema.key_column_usage kcu on (kcu.constraint_name = tc.constraint_name and kcu.table_name=tc.table_name and kcu.table_schema=tc.table_schema) WHERE tc.constraint_type='PRIMARY KEY') pkc ON (c.column_name=pkc.column_name AND c.table_schema = pkc.table_schema AND c.table_name=pkc.table_name) WHERE c.table_schema='", schema, "' AND c.table_name='", table, "' ORDER BY c.ordinal_position");
PHALCON_CONCAT_SVSVS(sql, "SELECT DISTINCT c.column_name AS Field, c.data_type AS Type, c.character_maximum_length AS Size, c.numeric_precision AS NumericSize, c.numeric_scale AS NumericScale, c.is_nullable AS Null, CASE WHEN pkc.column_name NOTNULL THEN 'PRI' ELSE '' END AS Key, CASE WHEN c.data_type LIKE '%int%' AND c.column_default LIKE '%nextval%' THEN 'auto_increment' ELSE '' END AS Extra, c.ordinal_position AS Position FROM information_schema.columns c LEFT JOIN ( SELECT kcu.column_name, kcu.table_name, kcu.table_schema FROM information_schema.table_constraints tc INNER JOIN information_schema.key_column_usage kcu on (kcu.constraint_name = tc.constraint_name and kcu.table_name=tc.table_name and kcu.table_schema=tc.table_schema) WHERE tc.constraint_type='PRIMARY KEY') pkc ON (c.column_name=pkc.column_name AND c.table_schema = pkc.table_schema AND c.table_name=pkc.table_name) WHERE c.table_schema='", schema, "' AND c.table_name='", table, "' ORDER BY c.ordinal_position");
} else {
PHALCON_INIT_NVAR(sql);
PHALCON_CONCAT_SVS(sql, "SELECT DISTINCT c.column_name AS Field, c.data_type AS Type, c.character_maximum_length AS Size, c.numeric_precision AS NumericSize, c.is_nullable AS Null, CASE WHEN pkc.column_name NOTNULL THEN 'PRI' ELSE '' END AS Key, CASE WHEN c.data_type LIKE '%int%' AND c.column_default LIKE '%nextval%' THEN 'auto_increment' ELSE '' END AS Extra, c.ordinal_position AS Position FROM information_schema.columns c LEFT JOIN ( SELECT kcu.column_name, kcu.table_name, kcu.table_schema FROM information_schema.table_constraints tc INNER JOIN information_schema.key_column_usage kcu on (kcu.constraint_name = tc.constraint_name and kcu.table_name=tc.table_name and kcu.table_schema=tc.table_schema) WHERE tc.constraint_type='PRIMARY KEY') pkc ON (c.column_name=pkc.column_name AND c.table_schema = pkc.table_schema AND c.table_name=pkc.table_name) WHERE c.table_schema='public' AND c.table_name='", table, "' ORDER BY c.ordinal_position");
PHALCON_CONCAT_SVS(sql, "SELECT DISTINCT c.column_name AS Field, c.data_type AS Type, c.character_maximum_length AS Size, c.numeric_precision AS NumericSize, c.numeric_scale AS NumericScale, c.is_nullable AS Null, CASE WHEN pkc.column_name NOTNULL THEN 'PRI' ELSE '' END AS Key, CASE WHEN c.data_type LIKE '%int%' AND c.column_default LIKE '%nextval%' THEN 'auto_increment' ELSE '' END AS Extra, c.ordinal_position AS Position FROM information_schema.columns c LEFT JOIN ( SELECT kcu.column_name, kcu.table_name, kcu.table_schema FROM information_schema.table_constraints tc INNER JOIN information_schema.key_column_usage kcu on (kcu.constraint_name = tc.constraint_name and kcu.table_name=tc.table_name and kcu.table_schema=tc.table_schema) WHERE tc.constraint_type='PRIMARY KEY') pkc ON (c.column_name=pkc.column_name AND c.table_schema = pkc.table_schema AND c.table_name=pkc.table_name) WHERE c.table_schema='public' AND c.table_name='", table, "' ORDER BY c.ordinal_position");
}

RETURN_CTOR(sql);
Expand Down
6 changes: 3 additions & 3 deletions unit-tests/DbDescribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function getExpectedColumnsMysql()
'_type' => 3,
'_isNumeric' => true,
'_size' => 16,
'_scale' => 0,
'_scale' => 2,
'_unsigned' => false,
'_notNull' => true,
'_autoIncrement' => false,
Expand Down Expand Up @@ -318,7 +318,7 @@ public function getExpectedColumnsPostgresql()
'_type' => 3,
'_isNumeric' => true,
'_size' => 16,
'_scale' => 0,
'_scale' => 2,
'_unsigned' => false,
'_notNull' => true,
'_autoIncrement' => false,
Expand Down Expand Up @@ -480,7 +480,7 @@ public function getExpectedColumnsSqlite()
'_type' => 3,
'_isNumeric' => true,
'_size' => 16,
'_scale' => 0,
'_scale' => 2,
'_unsigned' => false,
'_notNull' => true,
'_autoIncrement' => false,
Expand Down

0 comments on commit 7a0c019

Please sign in to comment.