Subversion Repositories phpLibraryV2

Rev

Rev 40 | Rev 43 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 40 Rev 42
Line 50... Line 50...
50
             'returnData' => array(),
50
             'returnData' => array(),
51
             // an array that contains the meta data from the query for each column
51
             // an array that contains the meta data from the query for each column
52
             'columnMetaData' => array(),
52
             'columnMetaData' => array(),
53
             // number of columns (fields) returned by query (select only)
53
             // number of columns (fields) returned by query (select only)
54
             'numfields' => 0,
54
             'numfields' => 0,
55
             // type of data returned, array of array, array of hash, or both
-
 
56
             'fetchType' => MYSQLI_BOTH,
-
 
57
             // human readable form of fetchType
55
             // human readable form of fetchType
58
             'returnType' => 'both',
56
             'returnType' => 'both',
59
             // array of any errors which occurred
57
             // array of any errors which occurred
60
             'error' => array(),
58
             'error' => array(),
61
             // if set to a table name, all modifying queries are written to it
59
             // if set to a table name, all modifying queries are written to it
Line 211... Line 209...
211
      public function doSQL( $query = null, $parameters = array() ) {
209
      public function doSQL( $query = null, $parameters = array() ) {
212
         $errors = array();
210
         $errors = array();
213
         if ( isset( $query ) ) {
211
         if ( isset( $query ) ) {
214
            $this->parameters['query'] = $query;
212
            $this->parameters['query'] = $query;
215
         }
213
         }
-
 
214
         if ( isset( $parameters['returnType'] ) ) {
-
 
215
            $this->parameters['returnType'] = $parameters['returnType'];
-
 
216
         }
216
         // if it is a "selectstatement" it doesn't modify data
217
         // if it is a "selectstatement" it doesn't modify data
217
         // if query is an array, assume it modifies something
218
         // if query is an array, assume it modifies something
218
         // if it is a single statement, look for the regex
219
         // if it is a single statement, look for the regex
219
         $selectStatement = is_array( $this->parameters['query'] ) ?
220
         $selectStatement = is_array( $this->parameters['query'] ) ?
220
            false :
221
            false :
221
            ( preg_match( '/^\s*(select)|(show)|(describe)|(explain)/xsi', $this->parameters['query'] ) === 1 );
222
            ( preg_match( '/^\s*(select)|(show)|(describe)|(explain)/xsi', $this->parameters['query'] ) === 1 );
222
        
223
        
223
         // different actions based on whether it modifies data or not
224
         // different actions based on whether it modifies data or not
224
         if ( $selectStatement ) { // if a select, simply return the rows
225
         if ( $selectStatement ) { // if a select, simply return the rows
225
            // dataset is small enough, we just read it into memory all at one time.
226
            // dataset is small enough, we just read it into memory all at one time.
226
            // NOTE: fetch_all is nice, but tied to mysqlnd, which has reports of problems, so we do it the old way
227
            // set MYSQLI_USE_RESULT for unbuffered results
227
            if ( $sth = parent::query( $this->parameters['query'], MYSQLI_USE_RESULT  ) ) {
228
            if ( $sth = parent::query( $this->parameters['query'], MYSQLI_USE_RESULT  ) ) {
228
               if ( $sth === false ) {
229
               if ( $sth === false ) {
229
                  $errors[] = array( 'id' => $this->errno, 'message' => $this->error );
230
                  $errors[] = array( 'id' => $this->errno, 'message' => $this->error );
230
               } else {
231
               } else {
-
 
232
                  // query can return either an array of array, array of hash, or both. Default is both
-
 
233
                  $returnType = MYSQLI_BOTH;
231
                  $this->parameters['columnMetaData'] = $sth->fetch_fields(); // get metadata
234
                  $this->parameters['columnMetaData'] = $sth->fetch_fields(); // get metadata
232
                  $this->parameters['returnData'] = array(); // we'll put all the results in an array
235
                  $this->parameters['returnData'] = array(); // we'll put all the results in an array
233
                  // $fetchtype returns either an array of array, array of hash, or both. Default is array of hash
236
                  // $fetchtype returns either an array of array, array of hash, or both. Default is both
234
                  if ( isset( $this->parameters['returnType'] ) ) {
237
                  switch ( $this->parameters['returnType'] ) {
235
                     $this->parameters[ 'fetchType' ] = $this->parameters['returnType'] == 'array' ? MYSQLI_NUM : (
238
                     case 'array': 
236
                           ( $this->parameters['returnType'] == 'both' ) ? MYSQLI_BOTH : MYSQLI_ASSOC
239
                        $returnType = MYSQLI_NUM;
237
                           );
240
                        break;
238
                  } else { // default is both (hash and numeric)
241
                     case 'associative':
239
                     $this->parameters[ 'fetchType' ] =  MYSQLI_BOTH;
242
                        $returnType = MYSQLI_ASSOC;
-
 
243
                        break;
240
                     $this->parameters['returnType'] = 'both';
244
                     default: $returnType = MYSQLI_BOTH;
241
                  }
245
                  }
242
                  // slurp all the stuff in
246
                  // slurp all the stuff in
243
                  while ( $values =  $sth->fetch_array( $this->parameters[ 'fetchType' ] ) ) {
247
                  while ( $values =  $sth->fetch_array( $returnType ) ) {
244
                     $this->parameters['returnData'][] = $values;
248
                     $this->parameters['returnData'][] = $values;
245
                  }
249
                  }
246
                  $sth->free(); // cleanup memory, don't need two copies
250
                  $sth->free(); // cleanup memory, don't need two copies
247
               } // if we had no errors
251
               } // if we had no errors
248
            }
252
            }