Skip to content

Commit

Permalink
Improving support for compound primary keys in PostgreSQL #198
Browse files Browse the repository at this point in the history
  • Loading branch information
phalcon committed Nov 22, 2012
1 parent 9299661 commit 5c10b49
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 106 deletions.
220 changes: 174 additions & 46 deletions build/phalcon.c

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions build/phalcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -2471,13 +2471,15 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_db_adapter_fetchone, 0, 0, 1)
ZEND_ARG_INFO(0, sqlQuery)
ZEND_ARG_INFO(0, fetchMode)
ZEND_ARG_INFO(0, placeholders)
ZEND_ARG_INFO(0, bindParams)
ZEND_ARG_INFO(0, bindTypes)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_db_adapter_fetchall, 0, 0, 1)
ZEND_ARG_INFO(0, sqlQuery)
ZEND_ARG_INFO(0, fetchMode)
ZEND_ARG_INFO(0, placeholders)
ZEND_ARG_INFO(0, bindParams)
ZEND_ARG_INFO(0, bindTypes)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_db_adapter_insert, 0, 0, 2)
Expand Down
76 changes: 61 additions & 15 deletions ext/db/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,18 @@ PHP_METHOD(Phalcon_Db_Adapter, getEventsManager){
*
* @param string $sqlQuery
* @param int $fetchMode
* @param array $placeholders
* @param array $bindParams
* @param array $bindTypes
* @return array
*/
PHP_METHOD(Phalcon_Db_Adapter, fetchOne){

zval *sql_query, *fetch_mode = NULL, *placeholders = NULL;
zval *sql_query, *fetch_mode = NULL, *bind_params = NULL, *bind_types = NULL;
zval *result, *row, *empty_row;

PHALCON_MM_GROW();

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zz", &sql_query, &fetch_mode, &placeholders) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zzz", &sql_query, &fetch_mode, &bind_params, &bind_types) == FAILURE) {
PHALCON_MM_RESTORE();
RETURN_NULL();
}
Expand All @@ -191,19 +192,23 @@ PHP_METHOD(Phalcon_Db_Adapter, fetchOne){
ZVAL_LONG(fetch_mode, 2);
}

