/** * Executes a prepared statement binding. This function uses integer indexes starting from zero * *<code> * $statement = $db->prepare('SELECT * FROM robots WHERE name = :name'); * $result = $connection->executePrepared($statement, array('name' => 'Voltron')); *</code> * * @param \PDOStatement $statement * @param array $placeholders * @param array $dataTypes * @return \PDOStatement */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, executePrepared){ zval *statement = NULL, *placeholders = NULL, *data_types = NULL; zval *one, *value = NULL, *wildcard = NULL, *parameter = NULL, *type = NULL, *cast_value = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &statement, &placeholders, &data_types) == FAILURE) { RETURN_MM_NULL(); } PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); if (!phalcon_is_iterable_ex(placeholders, &ah0, &hp0, 0, 0)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(wildcard, ah0, hp0); PHALCON_GET_FOREACH_VALUE(value); if (Z_TYPE_P(wildcard) == IS_LONG) { PHALCON_INIT_NVAR(parameter); phalcon_add_function(parameter, wildcard, one TSRMLS_CC); } else { if (Z_TYPE_P(wildcard) == IS_STRING) { PHALCON_CPY_WRT(parameter, wildcard); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind parameter"); return; } } if (Z_TYPE_P(data_types) == IS_ARRAY) { if (likely(phalcon_array_isset(data_types, wildcard))) { /** * The bind type is double so we try to get the double value */ PHALCON_OBS_NVAR(type); phalcon_array_fetch(&type, data_types, wildcard, PH_NOISY); if (phalcon_compare_strict_long(type, 32 TSRMLS_CC)) { PHALCON_INIT_NVAR(cast_value); phalcon_cast(cast_value, value, IS_DOUBLE); PHALCON_INIT_NVAR(type); ZVAL_LONG(type, 1024); } else { PHALCON_CPY_WRT(cast_value, value); } /** * 1024 is ignore the bind type */ Z_SET_ISREF_P(cast_value); if (phalcon_compare_strict_long(type, 1024 TSRMLS_CC)) { phalcon_call_method_p2_noret(statement, "bindparam", parameter, cast_value); } else { phalcon_call_method_p3_noret(statement, "bindparam", parameter, cast_value, type); } Z_UNSET_ISREF_P(cast_value); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind type parameter"); return; } } else { Z_SET_ISREF_P(value); phalcon_call_method_p2_noret(statement, "bindparam", parameter, value); Z_UNSET_ISREF_P(value); } zend_hash_move_forward_ex(ah0, &hp0); } phalcon_call_method_noret(statement, "execute"); RETURN_CCTOR(statement); }
/** * Builds a SELECT statement * * @param array $definition * @return string */ PHP_METHOD(Phalcon_Db_Dialect_Oracle, select){ zval *definition, *escape_char = NULL, *columns, *selected_columns; zval *column = NULL, *column_item = NULL, *column_sql = NULL, *columns_sql = NULL; zval *column_domain = NULL, *column_domain_sql = NULL, *column_alias = NULL; zval *column_alias_sql = NULL, *tables, *selected_tables; zval *table = NULL, *sql_table = NULL, *tables_sql = NULL, *sql, *joins; zval *join = NULL, *type = NULL, *sql_join = NULL, *join_conditions_array = NULL; zval *join_expressions = NULL, *join_condition = NULL, *join_expression = NULL; zval *join_conditions = NULL, *where_conditions; zval *where_expression, *group_items, *group_fields; zval *group_field = NULL, *group_expression = NULL, *group_sql; zval *group_clause, *having_conditions, *having_expression; zval *order_fields, *order_items, *order_item = NULL; zval *order_expression = NULL, *order_sql_item = NULL, *sql_order_type = NULL; zval *order_sql_item_type = NULL, *order_sql, *limit_value; zval *number, *offset; zval *one, *ini_range, *end_range, *sql_limit; HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5; HashPosition hp0, hp1, hp2, hp3, hp4, hp5; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &definition) == FAILURE) { RETURN_MM_NULL(); } if (Z_TYPE_P(definition) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SELECT definition"); return; } if (!phalcon_array_isset_string(definition, SS("tables"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'tables' is required in the definition array"); return; } if (!phalcon_array_isset_string(definition, SS("columns"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'columns' is required in the definition array"); return; } if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_OBS_VAR(escape_char); phalcon_read_property_this(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY_CC); } else { PHALCON_INIT_NVAR(escape_char); } PHALCON_OBS_VAR(columns); phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY_CC); if (Z_TYPE_P(columns) == IS_ARRAY) { PHALCON_INIT_VAR(selected_columns); array_init(selected_columns); if (!phalcon_is_iterable_ex(columns, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(column); /** * Escape column name */ PHALCON_OBS_NVAR(column_item); phalcon_array_fetch_long(&column_item, column, 0, PH_NOISY_CC); if (Z_TYPE_P(column_item) == IS_ARRAY) { PHALCON_INIT_NVAR(column_sql); phalcon_call_method_p2(column_sql, this_ptr, "getsqlexpression", column_item, escape_char); } else { if (PHALCON_IS_STRING(column_item, "*")) { PHALCON_CPY_WRT(column_sql, column_item); } else { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_sql); PHALCON_CONCAT_VVV(column_sql, escape_char, column_item, escape_char); } else { PHALCON_CPY_WRT(columns_sql, column_item); } } } /** * Escape column domain */ if (phalcon_array_isset_long(column, 1)) { PHALCON_OBS_NVAR(column_domain); phalcon_array_fetch_long(&column_domain, column, 1, PH_NOISY_CC); if (zend_is_true(column_domain)) { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_domain_sql); PHALCON_CONCAT_VVVSV(column_domain_sql, escape_char, column_domain, escape_char, ".", column_sql); } else { PHALCON_INIT_NVAR(column_domain_sql); PHALCON_CONCAT_VSV(column_domain_sql, column_domain, ".", column_sql); } } else { PHALCON_CPY_WRT(column_domain_sql, column_sql); } } else { PHALCON_CPY_WRT(column_domain_sql, column_sql); } /** * Escape column alias */ if (phalcon_array_isset_long(column, 2)) { PHALCON_OBS_NVAR(column_alias); phalcon_array_fetch_long(&column_alias, column, 2, PH_NOISY_CC); if (zend_is_true(column_alias)) { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_alias_sql); PHALCON_CONCAT_VSVVV(column_alias_sql, column_domain_sql, " ", escape_char, column_alias, escape_char); } else { PHALCON_INIT_NVAR(column_alias_sql); PHALCON_CONCAT_VSV(column_alias_sql, column_domain_sql, " ", column_alias); } } else { PHALCON_CPY_WRT(column_alias_sql, column_domain_sql); } } else { PHALCON_CPY_WRT(column_alias_sql, column_domain_sql); } phalcon_array_append(&selected_columns, column_alias_sql, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(columns_sql); phalcon_fast_join_str(columns_sql, SL(", "), selected_columns TSRMLS_CC); } else { PHALCON_CPY_WRT(columns_sql, columns); } /** * Check and escape tables */ PHALCON_OBS_VAR(tables); phalcon_array_fetch_string(&tables, definition, SL("tables"), PH_NOISY_CC); if (Z_TYPE_P(tables) == IS_ARRAY) { PHALCON_INIT_VAR(selected_tables); array_init(selected_tables); if (!phalcon_is_iterable_ex(tables, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(table); PHALCON_INIT_NVAR(sql_table); phalcon_call_method_p2(sql_table, this_ptr, "getsqltable", table, escape_char); phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); } PHALCON_INIT_VAR(tables_sql); phalcon_fast_join_str(tables_sql, SL(", "), selected_tables TSRMLS_CC); } else { PHALCON_CPY_WRT(tables_sql, tables); } PHALCON_INIT_VAR(sql); PHALCON_CONCAT_SVSV(sql, "SELECT ", columns_sql, " FROM ", tables_sql); /** * Check for joins */ if (phalcon_array_isset_string(definition, SS("joins"))) { PHALCON_OBS_VAR(joins); phalcon_array_fetch_string(&joins, definition, SL("joins"), PH_NOISY_CC); if (!phalcon_is_iterable_ex(joins, &ah2, &hp2, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(join); PHALCON_OBS_NVAR(type); phalcon_array_fetch_string(&type, join, SL("type"), PH_NOISY_CC); PHALCON_OBS_NVAR(table); phalcon_array_fetch_string(&table, join, SL("source"), PH_NOISY_CC); PHALCON_INIT_NVAR(sql_table); phalcon_call_method_p2(sql_table, this_ptr, "getsqltable", table, escape_char); phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(sql_join); PHALCON_CONCAT_SVSV(sql_join, " ", type, " JOIN ", sql_table); /** * Check if the join has conditions */ if (phalcon_array_isset_string(join, SS("conditions"))) { PHALCON_OBS_NVAR(join_conditions_array); phalcon_array_fetch_string(&join_conditions_array, join, SL("conditions"), PH_NOISY_CC); if (phalcon_fast_count_ev(join_conditions_array TSRMLS_CC)) { PHALCON_INIT_NVAR(join_expressions); array_init(join_expressions); if (!phalcon_is_iterable_ex(join_conditions_array, &ah3, &hp3, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(join_condition); PHALCON_INIT_NVAR(join_expression); phalcon_call_method_p2(join_expression, this_ptr, "getsqlexpression", join_condition, escape_char); phalcon_array_append(&join_expressions, join_expression, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah3, &hp3); } PHALCON_INIT_NVAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), join_expressions TSRMLS_CC); PHALCON_SCONCAT_SV(sql_join, " ON ", join_conditions); } } phalcon_concat_self(&sql, sql_join TSRMLS_CC); zend_hash_move_forward_ex(ah2, &hp2); } } /** * Check for a WHERE clause */ if (phalcon_array_isset_string(definition, SS("where"))) { PHALCON_OBS_VAR(where_conditions); phalcon_array_fetch_string(&where_conditions, definition, SL("where"), PH_NOISY_CC); if (Z_TYPE_P(where_conditions) == IS_ARRAY) { PHALCON_INIT_VAR(where_expression); phalcon_call_method_p2(where_expression, this_ptr, "getsqlexpression", where_conditions, escape_char); PHALCON_SCONCAT_SV(sql, " WHERE ", where_expression); } else { PHALCON_SCONCAT_SV(sql, " WHERE ", where_conditions); } } /** * Check for a GROUP clause */ if (phalcon_array_isset_string(definition, SS("group"))) { PHALCON_INIT_VAR(group_items); array_init(group_items); PHALCON_OBS_VAR(group_fields); phalcon_array_fetch_string(&group_fields, definition, SL("group"), PH_NOISY_CC); if (!phalcon_is_iterable_ex(group_fields, &ah4, &hp4, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(group_field); PHALCON_INIT_NVAR(group_expression); phalcon_call_method_p2(group_expression, this_ptr, "getsqlexpression", group_field, escape_char); phalcon_array_append(&group_items, group_expression, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah4, &hp4); } PHALCON_INIT_VAR(group_sql); phalcon_fast_join_str(group_sql, SL(", "), group_items TSRMLS_CC); PHALCON_INIT_VAR(group_clause); PHALCON_CONCAT_SV(group_clause, " GROUP BY ", group_sql); phalcon_concat_self(&sql, group_clause TSRMLS_CC); /** * Check for a HAVING clause */ if (phalcon_array_isset_string(definition, SS("having"))) { PHALCON_OBS_VAR(having_conditions); phalcon_array_fetch_string(&having_conditions, definition, SL("having"), PH_NOISY_CC); PHALCON_INIT_VAR(having_expression); phalcon_call_method_p2(having_expression, this_ptr, "getsqlexpression", having_conditions, escape_char); PHALCON_SCONCAT_SV(sql, " HAVING ", having_expression); } } /** * Check for a ORDER clause */ if (phalcon_array_isset_string(definition, SS("order"))) { PHALCON_OBS_VAR(order_fields); phalcon_array_fetch_string(&order_fields, definition, SL("order"), PH_NOISY_CC); PHALCON_INIT_VAR(order_items); array_init(order_items); if (!phalcon_is_iterable_ex(order_fields, &ah5, &hp5, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(order_item); PHALCON_OBS_NVAR(order_expression); phalcon_array_fetch_long(&order_expression, order_item, 0, PH_NOISY_CC); PHALCON_INIT_NVAR(order_sql_item); phalcon_call_method_p2(order_sql_item, this_ptr, "getsqlexpression", order_expression, escape_char); /** * In the numeric 1 position could be a ASC/DESC clause */ if (phalcon_array_isset_long(order_item, 1)) { PHALCON_OBS_NVAR(sql_order_type); phalcon_array_fetch_long(&sql_order_type, order_item, 1, PH_NOISY_CC); PHALCON_INIT_NVAR(order_sql_item_type); PHALCON_CONCAT_VSV(order_sql_item_type, order_sql_item, " ", sql_order_type); } else { PHALCON_CPY_WRT(order_sql_item_type, order_sql_item); } phalcon_array_append(&order_items, order_sql_item_type, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah5, &hp5); } PHALCON_INIT_VAR(order_sql); phalcon_fast_join_str(order_sql, SL(", "), order_items TSRMLS_CC); PHALCON_SCONCAT_SV(sql, " ORDER BY ", order_sql); } /** * Oracle does not implement the LIMIT clause as some RDBMS do. * We have to simulate it with subqueries and ROWNUM. * Unfortunately because we use the column wildcard "*", * this puts an extra column into the query result set. */ if (phalcon_array_isset_string(definition, SS("limit"))) { PHALCON_OBS_VAR(limit_value); phalcon_array_fetch_string(&limit_value, definition, SL("limit"), PH_NOISY_CC); if (Z_TYPE_P(limit_value) == IS_ARRAY) { PHALCON_OBS_VAR(number); phalcon_array_fetch_string(&number, limit_value, SL("number"), PH_NOISY_CC); if (phalcon_array_isset_string(limit_value, SS("offset"))) { PHALCON_OBS_VAR(offset); phalcon_array_fetch_string(&offset, limit_value, SL("offset"), PH_NOISY_CC); } else { PHALCON_INIT_VAR(offset); ZVAL_LONG(offset, 0); } PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_INIT_VAR(ini_range); phalcon_add_function(ini_range, offset, one TSRMLS_CC); PHALCON_INIT_VAR(end_range); phalcon_add_function(end_range, offset, number TSRMLS_CC); PHALCON_INIT_VAR(sql_limit); PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ", end_range ); PHALCON_CPY_WRT(sql, sql_limit); } else { PHALCON_INIT_VAR(offset); ZVAL_LONG(offset, 0); PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_INIT_VAR(ini_range); phalcon_add_function(ini_range, offset, one TSRMLS_CC); PHALCON_INIT_VAR(end_range); phalcon_add_function(end_range, offset, limit_value TSRMLS_CC); PHALCON_INIT_VAR(sql_limit); PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ", end_range ); PHALCON_CPY_WRT(sql, sql_limit); } } RETURN_CTOR(sql); }
/** * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. * Call it when you need to restore a database connection * *<code> * //Make a connection * $connection = new Phalcon\Db\Adapter\Pdo\Mysql(array( * 'host' => '192.168.0.11', * 'username' => 'sigma', * 'password' => 'secret', * 'dbname' => 'blog', * )); * * //Reconnect * $connection->connect(); * </code> * * @param array $descriptor * @return boolean */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, connect){ zval *descriptor = NULL, *username = NULL, *password = NULL, *dsn_parts; zval *value = NULL, *key = NULL, *dsn_attribute = NULL, *dsn_attributes = NULL; zval *pdo_type, *dsn, *options = NULL, *persistent, *pdo; zend_class_entry *ce; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &descriptor) == FAILURE) { RETURN_MM_NULL(); } if (!descriptor) { PHALCON_INIT_VAR(descriptor); } else { PHALCON_SEPARATE_PARAM(descriptor); } if (Z_TYPE_P(descriptor) == IS_NULL) { PHALCON_OBS_NVAR(descriptor); phalcon_read_property(&descriptor, this_ptr, SL("_descriptor"), PH_NOISY_CC); } /** * Check for a username or use null as default */ if (phalcon_array_isset_string(descriptor, SS("username"))) { PHALCON_OBS_VAR(username); phalcon_array_fetch_string(&username, descriptor, SL("username"), PH_NOISY); phalcon_array_unset_string(&descriptor, SS("username"), PH_SEPARATE); } else { PHALCON_INIT_NVAR(username); } /** * Check for a password or use null as default */ if (phalcon_array_isset_string(descriptor, SS("password"))) { PHALCON_OBS_VAR(password); phalcon_array_fetch_string(&password, descriptor, SL("password"), PH_NOISY); phalcon_array_unset_string(&descriptor, SS("password"), PH_SEPARATE); } else { PHALCON_INIT_NVAR(password); } /** * Check if the developer has defined custom options or create one from scratch */ if (phalcon_array_isset_string(descriptor, SS("options"))) { PHALCON_OBS_VAR(options); phalcon_array_fetch_string(&options, descriptor, SL("options"), PH_NOISY); phalcon_array_unset_string(&descriptor, SS("options"), PH_SEPARATE); } else { PHALCON_INIT_NVAR(options); array_init(options); } /** * Check if the user has defined a custom dsn */ if (!phalcon_array_isset_string(descriptor, SS("dsn"))) { PHALCON_INIT_VAR(dsn_parts); array_init(dsn_parts); if (!phalcon_is_iterable_ex(descriptor, &ah0, &hp0, 0, 0)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(key, ah0, hp0); PHALCON_GET_FOREACH_VALUE(value); PHALCON_INIT_NVAR(dsn_attribute); PHALCON_CONCAT_VSV(dsn_attribute, key, "=", value); phalcon_array_append(&dsn_parts, dsn_attribute, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(dsn_attributes); phalcon_fast_join_str(dsn_attributes, SL(";"), dsn_parts TSRMLS_CC); } else { PHALCON_OBS_NVAR(dsn_attributes); phalcon_array_fetch_string(&dsn_attributes, descriptor, SL("dsn"), PH_NOISY); } PHALCON_OBS_VAR(pdo_type); phalcon_read_property(&pdo_type, this_ptr, SL("_type"), PH_NOISY_CC); PHALCON_INIT_VAR(dsn); PHALCON_CONCAT_VSV(dsn, pdo_type, ":", dsn_attributes); /** * Default options */ phalcon_array_update_long_long(&options, PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION, PH_SEPARATE); //phalcon_array_update_long_long(&options, PDO_ATTR_CASE, PDO_CASE_LOWER, PH_SEPARATE); //phalcon_array_update_long_long(&options, PDO_ATTR_CURSOR, PDO_CURSOR_SCROLL, PH_SEPARATE); /** * Check if the connection must be persistent */ if (phalcon_array_isset_string(descriptor, SS("persistent"))) { PHALCON_OBS_VAR(persistent); phalcon_array_fetch_string(&persistent, descriptor, SL("persistent"), PH_NOISY); if (zend_is_true(persistent)) { phalcon_array_update_long_bool(&options, PDO_ATTR_PERSISTENT, 1, PH_SEPARATE); } } /** * Create the connection using PDO */ ce = zend_fetch_class(SL("PDO"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(pdo); object_init_ex(pdo, ce); phalcon_call_method_p4_noret(pdo, "__construct", dsn, username, password, options); phalcon_update_property_zval(this_ptr, SL("_pdo"), pdo TSRMLS_CC); PHALCON_MM_RESTORE(); }