1 | <?php |
---|
2 | |
---|
3 | class DBConnection |
---|
4 | { |
---|
5 | function DBConnection() |
---|
6 | { |
---|
7 | static $conn_id; |
---|
8 | |
---|
9 | if (is_resource($conn_id)) |
---|
10 | { |
---|
11 | $this->id_ = $conn_id; |
---|
12 | return; |
---|
13 | } |
---|
14 | if (!$this->id_ = mysql_connect(DB_HOST, DB_USER, DB_PASS)) |
---|
15 | die("Unable to connect to mysql database."); |
---|
16 | |
---|
17 | mysql_select_db(DB_NAME); |
---|
18 | $conn_id = $this->id_; |
---|
19 | } |
---|
20 | |
---|
21 | function id() |
---|
22 | { |
---|
23 | return $this->id_; |
---|
24 | } |
---|
25 | |
---|
26 | function affectedRows() |
---|
27 | { |
---|
28 | return mysql_affected_rows($this->id_); |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | class DBQuery |
---|
33 | { |
---|
34 | var $object; |
---|
35 | |
---|
36 | // php5 style object overloading |
---|
37 | // we internally load up the wanted object and reroute all |
---|
38 | // object actions to it |
---|
39 | function __construct($forceNormal = false) |
---|
40 | { |
---|
41 | if (DB_USE_MEMCACHE === true && !$forceNormal) |
---|
42 | { |
---|
43 | $object = new DBMemCachedQuery(); |
---|
44 | } |
---|
45 | elseif (DB_USE_QCACHE === true && !$forceNormal) |
---|
46 | { |
---|
47 | $object = new DBCachedQuery(); |
---|
48 | } |
---|
49 | else |
---|
50 | { |
---|
51 | $object = new DBNormalQuery(); |
---|
52 | } |
---|
53 | $this->object = $object; |
---|
54 | } |
---|
55 | |
---|
56 | function __call($name, $args) |
---|
57 | { |
---|
58 | return call_user_func_array(array($this->object, $name), $args); |
---|
59 | } |
---|
60 | |
---|
61 | function __set($name, $value) |
---|
62 | { |
---|
63 | $this->object->$name = $value; |
---|
64 | } |
---|
65 | |
---|
66 | function __unset($name) |
---|
67 | { |
---|
68 | unset($this->object->$name); |
---|
69 | } |
---|
70 | |
---|
71 | function __isset($name) |
---|
72 | { |
---|
73 | return isset($this->object->$name); |
---|
74 | } |
---|
75 | |
---|
76 | function __get($name) |
---|
77 | { |
---|
78 | return $this->object->$name; |
---|
79 | } |
---|
80 | |
---|
81 | // php4 style object overloading |
---|
82 | // we just hijack $this but we need to use a helper |
---|
83 | // function for this because php5 fatals if it sees |
---|
84 | // $this = ... in the src |
---|
85 | function DBQuery($forceNormal = false) |
---|
86 | { |
---|
87 | $object = &$this->getRef($this); |
---|
88 | if (DB_USE_MEMCACHE === true && !$forceNormal) |
---|
89 | { |
---|
90 | $object = new DBMemCachedQuery(); |
---|
91 | } |
---|
92 | elseif (DB_USE_QCACHE === true && !$forceNormal) |
---|
93 | { |
---|
94 | $object = new DBCachedQuery(); |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | $object = new DBNormalQuery(); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | function &getRef(&$var) |
---|
103 | { |
---|
104 | return $var; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | class DBNormalQuery |
---|
109 | { |
---|
110 | function DBNormalQuery() |
---|
111 | { |
---|
112 | $this->executed_ = false; |
---|
113 | $this->dbconn_ = new DBConnection; |
---|
114 | } |
---|
115 | |
---|
116 | function queryCount($increase = false) |
---|
117 | { |
---|
118 | static $count; |
---|
119 | |
---|
120 | if ($increase) |
---|
121 | { |
---|
122 | $count++; |
---|
123 | } |
---|
124 | |
---|
125 | return $count; |
---|
126 | } |
---|
127 | |
---|
128 | function execute($sql) |
---|
129 | { |
---|
130 | $t1 = strtok(microtime(), ' ') + strtok(''); |
---|
131 | |
---|
132 | $this->resid_ = mysql_query($sql, $this->dbconn_->id()); |
---|
133 | |
---|
134 | if ($this->resid_ === false) |
---|
135 | { |
---|
136 | if (defined('DB_HALTONERROR') && DB_HALTONERROR) |
---|
137 | { |
---|
138 | echo "Database error: " . mysql_error($this->dbconn_->id()) . "<br>"; |
---|
139 | echo "SQL: " . $sql . "<br>"; |
---|
140 | exit; |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | return false; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | $this->exectime_ = strtok(microtime(), ' ') + strtok('') - $t1; |
---|
149 | $this->executed_ = true; |
---|
150 | |
---|
151 | if (KB_PROFILE == 2) |
---|
152 | { |
---|
153 | file_put_contents('/tmp/profile.lst', $sql . "\nExecution time: " . $this->exectime_ . "\n", FILE_APPEND); |
---|
154 | } |
---|
155 | |
---|
156 | $this->queryCount(true); |
---|
157 | |
---|
158 | return true; |
---|
159 | } |
---|
160 | |
---|
161 | function recordCount() |
---|
162 | { |
---|
163 | return mysql_num_rows($this->resid_); |
---|
164 | } |
---|
165 | |
---|
166 | function getRow() |
---|
167 | { |
---|
168 | if ($this->resid_) |
---|
169 | { |
---|
170 | return mysql_fetch_assoc($this->resid_); |
---|
171 | } |
---|
172 | return false; |
---|
173 | } |
---|
174 | |
---|
175 | function rewind() |
---|
176 | { |
---|
177 | @mysql_data_seek($this->resid_, 0); |
---|
178 | } |
---|
179 | |
---|
180 | function getInsertID() |
---|
181 | { |
---|
182 | return mysql_insert_id(); |
---|
183 | } |
---|
184 | |
---|
185 | function execTime() |
---|
186 | { |
---|
187 | return $this->exectime_; |
---|
188 | } |
---|
189 | |
---|
190 | function executed() |
---|
191 | { |
---|
192 | return $this->executed_; |
---|
193 | } |
---|
194 | |
---|
195 | function getErrorMsg() |
---|
196 | { |
---|
197 | $msg = $this->sql_ . "<br>"; |
---|
198 | $msg .= "Query failed. " . mysql_error($this->dbconn_->id()); |
---|
199 | |
---|
200 | return $msg; |
---|
201 | } |
---|
202 | } |
---|
203 | ?> |
---|