Skip to content

Commit

Permalink
Merge pull request #1693 from sjinks/db-opt
Browse files Browse the repository at this point in the history
Phalcon\Db Optimization
  • Loading branch information
Phalcon committed Dec 15, 2013
2 parents 9ace2b9 + dbd99a5 commit 1f30f48
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 184 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
- Fixed issue with RawValue('default') on composite primary key (#1534)
- Added support for the following fetch modes from PDO: FETCH_LAZY, FETCH_BOUND, FETCH_COLUMN, FETCH_CLASS, FETCH_INTO, FETCH_FUNC, FETCH_NAMED, FETCH_KEY_PAIR (#1642)
- Added support for the following fetch mode modifiers from PDO: FETCH_GROUP, FETCH_UNIQUE, FETCH_CLASSTYPE, FETCH_SERIALIZE, FETCH_PROPS_LATE (#1642)
- Phalcon\Db optimizations (#1693)
- Phalcon\Db\Adapter optimizations (#1693)
- Phalcon\Db\Index optimizations (#1693)
- Phalcon\Db\Profiler optimizations (#1693)
- Phalcon\Db\Reference optimizations (#1693)
- Phalcon\Db\Result\Pdo optimizations (#1693)
- Phalcon\Debug:
- Added support for UTF-8 to \Phalcon\Debug (#1099)
- Phalcon\Debug::uri now supports both http and https (#987)
Expand Down
17 changes: 5 additions & 12 deletions ext/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,17 @@ PHP_METHOD(Phalcon_Db, setup){

zval *options, *escape_identifiers;

PHALCON_MM_GROW();
phalcon_fetch_params(0, 1, 0, &options);

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

if (Z_TYPE_P(options) != IS_ARRAY) {
PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Options must be an array");
PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Options must be an array");
return;
}
/**

/**
* Enables/Disables globally the escaping of SQL identifiers
*/
if (phalcon_array_isset_string(options, SS("escapeSqlIdentifiers"))) {
PHALCON_OBS_VAR(escape_identifiers);
phalcon_array_fetch_string(&escape_identifiers, options, SL("escapeSqlIdentifiers"), PH_NOISY);
if (phalcon_array_isset_string_fetch(&escape_identifiers, options, SS("escapeSqlIdentifiers"))) {
PHALCON_GLOBAL(db).escape_identifiers = zend_is_true(escape_identifiers);
}

PHALCON_MM_RESTORE();
}

119 changes: 56 additions & 63 deletions ext/db/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ PHP_METHOD(Phalcon_Db_Adapter, fetchOne){
if (Z_TYPE_P(fetch_mode) != IS_NULL) {
phalcon_call_method_p1_noret(result, "setfetchmode", fetch_mode);
}
phalcon_call_method(return_value, result, "fetch");
phalcon_return_call_method_p0(result, "fetch");
RETURN_MM();
}

Expand Down Expand Up @@ -446,14 +446,14 @@ PHP_METHOD(Phalcon_Db_Adapter, insert){
PHALCON_INIT_VAR(insert_sql);
PHALCON_CONCAT_SVSVSVS(insert_sql, "INSERT INTO ", escaped_table, " (", joined_fields, ") VALUES (", joined_values, ")");
} else {
PHALCON_INIT_NVAR(insert_sql);
PHALCON_INIT_VAR(insert_sql);
PHALCON_CONCAT_SVSVS(insert_sql, "INSERT INTO ", escaped_table, " VALUES (", joined_values, ")");
}

/**
* Perform the execution via PDO::execute
* Perform the execution via execute
*/
phalcon_call_method_p3(return_value, this_ptr, "execute", insert_sql, insert_values, bind_data_types);
phalcon_return_call_method_p3(this_ptr, "execute", insert_sql, insert_values, bind_data_types);
RETURN_MM();
}

Expand Down Expand Up @@ -633,9 +633,9 @@ PHP_METHOD(Phalcon_Db_Adapter, update){
}

/**
* Perform the update via PDO::execute
* Perform the update via execute
*/
phalcon_call_method_p3(return_value, this_ptr, "execute", update_sql, update_values, bind_data_types);
phalcon_return_call_method_p3(this_ptr, "execute", update_sql, update_values, bind_data_types);
RETURN_MM();
}

Expand Down Expand Up @@ -695,9 +695,9 @@ PHP_METHOD(Phalcon_Db_Adapter, delete){
}

/**
* Perform the update via PDO::execute
* Perform the update via execute
*/
phalcon_call_method_p3(return_value, this_ptr, "execute", sql, placeholders, data_types);
phalcon_return_call_method_p3(this_ptr, "execute", sql, placeholders, data_types);
RETURN_MM();
}

Expand All @@ -716,7 +716,7 @@ PHP_METHOD(Phalcon_Db_Adapter, getColumnList){
phalcon_fetch_params(1, 1, 0, &column_list);

dialect = phalcon_fetch_nproperty_this(this_ptr, SL("_dialect"), PH_NOISY_CC);
phalcon_call_method_p1(return_value, dialect, "getcolumnlist", column_list);
phalcon_return_call_method_p1(dialect, "getcolumnlist", column_list);
RETURN_MM();
}

Expand All @@ -740,7 +740,7 @@ PHP_METHOD(Phalcon_Db_Adapter, limit){
phalcon_fetch_params(1, 2, 0, &sql_query, &number);

dialect = phalcon_fetch_nproperty_this(this_ptr, SL("_dialect"), PH_NOISY_CC);
phalcon_call_method_p2(return_value, dialect, "limit", sql_query, number);
phalcon_return_call_method_p2(dialect, "limit", sql_query, number);
RETURN_MM();
}

Expand Down Expand Up @@ -839,7 +839,7 @@ PHP_METHOD(Phalcon_Db_Adapter, forUpdate){
phalcon_fetch_params(1, 1, 0, &sql_query);

dialect = phalcon_fetch_nproperty_this(this_ptr, SL("_dialect"), PH_NOISY_CC);
phalcon_call_method_p1(return_value, dialect, "forupdate", sql_query);
phalcon_return_call_method_p1(dialect, "forupdate", sql_query);
RETURN_MM();
}

Expand All @@ -858,7 +858,7 @@ PHP_METHOD(Phalcon_Db_Adapter, sharedLock){
phalcon_fetch_params(1, 1, 0, &sql_query);

dialect = phalcon_fetch_nproperty_this(this_ptr, SL("_dialect"), PH_NOISY_CC);
phalcon_call_method_p1(return_value, dialect, "sharedlock", sql_query);
phalcon_return_call_method_p1(dialect, "sharedlock", sql_query);
RETURN_MM();
}

Expand All @@ -880,7 +880,7 @@ PHP_METHOD(Phalcon_Db_Adapter, createTable){

phalcon_fetch_params(1, 3, 0, &table_name, &schema_name, &definition);

if (Z_TYPE_P(definition) != IS_ARRAY) {
if (Z_TYPE_P(definition) != IS_ARRAY) {
PHALCON_INIT_VAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Invalid definition to create the table '", table_name, "'");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_db_exception_ce, exception_message);
Expand All @@ -901,7 +901,7 @@ PHP_METHOD(Phalcon_Db_Adapter, createTable){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "createtable", table_name, schema_name, definition);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand Down Expand Up @@ -934,7 +934,7 @@ PHP_METHOD(Phalcon_Db_Adapter, dropTable){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "droptable", table_name, schema_name, if_exists);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand Down Expand Up @@ -975,7 +975,7 @@ PHP_METHOD(Phalcon_Db_Adapter, createView){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "createview", view_name, definition, schema_name);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand Down Expand Up @@ -1008,7 +1008,7 @@ PHP_METHOD(Phalcon_Db_Adapter, dropView){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "dropview", view_name, schema_name, if_exists);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1033,7 +1033,7 @@ PHP_METHOD(Phalcon_Db_Adapter, addColumn){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "addcolumn", table_name, schema_name, column);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1058,7 +1058,7 @@ PHP_METHOD(Phalcon_Db_Adapter, modifyColumn){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "modifycolumn", table_name, schema_name, column);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1083,7 +1083,7 @@ PHP_METHOD(Phalcon_Db_Adapter, dropColumn){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "dropcolumn", table_name, schema_name, column_name);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1108,7 +1108,7 @@ PHP_METHOD(Phalcon_Db_Adapter, addIndex){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "addindex", table_name, schema_name, index);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1133,7 +1133,7 @@ PHP_METHOD(Phalcon_Db_Adapter, dropIndex){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "dropindex", table_name, schema_name, index_name);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1158,7 +1158,7 @@ PHP_METHOD(Phalcon_Db_Adapter, addPrimaryKey){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "addprimarykey", table_name, schema_name, index);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1181,7 +1181,7 @@ PHP_METHOD(Phalcon_Db_Adapter, dropPrimaryKey){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p2(sql, dialect, "dropprimarykey", table_name, schema_name);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1206,7 +1206,7 @@ PHP_METHOD(Phalcon_Db_Adapter, addForeignKey){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "addforeignkey", table_name, schema_name, reference);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1231,7 +1231,7 @@ PHP_METHOD(Phalcon_Db_Adapter, dropForeignKey){

PHALCON_INIT_VAR(sql);
phalcon_call_method_p3(sql, dialect, "dropforeignkey", table_name, schema_name, reference_name);
phalcon_call_method_p1(return_value, this_ptr, "execute", sql);
phalcon_return_call_method_p1(this_ptr, "execute", sql);
RETURN_MM();
}

Expand All @@ -1250,7 +1250,7 @@ PHP_METHOD(Phalcon_Db_Adapter, getColumnDefinition){
phalcon_fetch_params(1, 1, 0, &column);

dialect = phalcon_fetch_nproperty_this(this_ptr, SL("_dialect"), PH_NOISY_CC);
phalcon_call_method_p1(return_value, dialect, "getcolumndefinition", column);
phalcon_return_call_method_p1(dialect, "getcolumndefinition", column);
RETURN_MM();
}

Expand All @@ -1267,10 +1267,8 @@ PHP_METHOD(Phalcon_Db_Adapter, getColumnDefinition){
PHP_METHOD(Phalcon_Db_Adapter, listTables){

zval *schema_name = NULL, *dialect, *sql, *fetch_num, *tables;
zval *table = NULL, *table_name = NULL;
HashTable *ah0;
zval **table, *table_name;
HashPosition hp0;
zval **hd;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -1300,19 +1298,18 @@ PHP_METHOD(Phalcon_Db_Adapter, listTables){
PHALCON_INIT_VAR(tables);
phalcon_call_method_p2(tables, this_ptr, "fetchall", sql, fetch_num);

array_init(return_value);

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

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

PHALCON_GET_HVALUE(table);

PHALCON_OBS_NVAR(table_name);
phalcon_array_fetch_long(&table_name, table, 0, PH_NOISY);
phalcon_array_append(&return_value, table_name, PH_SEPARATE);

zend_hash_move_forward_ex(ah0, &hp0);
if (Z_TYPE_P(tables) == IS_ARRAY) {
array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(tables)));

for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(tables), &hp0);
zend_hash_get_current_data_ex(Z_ARRVAL_P(tables), (void**)&table, &hp0) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(tables), &hp0)
) {
if (phalcon_array_isset_long_fetch(&table_name, *table, 0)) {
phalcon_array_append(&return_value, table_name, 0);
}
}
}

PHALCON_MM_RESTORE();
Expand All @@ -1331,10 +1328,8 @@ PHP_METHOD(Phalcon_Db_Adapter, listTables){
PHP_METHOD(Phalcon_Db_Adapter, listViews){

zval *schema_name = NULL, *dialect, *sql, *fetch_num, *tables;
zval *table = NULL, *table_name = NULL;
HashTable *ah0;
zval **table, *table_name;
HashPosition hp0;
zval **hd;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -1364,19 +1359,18 @@ PHP_METHOD(Phalcon_Db_Adapter, listViews){
PHALCON_INIT_VAR(tables);
phalcon_call_method_p2(tables, this_ptr, "fetchall", sql, fetch_num);

array_init(return_value);

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

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

PHALCON_GET_HVALUE(table);

PHALCON_OBS_NVAR(table_name);
phalcon_array_fetch_long(&table_name, table, 0, PH_NOISY);
phalcon_array_append(&return_value, table_name, PH_SEPARATE);

zend_hash_move_forward_ex(ah0, &hp0);
if (Z_TYPE_P(tables) == IS_ARRAY) {
array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(tables)));

for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(tables), &hp0);
zend_hash_get_current_data_ex(Z_ARRVAL_P(tables), (void**)&table, &hp0) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(tables), &hp0)
) {
if (phalcon_array_isset_long_fetch(&table_name, *table, 0)) {
phalcon_array_append(&return_value, table_name, 0);
}
}
}

PHALCON_MM_RESTORE();
Expand Down Expand Up @@ -1444,7 +1438,7 @@ PHP_METHOD(Phalcon_Db_Adapter, describeIndexes){
if (!phalcon_array_isset(indexes, key_name)) {
PHALCON_INIT_NVAR(empty_arr);
array_init(empty_arr);
phalcon_array_update_zval(&indexes, key_name, &empty_arr, PH_COPY | PH_SEPARATE);
phalcon_array_update_zval(&indexes, key_name, &empty_arr, PH_COPY);
}

PHALCON_OBS_NVAR(column_name);
Expand All @@ -1470,7 +1464,7 @@ PHP_METHOD(Phalcon_Db_Adapter, describeIndexes){
object_init_ex(index, phalcon_db_index_ce);
phalcon_call_method_p2_noret(index, "__construct", name, index_columns);

phalcon_array_update_zval(&return_value, name, &index, PH_COPY | PH_SEPARATE);
phalcon_array_update_zval(&return_value, name, &index, PH_COPY);

zend_hash_move_forward_ex(ah1, &hp1);
}
Expand Down Expand Up @@ -1826,7 +1820,7 @@ PHP_METHOD(Phalcon_Db_Adapter, getDefaultIdValue){

PHALCON_MM_GROW();

PHALCON_INIT_VAR(null_value);
PHALCON_ALLOC_GHOST_ZVAL(null_value);
ZVAL_STRING(null_value, "null", 1);
object_init_ex(return_value, phalcon_db_rawvalue_ce);
phalcon_call_method_p1_noret(return_value, "__construct", null_value);
Expand Down Expand Up @@ -1943,4 +1937,3 @@ PHP_METHOD(Phalcon_Db_Adapter, getDialectType){

RETURN_MEMBER(this_ptr, "_dialectType");
}

Loading

0 comments on commit 1f30f48

Please sign in to comment.