Subversion Repositories phpLibraryV2

Rev

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

Rev 36 Rev 37
Line 62... Line 62...
62
             // must contain, at a minimum, columns whenrun timestamp, whoran varchar and query text
62
             // must contain, at a minimum, columns whenrun timestamp, whoran varchar and query text
63
             // can be created with $this->buildAuditTable
63
             // can be created with $this->buildAuditTable
64
             'auditTable' => '_activity_log'
64
             'auditTable' => '_activity_log'
65
           );
65
           );
66
 
66
 
-
 
67
      public function __construct( $server, $username, $password, $database ) {
-
 
68
         parent::__construct(  $server, $username, $password, $database );
67
      
69
      }
68
 
70
 
69
      /*
71
      /*
70
       * static function which simply parses out an error and returns
72
       * static function which simply parses out an error and returns
71
       * a string suitable for printing. NOTE: it includes line returns
73
       * a string suitable for printing. NOTE: it includes line returns
72
      */
74
      */
Line 322... Line 324...
322
         $this->run();
324
         $this->run();
323
         $useAssociativeArray = $save;
325
         $useAssociativeArray = $save;
324
         if ( $this->parameters[ 'rowsAffected' ] == 1 ) {
326
         if ( $this->parameters[ 'rowsAffected' ] == 1 ) {
325
            $this->parameters[ 'returnData' ] = $this->parameters[ 'returnData' ][0];
327
            $this->parameters[ 'returnData' ] = $this->parameters[ 'returnData' ][0];
326
            return $this->parameters[ 'returnData' ];
328
            return $this->parameters[ 'returnData' ];
327
         } else
329
         } else {
328
            $this->parameters['error'] = "$this->parameters[query] did not return a unique row in getOneRow";
330
            $this->parameters['error'] = "$this->parameters[query] did not return a unique row in getOneRow";
-
 
331
            print_r( $this->parameters['query'] ); die;
-
 
332
         }
329
         return false;
333
         return false;
330
      } // getOneRow
334
      } // getOneRow
331
         
335
         
332
 
336
 
333
      // function returns the first column of the first row of data returned from query
337
      // function returns the first column of the first row of data returned from query
Line 432... Line 436...
432
      }
436
      }
433
 
437
 
434
      /**
438
      /**
435
       * runs an SQL script with multiple statements in it
439
       * runs an SQL script with multiple statements in it
436
       * 
440
       * 
-
 
441
       * If $sql is an array, it is assumed each row is a separate query
-
 
442
       * 
-
 
443
       * If $sql is a string, will separate it into different lines and
-
 
444
       * execute them
-
 
445
       * 
437
       * NOTE: it does this by breaking it based on the semicolon, so
446
       * NOTE: it does this by breaking it based on the semicolon, so
438
       * in some weird situations, it will break at the wrong place.
447
       * in some weird situations, it will break at the wrong place.
439
       * 
448
       * 
440
       */
449
       */
441
      public function runSQLScript( $sql ) {
450
      public function runSQLScript( $sql ) {
-
 
451
         if ( is_array( $sql ) ) {
-
 
452
            $queries = $sql;
-
 
453
         } else {
442
         $sql = $this->strip_sqlcomment( $sql );
454
            $sql = $this->strip_sqlcomment( $sql );
443
         $queries = explode( ";", $sql );
455
            $queries = explode( ";", $sql );
-
 
456
         }
444
         //print "<pre>" . print_r( $queries, true ) . '</pre>'; die;
457
         //print "<pre>" . print_r( $queries, true ) . '</pre>'; die;
445
         foreach ( $queries as $query ) {
458
         foreach ( $queries as $query ) {
446
            $this->doSQL( trim( implode( ' ', explode("\n",$query ) ) ) );
459
            $this->doSQL( trim( implode( ' ', explode("\n",$query ) ) ) );
447
         }
460
         }
-
 
461
      } // runSQLScript
-
 
462
      
-
 
463
      /**
-
 
464
       * Overrides real_escape_string to change behaviour slightly
-
 
465
       * 
-
 
466
       * Will check if string is pure numeric and, if it is, will return
-
 
467
       * as it is. Otherwise, will call real_escape_string, then wrap
-
 
468
       * result in single quotes
-
 
469
       */
-
 
470
       public function my_escape_string( $string, $additionalEscapes = array() ) {
-
 
471
          if ( is_numeric( $string ) )
-
 
472
             return $string;
-
 
473
          $string = $this->real_escape_string( $string );
-
 
474
          if ( count( $additionalEscapes ) ) 
-
 
475
             $string = addcslashes( $string, implode( '', $additionalEscapes ) );
-
 
476
          return "'$string'";
-
 
477
       }
-
 
478
      
-
 
479
      /**
-
 
480
       * Builds a query of form update $tablename set $fields where $where
-
 
481
       * 
-
 
482
       * Creates a query that will update table $tablename. It assumes
-
 
483
       * $fields is an array where the indexes are fieldnames and the values
-
 
484
       * are the new values for the field. Will escape the values.
-
 
485
       * 
-
 
486
       * Appends $where, again where index is a field name
-
 
487
       */
-
 
488
      public function updateQuery( $tablename, $where, $fields ) {
-
 
489
         /*
-
 
490
         print "<pre>updateQuery\n\ntable\n$tablename\n</pre>";
-
 
491
         print "<pre>\nwhere\n" . print_r( $where, true ) . "\n</pre>";
-
 
492
         print "<pre>fields\n" . print_r( $fields, true ) . "\n</pre>"; die;
-
 
493
         */
-
 
494
         $sql = '';
-
 
495
         $updateFields = array();
-
 
496
         foreach ( $fields as $fieldname => $value ) {
-
 
497
            $updateFields[] = sprintf( "%s = %s", $fieldname, $this->my_escape_string( $value ) );
-
 
498
         }
-
 
499
         $sql = "update $tablename set " . implode( ", ", $updateFields );
-
 
500
         $updateFields = array();
-
 
501
         foreach ( $where as $fieldname => $value ) {
-
 
502
            $updateFields[] = sprintf( "%s = %s", $fieldname, $this->my_escape_string( $value ) );
-
 
503
         }
-
 
504
         if ( count( $updateFields ) ) {
-
 
505
            $sql .= ' where ' . implode( ' and ', $updateFields );
-
 
506
         }
-
 
507
         return $sql;
-
 
508
      }
-
 
509
      
-
 
510
      /**
-
 
511
       * Creates an insert query from $fields
-
 
512
       * 
-
 
513
       */
-
 
514
       public function insertQuery( $tablename, $fields ) {
-
 
515
          print "<pre>insertQuery\n\ntable\n$tablename\n</pre>";
-
 
516
          print "<pre>fields\n" . print_r( $fields, true ) . "\n</pre>"; die;
-
 
517
          $query = "insert into $tablename (" . implode( ',',array_keys($fields) );
-
 
518
          $query .= " values (" . implode( ',', array_map( array($this, 'real_escape_string'), array_values( $fields ) ) );
-
 
519
          return "$query)";
-
 
520
       } // insertQuery
448
      }      
521
            
449
 
522
 
450
   } // class DBQuery
523
   } // class DBQuery
451
 
524
 
452
/*
525
/*
453
 *    $db = new DBQuery( '127.0.0.1', 'camp', 'camp', 'camp' );
526
 *    $db = new DBQuery( '127.0.0.1', 'camp', 'camp', 'camp' );