/** * Updates data on a table using custom RBDM SQL syntax * * * * @param string $table * @param array $fields * @param array $values * @param string $whereCondition * @param boolean $automaticQuotes * @return boolean */ PHP_METHOD(Phalcon_Db, update){ zval *table = NULL, *fields = NULL, *values = NULL, *where_condition = NULL; zval *automatic_quotes = NULL, *i = NULL, *update_values = NULL, *update_sql = NULL; zval *field = NULL; zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL; zval *r7 = NULL, *r8 = NULL, *r9 = NULL, *r10 = NULL, *r11 = NULL, *r12 = NULL, *r13 = NULL; zval *r14 = NULL, *r15 = NULL, *r16 = NULL, *r17 = NULL, *r18 = NULL; zval *a0 = NULL; zval *c0 = NULL; zval *t0 = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz|zz", &table, &fields, &values, &where_condition, &automatic_quotes) == FAILURE) { PHALCON_MM_RESTORE(); RETURN_NULL(); } PHALCON_SEPARATE_PARAM(values); if (!where_condition) { PHALCON_INIT_VAR(where_condition); ZVAL_NULL(where_condition); } if (!automatic_quotes) { PHALCON_INIT_VAR(automatic_quotes); ZVAL_BOOL(automatic_quotes, 0); } PHALCON_ALLOC_ZVAL_MM(r0); phalcon_fast_count(r0, fields TSRMLS_CC); PHALCON_ALLOC_ZVAL_MM(r1); phalcon_fast_count(r1, values TSRMLS_CC); PHALCON_INIT_VAR(r2); is_not_equal_function(r2, r0, r1 TSRMLS_CC); if (zend_is_true(r2)) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The number of values to update is not the same as fields"); return; } PHALCON_INIT_VAR(i); ZVAL_LONG(i, 0); PHALCON_INIT_VAR(a0); array_init(a0); PHALCON_CPY_WRT(update_values, a0); PHALCON_ALLOC_ZVAL_MM(r3); PHALCON_CONCAT_SVS(r3, "UPDATE ", table, " SET "); PHALCON_CPY_WRT(update_sql, r3); if (phalcon_valid_foreach(fields TSRMLS_CC)) { ah0 = Z_ARRVAL_P(fields); zend_hash_internal_pointer_reset_ex(ah0, &hp0); fes_e7f0_2: if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){ goto fee_e7f0_2; } PHALCON_INIT_VAR(field); ZVAL_ZVAL(field, *hd, 1, 0); if (zend_is_true(automatic_quotes)) { PHALCON_INIT_VAR(r4); phalcon_array_fetch(&r4, values, i, PHALCON_NOISY TSRMLS_CC); if (Z_TYPE_P(r4) == IS_OBJECT) { PHALCON_INIT_VAR(r5); phalcon_array_fetch(&r5, values, i, PHALCON_NOISY TSRMLS_CC); PHALCON_INIT_VAR(r6); phalcon_instance_of(r6, r5, phalcon_db_rawvalue_ce TSRMLS_CC); if (zend_is_true(r6)) { PHALCON_INIT_VAR(r7); PHALCON_INIT_VAR(r8); PHALCON_INIT_VAR(r9); phalcon_array_fetch(&r9, values, i, PHALCON_NOISY TSRMLS_CC); PHALCON_CALL_METHOD(r8, r9, "getvalue", PHALCON_NO_CHECK); PHALCON_CALL_METHOD_PARAMS_1(r7, this_ptr, "escapestring", r8, PHALCON_NO_CHECK); phalcon_array_update(&values, i, &r7, PHALCON_SEPARATE_PLZ, PHALCON_COPY, PHALCON_NO_CTOR TSRMLS_CC); goto fes_e7f0_2; } } PHALCON_INIT_VAR(r10); PHALCON_INIT_VAR(r11); PHALCON_INIT_VAR(r12); phalcon_array_fetch(&r12, values, i, PHALCON_NOISY TSRMLS_CC); PHALCON_CALL_METHOD_PARAMS_1(r11, this_ptr, "escapestring", r12, PHALCON_NO_CHECK); PHALCON_CONCAT_SVS(r10, "'", r11, "'"); phalcon_array_update(&values, i, &r10, PHALCON_SEPARATE_PLZ, PHALCON_COPY, PHALCON_NO_CTOR TSRMLS_CC); } PHALCON_INIT_VAR(r13); PHALCON_INIT_VAR(r14); phalcon_array_fetch(&r14, values, i, PHALCON_NOISY TSRMLS_CC); PHALCON_CONCAT_VSV(r13, field, " = ", r14); phalcon_array_append(&update_values, r13, PHALCON_SEPARATE_PLZ TSRMLS_CC); PHALCON_SEPARATE(i); increment_function(i); zend_hash_move_forward_ex(ah0, &hp0); goto fes_e7f0_2; fee_e7f0_2: if(0){} } else { return; } PHALCON_INIT_VAR(c0); ZVAL_STRING(c0, ", ", 1); PHALCON_ALLOC_ZVAL_MM(r15); phalcon_fast_join(r15, c0, update_values TSRMLS_CC); phalcon_concat_self(&update_sql, r15 TSRMLS_CC); PHALCON_INIT_VAR(t0); ZVAL_NULL(t0); PHALCON_INIT_VAR(r16); is_not_equal_function(r16, where_condition, t0 TSRMLS_CC); if (zend_is_true(r16)) { PHALCON_ALLOC_ZVAL_MM(r17); PHALCON_CONCAT_SV(r17, " WHERE ", where_condition); phalcon_concat_self(&update_sql, r17 TSRMLS_CC); } PHALCON_ALLOC_ZVAL_MM(r18); PHALCON_CALL_METHOD_PARAMS_1(r18, this_ptr, "query", update_sql, PHALCON_NO_CHECK); PHALCON_RETURN_DZVAL(r18); }
/** * Handles a MVC request * * @param string $uri * @return Phalcon\Http\ResponseInterface */ PHP_METHOD(Phalcon_Mvc_Application, handle){ zval *uri = NULL, *dependency_injector, *events_manager; zval *event_name = NULL, *status = NULL, *service = NULL, *router, *module_name = NULL; zval *module_object = NULL, *modules, *exception_msg = NULL; zval *module, *class_name = NULL, *path, *module_params; zval *implicit_view, *view, *namespace_name; zval *controller_name = NULL, *action_name = NULL, *params = NULL; zval *dispatcher, *controller, *returned_response = NULL; zval *possible_response, *render_status = NULL, *response = NULL; zval *content; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &uri); if (!uri) { PHALCON_INIT_VAR(uri); } 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_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "A dependency injection object is required to access internal services"); return; } PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); /** * Call boot event, this allow the developer to perform initialization actions */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "application:boot", 1); PHALCON_INIT_VAR(status); phalcon_call_method_p2(status, events_manager, "fire", event_name, this_ptr); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "router", 1); PHALCON_INIT_VAR(router); phalcon_call_method_p1(router, dependency_injector, "getshared", service); /** * Handle the URI pattern (if any) */ phalcon_call_method_p1_noret(router, "handle", uri); /** * Load module config */ PHALCON_INIT_VAR(module_name); 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)) { PHALCON_OBS_NVAR(module_name); phalcon_read_property_this(&module_name, this_ptr, SL("_defaultModule"), PH_NOISY_CC); } PHALCON_INIT_VAR(module_object); /** * Process the module definition */ if (zend_is_true(module_name)) { if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "application:beforeStartModule", 1); PHALCON_INIT_NVAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } /** * Check if the module passed by the router is registered in the modules container */ PHALCON_OBS_VAR(modules); phalcon_read_property_this(&modules, this_ptr, SL("_modules"), PH_NOISY_CC); if (!phalcon_array_isset(modules, module_name)) { PHALCON_INIT_VAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module '", module_name, "' isn't registered in the application container"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_application_exception_ce, exception_msg); return; } /** * A module definition must ne an array or an object */ PHALCON_OBS_VAR(module); phalcon_array_fetch(&module, modules, module_name, PH_NOISY); if (Z_TYPE_P(module) != IS_ARRAY) { if (Z_TYPE_P(module) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_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_string(module, SS("className"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, module, SL("className"), PH_NOISY); } else { PHALCON_INIT_NVAR(class_name); ZVAL_STRING(class_name, "Module", 1); } /** * If developer specify a path try to include the file */ if (phalcon_array_isset_string(module, SS("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_string(&path, module, SL("path"), PH_NOISY); if (!phalcon_class_exists(class_name, 0 TSRMLS_CC)) { if (phalcon_file_exists(path TSRMLS_CC) == SUCCESS) { if (phalcon_require(path TSRMLS_CC) == FAILURE) { return; } } else { PHALCON_INIT_NVAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module definition path '", path, "' doesn't exist"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_application_exception_ce, exception_msg); return; } } } phalcon_call_method_p1(module_object, dependency_injector, "get", class_name); /** * 'registerAutoloaders' and 'registerServices' are automatically called */ phalcon_call_method_p1_noret(module_object, "registerautoloaders", dependency_injector); phalcon_call_method_p1_noret(module_object, "registerservices", dependency_injector); } else { /** * A module definition object, can be a Closure instance */ if (phalcon_is_instance_of(module, SL("Closure") TSRMLS_CC)) { PHALCON_INIT_VAR(module_params); array_init_size(module_params, 1); phalcon_array_append(&module_params, dependency_injector, PH_SEPARATE); PHALCON_INIT_NVAR(status); PHALCON_CALL_USER_FUNC_ARRAY(status, module, module_params); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "Invalid module definition"); return; } } /** * Calling afterStartModule event */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_this(this_ptr, SL("_moduleObject"), module_object TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "application:afterStartModule", 1); PHALCON_INIT_NVAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } } /** * Check whether use implicit views or not */ PHALCON_OBS_VAR(implicit_view); phalcon_read_property_this(&implicit_view, this_ptr, SL("_implicitView"), PH_NOISY_CC); if (PHALCON_IS_TRUE(implicit_view)) { PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "view", 1); PHALCON_INIT_VAR(view); phalcon_call_method_p1(view, dependency_injector, "getshared", service); } /** * We get the parameters from the router and assign them to the dispatcher */ PHALCON_INIT_NVAR(module_name); phalcon_call_method(module_name, router, "getmodulename"); PHALCON_INIT_VAR(namespace_name); phalcon_call_method(namespace_name, router, "getnamespacename"); PHALCON_INIT_VAR(controller_name); phalcon_call_method(controller_name, router, "getcontrollername"); PHALCON_INIT_VAR(action_name); phalcon_call_method(action_name, router, "getactionname"); PHALCON_INIT_VAR(params); phalcon_call_method(params, router, "getparams"); PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "dispatcher", 1); PHALCON_INIT_VAR(dispatcher); phalcon_call_method_p1(dispatcher, dependency_injector, "getshared", service); /** * Assign the values passed from the router */ phalcon_call_method_p1_noret(dispatcher, "setmodulename", module_name); phalcon_call_method_p1_noret(dispatcher, "setnamespacename", namespace_name); phalcon_call_method_p1_noret(dispatcher, "setcontrollername", controller_name); phalcon_call_method_p1_noret(dispatcher, "setactionname", action_name); phalcon_call_method_p1_noret(dispatcher, "setparams", params); /** * Start the view component (start output buffering) */ if (PHALCON_IS_TRUE(implicit_view)) { phalcon_call_method_noret(view, "start"); } /** * Calling beforeHandleRequest */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "application:beforeHandleRequest", 1); PHALCON_INIT_NVAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, dispatcher); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } /** * The dispatcher must return an object */ PHALCON_INIT_VAR(controller); phalcon_call_method(controller, dispatcher, "dispatch"); PHALCON_INIT_VAR(returned_response); ZVAL_BOOL(returned_response, 0); /** * Get the latest value returned by an action */ PHALCON_INIT_VAR(possible_response); phalcon_call_method(possible_response, dispatcher, "getreturnedvalue"); if (Z_TYPE_P(possible_response) == IS_OBJECT) { /** * Check if the returned object is already a response */ phalcon_instance_of(returned_response, possible_response, phalcon_http_responseinterface_ce TSRMLS_CC); } /** * Calling afterHandleRequest */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "application:afterHandleRequest", 1); phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, controller); } /** * If the dispatcher returns an object we try to render the view in auto-rendering * mode */ if (PHALCON_IS_FALSE(returned_response)) { if (PHALCON_IS_TRUE(implicit_view)) { if (Z_TYPE_P(controller) == IS_OBJECT) { PHALCON_INIT_VAR(render_status); ZVAL_BOOL(render_status, 1); /** * This allows to make a custom view render */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "application:viewRender", 1); phalcon_call_method_p3(render_status, events_manager, "fire", event_name, this_ptr, view); } /** * Check if the view process has been treated by the developer */ if (PHALCON_IS_NOT_FALSE(render_status)) { PHALCON_INIT_NVAR(controller_name); phalcon_call_method(controller_name, dispatcher, "getcontrollername"); PHALCON_INIT_NVAR(action_name); phalcon_call_method(action_name, dispatcher, "getactionname"); PHALCON_INIT_NVAR(params); phalcon_call_method(params, dispatcher, "getparams"); /** * Automatic render based on the latest controller executed */ phalcon_call_method_p3_noret(view, "render", controller_name, action_name, params); } } } } /** * Finish the view component (stop output buffering) */ if (PHALCON_IS_TRUE(implicit_view)) { phalcon_call_method_noret(view, "finish"); } if (PHALCON_IS_FALSE(returned_response)) { PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "response", 1); PHALCON_INIT_VAR(response); phalcon_call_method_p1(response, dependency_injector, "getshared", service); if (PHALCON_IS_TRUE(implicit_view)) { /** * The content returned by the view is passed to the response service */ PHALCON_INIT_VAR(content); phalcon_call_method(content, view, "getcontent"); phalcon_call_method_p1_noret(response, "setcontent", content); } } else { /** * We don't need to create a response because there is a one already created */ PHALCON_CPY_WRT(response, possible_response); } /** * Calling beforeSendResponse */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "application:beforeSendResponse", 1); phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, response); } /** * Headers are automatically send */ phalcon_call_method_noret(response, "sendheaders"); /** * Cookies are automatically send */ phalcon_call_method_noret(response, "sendcookies"); /** * Return the response */ RETURN_CCTOR(response); }
/** * Inserts data into a table using custom RBDM SQL syntax * * * * @param string $table * @param array $values * @param array $fields * @param boolean $automaticQuotes * @return boolean */ PHP_METHOD(Phalcon_Db, insert){ zval *table = NULL, *values = NULL, *fields = NULL, *automatic_quotes = NULL; zval *value = NULL, *key = NULL, *joined_values = NULL, *insert_sql = NULL; zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL; zval *r7 = NULL, *r8 = NULL, *r9 = NULL, *r10 = NULL; zval *i0 = NULL; zval *c0 = NULL, *c1 = NULL; HashTable *ah0; HashPosition hp0; zval **hd; char *hash_index; uint hash_index_len; ulong hash_num; int hash_type; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|zz", &table, &values, &fields, &automatic_quotes) == FAILURE) { PHALCON_MM_RESTORE(); RETURN_NULL(); } PHALCON_SEPARATE_PARAM(values); if (!fields) { PHALCON_INIT_VAR(fields); ZVAL_NULL(fields); } if (!automatic_quotes) { PHALCON_INIT_VAR(automatic_quotes); ZVAL_BOOL(automatic_quotes, 0); } if (Z_TYPE_P(values) == IS_ARRAY) { PHALCON_ALLOC_ZVAL_MM(r0); phalcon_fast_count(r0, values TSRMLS_CC); if (!zend_is_true(r0)) { PHALCON_ALLOC_ZVAL_MM(i0); object_init_ex(i0, phalcon_db_exception_ce); PHALCON_ALLOC_ZVAL_MM(r1); PHALCON_CONCAT_SVS(r1, "Unable to insert into ", table, " without data"); PHALCON_CALL_METHOD_PARAMS_1_NORETURN(i0, "__construct", r1, PHALCON_CHECK); phalcon_throw_exception(i0 TSRMLS_CC); return; } else { if (zend_is_true(automatic_quotes)) { if (phalcon_valid_foreach(values TSRMLS_CC)) { ALLOC_HASHTABLE(ah0); zend_hash_init(ah0, 0, NULL, NULL, 0); zend_hash_copy(ah0, Z_ARRVAL_P(values), NULL, NULL, sizeof(zval*)); zend_hash_internal_pointer_reset_ex(ah0, &hp0); fes_e7f0_1: if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){ goto fee_e7f0_1; } else { PHALCON_INIT_VAR(key); PHALCON_GET_FOREACH_KEY(key, ah0, hp0); } PHALCON_INIT_VAR(value); ZVAL_ZVAL(value, *hd, 1, 0); if (Z_TYPE_P(value) == IS_OBJECT) { PHALCON_INIT_VAR(r2); phalcon_instance_of(r2, value, phalcon_db_rawvalue_ce TSRMLS_CC); if (zend_is_true(r2)) { PHALCON_INIT_VAR(r3); PHALCON_CALL_METHOD_PARAMS_1(r3, this_ptr, "escapestring", value, PHALCON_NO_CHECK); phalcon_array_update(&values, key, &r3, PHALCON_SEPARATE_PLZ, PHALCON_COPY, PHALCON_NO_CTOR TSRMLS_CC); goto fes_e7f0_1; } } PHALCON_INIT_VAR(r4); PHALCON_INIT_VAR(r5); PHALCON_CALL_METHOD_PARAMS_1(r5, this_ptr, "escapestring", value, PHALCON_NO_CHECK); PHALCON_CONCAT_SVS(r4, "'", r5, "'"); phalcon_array_update(&values, key, &r4, PHALCON_SEPARATE_PLZ, PHALCON_COPY, PHALCON_NO_CTOR TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); goto fes_e7f0_1; fee_e7f0_1: zend_hash_destroy(ah0); efree(ah0); } else { return; } } } PHALCON_INIT_VAR(c0); ZVAL_STRING(c0, ", ", 1); PHALCON_ALLOC_ZVAL_MM(r6); phalcon_fast_join(r6, c0, values TSRMLS_CC); PHALCON_CPY_WRT(joined_values, r6); if (Z_TYPE_P(fields) == IS_ARRAY) { PHALCON_ALLOC_ZVAL_MM(r7); PHALCON_INIT_VAR(c1); ZVAL_STRING(c1, ", ", 1); PHALCON_ALLOC_ZVAL_MM(r8); phalcon_fast_join(r8, c1, fields TSRMLS_CC); PHALCON_CONCAT_SVSVSVS(r7, "INSERT INTO ", table, " (", r8, ") VALUES (", joined_values, ")"); PHALCON_CPY_WRT(insert_sql, r7); } else { PHALCON_ALLOC_ZVAL_MM(r9); PHALCON_CONCAT_SVSVS(r9, "INSERT INTO ", table, " VALUES (", joined_values, ")"); PHALCON_CPY_WRT(insert_sql, r9); } PHALCON_ALLOC_ZVAL_MM(r10); PHALCON_CALL_METHOD_PARAMS_1(r10, this_ptr, "query", insert_sql, PHALCON_NO_CHECK); PHALCON_RETURN_DZVAL(r10); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The second parameter for insert isn't an Array"); return; } PHALCON_MM_RESTORE(); }