@@ -39,6 +39,7 @@ static PHP_GINIT_FUNCTION(sqlite3);
3939static int php_sqlite3_authorizer (void * autharg , int action , const char * arg1 , const char * arg2 , const char * arg3 , const char * arg4 );
4040static void sqlite3_param_dtor (zval * data );
4141static int php_sqlite3_compare_stmt_free (php_sqlite3_stmt * * stmt_obj_ptr , sqlite3_stmt * statement );
42+ static zend_always_inline void php_sqlite3_fetch_one (int n_cols , php_sqlite3_result * result_obj , zend_long mode , zval * result );
4243
4344#define SQLITE3_CHECK_INITIALIZED (db_obj , member , class_name ) \
4445 if (!(db_obj) || !(member)) { \
@@ -1991,7 +1992,7 @@ PHP_METHOD(SQLite3Result, fetchArray)
19911992{
19921993 php_sqlite3_result * result_obj ;
19931994 zval * object = ZEND_THIS ;
1994- int i , ret ;
1995+ int ret ;
19951996 zend_long mode = PHP_SQLITE3_BOTH ;
19961997 result_obj = Z_SQLITE3_RESULT_P (object );
19971998
@@ -2028,26 +2029,8 @@ PHP_METHOD(SQLite3Result, fetchArray)
20282029
20292030 array_init (return_value );
20302031
2031- for (i = 0 ; i < n_cols ; i ++ ) {
2032- zval data ;
2033-
2034- sqlite_value_to_zval (result_obj -> stmt_obj -> stmt , i , & data );
2035-
2036- if (mode & PHP_SQLITE3_NUM ) {
2037- add_index_zval (return_value , i , & data );
2038- }
2032+ php_sqlite3_fetch_one (n_cols , result_obj , mode , return_value );
20392033
2040- if (mode & PHP_SQLITE3_ASSOC ) {
2041- if (mode & PHP_SQLITE3_NUM ) {
2042- if (Z_REFCOUNTED (data )) {
2043- Z_ADDREF (data );
2044- }
2045- }
2046- /* Note: we can't use the "add_new" variant here instead of "update" because
2047- * when the same column name is encountered, the last result should be taken. */
2048- zend_symtable_update (Z_ARR_P (return_value ), result_obj -> column_names [i ], & data );
2049- }
2050- }
20512034 break ;
20522035
20532036 case SQLITE_DONE :
@@ -2071,6 +2054,61 @@ static void sqlite3result_clear_column_names_cache(php_sqlite3_result *result) {
20712054 result -> column_count = -1 ;
20722055}
20732056
2057+ PHP_METHOD (SQLite3Result , fetchAll )
2058+ {
2059+ int i , nb_cols ;
2060+ bool done = false;
2061+ php_sqlite3_result * result_obj ;
2062+ zval * object = ZEND_THIS ;
2063+ zend_long mode = PHP_SQLITE3_BOTH ;
2064+ result_obj = Z_SQLITE3_RESULT_P (object );
2065+
2066+ ZEND_PARSE_PARAMETERS_START (0 , 1 )
2067+ Z_PARAM_OPTIONAL
2068+ Z_PARAM_LONG (mode )
2069+ ZEND_PARSE_PARAMETERS_END ();
2070+
2071+ SQLITE3_CHECK_INITIALIZED (result_obj -> db_obj , result_obj -> stmt_obj -> initialised , SQLite3Result )
2072+
2073+ nb_cols = sqlite3_column_count (result_obj -> stmt_obj -> stmt );
2074+ if (mode & PHP_SQLITE3_ASSOC ) {
2075+ sqlite3result_clear_column_names_cache (result_obj );
2076+ result_obj -> column_names = emalloc (nb_cols * sizeof (zend_string * ));
2077+
2078+ for (i = 0 ; i < nb_cols ; i ++ ) {
2079+ const char * column = sqlite3_column_name (result_obj -> stmt_obj -> stmt , i );
2080+ result_obj -> column_names [i ] = zend_string_init (column , strlen (column ), 0 );
2081+ }
2082+ }
2083+ result_obj -> column_count = nb_cols ;
2084+ array_init (return_value );
2085+
2086+ while (!done ) {
2087+ int step = sqlite3_step (result_obj -> stmt_obj -> stmt );
2088+
2089+ switch (step ) {
2090+ case SQLITE_ROW : {
2091+ zval result ;
2092+ array_init_size (& result , result_obj -> column_count );
2093+
2094+ php_sqlite3_fetch_one (result_obj -> column_count , result_obj , mode , & result );
2095+
2096+ add_next_index_zval (return_value , & result );
2097+ break ;
2098+ }
2099+ case SQLITE_DONE :
2100+ done = true;
2101+ break ;
2102+ default :
2103+ if (!EG (exception )) {
2104+ php_sqlite3_error (result_obj -> db_obj , sqlite3_errcode (sqlite3_db_handle (result_obj -> stmt_obj -> stmt )), "Unable to execute statement: %s" , sqlite3_errmsg (sqlite3_db_handle (result_obj -> stmt_obj -> stmt )));
2105+ }
2106+ zval_ptr_dtor (return_value );
2107+ RETURN_FALSE ;
2108+ }
2109+ }
2110+ }
2111+
20742112/* {{{ Resets the result set back to the first row. */
20752113PHP_METHOD (SQLite3Result , reset )
20762114{
@@ -2429,6 +2467,29 @@ static void sqlite3_param_dtor(zval *data) /* {{{ */
24292467}
24302468/* }}} */
24312469
2470+ static zend_always_inline void php_sqlite3_fetch_one (int n_cols , php_sqlite3_result * result_obj , zend_long mode , zval * result )
2471+ {
2472+ for (int i = 0 ; i < n_cols ; i ++ ) {
2473+ zval data ;
2474+ sqlite_value_to_zval (result_obj -> stmt_obj -> stmt , i , & data );
2475+
2476+ if (mode & PHP_SQLITE3_NUM ) {
2477+ add_index_zval (result , i , & data );
2478+ }
2479+
2480+ if (mode & PHP_SQLITE3_ASSOC ) {
2481+ if (mode & PHP_SQLITE3_NUM ) {
2482+ if (Z_REFCOUNTED (data )) {
2483+ Z_ADDREF (data );
2484+ }
2485+ }
2486+ /* Note: we can't use the "add_new" variant here instead of "update" because
2487+ * when the same column name is encountered, the last result should be taken. */
2488+ zend_symtable_update (Z_ARR_P (result ), result_obj -> column_names [i ], & data );
2489+ }
2490+ }
2491+ }
2492+
24322493/* {{{ PHP_MINIT_FUNCTION */
24332494PHP_MINIT_FUNCTION (sqlite3 )
24342495{
0 commit comments