Subversion Repositories phpLibraryV2

Rev

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

Rev 17 Rev 29
Line 45... Line 45...
45
             // the number of rows 1) affectd by an insert, update or delete or 2) returned by a select
45
             // the number of rows 1) affectd by an insert, update or delete or 2) returned by a select
46
             'rowsAffected' => '',
46
             'rowsAffected' => '',
47
             // the value of the last row inserted
47
             // the value of the last row inserted
48
             'lastInsertKey' => '',
48
             'lastInsertKey' => '',
49
             // an array that contains the data retrieved by a query (select only)
49
             // an array that contains the data retrieved by a query (select only)
50
             'returnData' => '',
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' => '',
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
55
             // type of data returned, array of array, array of hash, or both
56
             'fetchType' => MYSQLI_ASSOC,
56
             'fetchType' => MYSQLI_ASSOC,
57
             // human readable form of fetchType
57
             // human readable form of fetchType
Line 131... Line 131...
131
         if ( $tablename ) // they sent us one, so set it
131
         if ( $tablename ) // they sent us one, so set it
132
            $this->parameters[ 'auditTable' ] = $tablename;
132
            $this->parameters[ 'auditTable' ] = $tablename;
133
         if ( ! $createStatement ) { // they did not set createStatement, so use our default
133
         if ( ! $createStatement ) { // they did not set createStatement, so use our default
134
            $auditTable = $this->parameters['auditTable'];
134
            $auditTable = $this->parameters['auditTable'];
135
            $createStatement = "
135
            $createStatement = "
136
               create table $auditTable (
136
               create table if not exists $auditTable (
137
                  _activity_log_id int unsigned not null auto_increment,
137
                  _activity_log_id int unsigned not null auto_increment,
138
                  timestamp  timestamp,
138
                  timestamp  timestamp,
139
                  user       varchar(64),
139
                  user       varchar(64),
140
                  query      text,
140
                  query      text,
141
                  primary key(_activity_log_id)
141
                  primary key(_activity_log_id)
Line 214... Line 214...
214
         // if it is a "selectstatement" it doesn't modify data
214
         // if it is a "selectstatement" it doesn't modify data
215
         // if query is an array, assume it modifies something
215
         // if query is an array, assume it modifies something
216
         // if it is a single statement, look for the regex
216
         // if it is a single statement, look for the regex
217
         $selectStatement = is_array( $this->parameters['query'] ) ?
217
         $selectStatement = is_array( $this->parameters['query'] ) ?
218
            false :
218
            false :
219
            ( preg_match( '/^\s*(select)|(show)|(describe)|(explain)/i', $this->parameters['query'] ) === 1 );
219
            ( preg_match( '/^\s*(select)|(show)|(describe)|(explain)/xsi', $this->parameters['query'] ) === 1 );
220
        
220
        
221
         // different actions based on whether it modifies data or not
221
         // different actions based on whether it modifies data or not
222
         if ( $selectStatement ) { // if a select, simply return the rows
222
         if ( $selectStatement ) { // if a select, simply return the rows
223
            // dataset is small enough, we just read it into memory all at one time.
223
            // dataset is small enough, we just read it into memory all at one time.
224
            // NOTE: fetch_all is nice, but tied to mysqlnd, which has reports of problems, so we do it the old way
224
            // NOTE: fetch_all is nice, but tied to mysqlnd, which has reports of problems, so we do it the old way
Line 383... Line 383...
383
                        break;
383
                        break;
384
         } // switch
384
         } // switch
385
         return $value;
385
         return $value;
386
      }
386
      }
387
 
387
 
-
 
388
      /*
-
 
389
         '[^']*(?!\\)'(*SKIP)(*F)       # Make sure we're not matching inside of quotes
-
 
390
         |(?m-s:\s*(?:\-{2}|\#)[^\n]*$) # Single line comment
-
 
391
         |(?:
-
 
392
           \/\*.*?\*\/                  # Multi-line comment
-
 
393
           (?(?=(?m-s:\h+$))         # Get trailing whitespace if any exists and only if it's the rest of the line
-
 
394
             \h+
-
 
395
           )
-
 
396
         )
-
 
397
      */
-
 
398
      /**
-
 
399
       * Functions strips SQL queries from a file. Above is a commented copy of the regex used
-
 
400
       * 
-
 
401
       * @param string $query An arbitrary sized SQL script
-
 
402
       * @returns string $query, with all SQL comments removed
-
 
403
       */
-
 
404
 
-
 
405
      public function strip_sqlcomment ($query = '') {
-
 
406
         $regex = '\'[^\']*(?!\\\)\'(*SKIP)(*F)|(?m-s:\\s*(?:\\-{2}|\\#)[^\\n]*$)|(?:\\/\\*.*?\\*\\/(?(?=(?m-s:\h+$))\\h+))';
-
 
407
         //print "<pre>$regex</pre>" ; die;
-
 
408
          return (($query == '') ?  '' : preg_replace( "/$regex/xs", '', $query ));
-
 
409
      }
-
 
410
 
-
 
411
      /**
-
 
412
       * runs an SQL script with multiple statements in it
-
 
413
       * 
-
 
414
       * NOTE: it does this by breaking it based on the semicolon, so
-
 
415
       * in some weird situations, it will break at the wrong place.
-
 
416
       * 
-
 
417
       */
-
 
418
      public function runSQLScript( $sql ) {
-
 
419
         $sql = $this->strip_sqlcomment( $sql );
-
 
420
         $queries = explode( ";", $sql );
-
 
421
         //print "<pre>" . print_r( $queries, true ) . '</pre>'; die;
-
 
422
         foreach ( $queries as $query ) {
-
 
423
            $this->doSQL( trim( implode( ' ', explode("\n",$query ) ) ) );
-
 
424
         }
-
 
425
      }      
388
 
426
 
389
   } // class DBQuery
427
   } // class DBQuery
390
 
428
 
391
/*
429
/*
392
 *    $db = new DBQuery( '127.0.0.1', 'camp', 'camp', 'camp' );
430
 *    $db = new DBQuery( '127.0.0.1', 'camp', 'camp', 'camp' );