/** * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST * * @param Phalcon\DiInterface $dependencyInjector * @param string $modelName * @param array $data * @return Phalcon\Mvc\Model\Criteria */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){ zval *dependency_injector, *model_name, *data; zval *conditions, *service, *meta_data = NULL, *model; zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL; zval *value_pattern = NULL, *join_conditions; zval *column_map = NULL; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data); if (Z_TYPE_P(data) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array"); return; } if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services"); return; } object_init_ex(return_value, phalcon_mvc_model_criteria_ce); if (zend_hash_num_elements(Z_ARRVAL_P(data))) { PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_modelsMetadata); PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce); ce0 = phalcon_fetch_class(model_name TSRMLS_CC); PHALCON_INIT_VAR(model); object_init_ex(model, ce0); if (phalcon_has_constructor(model TSRMLS_CC)) { PHALCON_CALL_METHOD(NULL, model, "__construct"); } PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1); if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", model); if (Z_TYPE_P(column_map) != IS_ARRAY) { PHALCON_INIT_NVAR(column_map); } } else { column_map = PHALCON_GLOBAL(z_null); } PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model); PHALCON_INIT_VAR(bind); array_init(bind); PHALCON_INIT_VAR(conditions); array_init(conditions); /** * We look for attributes in the array passed as data */ phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { zval *real_field; PHALCON_GET_HKEY(field, ah0, hp0); PHALCON_GET_HVALUE(value); if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) { real_field = field; } if (phalcon_array_isset_fetch(&type, data_types, real_field)) { if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) { if (PHALCON_IS_LONG(type, 2)) { /** * For varchar types we use LIKE operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":"); PHALCON_INIT_NVAR(value_pattern); PHALCON_CONCAT_SVS(value_pattern, "%", value, "%"); phalcon_array_update_zval(&bind, field, value_pattern, PH_COPY); } else { /** * For the rest of data types we use a plain = operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":"); phalcon_array_update_zval(&bind, field, value, PH_COPY); } phalcon_array_append(&conditions, condition, 0); } } zend_hash_move_forward_ex(ah0, &hp0); } if (zend_hash_num_elements(Z_ARRVAL_P(conditions))) { PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); PHALCON_CALL_METHOD(NULL, return_value, "where", join_conditions, bind); } } PHALCON_CALL_METHOD(NULL, return_value, "setmodelname", model_name); RETURN_MM(); }
/** * Returns the translation related to the given key * * @param string $index * @param array $placeholders * @return string */ PHP_METHOD(Phalcon_Translate_Adapter_NativeArray, query){ zval *index, *placeholders = NULL, *translate, *translation = NULL; zval *value = NULL, *key = NULL, *key_placeholder = NULL, *replaced = NULL; HashTable *ah0; HashPosition hp0; zval **hd; char *hash_index; uint hash_index_len; ulong hash_num; int hash_type; int eval_int; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &index, &placeholders) == FAILURE) { PHALCON_MM_RESTORE(); RETURN_NULL(); } if (!placeholders) { PHALCON_INIT_NVAR(placeholders); } PHALCON_INIT_VAR(translate); phalcon_read_property(&translate, this_ptr, SL("_translate"), PH_NOISY_CC); eval_int = phalcon_array_isset(translate, index); if (eval_int) { PHALCON_INIT_VAR(translation); phalcon_array_fetch(&translation, translate, index, PH_NOISY_CC); if (Z_TYPE_P(placeholders) == IS_ARRAY) { if (phalcon_fast_count_ev(placeholders TSRMLS_CC)) { if (!phalcon_valid_foreach(placeholders TSRMLS_CC)) { return; } ah0 = Z_ARRVAL_P(placeholders); zend_hash_internal_pointer_reset_ex(ah0, &hp0); ph_cycle_start_0: if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) { goto ph_cycle_end_0; } PHALCON_GET_FOREACH_KEY(key, ah0, hp0); PHALCON_GET_FOREACH_VALUE(value); PHALCON_INIT_NVAR(key_placeholder); PHALCON_CONCAT_SVS(key_placeholder, "%", key, "%"); PHALCON_INIT_NVAR(replaced); phalcon_fast_str_replace(replaced, key_placeholder, value, translation TSRMLS_CC); PHALCON_CPY_WRT(translation, replaced); zend_hash_move_forward_ex(ah0, &hp0); goto ph_cycle_start_0; ph_cycle_end_0: if(0){} } } RETURN_CCTOR(translation); } RETURN_CCTOR(index); }
/** * Read the model's column map, this can't be inferred * * @param Phalcon\Mvc\ModelInterface $model * @param Phalcon\DiInterface $dependencyInjector * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getColumnMaps){ zval *model, *dependency_injector, *service; zval *ordered_column_map, *reversed_column_map = NULL; zval *annotations = NULL, *class_name, *reflection = NULL; zval *exception_message = NULL, *properties_annotations = NULL; zval *column_annot_name, *column_map_name; zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL; zval *column_annotation = NULL, *real_property = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &model, &dependency_injector); if (!PHALCON_GLOBAL(orm).column_renaming) { RETURN_MM_NULL(); } if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid"); return; } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "annotations", 1); PHALCON_CALL_METHOD(&annotations, dependency_injector, "get", service); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, model, 0 TSRMLS_CC); PHALCON_CALL_METHOD(&reflection, annotations, "get", class_name); if (Z_TYPE_P(reflection) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Get the properties defined in */ PHALCON_CALL_METHOD(&properties_annotations, reflection, "getpropertiesannotations"); if (!phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Initialize meta-data */ PHALCON_INIT_VAR(ordered_column_map); array_init(ordered_column_map); PHALCON_INIT_VAR(reversed_column_map); array_init(reversed_column_map); PHALCON_INIT_VAR(column_annot_name); ZVAL_STRING(column_annot_name, "Column", 1); PHALCON_INIT_VAR(column_map_name); ZVAL_STRING(column_map_name, "column", 1); phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(property, ah0, hp0); PHALCON_GET_HVALUE(prop_annotations); /** * All columns marked with the 'Column' annotation are considered columns */ PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", column_annot_name); if (!zend_is_true(has_annotation)) { zend_hash_move_forward_ex(ah0, &hp0); continue; } /** * Fetch the 'column' annotation */ PHALCON_CALL_METHOD(&column_annotation, prop_annotations, "get", column_annot_name); /** * Check column map */ PHALCON_CALL_METHOD(&real_property, column_annotation, "getargument", column_map_name); if (!PHALCON_IS_EMPTY(real_property)) { phalcon_array_update_zval(&ordered_column_map, real_property, property, PH_COPY); phalcon_array_update_zval(&reversed_column_map, property, real_property, PH_COPY); } else { phalcon_array_update_zval(&ordered_column_map, property, property, PH_COPY); phalcon_array_update_zval(&reversed_column_map, property, property, PH_COPY); } zend_hash_move_forward_ex(ah0, &hp0); } array_init_size(return_value, 2); phalcon_array_update_long(&return_value, 0, ordered_column_map, PH_COPY); phalcon_array_update_long(&return_value, 1, reversed_column_map, PH_COPY); PHALCON_MM_RESTORE(); }
/** * Phalcon\Mvc\Model\MetaData\Redis constructor * * @param array $options */ PHP_METHOD(Phalcon_Mvc_Model_MetaData_Redis, __construct){ zval *options = NULL; zval *host, *port, *auth, *persistent, *lifetime, *prefix; zval *frontend_data, *redis, *option; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &options); if (Z_TYPE_P(options) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The options must be an array"); return; } if (!phalcon_array_isset_string_fetch(&host, options, SS("host"))) { host = NULL; } if (!phalcon_array_isset_string_fetch(&port, options, SS("port"))) { port = NULL; } if (!phalcon_array_isset_string_fetch(&auth, options, SS("auth"))) { auth = NULL; } if (!phalcon_array_isset_string_fetch(&persistent, options, SS("persistent"))) { persistent = NULL; } if (!phalcon_array_isset_string_fetch(&lifetime, options, SS("lifetime"))) { PHALCON_INIT_VAR(lifetime); ZVAL_LONG(lifetime, 8600); } phalcon_update_property_this(this_ptr, SL("_lifetime"), lifetime TSRMLS_CC); if (!phalcon_array_isset_string_fetch(&prefix, options, SS("prefix"))) { PHALCON_INIT_VAR(prefix); ZVAL_EMPTY_STRING(prefix); } /* create redis instance */ PHALCON_INIT_VAR(option); array_init_size(option, 1); phalcon_array_update_string(&option, SL("lifetime"), lifetime, PH_COPY); PHALCON_INIT_VAR(frontend_data); object_init_ex(frontend_data, phalcon_cache_frontend_data_ce); PHALCON_CALL_METHOD(NULL, frontend_data, "__construct", option); PHALCON_INIT_NVAR(option); array_init(option); phalcon_array_update_string_string(&option, SL("statsKey"), SL("$PMM$"), PH_COPY); if (host) { phalcon_array_update_string(&option, SL("host"), host, PH_COPY); } if (port) { phalcon_array_update_string(&option, SL("port"), port, PH_COPY); } if (auth) { phalcon_array_update_string(&option, SL("auth"), auth, PH_COPY); } if (persistent) { phalcon_array_update_string(&option, SL("persistent"), persistent, PH_COPY); } phalcon_array_update_string(&option, SL("prefix"), prefix, PH_COPY); PHALCON_INIT_VAR(redis); object_init_ex(redis, phalcon_cache_backend_redis_ce); PHALCON_CALL_METHOD(NULL, redis, "__construct", frontend_data, option); phalcon_update_property_this(this_ptr, SL("_redis"), redis TSRMLS_CC); phalcon_update_property_empty_array(this_ptr, SL("_metaData") TSRMLS_CC); PHALCON_MM_RESTORE(); }
/** * Check whether internal resource has rows to fetch * * @return boolean */ PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){ zval *type = NULL, *result, *row = NULL, *rows, *underscore, *empty_str; zval *active_row, *columns_types, *column = NULL, *alias = NULL; zval *source = NULL, *instance = NULL, *attributes = NULL, *column_map = NULL; zval *row_model = NULL, *attribute = NULL, *column_alias = NULL, *value = NULL; zval *model_attribute = NULL, *sql_alias = NULL, *n_alias = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; char *hash_index; uint hash_index_len; ulong hash_num; int hash_type; int eval_int; PHALCON_MM_GROW(); PHALCON_INIT_VAR(type); phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC); if (zend_is_true(type)) { PHALCON_INIT_VAR(result); phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC); if (PHALCON_IS_NOT_FALSE(result)) { PHALCON_INIT_VAR(row); PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetch", result, PH_NO_CHECK); } else { PHALCON_INIT_NVAR(row); ZVAL_BOOL(row, 0); } } else { PHALCON_INIT_VAR(rows); phalcon_read_property(&rows, this_ptr, SL("_rows"), PH_NOISY_CC); Z_SET_ISREF_P(rows); PHALCON_INIT_NVAR(row); PHALCON_CALL_FUNC_PARAMS_1(row, "current", rows); Z_UNSET_ISREF_P(rows); if (zend_is_true(row)) { Z_SET_ISREF_P(rows); PHALCON_CALL_FUNC_PARAMS_1_NORETURN("next", rows); Z_UNSET_ISREF_P(rows); } } if (PHALCON_IS_NOT_FALSE(row)) { PHALCON_INIT_VAR(underscore); ZVAL_STRING(underscore, "_", 1); PHALCON_INIT_VAR(empty_str); ZVAL_STRING(empty_str, "", 1); PHALCON_INIT_VAR(active_row); object_init_ex(active_row, phalcon_mvc_model_row_ce); PHALCON_INIT_VAR(columns_types); phalcon_read_property(&columns_types, this_ptr, SL("_columnTypes"), PH_NOISY_CC); if (!phalcon_valid_foreach(columns_types TSRMLS_CC)) { return; } ah0 = Z_ARRVAL_P(columns_types); zend_hash_internal_pointer_reset_ex(ah0, &hp0); ph_cycle_start_0: if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) { goto ph_cycle_end_0; } PHALCON_GET_FOREACH_KEY(alias, ah0, hp0); PHALCON_GET_FOREACH_VALUE(column); PHALCON_INIT_NVAR(type); phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY_CC); if (PHALCON_COMPARE_STRING(type, "object")) { /** * Object columns are assigned column by column */ PHALCON_INIT_NVAR(source); phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY_CC); PHALCON_INIT_NVAR(instance); phalcon_array_fetch_string(&instance, column, SL("instance"), PH_NOISY_CC); PHALCON_INIT_NVAR(attributes); phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY_CC); PHALCON_INIT_NVAR(column_map); phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY_CC); /** * Assign the values from the _source_attribute notation to its real column name */ PHALCON_INIT_NVAR(row_model); array_init(row_model); if (!phalcon_valid_foreach(attributes TSRMLS_CC)) { return; } ah1 = Z_ARRVAL_P(attributes); zend_hash_internal_pointer_reset_ex(ah1, &hp1); ph_cycle_start_1: if (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) != SUCCESS) { goto ph_cycle_end_1; } PHALCON_GET_FOREACH_VALUE(attribute); PHALCON_INIT_NVAR(column_alias); PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute); PHALCON_INIT_NVAR(value); phalcon_array_fetch(&value, row, column_alias, PH_NOISY_CC); phalcon_array_update_zval(&row_model, attribute, &value, PH_COPY | PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); goto ph_cycle_start_1; ph_cycle_end_1: /** * Assign the values to the attributes using a column map */ PHALCON_INIT_NVAR(model_attribute); PHALCON_CALL_STATIC_PARAMS_3(model_attribute, "phalcon\\mvc\\model", "dumpresultmap", instance, row_model, column_map); /** * The complete object is assigned to an attribute with the name of the alias or * the model name */ PHALCON_INIT_NVAR(attribute); phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY_CC); phalcon_update_property_zval_zval(active_row, attribute, model_attribute TSRMLS_CC); } else { /** * Scalar columns are simply assigned to the result object */ eval_int = phalcon_array_isset_string(column, SS("sqlAlias")); if (eval_int) { PHALCON_INIT_NVAR(sql_alias); phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY_CC); PHALCON_INIT_NVAR(value); phalcon_array_fetch(&value, row, sql_alias, PH_NOISY_CC); } else { PHALCON_INIT_NVAR(value); phalcon_array_fetch(&value, row, alias, PH_NOISY_CC); } /** * If a 'balias' is defined is not an unnamed scalar */ eval_int = phalcon_array_isset_string(column, SS("balias")); if (eval_int) { phalcon_update_property_zval_zval(active_row, alias, value TSRMLS_CC); } else { PHALCON_INIT_NVAR(n_alias); phalcon_fast_str_replace(n_alias, underscore, empty_str, alias TSRMLS_CC); phalcon_update_property_zval_zval(active_row, n_alias, value TSRMLS_CC); } } zend_hash_move_forward_ex(ah0, &hp0); goto ph_cycle_start_0; ph_cycle_end_0: phalcon_update_property_zval(this_ptr, SL("_activeRow"), active_row TSRMLS_CC); PHALCON_MM_RESTORE(); RETURN_TRUE; } else { phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC); } PHALCON_MM_RESTORE(); RETURN_FALSE; }
/** * Moves internal resulset cursor to another position letting us to fetch a certain row * *<code> * $result = $connection->query("SELECT * FROM robots ORDER BY name"); * $result->dataSeek(2); // Move to third row on result * $row = $result->fetch(); // Fetch third row *</code> * * @param int $number */ PHP_METHOD(Phalcon_Db_Result_Pdo, dataSeek){ long number = 0, n; zval *connection, *pdo, *sql_statement; zval *bind_params, *bind_types, *statement = NULL; zval *temp_statement = NULL; pdo_stmt_t *stmt; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &number) == FAILURE) { PHALCON_MM_RESTORE(); RETURN_NULL(); } PHALCON_INIT_VAR(connection); phalcon_read_property(&connection, this_ptr, SL("_connection"), PH_NOISY_CC); PHALCON_INIT_VAR(pdo); PHALCON_CALL_METHOD(pdo, connection, "getinternalhandler", PH_NO_CHECK); PHALCON_INIT_VAR(sql_statement); phalcon_read_property(&sql_statement, this_ptr, SL("_sqlStatement"), PH_NOISY_CC); PHALCON_INIT_VAR(bind_params); phalcon_read_property(&bind_params, this_ptr, SL("_bindParams"), PH_NOISY_CC); /** * PDO doesn't support scrollable cursors, so we need to re-execute the statement again */ if (Z_TYPE_P(bind_params) == IS_ARRAY) { PHALCON_INIT_VAR(bind_types); phalcon_read_property(&bind_types, this_ptr, SL("_bindTypes"), PH_NOISY_CC); PHALCON_INIT_VAR(statement); PHALCON_CALL_METHOD_PARAMS_1(statement, pdo, "prepare", sql_statement, PH_NO_CHECK); if (Z_TYPE_P(statement) == IS_OBJECT) { PHALCON_INIT_VAR(temp_statement); PHALCON_CALL_METHOD_PARAMS_3(temp_statement, connection, "executeprepared", statement, bind_params, bind_types, PH_NO_CHECK); PHALCON_CPY_WRT(statement, temp_statement); } } else { PHALCON_INIT_NVAR(statement); PHALCON_CALL_METHOD_PARAMS_1(statement, pdo, "query", sql_statement, PH_NO_CHECK); } phalcon_update_property_zval(this_ptr, SL("_pdoStatement"), statement TSRMLS_CC); /** * This a fetch scroll to reach the desired position, however with a big number of records * maybe it may be very slow */ stmt = (pdo_stmt_t*) zend_object_store_get_object(statement TSRMLS_CC); if (!stmt->dbh) { PHALCON_MM_RESTORE(); RETURN_FALSE; } n = -1; number--; while (n != number) { if(!stmt->methods->fetcher(stmt, PDO_FETCH_ORI_NEXT, 0 TSRMLS_CC)) { PHALCON_MM_RESTORE(); RETURN_NULL(); } n++; } PHALCON_MM_RESTORE(); }
/** * Handles a MVC request * * @param string $uri * @return Phalcon\Http\ResponseInterface */ PHP_METHOD(Phalcon_Mvc_JsonRpc, handle){ zval *uri = NULL, *dependency_injector, *events_manager; zval *status = NULL, *service = NULL, *request = NULL, *response = NULL; zval *json = NULL, *data = NULL, *jsonrpc_message, *jsonrpc_error, *jsonrpc_result = NULL; zval *jsonrpc_method, *jsonrpc_params, *jsonrpc_id; zval *url = NULL, *router = NULL, *module_name = NULL; zval *module_object = NULL, *modules; zval *module, *class_name = NULL, *module_params; zval *namespace_name = NULL; zval *controller_name = NULL, *action_name = NULL, *params = NULL, *exact = NULL; zval *dispatcher = NULL, *controller = NULL, *returned_response = NULL; zval *path; PHALCON_MM_GROW(); dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "A dependency injection object is required to access internal services"); return; } events_manager = phalcon_read_property(getThis(), SL("_eventsManager"), PH_NOISY); if (Z_TYPE_P(events_manager) != IS_OBJECT) { events_manager = NULL; } else { PHALCON_VERIFY_INTERFACE_EX(events_manager, phalcon_events_managerinterface_ce, phalcon_mvc_jsonrpc_exception_ce, 1); } /* Call boot event, this allows the developer to perform initialization actions */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:boot", getThis(), NULL)) { RETURN_MM_FALSE; } /* Deserializer Json */ PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(request)); PHALCON_CALL_METHOD(&request, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(request, phalcon_http_requestinterface_ce); PHALCON_CALL_METHOD(&json, request, "getrawbody"); PHALCON_CALL_FUNCTION(&data, "json_decode", json, &PHALCON_GLOBAL(z_true)); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(response)); PHALCON_CALL_METHOD(&response, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(response, phalcon_http_responseinterface_ce); PHALCON_INIT_VAR(jsonrpc_message); array_init(jsonrpc_message); PHALCON_INIT_VAR(jsonrpc_error); array_init(jsonrpc_error); if (PHALCON_IS_EMPTY(data)) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY); } else if (Z_TYPE_P(data) != IS_ARRAY) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY); } else if (!phalcon_array_isset_str(data, SL("jsonrpc"))) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY); } else if (!phalcon_array_isset_str(data, SL("method"))) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY); } else { PHALCON_OBS_VAR(jsonrpc_method); phalcon_array_fetch_str(&jsonrpc_method, data, SL("method"), PH_NOISY); if (phalcon_array_isset_str(data, SL("params"))) { PHALCON_OBS_VAR(jsonrpc_params); phalcon_array_fetch_str(&jsonrpc_params, data, SL("params"), PH_NOISY); } else { PHALCON_INIT_VAR(jsonrpc_params); array_init(jsonrpc_params); } PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(url)); PHALCON_CALL_METHOD(&url, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(url, phalcon_mvc_urlinterface_ce); PHALCON_CALL_METHOD(&uri, url, "get", jsonrpc_method); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(router)); PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce); /* Handle the URI pattern (if any) */ PHALCON_CALL_METHOD(NULL, router, "handle", uri); /* Load module config */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); /* Load module config */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); /* If the router doesn't return a valid module we use the default module */ if (!zend_is_true(module_name)) { module_name = phalcon_read_property(getThis(), SL("_defaultModule"), PH_NOISY); } /** * Process the module definition */ if (zend_is_true(module_name)) { if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeStartModule", getThis(), module_name)) { RETURN_MM_FALSE; } /** * Check if the module passed by the router is registered in the modules container */ modules = phalcon_read_property(getThis(), SL("_modules"), PH_NOISY); if (!phalcon_array_isset_fetch(&module, modules, module_name)) { convert_to_string(module_name); zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module %s is not registered in the jsonrpc container", Z_STRVAL_P(module_name)); RETURN_MM(); } /** * A module definition must be an array or an object */ if (Z_TYPE_P(module) != IS_ARRAY && Z_TYPE_P(module) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition"); return; } /* An array module definition contains a path to a module definition class */ if (Z_TYPE_P(module) == IS_ARRAY) { /* Class name used to load the module definition */ if (phalcon_array_isset_str(module, SL("className"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_str(&class_name, module, SL("className"), PH_NOISY); } else { PHALCON_INIT_NVAR(class_name); ZVAL_STRING(class_name, "Module"); } /* If the developer has specified a path, try to include the file */ if (phalcon_array_isset_str(module, SL("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_str(&path, module, SL("path"), PH_NOISY); convert_to_string_ex(path); if (Z_TYPE_P(class_name) != IS_STRING || phalcon_class_exists(class_name, 0) == NULL) { if (phalcon_file_exists(path) == SUCCESS) { RETURN_MM_ON_FAILURE(phalcon_require(Z_STRVAL_P(path))); } else { zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module definition path '%s' does not exist", Z_STRVAL_P(path)); RETURN_MM(); } } } PHALCON_CALL_METHOD(&module_object, dependency_injector, "get", class_name); /** * 'registerAutoloaders' and 'registerServices' are automatically called */ PHALCON_CALL_METHOD(NULL, module_object, "registerautoloaders", dependency_injector); PHALCON_CALL_METHOD(NULL, module_object, "registerservices", dependency_injector); } else if (Z_TYPE_P(module) == IS_OBJECT && instanceof_function(Z_OBJCE_P(module), zend_ce_closure)) { /* A module definition object, can be a Closure instance */ PHALCON_INIT_VAR(module_params); array_init_size(module_params, 1); phalcon_array_append(module_params, dependency_injector, PH_COPY); PHALCON_CALL_USER_FUNC_ARRAY(&status, module, module_params); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition"); return; } /* Calling afterStartModule event */ if (events_manager) { if (!module_object) { module_object = &PHALCON_GLOBAL(z_null); } phalcon_update_property_this(getThis(), SL("_moduleObject"), module_object); if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterStartModule", getThis(), module_name)) { RETURN_MM_FALSE; } } } /* We get the parameters from the router and assign them to the dispatcher */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); PHALCON_CALL_METHOD(&namespace_name, router, "getnamespacename"); PHALCON_CALL_METHOD(&controller_name, router, "getcontrollername"); PHALCON_CALL_METHOD(&action_name, router, "getactionname"); PHALCON_CALL_METHOD(¶ms, router, "getparams"); PHALCON_CALL_METHOD(&exact, router, "isexactcontrollername"); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(dispatcher)); PHALCON_CALL_METHOD(&dispatcher, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(dispatcher, phalcon_dispatcherinterface_ce); /* Assign the values passed from the router */ PHALCON_CALL_METHOD(NULL, dispatcher, "setmodulename", module_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setnamespacename", namespace_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setcontrollername", controller_name, exact); PHALCON_CALL_METHOD(NULL, dispatcher, "setactionname", action_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setparams", jsonrpc_params); /* Calling beforeHandleRequest */ RETURN_MM_ON_FAILURE(phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeHandleRequest", getThis(), dispatcher)); /* The dispatcher must return an object */ PHALCON_CALL_METHOD(&controller, dispatcher, "dispatch"); PHALCON_INIT_VAR(returned_response); /* Get the latest value returned by an action */ PHALCON_CALL_METHOD(&jsonrpc_result, dispatcher, "getreturnedvalue"); } /* Calling afterHandleRequest */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterHandleRequest", getThis(), controller) && EG(exception)) { RETURN_MM(); } phalcon_array_update_str_str(jsonrpc_message, SL("jsonrpc"), SL("2.0"), PH_COPY); if (PHALCON_IS_NOT_EMPTY(jsonrpc_error)) { phalcon_array_update_str(jsonrpc_message, SL("error"), jsonrpc_error, PH_COPY); } if (jsonrpc_result != NULL) { phalcon_array_update_str(jsonrpc_message, SL("result"), jsonrpc_result, PH_COPY); } if (phalcon_array_isset_str_fetch(&jsonrpc_id, data, SL("id"))) { phalcon_array_update_str(jsonrpc_message, SL("id"), jsonrpc_id, PH_COPY); } else { phalcon_array_update_str(jsonrpc_message, SL("id"), &PHALCON_GLOBAL(z_null), PH_COPY); } PHALCON_CALL_METHOD(NULL, response, "setjsoncontent", jsonrpc_message); /* Calling beforeSendResponse */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeSendResponse", getThis(), response) && EG(exception)) { RETURN_MM(); } /* Headers are automatically sent */ PHALCON_CALL_METHOD(NULL, response, "sendheaders"); /* Cookies are automatically sent */ PHALCON_CALL_METHOD(NULL, response, "sendcookies"); /* Return the response */ RETURN_CCTOR(response); }
/** * Lists table references * *<code> * print_r($connection->describeReferences('robots_parts')); *</code> * * @param string $table * @param string $schema * @return Phalcon\Db\Reference[] */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, describeReferences){ zval *table, *schema = NULL, *dialect, *fetch_num, *sql, *empty_arr; zval *references, *describe, *reference = NULL, *constraint_name = NULL; zval *referenced_schema = NULL, *referenced_table = NULL; zval *reference_array = NULL, *column_name = NULL, *referenced_columns = NULL; zval *reference_objects, *array_reference = NULL; zval *name = NULL, *columns = NULL, *definition = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &table, &schema) == FAILURE) { RETURN_MM_NULL(); } if (!schema) { PHALCON_INIT_VAR(schema); } PHALCON_OBS_VAR(dialect); phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); /** * We're using FETCH_NUM to fetch the columns */ PHALCON_INIT_VAR(fetch_num); ZVAL_LONG(fetch_num, 3); /** * Get the SQL required to describe the references from the Dialect */ PHALCON_INIT_VAR(sql); PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describereferences", table, schema); PHALCON_INIT_VAR(empty_arr); array_init(empty_arr); PHALCON_INIT_VAR(references); array_init(references); /** * Execute the SQL returning the */ PHALCON_INIT_VAR(describe); PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_num); if (!phalcon_is_iterable(describe, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(reference); PHALCON_OBS_NVAR(constraint_name); phalcon_array_fetch_long(&constraint_name, reference, 2, PH_NOISY_CC); if (!phalcon_array_isset(references, constraint_name)) { PHALCON_OBS_NVAR(referenced_schema); phalcon_array_fetch_long(&referenced_schema, reference, 3, PH_NOISY_CC); PHALCON_OBS_NVAR(referenced_table); phalcon_array_fetch_long(&referenced_table, reference, 4, PH_NOISY_CC); PHALCON_INIT_NVAR(reference_array); array_init_size(reference_array, 4); phalcon_array_update_string(&reference_array, SL("referencedSchema"), &referenced_schema, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&reference_array, SL("referencedTable"), &referenced_table, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&reference_array, SL("columns"), &empty_arr, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&reference_array, SL("referencedColumns"), &empty_arr, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_zval(&references, constraint_name, &reference_array, PH_COPY | PH_SEPARATE TSRMLS_CC); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_long(&column_name, reference, 1, PH_NOISY_CC); phalcon_array_update_zval_string_append_multi_3(&references, constraint_name, SL("columns"), &column_name, 0 TSRMLS_CC); PHALCON_OBS_NVAR(referenced_columns); phalcon_array_fetch_long(&referenced_columns, reference, 5, PH_NOISY_CC); phalcon_array_update_zval_string_append_multi_3(&references, constraint_name, SL("referencedColumns"), &referenced_columns, 0 TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(reference_objects); array_init(reference_objects); if (!phalcon_is_iterable(references, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_KEY(name, ah1, hp1); PHALCON_GET_FOREACH_VALUE(array_reference); PHALCON_OBS_NVAR(referenced_schema); phalcon_array_fetch_string(&referenced_schema, array_reference, SL("referencedSchema"), PH_NOISY_CC); PHALCON_OBS_NVAR(referenced_table); phalcon_array_fetch_string(&referenced_table, array_reference, SL("referencedTable"), PH_NOISY_CC); PHALCON_OBS_NVAR(columns); phalcon_array_fetch_string(&columns, array_reference, SL("columns"), PH_NOISY_CC); PHALCON_OBS_NVAR(referenced_columns); phalcon_array_fetch_string(&referenced_columns, array_reference, SL("referencedColumns"), PH_NOISY_CC); PHALCON_INIT_NVAR(definition); array_init_size(definition, 4); phalcon_array_update_string(&definition, SL("referencedSchema"), &referenced_schema, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("referencedTable"), &referenced_table, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("columns"), &columns, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("referencedColumns"), &referenced_columns, PH_COPY | PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(reference); object_init_ex(reference, phalcon_db_reference_ce); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(reference, "__construct", name, definition); phalcon_array_update_zval(&reference_objects, name, &reference, PH_COPY | PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); } RETURN_CTOR(reference_objects); }
/** * 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(placeholders, &ah0, &hp0, 0, 0 TSRMLS_CC)) { 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 (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_CC); if (phalcon_compare_strict_long(type, 32 TSRMLS_CC)) { PHALCON_INIT_NVAR(cast_value); PHALCON_CALL_FUNC_PARAMS_1(cast_value, "doubleval", value); 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_PARAMS_2_NORETURN(statement, "bindparam", parameter, cast_value); } else { PHALCON_CALL_METHOD_PARAMS_3_NORETURN(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_PARAMS_2_NORETURN(statement, "bindparam", parameter, value); Z_UNSET_ISREF_P(value); } zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_CALL_METHOD_NORETURN(statement, "execute"); RETURN_CCTOR(statement); }
/** * Lists table indexes * *<code> * print_r($connection->describeIndexes('robots_parts')); *</code> * * @param string $table * @param string $schema * @return Phalcon\Db\Index[] */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, describeIndexes){ zval *table, *schema = NULL, *dialect, *fetch_num, *sql, *describe; zval *indexes, *index = NULL, *key_name = NULL, *empty_arr = NULL, *column_name = NULL; zval *index_objects, *index_columns = NULL, *name = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &table, &schema) == FAILURE) { RETURN_MM_NULL(); } if (!schema) { PHALCON_INIT_VAR(schema); } PHALCON_OBS_VAR(dialect); phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); /** * We're using FETCH_NUM to fetch the columns */ PHALCON_INIT_VAR(fetch_num); ZVAL_LONG(fetch_num, 3); /** * Get the SQL required to describe indexes from the Dialect */ PHALCON_INIT_VAR(sql); PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describeindexes", table, schema); /** * Cryptic Guide: 2: table, 3: from, 4: to */ PHALCON_INIT_VAR(describe); PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_num); PHALCON_INIT_VAR(indexes); array_init(indexes); if (!phalcon_is_iterable(describe, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(index); PHALCON_OBS_NVAR(key_name); phalcon_array_fetch_long(&key_name, index, 2, PH_NOISY_CC); 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 TSRMLS_CC); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_long(&column_name, index, 4, PH_NOISY_CC); phalcon_array_update_append_multi_2(&indexes, key_name, column_name, 0 TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(index_objects); array_init(index_objects); if (!phalcon_is_iterable(indexes, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_KEY(name, ah1, hp1); PHALCON_GET_FOREACH_VALUE(index_columns); /** * Every index is abstracted using a Phalcon\Db\Index instance */ PHALCON_INIT_NVAR(index); object_init_ex(index, phalcon_db_index_ce); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(index, "__construct", name, index_columns); phalcon_array_update_zval(&index_objects, name, &index, PH_COPY | PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); } RETURN_CTOR(index_objects); }
/** * 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_CC); 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_CC); phalcon_array_unset_string(&descriptor, SS("password"), PH_SEPARATE); } else { PHALCON_INIT_NVAR(password); } /** * 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(descriptor, &ah0, &hp0, 0, 0 TSRMLS_CC)) { 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 TSRMLS_CC); 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_CC); } 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); /** * 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_CC); } else { PHALCON_INIT_NVAR(options); array_init(options); } phalcon_array_update_long_long(&options, PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION, PH_SEPARATE TSRMLS_CC); //phalcon_array_update_long_long(&options, PDO_ATTR_CASE, PDO_CASE_LOWER, PH_SEPARATE TSRMLS_CC); phalcon_array_update_long_long(&options, PDO_ATTR_CURSOR, PDO_CURSOR_SCROLL, PH_SEPARATE TSRMLS_CC); /** * 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_CC); if (zend_is_true(persistent)) { phalcon_array_update_long_bool(&options, PDO_ATTR_PERSISTENT, 1, PH_SEPARATE TSRMLS_CC); } } /** * 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_PARAMS_4_NORETURN(pdo, "__construct", dsn, username, password, options); phalcon_update_property_zval(this_ptr, SL("_pdo"), pdo TSRMLS_CC); PHALCON_MM_RESTORE();}
/** * Gets the column name in Sqlite * * @param Phalcon\Db\ColumnInterface $column * @return string */ PHP_METHOD(Phalcon_Db_Dialect_Sqlite, getColumnDefinition){ zval *column, *size = NULL, *column_type = NULL, *column_sql = NULL; zval *scale = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &column); if (Z_TYPE_P(column) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column definition must be an instance of Phalcon\\Db\\Column"); return; } PHALCON_CALL_METHOD(&size, column, "getsize"); PHALCON_CALL_METHOD(&column_type, column, "gettype"); switch (phalcon_get_intval(column_type)) { case 0: PHALCON_INIT_VAR(column_sql); ZVAL_STRING(column_sql, "INT"); break; case 1: PHALCON_INIT_NVAR(column_sql); ZVAL_STRING(column_sql, "DATE"); break; case 2: PHALCON_INIT_NVAR(column_sql); PHALCON_CONCAT_SVS(column_sql, "VARCHAR(", size, ")"); break; case 3: PHALCON_CALL_METHOD(&scale, column, "getscale"); PHALCON_INIT_NVAR(column_sql); PHALCON_CONCAT_SVSVS(column_sql, "NUMERIC(", size, ",", scale, ")"); break; case 4: PHALCON_INIT_NVAR(column_sql); ZVAL_STRING(column_sql, "TIMESTAMP"); break; case 5: PHALCON_INIT_NVAR(column_sql); PHALCON_CONCAT_SVS(column_sql, "CHARACTER(", size, ")"); break; case 6: PHALCON_INIT_NVAR(column_sql); ZVAL_STRING(column_sql, "TEXT"); break; case 7: PHALCON_INIT_NVAR(column_sql); ZVAL_STRING(column_sql, "FLOAT"); break; default: PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Unrecognized SQLite data type"); return; } RETURN_CTOR(column_sql); }
/** * Executes validator * * @param Phalcon\Mvc\ModelInterface $record * @return boolean */ PHP_METHOD(Phalcon_Mvc_Model_Validator_Inclusionin, validate){ zval *record, *field = NULL, *is_set = NULL, *domain = NULL; zval *value = NULL, *option, *message = NULL, *joined_domain, *is_set_code = NULL, *code = NULL; zval *type; zval *allow_empty = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &record); PHALCON_INIT_VAR(option); ZVAL_STRING(option, "field", 1); PHALCON_CALL_METHOD(&field, this_ptr, "getoption", option); if (Z_TYPE_P(field) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Field name must be a string"); return; } /** * The 'domain' option must be a valid array of not allowed values */ PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "domain", 1); PHALCON_CALL_METHOD(&is_set, this_ptr, "issetoption", option); if (PHALCON_IS_FALSE(is_set)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'domain' is required for this validator"); return; } PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "domain", 1); PHALCON_CALL_METHOD(&domain, this_ptr, "getoption", option); if (Z_TYPE_P(domain) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Option 'domain' must be an array"); return; } PHALCON_CALL_METHOD(&value, record, "readattribute", field); /* * Allow empty */ PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "allowEmpty", 1); PHALCON_CALL_METHOD(&allow_empty, this_ptr, "getoption", option); if (allow_empty && zend_is_true(allow_empty) && PHALCON_IS_EMPTY(value)) { RETURN_MM_TRUE; } /** * Check if the value is contained in the array */ if (!phalcon_fast_in_array(value, domain TSRMLS_CC)) { /** * Check if the developer has defined a custom message */ PHALCON_INIT_NVAR(option); PHALCON_ZVAL_MAYBE_INTERNED_STRING(option, phalcon_interned_message); PHALCON_CALL_METHOD(&message, this_ptr, "getoption", option); if (!zend_is_true(message)) { PHALCON_INIT_VAR(joined_domain); phalcon_fast_join_str(joined_domain, SL(", "), domain TSRMLS_CC); PHALCON_INIT_NVAR(message); PHALCON_CONCAT_SVSV(message, "Value of field '", field, "' must be part of list: ", joined_domain); } PHALCON_INIT_VAR(type); ZVAL_STRING(type, "Inclusion", 1); /* * Is code set */ PHALCON_INIT_NVAR(option); PHALCON_ZVAL_MAYBE_INTERNED_STRING(option, phalcon_interned_code); PHALCON_CALL_METHOD(&is_set_code, this_ptr, "issetoption", option); if (zend_is_true(is_set_code)) { PHALCON_CALL_METHOD(&code, this_ptr, "getoption", option); } else { PHALCON_INIT_VAR(code); ZVAL_LONG(code, 0); } PHALCON_CALL_METHOD(NULL, this_ptr, "appendmessage", message, field, type, code); RETURN_MM_FALSE; } RETURN_MM_TRUE; }
/** * Appends an IN condition to the current conditions * *<code> * $criteria->inWhere('id', [1, 2, 3]); *</code> * * @param string $expr * @param array $values * @return Phalcon\Mvc\Model\CriteriaInterface */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, inWhere){ zval *expr, *values, *hidden_param, *bind_params; zval *bind_keys, *value = NULL, *key = NULL, *query_key = NULL, *joined_keys; zval *conditions; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &expr, &values); if (Z_TYPE_P(values) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Values must be an array"); return; } PHALCON_OBS_VAR(hidden_param); phalcon_read_property_this(&hidden_param, this_ptr, SL("_hiddenParamNumber"), PH_NOISY TSRMLS_CC); SEPARATE_ZVAL(&hidden_param); PHALCON_INIT_VAR(bind_params); array_init(bind_params); PHALCON_INIT_VAR(bind_keys); array_init(bind_keys); phalcon_is_iterable(values, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(value); /** * Key with auto bind-params */ PHALCON_INIT_NVAR(key); PHALCON_CONCAT_SV(key, "phi", hidden_param); PHALCON_INIT_NVAR(query_key); PHALCON_CONCAT_SVS(query_key, ":", key, ":"); phalcon_array_append(&bind_keys, query_key, 0); phalcon_array_update_zval(&bind_params, key, value, PH_COPY); phalcon_increment(hidden_param); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(joined_keys); phalcon_fast_join_str(joined_keys, SL(", "), bind_keys TSRMLS_CC); /** * Create a standard IN condition with bind params */ PHALCON_INIT_VAR(conditions); PHALCON_CONCAT_VSVS(conditions, expr, " IN (", joined_keys, ")"); /** * Append the IN to the current conditions using and 'and' */ PHALCON_CALL_METHOD(NULL, this_ptr, "andwhere", conditions, bind_params); phalcon_update_property_this(this_ptr, SL("_hiddenParamNumber"), hidden_param TSRMLS_CC); RETURN_THIS(); }
/** * Gets number of rows returned by a resulset * *<code> * $result = $connection->query("SELECT * FROM robots ORDER BY name"); * echo 'There are ', $result->numRows(), ' rows in the resulset'; *</code> * * @return int */ PHP_METHOD(Phalcon_Db_Result_Pdo, numRows){ zval *row_count = NULL, *connection, *type = NULL, *pdo_statement = NULL; zval *sql_statement, *bind_params, *bind_types; zval *matches, *pattern, *match, *else_clauses; zval *sql, *result = NULL, *row = NULL; PHALCON_MM_GROW(); row_count = phalcon_read_property(getThis(), SL("_rowCount"), PH_NOISY); if (PHALCON_IS_FALSE(row_count)) { connection = phalcon_read_property(getThis(), SL("_connection"), PH_NOISY); PHALCON_CALL_METHOD(&type, connection, "gettype"); /** * MySQL/PostgreSQL library property returns the number of records */ if (PHALCON_IS_STRING(type, "mysql") || PHALCON_IS_STRING(type, "pgsql")) { pdo_statement = phalcon_read_property(getThis(), SL("_pdoStatement"), PH_NOISY); PHALCON_CALL_METHOD(&row_count, pdo_statement, "rowcount"); } /** * We should get the count using a new statement :( */ if (PHALCON_IS_FALSE(row_count)) { /** * SQLite/Oracle/SQLServer returns resultsets that to the client eyes (PDO) has an * arbitrary number of rows, so we need to perform an extra count to know that */ sql_statement = phalcon_read_property(getThis(), SL("_sqlStatement"), PH_NOISY); /** * If the sql_statement starts with SELECT COUNT(*) we don't make the count */ if (!phalcon_start_with_str(sql_statement, SL("SELECT COUNT(*) "))) { bind_params = phalcon_read_property(getThis(), SL("_bindParams"), PH_NOISY); bind_types = phalcon_read_property(getThis(), SL("_bindTypes"), PH_NOISY); PHALCON_INIT_VAR(matches); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^SELECT\\s+(.*)$/i"); PHALCON_INIT_VAR(match); RETURN_MM_ON_FAILURE(phalcon_preg_match(match, pattern, sql_statement, matches)); if (zend_is_true(match)) { PHALCON_OBS_VAR(else_clauses); phalcon_array_fetch_long(&else_clauses, matches, 1, PH_NOISY); PHALCON_INIT_VAR(sql); PHALCON_CONCAT_SVS(sql, "SELECT COUNT(*) \"numrows\" FROM (SELECT ", else_clauses, ")"); PHALCON_CALL_METHOD(&result, connection, "query", sql, bind_params, bind_types); PHALCON_CALL_METHOD(&row, result, "fetch"); PHALCON_OBS_NVAR(row_count); phalcon_array_fetch_str(&row_count, row, SL("numrows"), PH_NOISY); } } else { PHALCON_INIT_NVAR(row_count); ZVAL_LONG(row_count, 1); } } /** * Update the value to avoid further calculations */ phalcon_update_property_this(getThis(), SL("_rowCount"), row_count); } RETURN_CCTOR(row_count); }
/** * Sends SQL statements to the database server returning the success state. * Use this method only when the SQL statement sent to the server doesn't return any row * *<code> * //Inserting data * $success = $connection->execute("INSERT INTO robots VALUES (1, 'Astro Boy')"); * $success = $connection->execute("INSERT INTO robots VALUES (?, ?)", array(1, 'Astro Boy')); *</code> * * @param string $sqlStatement * @param array $bindParams * @param array $bindTypes * @return boolean */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, execute){ zval *sql_statement, *bind_params = NULL, *bind_types = NULL; zval *events_manager, *event_name = NULL, *status, *pdo; zval *statement = NULL, *affected_rows = NULL; zval *r0 = NULL; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zz", &sql_statement, &bind_params, &bind_types) == FAILURE) { RETURN_MM_NULL(); } if (!bind_params) { PHALCON_INIT_VAR(bind_params); } if (!bind_types) { PHALCON_INIT_VAR(bind_types); } /** * Execute the beforeQuery event if a EventsManager is available */ PHALCON_OBS_VAR(events_manager); phalcon_read_property(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "db:beforeQuery", 1); phalcon_update_property_zval(this_ptr, SL("_sqlStatement"), sql_statement TSRMLS_CC); phalcon_update_property_zval(this_ptr, SL("_sqlVariables"), bind_params TSRMLS_CC); phalcon_update_property_zval(this_ptr, SL("_sqlBindTypes"), bind_types TSRMLS_CC); PHALCON_INIT_VAR(status); PHALCON_CALL_METHOD_PARAMS_3(status, events_manager, "fire", event_name, this_ptr, bind_params); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } PHALCON_OBS_VAR(pdo); phalcon_read_property(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC); if (Z_TYPE_P(bind_params) == IS_ARRAY) { PHALCON_INIT_VAR(statement); PHALCON_CALL_METHOD_PARAMS_1(statement, pdo, "prepare", sql_statement); if (Z_TYPE_P(statement) == IS_OBJECT) { PHALCON_INIT_VAR(r0); PHALCON_CALL_METHOD_PARAMS_3(r0, this_ptr, "executeprepared", statement, bind_params, bind_types); PHALCON_CPY_WRT(statement, r0); PHALCON_INIT_VAR(affected_rows); PHALCON_CALL_METHOD(affected_rows, statement, "rowcount"); } } else { PHALCON_INIT_NVAR(affected_rows); PHALCON_CALL_METHOD_PARAMS_1(affected_rows, pdo, "exec", sql_statement); } /** * Execute the afterQuery event if a EventsManager is available */ if (Z_TYPE_P(affected_rows) == IS_LONG) { phalcon_update_property_zval(this_ptr, SL("_affectedRows"), affected_rows TSRMLS_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "db:afterQuery", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, bind_params); } } RETURN_MM_TRUE; }
/** * Gets number of rows returned by a resulset * *<code> * $result = $connection->query("SELECT * FROM robots ORDER BY name"); * echo 'There are ', $result->numRows(), ' rows in the resulset'; *</code> * * @return int */ PHP_METHOD(Phalcon_Db_Result_Pdo, numRows){ zval *row_count = NULL, *connection, *type, *sql_statement; zval *bind_params, *bind_types, *matches, *pattern; zval *match, *else_clauses, *sql, *fetch_num, *result; zval *row, *pdo_statement; PHALCON_MM_GROW(); PHALCON_INIT_VAR(row_count); phalcon_read_property(&row_count, this_ptr, SL("_rowCount"), PH_NOISY_CC); if (PHALCON_IS_FALSE(row_count)) { PHALCON_INIT_VAR(connection); phalcon_read_property(&connection, this_ptr, SL("_connection"), PH_NOISY_CC); PHALCON_INIT_VAR(type); PHALCON_CALL_METHOD(type, connection, "gettype", PH_NO_CHECK); if (PHALCON_COMPARE_STRING(type, "sqlite")) { /** * SQLite returns resultsets that to the client eyes (PDO) has an arbitrary number * of rows, so we need to perform an extra count to know that */ PHALCON_INIT_VAR(sql_statement); phalcon_read_property(&sql_statement, this_ptr, SL("_sqlStatement"), PH_NOISY_CC); PHALCON_INIT_VAR(bind_params); phalcon_read_property(&bind_params, this_ptr, SL("_bindParams"), PH_NOISY_CC); PHALCON_INIT_VAR(bind_types); phalcon_read_property(&bind_types, this_ptr, SL("_bindTypes"), PH_NOISY_CC); PHALCON_INIT_VAR(matches); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^SELECT\\s+(.*)$/i", 1); Z_SET_ISREF_P(matches); PHALCON_INIT_VAR(match); PHALCON_CALL_FUNC_PARAMS_3(match, "preg_match", pattern, sql_statement, matches); Z_UNSET_ISREF_P(matches); if (zend_is_true(match)) { PHALCON_INIT_VAR(else_clauses); phalcon_array_fetch_long(&else_clauses, matches, 1, PH_NOISY_CC); PHALCON_INIT_VAR(sql); PHALCON_CONCAT_SVS(sql, "SELECT COUNT(*) FROM (SELECT ", else_clauses, ")"); PHALCON_INIT_VAR(fetch_num); ZVAL_LONG(fetch_num, 3); PHALCON_INIT_VAR(result); PHALCON_CALL_METHOD_PARAMS_3(result, connection, "query", sql, bind_params, bind_types, PH_NO_CHECK); PHALCON_INIT_VAR(row); PHALCON_CALL_METHOD(row, result, "fetch", PH_NO_CHECK); PHALCON_INIT_NVAR(row_count); phalcon_array_fetch_long(&row_count, row, 0, PH_NOISY_CC); } } else { PHALCON_INIT_VAR(pdo_statement); phalcon_read_property(&pdo_statement, this_ptr, SL("_pdoStatement"), PH_NOISY_CC); PHALCON_INIT_NVAR(row_count); PHALCON_CALL_METHOD(row_count, pdo_statement, "rowcount", PH_NO_CHECK); } phalcon_update_property_zval(this_ptr, SL("_rowCount"), row_count TSRMLS_CC); } RETURN_CCTOR(row_count); }
/** * Manually bind params to a SQL statement. This method requires an active connection to a database system * *<code> * $sql = $connection->bindParams('SELECT * FROM robots WHERE name = ?0', array('Bender')); * echo $sql; // SELECT * FROM robots WHERE name = 'Bender' *</code> * * @param string $sqlStatement * @param array $params * @return string */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, bindParams){ zval *sql_statement, *params, *sql = NULL, *pdo, *bind_value = NULL; zval *index = NULL, *value = NULL, *place_key = NULL, *replaced_sql = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &sql_statement, ¶ms) == FAILURE) { RETURN_MM_NULL(); } if (Z_TYPE_P(params) == IS_ARRAY) { if (phalcon_fast_count_ev(params TSRMLS_CC)) { PHALCON_CPY_WRT(sql, sql_statement); PHALCON_OBS_VAR(pdo); phalcon_read_property(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC); if (!phalcon_is_iterable(params, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(index, ah0, hp0); PHALCON_GET_FOREACH_VALUE(bind_value); if (phalcon_is_numeric(bind_value)) { PHALCON_CPY_WRT(value, bind_value); } else { if (Z_TYPE_P(bind_value) == IS_OBJECT) { PHALCON_INIT_NVAR(value); PHALCON_CALL_FUNC_PARAMS_1(value, "strval", bind_value); } else { PHALCON_INIT_NVAR(value); PHALCON_CALL_METHOD_PARAMS_1(value, pdo, "quote", bind_value); } } /** * Handle long parameters as numeric placeholders: ?0, ?1 */ if (Z_TYPE_P(index) == IS_LONG) { PHALCON_INIT_NVAR(place_key); PHALCON_CONCAT_SV(place_key, "?", index); PHALCON_INIT_NVAR(replaced_sql); phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC); PHALCON_CPY_WRT(sql, replaced_sql); zend_hash_move_forward_ex(ah0, &hp0); continue; } /** * Handle long parameters as string placeholders: :name:, :other: */ if (Z_TYPE_P(index) == IS_STRING) { PHALCON_INIT_NVAR(place_key); PHALCON_CONCAT_SVS(place_key, ":", index, ":"); PHALCON_INIT_NVAR(replaced_sql); phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC); PHALCON_CPY_WRT(sql, replaced_sql); zend_hash_move_forward_ex(ah0, &hp0); continue; } /** * Unrecognized parameter type */ PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind parameter"); return; zend_hash_move_forward_ex(ah0, &hp0); } RETURN_CCTOR(sql); } } RETURN_CCTOR(sql_statement); }
/** * Magic method __get * * @param string $propertyName */ PHP_METHOD(Phalcon_DI_Injectable, __get){ zval *property_name, *dependency_injector = NULL; zval *has_service, *service = NULL, *class_name, *arguments; zval *persistent; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &property_name); PHALCON_OBS_VAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_INIT_NVAR(dependency_injector); PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault"); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_di_exception_ce, "A dependency injection object is required to access the application services"); return; } } /** * Fallback to the PHP userland if the cache is not available */ PHALCON_INIT_VAR(has_service); phalcon_call_method_p1(has_service, dependency_injector, "has", property_name); if (zend_is_true(has_service)) { PHALCON_INIT_VAR(service); phalcon_call_method_p1(service, dependency_injector, "getshared", property_name); phalcon_update_property_zval_zval(this_ptr, property_name, service TSRMLS_CC); RETURN_CCTOR(service); } if (PHALCON_IS_STRING(property_name, "di")) { phalcon_update_property_this(this_ptr, SL("di"), dependency_injector TSRMLS_CC); RETURN_CCTOR(dependency_injector); } /** * Accessing the persistent property will create a session bag in any class */ if (PHALCON_IS_STRING(property_name, "persistent")) { PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, this_ptr, 0 TSRMLS_CC); PHALCON_INIT_VAR(arguments); array_init_size(arguments, 1); phalcon_array_append(&arguments, class_name, PH_SEPARATE); PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "sessionBag", 1); PHALCON_INIT_VAR(persistent); phalcon_call_method_p2(persistent, dependency_injector, "get", service, arguments); phalcon_update_property_this(this_ptr, SL("persistent"), persistent TSRMLS_CC); RETURN_CCTOR(persistent); } /** * A notice is shown if the property is not defined and isn't a valid service */ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Access to undefined property %s", Z_STRVAL_P(property_name)); RETURN_MM_NULL(); }
/** * Converts bound parameters such as :name: or ?1 into PDO bind params ? * *<code> * print_r($connection->convertBoundParams('SELECT * FROM robots WHERE name = :name:', array('Bender'))); *</code> * * @param string $sql * @param array $params * @return array */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, convertBoundParams){ zval *sql, *params, *query_params = NULL, *placeholders; zval *matches, *set_order, *bind_pattern, *status = NULL; zval *place_match = NULL, *numeric_place = NULL, *value = NULL, *str_place = NULL; zval *question, *bound_sql = NULL; zval *r0 = NULL; zval *p0[] = { NULL, NULL, NULL, NULL }; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &sql, ¶ms) == FAILURE) { RETURN_MM_NULL(); } PHALCON_INIT_VAR(query_params); array_init(query_params); PHALCON_INIT_VAR(placeholders); array_init(placeholders); PHALCON_INIT_VAR(matches); PHALCON_INIT_VAR(set_order); ZVAL_LONG(set_order, 2); PHALCON_INIT_VAR(bind_pattern); ZVAL_STRING(bind_pattern, "/\\?([0-9]+)|:([a-zA-Z0-9_]+):/", 1); p0[0] = bind_pattern; p0[1] = sql; Z_SET_ISREF_P(matches); p0[2] = matches; p0[3] = set_order; PHALCON_INIT_VAR(r0); PHALCON_CALL_FUNC_PARAMS(r0, "preg_match_all", 4, p0); Z_UNSET_ISREF_P(p0[2]); PHALCON_CPY_WRT(status, r0); if (zend_is_true(status)) { if (!phalcon_is_iterable(matches, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(place_match); PHALCON_OBS_NVAR(numeric_place); phalcon_array_fetch_long(&numeric_place, place_match, 1, PH_NOISY_CC); if (phalcon_array_isset(params, numeric_place)) { PHALCON_OBS_NVAR(value); phalcon_array_fetch(&value, params, numeric_place, PH_NOISY_CC); } else { if (phalcon_array_isset_long(place_match, 2)) { PHALCON_OBS_NVAR(str_place); phalcon_array_fetch_long(&str_place, place_match, 2, PH_NOISY_CC); if (phalcon_array_isset(params, str_place)) { PHALCON_OBS_NVAR(value); phalcon_array_fetch(&value, params, str_place, PH_NOISY_CC); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Matched parameter wasn't found in parameters list"); return; } } else { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Matched parameter wasn't found in parameters list"); return; } } phalcon_array_append(&placeholders, value, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(question); ZVAL_STRING(question, "?", 1); PHALCON_INIT_VAR(bound_sql); PHALCON_CALL_FUNC_PARAMS_3(bound_sql, "preg_replace", bind_pattern, question, sql); } else { PHALCON_CPY_WRT(bound_sql, sql); } /** * Returns an array with the processed SQL and paramters */ PHALCON_INIT_NVAR(query_params); array_init_size(query_params, 2); phalcon_array_update_string(&query_params, SL("sql"), &bound_sql, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&query_params, SL("params"), &placeholders, PH_COPY | PH_SEPARATE TSRMLS_CC); RETURN_CTOR(query_params); }
/** * Stores cached content into the APC backend and stops the frontend * * @param string $keyName * @param string $content * @param long $lifetime * @param boolean $stopBuffer */ PHP_METHOD(Phalcon_Cache_Backend_Apc, save) { zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL; zval *last_key = NULL, *prefix, *frontend, *cached_content = NULL; zval *prepared_content, *ttl = NULL, *is_buffering; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zzzz", &key_name, &content, &lifetime, &stop_buffer) == FAILURE) { RETURN_MM_NULL(); } if (!key_name) { PHALCON_INIT_VAR(key_name); } if (!content) { PHALCON_INIT_VAR(content); } if (!lifetime) { PHALCON_INIT_VAR(lifetime); } if (!stop_buffer) { PHALCON_INIT_VAR(stop_buffer); ZVAL_BOOL(stop_buffer, 1); } if (Z_TYPE_P(key_name) == IS_NULL) { PHALCON_OBS_VAR(last_key); phalcon_read_property_this(&last_key, this_ptr, SL("_lastKey"), PH_NOISY_CC); } else { PHALCON_OBS_VAR(prefix); phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC); PHALCON_INIT_NVAR(last_key); PHALCON_CONCAT_SVV(last_key, "_PHCA", prefix, key_name); } if (!zend_is_true(last_key)) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first"); return; } PHALCON_OBS_VAR(frontend); phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC); if (Z_TYPE_P(content) == IS_NULL) { PHALCON_INIT_VAR(cached_content); PHALCON_CALL_METHOD(cached_content, frontend, "getcontent"); } else { PHALCON_CPY_WRT(cached_content, content); } PHALCON_INIT_VAR(prepared_content); PHALCON_CALL_METHOD_PARAMS_1(prepared_content, frontend, "beforestore", cached_content); if (Z_TYPE_P(lifetime) == IS_NULL) { PHALCON_INIT_VAR(ttl); PHALCON_CALL_METHOD(ttl, frontend, "getlifetime"); } else { PHALCON_CPY_WRT(ttl, lifetime); } PHALCON_CALL_FUNC_PARAMS_3_NORETURN("apc_store", last_key, prepared_content, ttl); PHALCON_INIT_VAR(is_buffering); PHALCON_CALL_METHOD(is_buffering, frontend, "isbuffering"); if (PHALCON_IS_TRUE(stop_buffer)) { PHALCON_CALL_METHOD_NORETURN(frontend, "stop"); } if (PHALCON_IS_TRUE(is_buffering)) { zend_print_zval(cached_content, 0); } phalcon_update_property_bool(this_ptr, SL("_started"), 0 TSRMLS_CC); PHALCON_MM_RESTORE(); }
/** * Outputs a message formatting it with HTML * *<code> * $flash->outputMessage('error', $message); *</code> * * @param string $type * @param string $message */ PHP_METHOD(Phalcon_Flash, outputMessage){ zval *type, *message, *automatic_html, *classes; zval *type_classes, *joined_classes, *css_classes = NULL; zval *implicit_flush, *content, *msg = NULL, *html_message = NULL; int flag_automatic_html; int flag_implicit_flush; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &type, &message); automatic_html = phalcon_read_property(getThis(), SL("_automaticHtml"), PH_NOISY); flag_automatic_html = zend_is_true(automatic_html); if (flag_automatic_html) { classes = phalcon_read_property(getThis(), SL("_cssClasses"), PH_NOISY); PHALCON_INIT_VAR(css_classes); if (phalcon_array_isset_fetch(&type_classes, classes, type)) { if (Z_TYPE_P(type_classes) == IS_ARRAY) { PHALCON_INIT_VAR(joined_classes); phalcon_fast_join_str(joined_classes, SL(" "), type_classes); PHALCON_CONCAT_SVS(css_classes, " class=\"", joined_classes, "\""); } else { PHALCON_CONCAT_SVS(css_classes, " class=\"", type_classes, "\""); } } else { ZVAL_EMPTY_STRING(css_classes); } } implicit_flush = phalcon_read_property(getThis(), SL("_implicitFlush"), PH_NOISY); flag_implicit_flush = zend_is_true(implicit_flush); if (Z_TYPE_P(message) == IS_ARRAY) { /** * We create the message with implicit flush or other */ if (!flag_implicit_flush) { PHALCON_INIT_VAR(content); ZVAL_EMPTY_STRING(content); } /** * We create the message with implicit flush or other */ ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(message), msg) { /** * We create the applying formatting or not */ if (flag_automatic_html) { PHALCON_INIT_NVAR(html_message); PHALCON_CONCAT_SVSVS(html_message, "<div", css_classes, ">", msg, "</div>" PHP_EOL); } else { PHALCON_CPY_WRT(html_message, msg); } if (flag_implicit_flush) { zend_print_zval(html_message, 0); } else { phalcon_concat_self(content, html_message); } } ZEND_HASH_FOREACH_END(); /** * We return the message as string if the implicit_flush is turned off */ if (!flag_implicit_flush) { RETURN_CTOR(content); } } else { /** * We create the applying formatting or not */ if (flag_automatic_html) {
/** * Read the model's column map, this can't be infered * * @param Phalcon\Mvc\ModelInterface $model * @param Phalcon\DiInterface $dependencyInjector * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Introspection, getColumnMaps){ zval *model, *dependency_injector; zval *ordered_column_map = NULL, *columns = NULL, *column_name = NULL, *reversed_column_map = NULL; zval *user_name = NULL, *name = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &model, &dependency_injector); if (!PHALCON_GLOBAL(orm).column_renaming) { RETURN_MM_NULL(); } /** * Check for a columnMap() method on the model */ if (phalcon_method_exists_ex(model, SS("columnmap") TSRMLS_CC) == SUCCESS) { PHALCON_CALL_METHOD(&ordered_column_map, model, "columnmap"); if (Z_TYPE_P(ordered_column_map) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "columnMap() not returned an array"); return; } PHALCON_CALL_METHOD(&columns, model, "getcolumns"); if (Z_TYPE_P(columns) == IS_ARRAY) { phalcon_is_iterable(columns, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(column_name); if (!phalcon_array_isset(ordered_column_map, column_name)) { phalcon_array_update_zval(&ordered_column_map, column_name, column_name, PH_COPY); } zend_hash_move_forward_ex(ah0, &hp0); } } PHALCON_INIT_VAR(reversed_column_map); array_init(reversed_column_map); phalcon_is_iterable(ordered_column_map, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HKEY(name, ah1, hp1); PHALCON_GET_HVALUE(user_name); phalcon_array_update_zval(&reversed_column_map, user_name, name, PH_COPY); zend_hash_move_forward_ex(ah1, &hp1); } } else { PHALCON_INIT_NVAR(ordered_column_map); PHALCON_INIT_VAR(reversed_column_map); } /** * Store the column map */ array_init_size(return_value, 2); phalcon_array_update_long(&return_value, 0, ordered_column_map, PH_COPY); phalcon_array_update_long(&return_value, 1, reversed_column_map, PH_COPY); PHALCON_MM_RESTORE(); }
/** * Returns an array of Phalcon\Db\Column objects describing a table * * <code> * print_r($connection->describeColumns("posts")); ?> * </code> * * @param string $table * @param string $schema * @return Phalcon\Db\Column[] */ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){ zval *table, *schema = NULL, *columns, *dialect, *size_pattern; zval *sql, *fetch_assoc, *describe, *old_column = NULL; zval *field = NULL, *definition = NULL, *column_type = NULL, *pos = NULL, *attribute = NULL; zval *matches = NULL, *match_one = NULL, *column_name = NULL, *column = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &table, &schema) == FAILURE) { RETURN_MM_NULL(); } if (!schema) { PHALCON_INIT_VAR(schema); } PHALCON_INIT_VAR(columns); array_init(columns); PHALCON_OBS_VAR(dialect); phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); PHALCON_INIT_VAR(size_pattern); ZVAL_STRING(size_pattern, "#\\(([0-9]+)(,[0-9]+)*\\)#", 1); PHALCON_INIT_VAR(sql); PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describecolumns", table, schema); PHALCON_INIT_VAR(fetch_assoc); phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SS("FETCH_ASSOC") TSRMLS_CC); PHALCON_INIT_VAR(describe); PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc); PHALCON_INIT_VAR(old_column); if (!phalcon_is_iterable(describe, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(field); PHALCON_INIT_NVAR(definition); array_init_size(definition, 1); add_assoc_long_ex(definition, SS("bindType"), 2); PHALCON_OBS_NVAR(column_type); phalcon_array_fetch_string(&column_type, field, SL("type"), PH_NOISY_CC); PHALCON_INIT_NVAR(pos); phalcon_fast_stripos_str(pos, column_type, SL("int") TSRMLS_CC); if (PHALCON_IS_NOT_FALSE(pos)) { phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE TSRMLS_CC); PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_string(&attribute, field, SL("pk"), PH_NOISY_CC); if (zend_is_true(attribute)) { phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_SEPARATE TSRMLS_CC); } } else { if (phalcon_memnstr_str(column_type, SL("varchar") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("date") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("decimal") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("char") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("datetime") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("text") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("float") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("enum") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC); } else { phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC); } } } } } } } } } if (phalcon_memnstr_str(column_type, SL("(") TSRMLS_CC)) { PHALCON_INIT_NVAR(matches); array_init(matches); Z_SET_ISREF_P(matches); PHALCON_INIT_NVAR(pos); PHALCON_CALL_FUNC_PARAMS_3(pos, "preg_match", size_pattern, column_type, matches); Z_UNSET_ISREF_P(matches); if (zend_is_true(pos)) { if (phalcon_array_isset_long(matches, 1)) { PHALCON_OBS_NVAR(match_one); phalcon_array_fetch_long(&match_one, matches, 1, PH_NOISY_CC); phalcon_array_update_string(&definition, SL("size"), &match_one, PH_COPY | PH_SEPARATE TSRMLS_CC); } } } if (phalcon_memnstr_str(column_type, SL("unsigned") TSRMLS_CC)) { phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_SEPARATE TSRMLS_CC); } if (!zend_is_true(old_column)) { phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_SEPARATE TSRMLS_CC); } else { phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE TSRMLS_CC); } PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_string(&attribute, field, SL("pk"), PH_NOISY_CC); if (zend_is_true(attribute)) { phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE TSRMLS_CC); } PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_string(&attribute, field, SL("notnull"), PH_NOISY_CC); if (zend_is_true(attribute)) { phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE TSRMLS_CC); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_string(&column_name, field, SL("name"), PH_NOISY_CC); PHALCON_INIT_NVAR(column); object_init_ex(column, phalcon_db_column_ce); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(column, "__construct", column_name, definition); phalcon_array_append(&columns, column, PH_SEPARATE TSRMLS_CC); PHALCON_CPY_WRT(old_column, column_name); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_CTOR(columns); }
/** * The meta-data is obtained by reading the column descriptions from the database information schema * * @param Phalcon\Mvc\ModelInterface $model * @param Phalcon\DiInterface $dependencyInjector * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Introspection, getMetaData){ zval *model, *dependency_injector, *class_name; zval *schema, *table, *read_connection, *exists; zval *complete_table = NULL, *exception_message = NULL; zval *columns, *attributes, *primary_keys, *non_primary_keys; zval *numeric_typed, *not_null, *field_types; zval *field_bind_types, *automatic_default; zval *identity_field = NULL, *column = NULL, *field_name = NULL, *feature = NULL; zval *type = NULL, *bind_type = NULL, *model_metadata; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &model, &dependency_injector); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, model, 0 TSRMLS_CC); PHALCON_INIT_VAR(schema); phalcon_call_method(schema, model, "getschema"); PHALCON_INIT_VAR(table); phalcon_call_method(table, model, "getsource"); /** * Check if the mapped table exists on the database */ PHALCON_INIT_VAR(read_connection); phalcon_call_method(read_connection, model, "getreadconnection"); PHALCON_INIT_VAR(exists); phalcon_call_method_p2(exists, read_connection, "tableexists", table, schema); if (!zend_is_true(exists)) { if (zend_is_true(schema)) { PHALCON_INIT_VAR(complete_table); PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table); } else { PHALCON_CPY_WRT(complete_table, table); } /** * The table not exists */ PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVSV(exception_message, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Try to describe the table */ PHALCON_INIT_VAR(columns); phalcon_call_method_p2(columns, read_connection, "describecolumns", table, schema); if (!phalcon_fast_count_ev(columns TSRMLS_CC)) { if (zend_is_true(schema)) { PHALCON_INIT_NVAR(complete_table); PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table); } else { PHALCON_CPY_WRT(complete_table, table); } /** * The table not exists */ PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVSV(exception_message, "Cannot obtain table columns for the mapped source \"", complete_table, "\" used in model ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Initialize meta-data */ PHALCON_INIT_VAR(attributes); array_init(attributes); PHALCON_INIT_VAR(primary_keys); array_init(primary_keys); PHALCON_INIT_VAR(non_primary_keys); array_init(non_primary_keys); PHALCON_INIT_VAR(numeric_typed); array_init(numeric_typed); PHALCON_INIT_VAR(not_null); array_init(not_null); PHALCON_INIT_VAR(field_types); array_init(field_types); PHALCON_INIT_VAR(field_bind_types); array_init(field_bind_types); PHALCON_INIT_VAR(automatic_default); array_init(automatic_default); PHALCON_INIT_VAR(identity_field); ZVAL_BOOL(identity_field, 0); phalcon_is_iterable(columns, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(column); PHALCON_INIT_NVAR(field_name); phalcon_call_method(field_name, column, "getname"); phalcon_array_append(&attributes, field_name, PH_SEPARATE); /** * To mark fields as primary keys */ PHALCON_INIT_NVAR(feature); phalcon_call_method(feature, column, "isprimary"); if (PHALCON_IS_TRUE(feature)) { phalcon_array_append(&primary_keys, field_name, PH_SEPARATE); } else { phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE); } /** * To mark fields as numeric */ PHALCON_INIT_NVAR(feature); phalcon_call_method(feature, column, "isnumeric"); if (PHALCON_IS_TRUE(feature)) { phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE); } /** * To mark fields as not null */ PHALCON_INIT_NVAR(feature); phalcon_call_method(feature, column, "isnotnull"); if (PHALCON_IS_TRUE(feature)) { phalcon_array_append(¬_null, field_name, PH_SEPARATE); } /** * To mark fields as identity columns */ PHALCON_INIT_NVAR(feature); phalcon_call_method(feature, column, "isautoincrement"); if (PHALCON_IS_TRUE(feature)) { PHALCON_CPY_WRT(identity_field, field_name); } /** * To get the internal types */ PHALCON_INIT_NVAR(type); phalcon_call_method(type, column, "gettype"); phalcon_array_update_zval(&field_types, field_name, &type, PH_COPY | PH_SEPARATE); /** * To mark how the fields must be escaped */ PHALCON_INIT_NVAR(bind_type); phalcon_call_method(bind_type, column, "getbindtype"); phalcon_array_update_zval(&field_bind_types, field_name, &bind_type, PH_COPY | PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); } /** * Create an array using the MODELS_* constants as indexes */ PHALCON_INIT_VAR(model_metadata); array_init(model_metadata); phalcon_array_update_long(&model_metadata, 0, &attributes, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 3, ¬_null, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 4, &field_types, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE); RETURN_CTOR(model_metadata); }
/** * Lists table indexes * * @param string $table * @param string $schema * @return Phalcon\Db\Index[] */ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeIndexes){ zval *table, *schema = NULL, *dialect, *fetch_assoc, *sql; zval *describe, *indexes, *index = NULL, *key_name = NULL, *empty_arr = NULL; zval *sql_index_describe = NULL, *describe_index = NULL; zval *index_column = NULL, *column_name = NULL, *index_objects; zval *index_columns = NULL, *name = NULL; HashTable *ah0, *ah1, *ah2; HashPosition hp0, hp1, hp2; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &table, &schema) == FAILURE) { RETURN_MM_NULL(); } if (!schema) { PHALCON_INIT_VAR(schema); } PHALCON_OBS_VAR(dialect); phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); PHALCON_INIT_VAR(fetch_assoc); phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SS("FETCH_ASSOC") TSRMLS_CC); PHALCON_INIT_VAR(sql); PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describeindexes", table, schema); PHALCON_INIT_VAR(describe); PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc); PHALCON_INIT_VAR(indexes); array_init(indexes); if (!phalcon_is_iterable(describe, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(index); PHALCON_OBS_NVAR(key_name); phalcon_array_fetch_string(&key_name, index, SL("name"), PH_NOISY_CC); 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 TSRMLS_CC); } PHALCON_INIT_NVAR(sql_index_describe); PHALCON_CALL_METHOD_PARAMS_1(sql_index_describe, dialect, "describeindex", key_name); PHALCON_INIT_NVAR(describe_index); PHALCON_CALL_METHOD_PARAMS_2(describe_index, this_ptr, "fetchall", sql_index_describe, fetch_assoc); if (!phalcon_is_iterable(describe_index, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(index_column); PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_string(&column_name, index_column, SL("name"), PH_NOISY_CC); phalcon_array_update_append_multi_2(&indexes, key_name, column_name, 0 TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); } zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(index_objects); array_init(index_objects); if (!phalcon_is_iterable(indexes, &ah2, &hp2, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { PHALCON_GET_FOREACH_KEY(name, ah2, hp2); PHALCON_GET_FOREACH_VALUE(index_columns); PHALCON_INIT_NVAR(index); object_init_ex(index, phalcon_db_index_ce); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(index, "__construct", name, index_columns); phalcon_array_update_zval(&index_objects, name, &index, PH_COPY | PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah2, &hp2); } RETURN_CTOR(index_objects); }
/** * Stores cached content into the file backend and stops the frontend * * @param int|string $keyName * @param string $content * @param long $lifetime * @param boolean $stopBuffer */ PHP_METHOD(Phalcon_Cache_Backend_File, save){ zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL; zval *last_key = NULL, *prefix, *frontend, *options, *cache_dir; zval *cache_file, *cached_content = NULL, *prepared_content; zval *status, *is_buffering; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 4, &key_name, &content, &lifetime, &stop_buffer); if (!key_name) { PHALCON_INIT_VAR(key_name); } if (!content) { PHALCON_INIT_VAR(content); } if (!lifetime) { PHALCON_INIT_VAR(lifetime); } if (!stop_buffer) { PHALCON_INIT_VAR(stop_buffer); ZVAL_BOOL(stop_buffer, 1); } if (Z_TYPE_P(key_name) == IS_NULL) { PHALCON_OBS_VAR(last_key); phalcon_read_property_this(&last_key, this_ptr, SL("_lastKey"), PH_NOISY_CC); } else { PHALCON_OBS_VAR(prefix); phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC); PHALCON_INIT_NVAR(last_key); PHALCON_CONCAT_VV(last_key, prefix, key_name); } if (!zend_is_true(last_key)) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first"); return; } PHALCON_OBS_VAR(frontend); phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC); PHALCON_OBS_VAR(options); phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC); PHALCON_OBS_VAR(cache_dir); phalcon_array_fetch_string(&cache_dir, options, SL("cacheDir"), PH_NOISY); PHALCON_INIT_VAR(cache_file); PHALCON_CONCAT_VV(cache_file, cache_dir, last_key); if (!zend_is_true(content)) { PHALCON_INIT_VAR(cached_content); phalcon_call_method(cached_content, frontend, "getcontent"); } else { PHALCON_CPY_WRT(cached_content, content); } PHALCON_INIT_VAR(prepared_content); phalcon_call_method_p1(prepared_content, frontend, "beforestore", cached_content); /** * We use file_put_contents to respect open-base-dir directive */ PHALCON_INIT_VAR(status); phalcon_file_put_contents(status, cache_file, prepared_content TSRMLS_CC); if (PHALCON_IS_FALSE(status)) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cache directory can't be written"); return; } PHALCON_INIT_VAR(is_buffering); phalcon_call_method(is_buffering, frontend, "isbuffering"); if (PHALCON_IS_TRUE(stop_buffer)) { phalcon_call_method_noret(frontend, "stop"); } if (PHALCON_IS_TRUE(is_buffering)) { zend_print_zval(cached_content, 0); } phalcon_update_property_bool(this_ptr, SL("_started"), 0 TSRMLS_CC); PHALCON_MM_RESTORE(); }
/** * Lists table references * * @param string $table * @param string $schema * @return Phalcon\Db\Reference[] */ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeReferences){ zval *table, *schema = NULL, *dialect, *sql, *fetch_assoc; zval *describe, *reference_objects, *reference_describe = NULL; zval *number = NULL, *constraint_name = NULL, *referenced_table = NULL; zval *from = NULL, *to = NULL, *columns = NULL, *referenced_columns = NULL; zval *reference_array = NULL, *reference = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &table, &schema) == FAILURE) { RETURN_MM_NULL(); } if (!schema) { PHALCON_INIT_VAR(schema); } PHALCON_OBS_VAR(dialect); phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); PHALCON_INIT_VAR(sql); PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describereferences", table, schema); PHALCON_INIT_VAR(fetch_assoc); phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SS("FETCH_ASSOC") TSRMLS_CC); PHALCON_INIT_VAR(describe); PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc); PHALCON_INIT_VAR(reference_objects); array_init(reference_objects); if (!phalcon_is_iterable(describe, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(number, ah0, hp0); PHALCON_GET_FOREACH_VALUE(reference_describe); PHALCON_INIT_NVAR(constraint_name); PHALCON_CONCAT_SV(constraint_name, "foreign_key_", number); PHALCON_OBS_NVAR(referenced_table); phalcon_array_fetch_string(&referenced_table, reference_describe, SL("table"), PH_NOISY_CC); PHALCON_OBS_NVAR(from); phalcon_array_fetch_string(&from, reference_describe, SL("from"), PH_NOISY_CC); PHALCON_OBS_NVAR(to); phalcon_array_fetch_string(&to, reference_describe, SL("to"), PH_NOISY_CC); PHALCON_INIT_NVAR(columns); array_init_size(columns, 1); phalcon_array_append(&columns, from, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(referenced_columns); array_init_size(referenced_columns, 1); phalcon_array_append(&referenced_columns, to, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(reference_array); array_init_size(reference_array, 4); add_assoc_null_ex(reference_array, SS("referencedSchema")); phalcon_array_update_string(&reference_array, SL("referencedTable"), &referenced_table, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&reference_array, SL("columns"), &columns, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&reference_array, SL("referencedColumns"), &referenced_columns, PH_COPY | PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(reference); object_init_ex(reference, phalcon_db_reference_ce); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(reference, "__construct", constraint_name, reference_array); phalcon_array_update_zval(&reference_objects, constraint_name, &reference, PH_COPY | PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_CTOR(reference_objects); }
/** * The meta-data is obtained by reading the column descriptions from the database information schema * * @param Phalcon\Mvc\ModelInterface $model * @param Phalcon\DiInterface $dependencyInjector * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getMetaData){ zval *model, *dependency_injector, *service; zval *annotations = NULL, *class_name, *reflection = NULL; zval *exception_message = NULL, *properties_annotations = NULL; zval *attributes, *primary_keys, *non_primary_keys; zval *numeric_typed, *not_null, *field_types, *field_sizes, *field_bytes, *field_scales; zval *field_bind_types, *automatic_create_attributes, *automatic_update_attributes; zval *identity_field = NULL, *column_annot_name; zval *primary_annot_name, *id_annot_name; zval *column_map_name, *column_type_name, *column_size_name, *column_bytes_name, *column_scale_name, *column_nullable_name; zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL; zval *column_annotation = NULL, *real_property = NULL, *feature = NULL; zval *field_default_values, *column_default_value = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &model, &dependency_injector); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid"); return; } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "annotations", 1); PHALCON_CALL_METHOD(&annotations, dependency_injector, "get", service); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, model, 0 TSRMLS_CC); PHALCON_CALL_METHOD(&reflection, annotations, "get", class_name); if (Z_TYPE_P(reflection) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Get the properties defined in */ PHALCON_CALL_METHOD(&properties_annotations, reflection, "getpropertiesannotations"); if (!zend_is_true(properties_annotations) || !phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Initialize meta-data */ PHALCON_INIT_VAR(attributes); array_init(attributes); PHALCON_INIT_VAR(primary_keys); array_init(primary_keys); PHALCON_INIT_VAR(non_primary_keys); array_init(non_primary_keys); PHALCON_INIT_VAR(numeric_typed); array_init(numeric_typed); PHALCON_INIT_VAR(not_null); array_init(not_null); PHALCON_INIT_VAR(field_types); array_init(field_types); PHALCON_INIT_VAR(field_sizes); array_init(field_sizes); PHALCON_INIT_VAR(field_bytes); array_init(field_bytes); PHALCON_INIT_VAR(field_scales); array_init(field_scales); PHALCON_INIT_VAR(field_bind_types); array_init(field_bind_types); PHALCON_INIT_VAR(automatic_create_attributes); array_init(automatic_create_attributes); PHALCON_INIT_VAR(automatic_update_attributes); array_init(automatic_update_attributes); PHALCON_INIT_VAR(identity_field); ZVAL_FALSE(identity_field); PHALCON_INIT_VAR(field_default_values); array_init(field_default_values); PHALCON_INIT_VAR(column_annot_name); ZVAL_STRING(column_annot_name, "Column", 1); PHALCON_INIT_VAR(primary_annot_name); ZVAL_STRING(primary_annot_name, "Primary", 1); PHALCON_INIT_VAR(id_annot_name); ZVAL_STRING(id_annot_name, "Identity", 1); PHALCON_INIT_VAR(column_map_name); ZVAL_STRING(column_map_name, "column", 1); PHALCON_INIT_VAR(column_type_name); ZVAL_STRING(column_type_name, "type", 1); PHALCON_INIT_VAR(column_size_name); ZVAL_STRING(column_size_name, "size", 1); PHALCON_INIT_VAR(column_bytes_name); ZVAL_STRING(column_bytes_name, "bytes", 1); PHALCON_INIT_VAR(column_scale_name); ZVAL_STRING(column_scale_name, "scale", 1); PHALCON_INIT_VAR(column_default_value); ZVAL_STRING(column_default_value, "default", 1); PHALCON_INIT_VAR(column_nullable_name); ZVAL_STRING(column_nullable_name, "nullable", 1); phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(property, ah0, hp0); PHALCON_GET_HVALUE(prop_annotations); /** * All columns marked with the 'Column' annotation are considered columns */ PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", column_annot_name); if (!zend_is_true(has_annotation)) { zend_hash_move_forward_ex(ah0, &hp0); continue; } /** * Fetch the 'column' annotation */ PHALCON_CALL_METHOD(&column_annotation, prop_annotations, "get", column_annot_name); /** * Check column map */ PHALCON_CALL_METHOD(&real_property, column_annotation, "getargument", column_map_name); if (PHALCON_IS_EMPTY(real_property)) { PHALCON_CPY_WRT(real_property, property); } /** * Check if annotation has the 'type' named parameter */ PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_type_name); if (PHALCON_IS_STRING(feature, "integer")) { phalcon_array_update_zval_long(&field_types, real_property, 0, PH_COPY); phalcon_array_update_zval_long(&field_bind_types, real_property, 1, PH_COPY); phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY); } else if (PHALCON_IS_STRING(feature, "decimal")) { phalcon_array_update_zval_long(&field_types, real_property, 3, PH_COPY); phalcon_array_update_zval_long(&field_bind_types, real_property, 32, PH_COPY); phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY); } else if (PHALCON_IS_STRING(feature, "boolean")) { phalcon_array_update_zval_long(&field_types, real_property, 8, PH_COPY); phalcon_array_update_zval_long(&field_bind_types, real_property, 5, PH_COPY); } else { if (PHALCON_IS_STRING(feature, "date")) { phalcon_array_update_zval_long(&field_types, real_property, 1, PH_COPY); } else { /** * By default all columns are varchar/string */ phalcon_array_update_zval_long(&field_types, real_property, 2, PH_COPY); } phalcon_array_update_zval_long(&field_bind_types, real_property, 2, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_size_name); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_sizes, real_property, feature, PH_COPY); phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_bytes_name); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_scale_name); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_scales, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_default_values, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY); } /** * All columns marked with the 'Primary' annotation are considered primary keys */ PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", primary_annot_name); if (zend_is_true(has_annotation)) { phalcon_array_append(&primary_keys, real_property, PH_COPY); } else { phalcon_array_append(&non_primary_keys, real_property, PH_COPY); } /** * All columns marked with the 'Primary' annotation are considered primary keys */ PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", id_annot_name); if (zend_is_true(has_annotation)) { PHALCON_CPY_WRT(identity_field, real_property); } /** * Check if the column */ PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_nullable_name); if (!zend_is_true(feature)) { phalcon_array_append(¬_null, real_property, PH_COPY); } phalcon_array_append(&attributes, real_property, PH_COPY); zend_hash_move_forward_ex(ah0, &hp0); } /** * Create an array using the MODELS_* constants as indexes */ array_init_size(return_value, 14); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_ATTRIBUTES, attributes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_PRIMARY_KEY, primary_keys, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NON_PRIMARY_KEY, non_primary_keys, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NOT_NULL, not_null, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES, field_types, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_NUMERIC, numeric_typed, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_IDENTITY_COLUMN, identity_field, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_BIND, field_bind_types, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_INSERT, automatic_create_attributes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_UPDATE, automatic_update_attributes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_DEFAULT_VALUE, field_default_values, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SZIE, field_sizes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SCALE, field_scales, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_BYTE, field_bytes, PH_COPY); PHALCON_MM_RESTORE(); }
/** * Stores cached content into the XCache backend and stops the frontend * * @param string $keyName * @param string $content * @param long $lifetime * @param boolean $stopBuffer */ PHP_METHOD(Phalcon_Cache_Backend_Xcache, save){ zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL; zval *last_key = NULL, *prefix, *frontend, *cached_content = NULL; zval *prepared_content, *ttl = NULL, *success, *is_buffering; zval *options, *special_key, *keys = NULL, *zero; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 4, &key_name, &content, &lifetime, &stop_buffer); if (!key_name) { PHALCON_INIT_VAR(key_name); } if (!content) { PHALCON_INIT_VAR(content); } if (!lifetime) { PHALCON_INIT_VAR(lifetime); } if (!stop_buffer) { PHALCON_INIT_VAR(stop_buffer); ZVAL_BOOL(stop_buffer, 1); } if (Z_TYPE_P(key_name) == IS_NULL) { PHALCON_OBS_VAR(last_key); phalcon_read_property_this(&last_key, this_ptr, SL("_lastKey"), PH_NOISY_CC); } else { PHALCON_OBS_VAR(prefix); phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC); PHALCON_INIT_NVAR(last_key); PHALCON_CONCAT_SVV(last_key, "_PHCX", prefix, key_name); } if (!zend_is_true(last_key)) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first"); return; } PHALCON_OBS_VAR(frontend); phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC); if (Z_TYPE_P(content) == IS_NULL) { PHALCON_INIT_VAR(cached_content); phalcon_call_method(cached_content, frontend, "getcontent"); } else { PHALCON_CPY_WRT(cached_content, content); } PHALCON_INIT_VAR(prepared_content); phalcon_call_method_p1(prepared_content, frontend, "beforestore", cached_content); /** * Take the lifetime from the frontend or read it from the set in start() */ if (Z_TYPE_P(lifetime) == IS_NULL) { PHALCON_OBS_NVAR(ttl); phalcon_read_property_this(&ttl, this_ptr, SL("_lastLifetime"), PH_NOISY_CC); if (Z_TYPE_P(ttl) == IS_NULL) { PHALCON_INIT_NVAR(ttl); phalcon_call_method(ttl, frontend, "getlifetime"); } } else { PHALCON_CPY_WRT(ttl, lifetime); } /** * Call xcache_set in the PHP userland since most of the time it isn't available at * compile time */ PHALCON_INIT_VAR(success); phalcon_call_func_p3(success, "xcache_set", last_key, prepared_content, ttl); PHALCON_INIT_VAR(is_buffering); phalcon_call_method(is_buffering, frontend, "isbuffering"); if (PHALCON_IS_TRUE(stop_buffer)) { phalcon_call_method_noret(frontend, "stop"); } if (PHALCON_IS_TRUE(is_buffering)) { zend_print_zval(cached_content, 0); } phalcon_update_property_bool(this_ptr, SL("_started"), 0 TSRMLS_CC); /** * xcache_set() could fail because of Out of Memory condition. I don't think it is * appropriate to throw an exception here (like * Phalcon\Cache\Backend\Memcache::save() does): first, to be consistent with * Phalcon\Cache\Backend\Apc::save(), second, because xCache is usually given much * less RAM than memcached */ if (zend_is_true(success)) { PHALCON_OBS_VAR(options); phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC); PHALCON_OBS_VAR(special_key); phalcon_array_fetch_string(&special_key, options, SL("statsKey"), PH_NOISY); /** * xcache_list() is available only to the administrator (unless XCache was * patched). We have to update the list of the stored keys. */ PHALCON_INIT_VAR(keys); phalcon_call_func_p1(keys, "xcache_get", special_key); if (Z_TYPE_P(keys) != IS_ARRAY) { PHALCON_INIT_NVAR(keys); array_init(keys); } phalcon_array_update_zval(&keys, last_key, &ttl, PH_COPY | PH_SEPARATE); PHALCON_INIT_VAR(zero); ZVAL_LONG(zero, 0); phalcon_call_func_p3_noret("xcache_set", special_key, keys, zero); } PHALCON_MM_RESTORE(); }