Subversion Repositories phpLibraryV2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
<?php
2
 
3
   require_once "DBDatabase.class.php";
4
   require_once "Logging.class.php"; // For logging
5
 
6
   global $LOGFILE;
7
 
8
   define( 'HTML_QUOTE_CHAR', '"' );
9
   define( 'CONSTANT_NO_VALUE_DROPDOWN', '--------' );
10
 
11
   class DBQuery {
12
      protected $query;          // the query to be processed
13
      protected $rowsAffected;   // the number of rows 1) affectd by an insert, update or delete or 2) returned by a select
14
      protected $lastInsertKey;  // the value of the last row inserted (mysql only)
15
      protected $returnData;     // an array that contains the data retrieved by a query (select only)
16
      protected $columnMetaData; // an array that contains the meta data from the query
17
      protected $useAssociativeArray;   // if true, $returnData is an array of associative array, with the column names being the key fields
18
      protected $showErrors;     // if true, will return errors to STDOUT
19
      protected $error;          // set to last error returned
20
      protected $log;            // Wil be instantiation of a  Logging instance if set
21
      protected $auditTable;     // if set to a table name, all queries are written to it
22
      protected $orderBy;
23
 
24
      public function __construct( $query, $whereClause = null, $orderBy = null, $runImmediate = false ) {
25
         $this->query = $query;
26
         if ( isset( $whereClause ) ) {
27
            $this->query .= $this->makeWhereClause( $whereClause );
28
         }
29
         if ( isset( $orderBy ) ) {
30
            $this->orderBy .= ' order by ' . implode( ',', $orderBy );
31
         }
32
         $this->rowsAffected = 0;
33
         $this->lastInsertKey = '';
34
         $this->returnData = array();
35
         $this->columnMetaData = array();
36
         $this->useAssociativeArray = true;
37
         $this->showErrors = true;
38
         $this->error = '';
39
         if (isset( $LOGFILE ) ) $this->logFile =  new Logging( $LOGFILE );
40
         if ( $runImmediate ) {
41
            $this->run();
42
         // if (isset( $AUDITTABLE ) ) $this->auditTable =  new Logging( $LOGFILE );
43
      }
44
 
45
      public function __set( $name, $value ) {
46
         $this->$name = $value;
47
         return $value
48
      }
49
 
50
      public function __get( $name ) {
51
         return isset( $this->$name ) ? $this->$name : null;
52
      }
53
 
54
      public function __isset( $name ) {
55
         return isset( $this->$name );
56
      }
57
 
58
      /* 
59
         function actually executes the query, populating the members with the results
60
         returns true if no errors, false if errors (see $this->error for code)
61
      */
62
      function run () {
63
         // print "<pre>In Query database\n---------------------\n$query\n---------------------\n</pre>";
64
         if ( isset($this->log) ) $this->log->printLog( $query );
65
//         if ( isset( $this->auditTable) ) audit( $query );
66
         $result = @mysql_query($this->query);
67
         if( mysql_errno() ) {
68
            $this->error = 'MYSQL ERROR #'.mysql_errno().' : <small>' . mysql_error(). "</small><br><VAR>$query</VAR>";
69
            if ( $this->showErrors ) echo($this->error);
70
            return false;
71
         } // if
72
         if( preg_match ( '/^\s*select/i', $this->query ) ) { // this is a select statement
73
            $this->rowsAffected = @mysql_num_rows($result);
74
            $this->columnMetaData = array();
75
            for ($i = 0; $i < mysql_num_fields( $result ); $i++) {
76
               $meta = mysql_fetch_field ( $result );
77
               $this->columnMetaData[] = array('name' => $meta->name, 'length' => $meta->max_length, 'numeric' => $meta->numeric, 'type' => $meta->type );
78
            }
79
            if( ! $this->rowsAffected ) return;
80
            $this->returnData = array();
81
            for( $i = 0; $i < $this->rowsAffected; $i++ ) {
82
               $this->returnData[] = $this->useAssociativeArray ? mysql_fetch_assoc($result) : mysql_fetch_row($result);
83
            }
84
            mysql_free_result($result);
85
         } else { // not a select statement
86
            $this->rowsAffected = mysql_affected_rows();
87
            $this->lastInsertKey = mysql_insert_id();
88
         }
89
         return true;
90
      } // function run
91
 
92
 
93
 
94
      /*
95
       * function will return one and only one row, NOT as an array of array
96
       * but as a single row array
97
       * if more than one row is returned by query, error is set and function
98
       * returns false.
99
       * Otherwise, function returns true
100
       */
101
      public function getOneRow( $sql = null ) {
102
         if ( isset( $sql ) )
103
            $this->query = $sql;
104
         $save = $this->useAssociativeArray;
105
         $useAssociativeArray = true;
106
         $this->run();
107
         $useAssociativeArray = $save;
108
         if ( $this->rowsAffected == 1 ) {
109
            $this->returnData = $this->returnData[0];
110
            return true;
111
         } else
112
            $this->error = "$this->rowsAffected rows returned from getOneRow";
113
         return false;
114
      } // getOneRow
115
 
116
 
117
      // function returns the first column of the first row of data returned from query
118
      // or null no value returned
119
      function getOneDBValue( $sql = null ) {
120
         if ( isset( $sql ) )
121
            $this->query = $sql;
122
         $save = $this->useAssociativeArray;
123
         $useAssociativeArray = false;
124
         $this->run();
125
         $useAssociativeArray = $save;
126
         return $this->rowsAffected ? $this->returnData[0][0] : null;
127
      }
128
 
129
      function countNumberOfRows ( $sql = null ) {
130
         if ( isset( $sql ) )
131
            $this->query = $sql;
132
         $save = $this->useAssociativeArray;
133
         $useAssociativeArray = false;
134
         $this->run();
135
         $useAssociativeArray = $save;
136
         return $this->rowsAffected;
137
      }
138
 
139
      function makeWhereClause ($conditions, $joinedBy = 'and') {
140
         $joinedBy = " $joinedBy "; // make sure there are spaces around it
141
         $whereClause = ' where ' . implode ( $joinedBy, $conditions );
142
         return $whereClause;
143
      }
144
 
145
      /* 
146
         values is an associative array of name/value pairs
147
         function will replace all items of the form <$name> with its values (the less than and greater than symbols around the key)
148
      */
149
      function insertValuesIntoQuery( $values ) {
150
         foreach ( $values as $name => $value ) {
151
            $this->query = search_replace_string($this->query, "<$name>", $value );
152
         }
153
      }
154
 
155
   } // class DBQuery
156
 
157
?>