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

Add Decimal scale #935

Merged
merged 8 commits into from
Jul 27, 2013
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
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]+)[,]?([0-9]+)*\\)#", 1);
Copy link

Choose a reason for hiding this comment

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

Could you please try this pattern:

#\\(([0-9]++)(?:,\\s*([0-9]++))?\\)#

It seems to be more efficient


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]+)[,]?([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
29 changes: 24 additions & 5 deletions ext/db/column.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ PHP_METHOD(Phalcon_Db_Column, __construct){
return;
}
}

/**
* Check if the field is unsigned (only MySQL)
*/
Expand All @@ -193,7 +193,7 @@ PHP_METHOD(Phalcon_Db_Column, __construct){
phalcon_array_fetch_string(&dunsigned, definition, SL("unsigned"), PH_NOISY);
phalcon_update_property_this(this_ptr, SL("_unsigned"), dunsigned TSRMLS_CC);
}

/**
* Check if the field is numeric
*/
Expand Down 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 *is_numeric, *first, *bind_type;
zval *not_null, *primary, *size, *scale, *dunsigned, *after;
zval *is_numeric = NULL, *first, *bind_type;
zval *t0 = NULL, *t1 = NULL;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -443,6 +444,24 @@ PHP_METHOD(Phalcon_Db_Column, __set_state){
phalcon_array_fetch_string(&size, data, SL("_size"), PH_NOISY);
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);
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