Skip to content

Commit

Permalink
Map Phalcon\Db\FETCH_XXX constants to PDO::FETCH_XXX
Browse files Browse the repository at this point in the history
  * Fix #1642
  * Get rid of hardcoded numbers for fetch modes
  • Loading branch information
sjinks committed Dec 11, 2013
1 parent 133aa32 commit 33666e0
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 38 deletions.
25 changes: 21 additions & 4 deletions ext/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -81,10 +83,25 @@ PHALCON_INIT_CLASS(Phalcon_Db){

PHALCON_REGISTER_CLASS(Phalcon, Db, db, phalcon_db_method_entry, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);

zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_ASSOC"), 1 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_BOTH"), 2 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_NUM"), 3 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_OBJ"), 4 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_USE_DEFAULT"), (long int)PDO_FETCH_USE_DEFAULT TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_LAZY"), (long int)PDO_FETCH_LAZY TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_ASSOC"), (long int)PDO_FETCH_ASSOC TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_NUM"), (long int)PDO_FETCH_NUM TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_BOTH"), (long int)PDO_FETCH_BOTH TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_OBJ"), (long int)PDO_FETCH_OBJ TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_BOUND"), (long int)PDO_FETCH_BOUND TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_COLUMN"), (long int)PDO_FETCH_COLUMN TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_CLASS"), (long int)PDO_FETCH_CLASS TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_INTO"), (long int)PDO_FETCH_INTO TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_FUNC"), (long int)PDO_FETCH_FUNC TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_NAMED"), (long int)PDO_FETCH_NAMED TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_KEY_PAIR") , (long int)PDO_FETCH_KEY_PAIR TSRMLS_CC);

zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_GROUP"), (long int)PDO_FETCH_GROUP TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_UNIQUE"), (long int)PDO_FETCH_UNIQUE TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_CLASSTYPE"), (long int)PDO_FETCH_CLASSTYPE TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_SERIALIZE"), (long int)PDO_FETCH_SERIALIZE TSRMLS_CC);
zend_declare_class_constant_long(phalcon_db_ce, SL("FETCH_PROPS_LATE"), (long int)PDO_FETCH_PROPS_LATE TSRMLS_CC);