if (!placeholders) {
PHALCON_INIT_NVAR(placeholders);
if (!bind_params) {
PHALCON_INIT_NVAR(bind_params);
}

if (!bind_types) {
PHALCON_INIT_NVAR(bind_types);
}

PHALCON_INIT_VAR(result);
PHALCON_CALL_METHOD_PARAMS_2(result, this_ptr, "query", sql_query, placeholders, PH_NO_CHECK);
PHALCON_CALL_METHOD_PARAMS_3(result, this_ptr, "query", sql_query, bind_params, bind_types, PH_NO_CHECK);
if (Z_TYPE_P(result) == IS_OBJECT) {
if (Z_TYPE_P(fetch_mode) != IS_NULL) {
PHALCON_CALL_METHOD_PARAMS_1_NORETURN(result, "setfetchmode", fetch_mode, PH_NO_CHECK);
}

PHALCON_INIT_VAR(row);
PHALCON_CALL_METHOD(row, result, "fetcharray", PH_NO_CHECK);
PHALCON_CALL_METHOD(row, result, "fetch", PH_NO_CHECK);

RETURN_CCTOR(row);
}
Expand Down Expand Up @@ -233,18 +238,19 @@ PHP_METHOD(Phalcon_Db_Adapter, fetchOne){
*
* @param string $sqlQuery
* @param int $fetchMode
* @param array $placeholders
* @param array $bindParams
* @param array $bindTypes
* @return array
*/
PHP_METHOD(Phalcon_Db_Adapter, fetchAll){

zval *sql_query, *fetch_mode = NULL, *placeholders = NULL;
zval *sql_query, *fetch_mode = NULL, *bind_params = NULL, *bind_types = NULL;
zval *results, *result, *row = NULL;
zval *r0 = NULL;

PHALCON_MM_GROW();

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zz", &sql_query, &fetch_mode, &placeholders) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zzz", &sql_query, &fetch_mode, &bind_params, &bind_types) == FAILURE) {
PHALCON_MM_RESTORE();
RETURN_NULL();
}
Expand All @@ -254,23 +260,27 @@ PHP_METHOD(Phalcon_Db_Adapter, fetchAll){
ZVAL_LONG(fetch_mode, 2);
}

if (!placeholders) {
PHALCON_INIT_NVAR(placeholders);
if (!bind_params) {
PHALCON_INIT_NVAR(bind_params);
}

if (!bind_types) {
PHALCON_INIT_NVAR(bind_types);
}

PHALCON_INIT_VAR(results);
array_init(results);

PHALCON_INIT_VAR(result);
PHALCON_CALL_METHOD_PARAMS_2(result, this_ptr, "query", sql_query, placeholders, PH_NO_CHECK);
PHALCON_CALL_METHOD_PARAMS_3(result, this_ptr, "query", sql_query, bind_params, bind_types, PH_NO_CHECK);
if (Z_TYPE_P(result) == IS_OBJECT) {
if (Z_TYPE_P(fetch_mode) != IS_NULL) {
PHALCON_CALL_METHOD_PARAMS_1_NORETURN(result, "setfetchmode", fetch_mode, PH_NO_CHECK);
}
ph_cycle_start_0:

PHALCON_INIT_NVAR(r0);
PHALCON_CALL_METHOD(r0, result, "fetcharray", PH_NO_CHECK);
PHALCON_CALL_METHOD(r0, result, "fetch", PH_NO_CHECK);
PHALCON_CPY_WRT(row, r0);
if (!zend_is_true(row)) {
goto ph_cycle_end_0;
Expand Down Expand Up @@ -493,6 +503,7 @@ PHP_METHOD(Phalcon_Db_Adapter, update){
zval *bind_data_types = NULL, *value = NULL, *position = NULL, *field = NULL;
zval *escaped_field = NULL, *set_clause_part = NULL, *bind_type = NULL;
zval *escaped_table, *set_clause, *update_sql = NULL;
zval *conditions, *where_bind, *where_types;
zval *success;
HashTable *ah0;
HashPosition hp0;
Expand Down Expand Up @@ -601,7 +612,42 @@ PHP_METHOD(Phalcon_Db_Adapter, update){
phalcon_fast_join_str(set_clause, SL(", "), placeholders TSRMLS_CC);
if (Z_TYPE_P(where_condition) != IS_NULL) {
PHALCON_INIT_VAR(update_sql);
PHALCON_CONCAT_SVSVSV(update_sql, "UPDATE ", escaped_table, " SET ", set_clause, " WHERE ", where_condition);
PHALCON_CONCAT_SVSVS(update_sql, "UPDATE ", escaped_table, " SET ", set_clause, " WHERE ");

/**
* String conditions are simply appended to the SQL
*/
if (Z_TYPE_P(where_condition) == IS_STRING) {
phalcon_concat_self(update_sql, where_condition TSRMLS_CC);
} else {
/**
* Array conditions may have bound params and bound types
*/
if (Z_TYPE_P(where_condition) == IS_ARRAY) {
eval_int = phalcon_array_isset_string(where_condition, SS("conditions"));
if (eval_int) {
PHALCON_INIT_VAR(conditions);
phalcon_array_fetch_string(&conditions, where_condition, SL("conditions"), PH_NOISY_CC);
phalcon_concat_self(update_sql, conditions TSRMLS_CC);
}
eval_int = phalcon_array_isset_string(where_condition, SS("bind"));
if (eval_int) {
PHALCON_INIT_VAR(where_bind);
phalcon_array_fetch_string(&where_bind, where_condition, SL("bind"), PH_NOISY_CC);
phalcon_merge_append(update_values, where_bind TSRMLS_CC);
}

eval_int = phalcon_array_isset_string(where_condition, SS("bindTypes"));
if (eval_int) {
PHALCON_INIT_VAR(where_types);
phalcon_array_fetch_string(&where_types, where_condition, SL("bindTypes"), PH_NOISY_CC);
phalcon_merge_append(bind_data_types, where_types TSRMLS_CC);
}
} else {
PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid WHERE clause conditions");
return;
}
}
} else {
PHALCON_INIT_NVAR(update_sql);
PHALCON_CONCAT_SVSV(update_sql, "UPDATE ", escaped_table, " SET ", set_clause);
Expand Down
6 changes: 4 additions & 2 deletions ext/db/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_db_adapter_fetchone, 0, 0, 1)
ZEND_ARG_INFO(0, sqlQuery)
ZEND_ARG_INFO(0, fetchMode)
ZEND_ARG_INFO(0, placeholders)
ZEND_ARG_INFO(0, bindParams)
ZEND_ARG_INFO(0, bindTypes)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_db_adapter_fetchall, 0, 0, 1)
ZEND_ARG_INFO(0, sqlQuery)
ZEND_ARG_INFO(0, fetchMode)
ZEND_ARG_INFO(0, placeholders)
ZEND_ARG_INFO(0, bindParams)
ZEND_ARG_INFO(0, bindTypes)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_db_adapter_insert, 0, 0, 2)
Expand Down
33 changes: 33 additions & 0 deletions ext/kernel/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,3 +891,36 @@ void phalcon_array_update_zval_string_string_multi_3(zval **arr, zval *index1, c
zval_ptr_dtor(&temp2);
}
}

/**
* Appends every element of an array at the end of the left array
*/
void phalcon_merge_append(zval *left, zval *values TSRMLS_DC){

zval **tmp;
HashTable *arr_values;
HashPosition pos;

if (Z_TYPE_P(left) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "First parameter of phalcon_merge_append must be an array");
return;
}

if (Z_TYPE_P(values) == IS_ARRAY) {

arr_values = Z_ARRVAL_P(values);
zend_hash_internal_pointer_reset_ex(arr_values, &pos);

while (zend_hash_get_current_data_ex(arr_values, (void **) &tmp, &pos) == SUCCESS) {

Z_ADDREF_PP(tmp);
add_next_index_zval(left, *tmp);

zend_hash_move_forward_ex(arr_values, &pos);
}

} else {
Z_ADDREF_P(values);
add_next_index_zval(left, values);
}
}
2 changes: 2 additions & 0 deletions ext/kernel/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ extern int phalcon_array_fetch(zval **return_value, zval *arr, zval *index, int
extern int phalcon_array_fetch_string(zval **return_value, zval *arr, char *index, uint index_length, int silent TSRMLS_DC);
extern int phalcon_array_fetch_long(zval **return_value, zval *arr, ulong index, int silent TSRMLS_DC);

/** Merge+Append */
extern void phalcon_merge_append(zval *left, zval *values TSRMLS_DC);
4 changes: 2 additions & 2 deletions ext/kernel/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "kernel/memory.h"

/**
* Fast call to join php strlen
* Fast call to php strlen
*/
void phalcon_fast_strlen(zval *return_value, zval *str){

Expand All @@ -49,7 +49,7 @@ void phalcon_fast_strlen(zval *return_value, zval *str){
}

/**
* Fast call to join php function
* Fast call to php join function
*/
void phalcon_fast_join(zval *result, zval *glue, zval *pieces TSRMLS_DC){

Expand Down
Loading

0 comments on commit 5c10b49

Please sign in to comment.