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

Phalcon\Db Optimization #1693

Merged
merged 7 commits into from Dec 15, 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
Phalcon\Db\Reference optimization
  • Loading branch information
sjinks committed Dec 14, 2013
commit bc09fe0f0e43940839340fbf440a265ac5ccaba0
1 change: 0 additions & 1 deletion ext/db/rawvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,3 @@ PHP_METHOD(Phalcon_Db_RawValue, __toString){

RETURN_MEMBER(this_ptr, "_value");
}

89 changes: 31 additions & 58 deletions ext/db/reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Expand Down Expand Up @@ -86,60 +85,49 @@ PHP_METHOD(Phalcon_Db_Reference, __construct){
zval *referenced_schema, *number_columns;
zval *number_referenced_columns;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 2, 0, &reference_name, &definition);
phalcon_fetch_params(0, 2, 0, &reference_name, &definition);

phalcon_update_property_this(this_ptr, SL("_referenceName"), reference_name TSRMLS_CC);
if (phalcon_array_isset_string(definition, SS("referencedTable"))) {
PHALCON_OBS_VAR(referenced_table);
phalcon_array_fetch_string(&referenced_table, definition, SL("referencedTable"), PH_NOISY);
if (phalcon_array_isset_string_fetch(&referenced_table, definition, SS("referencedTable"))) {
phalcon_update_property_this(this_ptr, SL("_referencedTable"), referenced_table TSRMLS_CC);
} else {
PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Referenced table is required");
PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Referenced table is required");
return;
}

if (phalcon_array_isset_string(definition, SS("columns"))) {
PHALCON_OBS_VAR(columns);
phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY);
if (phalcon_array_isset_string_fetch(&columns, definition, SS("columns"))) {
phalcon_update_property_this(this_ptr, SL("_columns"), columns TSRMLS_CC);
} else {
PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Foreign key columns are required");
PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Foreign key columns are required");
return;
}

if (phalcon_array_isset_string(definition, SS("referencedColumns"))) {
PHALCON_OBS_VAR(referenced_columns);
phalcon_array_fetch_string(&referenced_columns, definition, SL("referencedColumns"), PH_NOISY);
if (phalcon_array_isset_string_fetch(&referenced_columns, definition, SS("referencedColumns"))) {
phalcon_update_property_this(this_ptr, SL("_referencedColumns"), referenced_columns TSRMLS_CC);
} else {
PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Referenced columns of the foreign key are required");
PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Referenced columns of the foreign key are required");
return;
}

if (phalcon_array_isset_string(definition, SS("schema"))) {
PHALCON_OBS_VAR(schema);
phalcon_array_fetch_string(&schema, definition, SL("schema"), PH_NOISY);
if (phalcon_array_isset_string_fetch(&schema, definition, SS("schema"))) {
phalcon_update_property_this(this_ptr, SL("_schemaName"), schema TSRMLS_CC);
}

if (phalcon_array_isset_string(definition, SS("referencedSchema"))) {
PHALCON_OBS_VAR(referenced_schema);
phalcon_array_fetch_string(&referenced_schema, definition, SL("referencedSchema"), PH_NOISY);
if (phalcon_array_isset_string_fetch(&referenced_schema, definition, SS("referencedSchema"))) {
phalcon_update_property_this(this_ptr, SL("_referencedSchema"), referenced_schema TSRMLS_CC);
}


PHALCON_MM_GROW();
PHALCON_INIT_VAR(number_columns);
phalcon_fast_count(number_columns, columns TSRMLS_CC);

PHALCON_INIT_VAR(number_referenced_columns);
phalcon_fast_count(number_referenced_columns, referenced_columns TSRMLS_CC);
if (!PHALCON_IS_EQUAL(number_columns, number_referenced_columns)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Number of columns is not equals than the number of columns referenced");
return;
}

PHALCON_MM_RESTORE();
}

Expand Down Expand Up @@ -217,58 +205,43 @@ PHP_METHOD(Phalcon_Db_Reference, getReferencedColumns){
*/
PHP_METHOD(Phalcon_Db_Reference, __set_state){

zval *data, *constraint_name, *referenced_schema = NULL;
zval *referenced_table = NULL, *columns = NULL, *referenced_columns = NULL;
zval *data, *constraint_name, *referenced_schema;
zval *referenced_table, *columns, *referenced_columns;
zval *definition;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &data);

if (!phalcon_array_isset_string(data, SS("_referenceName"))) {
if (!phalcon_array_isset_string_fetch(&constraint_name, data, SS("_referenceName"))) {
PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "_referenceName parameter is required");
return;
} else {
PHALCON_OBS_VAR(constraint_name);
phalcon_array_fetch_string(&constraint_name, data, SL("_referenceName"), PH_NOISY);
}
if (phalcon_array_isset_string(data, SS("_referencedSchema"))) {
PHALCON_OBS_VAR(referenced_schema);
phalcon_array_fetch_string(&referenced_schema, data, SL("_referencedSchema"), PH_NOISY);
} else {
PHALCON_INIT_NVAR(referenced_schema);

if (!phalcon_array_isset_string_fetch(&referenced_schema, data, SS("_referencedSchema"))) {
referenced_schema = PHALCON_GLOBAL(z_null);
}

if (phalcon_array_isset_string(data, SS("_referencedTable"))) {
PHALCON_OBS_VAR(referenced_table);
phalcon_array_fetch_string(&referenced_table, data, SL("_referencedTable"), PH_NOISY);
} else {
PHALCON_INIT_NVAR(referenced_table);
if (!phalcon_array_isset_string_fetch(&referenced_table, data, SS("_referencedTable"))) {
referenced_table = PHALCON_GLOBAL(z_null);
}

if (phalcon_array_isset_string(data, SS("_columns"))) {
PHALCON_OBS_VAR(columns);
phalcon_array_fetch_string(&columns, data, SL("_columns"), PH_NOISY);
} else {
PHALCON_INIT_NVAR(columns);
if (!phalcon_array_isset_string_fetch(&columns, data, SS("_columns"))) {
columns = PHALCON_GLOBAL(z_null);
}

if (phalcon_array_isset_string(data, SS("_referencedColumns"))) {
PHALCON_OBS_VAR(referenced_columns);
phalcon_array_fetch_string(&referenced_columns, data, SL("_referencedColumns"), PH_NOISY);
} else {
PHALCON_INIT_NVAR(referenced_columns);
if (!phalcon_array_isset_string_fetch(&referenced_columns, data, SS("_referencedColumns"))) {
referenced_columns = PHALCON_GLOBAL(z_null);
}
PHALCON_INIT_VAR(definition);

PHALCON_ALLOC_GHOST_ZVAL(definition);
array_init_size(definition, 4);
phalcon_array_update_string(&definition, SL("referencedSchema"), &referenced_schema, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&definition, SL("referencedTable"), &referenced_table, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&definition, SL("columns"), &columns, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&definition, SL("referencedColumns"), &referenced_columns, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&definition, SL("referencedSchema"), &referenced_schema, PH_COPY);
phalcon_array_update_string(&definition, SL("referencedTable"), &referenced_table, PH_COPY);
phalcon_array_update_string(&definition, SL("columns"), &columns, PH_COPY);
phalcon_array_update_string(&definition, SL("referencedColumns"), &referenced_columns, PH_COPY);
object_init_ex(return_value, phalcon_db_reference_ce);
phalcon_call_method_p2_noret(return_value, "__construct", constraint_name, definition);

RETURN_MM();
}