return SUCCESS;
}
Expand Down
20 changes: 11 additions & 9 deletions ext/db/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -209,7 +211,7 @@ PHP_METHOD(Phalcon_Db_Adapter, fetchOne){

if (!fetch_mode) {
PHALCON_INIT_VAR(fetch_mode);
ZVAL_LONG(fetch_mode, 2);
ZVAL_LONG(fetch_mode, PDO_FETCH_BOTH);
}

if (!bind_params) {
Expand Down Expand Up @@ -271,7 +273,7 @@ PHP_METHOD(Phalcon_Db_Adapter, fetchAll){

if (!fetch_mode) {
PHALCON_INIT_VAR(fetch_mode);
ZVAL_LONG(fetch_mode, 2);
ZVAL_LONG(fetch_mode, PDO_FETCH_BOTH);
}

if (!bind_params) {
Expand Down Expand Up @@ -785,7 +787,7 @@ PHP_METHOD(Phalcon_Db_Adapter, tableExists){
phalcon_call_method_p2(sql, dialect, "tableexists", table_name, schema_name);

PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

PHALCON_INIT_VAR(num);
phalcon_call_method_p2(num, this_ptr, "fetchone", sql, fetch_num);
Expand Down Expand Up @@ -825,7 +827,7 @@ PHP_METHOD(Phalcon_Db_Adapter, viewExists){
phalcon_call_method_p2(sql, dialect, "viewexists", view_name, schema_name);

PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

PHALCON_INIT_VAR(num);
phalcon_call_method_p2(num, this_ptr, "fetchone", sql, fetch_num);
Expand Down Expand Up @@ -1303,7 +1305,7 @@ PHP_METHOD(Phalcon_Db_Adapter, listTables){
* Use fetch Num
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

/**
* Execute the SQL returning the tables
Expand Down Expand Up @@ -1367,7 +1369,7 @@ PHP_METHOD(Phalcon_Db_Adapter, listViews){
* Use fetch Num
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

/**
* Execute the SQL returning the tables
Expand Down Expand Up @@ -1427,7 +1429,7 @@ PHP_METHOD(Phalcon_Db_Adapter, describeIndexes){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

/**
* Get the SQL required to describe indexes from the Dialect
Expand Down Expand Up @@ -1526,7 +1528,7 @@ PHP_METHOD(Phalcon_Db_Adapter, describeReferences){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

/**
* Get the SQL required to describe the references from the Dialect
Expand Down Expand Up @@ -1651,7 +1653,7 @@ PHP_METHOD(Phalcon_Db_Adapter, tableOptions){
phalcon_call_method_p2(sql, dialect, "tableoptions", table_name, schema_name);
if (zend_is_true(sql)) {
PHALCON_INIT_VAR(fetch_assoc);
ZVAL_LONG(fetch_assoc, 1);
ZVAL_LONG(fetch_assoc, PDO_FETCH_ASSOC);

PHALCON_INIT_VAR(describe);
phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_assoc);
Expand Down
4 changes: 3 additions & 1 deletion ext/db/adapter/pdo/mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -156,7 +158,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

/**
* Get the describe
Expand Down
6 changes: 4 additions & 2 deletions ext/db/adapter/pdo/oracle.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -176,7 +178,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Oracle, describeColumns){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

PHALCON_INIT_VAR(describe);
phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num);
Expand Down Expand Up @@ -345,7 +347,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Oracle, lastInsertId){
PHALCON_CONCAT_SVS(sql, "SELECT ", sequence_name, ".CURRVAL FROM dual");

PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

PHALCON_INIT_VAR(ret);
phalcon_call_method_p2(ret, this_ptr, "fetchall", sql, fetch_num);
Expand Down
4 changes: 3 additions & 1 deletion ext/db/adapter/pdo/postgresql.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -163,7 +165,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

PHALCON_INIT_VAR(describe);
phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num);
Expand Down
8 changes: 5 additions & 3 deletions ext/db/adapter/pdo/sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -157,7 +159,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

PHALCON_INIT_VAR(describe);
phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num);
Expand Down Expand Up @@ -333,7 +335,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeIndexes){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

PHALCON_INIT_VAR(sql);
phalcon_call_method_p2(sql, dialect, "describeindexes", table, schema);
Expand Down Expand Up @@ -444,7 +446,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeReferences){
* We're using FETCH_NUM to fetch the columns
*/
PHALCON_INIT_VAR(fetch_num);
ZVAL_LONG(fetch_num, 3);
ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

/**
* Execute the SQL describing the references
Expand Down
18 changes: 4 additions & 14 deletions ext/db/result/pdo.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

#include "ext/pdo/php_pdo_driver.h"
#include "kernel/exception.h"
#include "kernel/object.h"
#include "kernel/fcall.h"
Expand Down Expand Up @@ -65,7 +65,7 @@ PHALCON_INIT_CLASS(Phalcon_Db_Result_Pdo){

zend_declare_property_null(phalcon_db_result_pdo_ce, SL("_connection"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_db_result_pdo_ce, SL("_result"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(phalcon_db_result_pdo_ce, SL("_fetchMode"), 4, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(phalcon_db_result_pdo_ce, SL("_fetchMode"), PDO_FETCH_OBJ, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_db_result_pdo_ce, SL("_pdoStatement"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_db_result_pdo_ce, SL("_sqlStatement"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_db_result_pdo_ce, SL("_bindParams"), ZEND_ACC_PROTECTED TSRMLS_CC);
Expand Down Expand Up @@ -450,20 +450,10 @@ PHP_METHOD(Phalcon_Db_Result_Pdo, setFetchMode){
}

PHALCON_INIT_VAR(fetch_type);
ZVAL_LONG(fetch_type, fetch_mode);

PHALCON_OBS_VAR(pdo_statement);
phalcon_read_property(&pdo_statement, this_ptr, SL("_pdoStatement"), PH_NOISY_CC);
if (fetch_mode == 1) {
ZVAL_LONG(fetch_type, 2);
} else if (fetch_mode == 2) {
ZVAL_LONG(fetch_type, 4);
} else if (fetch_mode == 3) {
ZVAL_LONG(fetch_type, 3);
} else if (fetch_mode == 4) {
ZVAL_LONG(fetch_type, 5);
} else {
ZVAL_LONG(fetch_type, 0);
}

if (Z_LVAL_P(fetch_type) != 0) {
phalcon_call_method_p1_noret(pdo_statement, "setfetchmode", fetch_type);
Expand Down
5 changes: 3 additions & 2 deletions ext/mvc/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

#include "kernel/fcall.h"
#include "kernel/exception.h"
#include "kernel/object.h"
Expand Down Expand Up @@ -4919,7 +4920,7 @@ PHP_METHOD(Phalcon_Mvc_Model, refresh){
phalcon_call_method_p1(sql, dialect, "select", select);

PHALCON_INIT_VAR(fetch_type);
ZVAL_LONG(fetch_type, 1);
ZVAL_LONG(fetch_type, PDO_FETCH_ASSOC);

PHALCON_INIT_VAR(row);
phalcon_call_method_p4(row, read_connection, "fetchone", sql, fetch_type, unique_params, unique_types);
Expand Down
4 changes: 3 additions & 1 deletion ext/mvc/model/resultset/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -111,7 +113,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, __construct){
*/
if (Z_TYPE_P(result) == IS_OBJECT) {
PHALCON_INIT_VAR(fetch_assoc);
ZVAL_LONG(fetch_assoc, 1);
ZVAL_LONG(fetch_assoc, PDO_FETCH_ASSOC);
phalcon_call_method_p1_noret(result, "setfetchmode", fetch_assoc);
}

Expand Down
4 changes: 3 additions & 1 deletion ext/mvc/model/resultset/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "ext/pdo/php_pdo_driver.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -102,7 +104,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Resultset_Simple, __construct){
* Use only fetch assoc
*/
PHALCON_INIT_VAR(fetch_assoc);
ZVAL_LONG(fetch_assoc, 1);
ZVAL_LONG(fetch_assoc, PDO_FETCH_ASSOC);
phalcon_call_method_p1_noret(result, "setfetchmode", fetch_assoc);

PHALCON_INIT_VAR(limit);
Expand Down

0 comments on commit 33666e0

Please sign in to comment.