| 875 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
// create instance protocol://user:pass@host/db?charset=UTF8&persist=TRUE&timezone=Europe/Sofia
|
|
|
4 |
$db = DB::get('mysqli://root@127.0.0.1/test');
|
|
|
5 |
$db = DB::get('oracle://root:pass@VIRT/?charset=AL32UTF8');
|
|
|
6 |
// execute a non-resulting query (returns boolean true)
|
|
|
7 |
$db->query('UPDATE table SET value = 1 WHERE id = ?', array(1));
|
|
|
8 |
// get all results as array
|
|
|
9 |
$db->all('SELECT * FROM table WHERE id = ?', array(1), "array_key", bool_skip_key, "assoc"/"num");
|
|
|
10 |
// get one result
|
|
|
11 |
$db->one('SELECT * FROM table WHERE id = ?', array(1), "assoc"/"num");
|
|
|
12 |
// get a traversable object to pass to foreach, or use count(), or use direct access: [INDEX]
|
|
|
13 |
$db->get('SELECT * FROM table WHERE id = ?', array(1), "assoc"/"num")[1];
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
namespace
|
|
|
17 |
{
|
|
|
18 |
class db
|
|
|
19 |
{
|
|
|
20 |
private function __construct() {
|
|
|
21 |
}
|
|
|
22 |
public function __clone() {
|
|
|
23 |
throw new \vakata\database\Exception('Cannot clone static DB');
|
|
|
24 |
}
|
|
|
25 |
public static function get($settings = null) {
|
|
|
26 |
return new \vakata\database\DBC($settings);
|
|
|
27 |
}
|
|
|
28 |
public static function getc($settings = null, \vakata\cache\ICache $c = null) {
|
|
|
29 |
if($c === null) { $c = \vakata\cache\cache::inst(); }
|
|
|
30 |
return new \vakata\database\DBCCached($settings, $c);
|
|
|
31 |
}
|
|
|
32 |
}
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
namespace vakata\database
|
|
|
36 |
{
|
|
|
37 |
class Exception extends \Exception
|
|
|
38 |
{
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
class Settings
|
|
|
42 |
{
|
|
|
43 |
public $type = null;
|
|
|
44 |
public $username = 'root';
|
|
|
45 |
public $password = null;
|
|
|
46 |
public $database = null;
|
|
|
47 |
public $servername = 'localhost';
|
|
|
48 |
public $serverport = null;
|
|
|
49 |
public $persist = false;
|
|
|
50 |
public $timezone = null;
|
|
|
51 |
public $charset = 'UTF8';
|
|
|
52 |
|
|
|
53 |
public function __construct($settings) {
|
|
|
54 |
$str = parse_url($settings);
|
|
|
55 |
if(!$str) {
|
|
|
56 |
throw new Exception('Malformed DB settings string: ' . $settings);
|
|
|
57 |
}
|
|
|
58 |
if(array_key_exists('scheme',$str)) {
|
|
|
59 |
$this->type = rawurldecode($str['scheme']);
|
|
|
60 |
}
|
|
|
61 |
if(array_key_exists('user',$str)) {
|
|
|
62 |
$this->username = rawurldecode($str['user']);
|
|
|
63 |
}
|
|
|
64 |
if(array_key_exists('pass',$str)) {
|
|
|
65 |
$this->password = rawurldecode($str['pass']);
|
|
|
66 |
}
|
|
|
67 |
if(array_key_exists('path',$str)) {
|
|
|
68 |
$this->database = trim(rawurldecode($str['path']),'/');
|
|
|
69 |
}
|
|
|
70 |
if(array_key_exists('host',$str)) {
|
|
|
71 |
$this->servername = rawurldecode($str['host']);
|
|
|
72 |
}
|
|
|
73 |
if(array_key_exists('port',$str)) {
|
|
|
74 |
$this->serverport = rawurldecode($str['port']);
|
|
|
75 |
}
|
|
|
76 |
if(array_key_exists('query',$str)) {
|
|
|
77 |
parse_str($str['query'], $str);
|
|
|
78 |
$this->persist = (array_key_exists('persist', $str) && $str['persist'] === 'TRUE');
|
|
|
79 |
if(array_key_exists('charset', $str)) {
|
|
|
80 |
$this->charset = $str['charset'];
|
|
|
81 |
}
|
|
|
82 |
if(array_key_exists('timezone', $str)) {
|
|
|
83 |
$this->timezone = $str['timezone'];
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
interface IDB
|
|
|
90 |
{
|
|
|
91 |
public function connect();
|
|
|
92 |
public function query($sql, $vars);
|
|
|
93 |
public function get($sql, $data, $key, $skip_key, $mode);
|
|
|
94 |
public function all($sql, $data, $key, $skip_key, $mode);
|
|
|
95 |
public function one($sql, $data, $mode);
|
|
|
96 |
public function raw($sql);
|
|
|
97 |
public function prepare($sql);
|
|
|
98 |
public function execute($data);
|
|
|
99 |
public function disconnect();
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
interface IDriver
|
|
|
103 |
{
|
|
|
104 |
public function prepare($sql);
|
|
|
105 |
public function execute($data);
|
|
|
106 |
public function query($sql, $data);
|
|
|
107 |
public function nextr($result);
|
|
|
108 |
public function seek($result, $row);
|
|
|
109 |
public function nf($result);
|
|
|
110 |
public function af();
|
|
|
111 |
public function insert_id();
|
|
|
112 |
public function real_query($sql);
|
|
|
113 |
public function get_settings();
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
abstract class ADriver implements IDriver
|
|
|
117 |
{
|
|
|
118 |
protected $lnk = null;
|
|
|
119 |
protected $settings = null;
|
|
|
120 |
|
|
|
121 |
public function __construct(Settings $settings) {
|
|
|
122 |
$this->settings = $settings;
|
|
|
123 |
}
|
|
|
124 |
public function __destruct() {
|
|
|
125 |
if($this->is_connected()) {
|
|
|
126 |
$this->disconnect();
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
public function get_settings() {
|
|
|
130 |
return $this->settings;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
public function connect() {
|
|
|
134 |
}
|
|
|
135 |
public function is_connected() {
|
|
|
136 |
return $this->lnk !== null;
|
|
|
137 |
}
|
|
|
138 |
public function disconnect() {
|
|
|
139 |
}
|
|
|
140 |
public function query($sql, $data = array()) {
|
|
|
141 |
return $this->execute($this->prepare($sql), $data);
|
|
|
142 |
}
|
|
|
143 |
public function prepare($sql) {
|
|
|
144 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
145 |
return $sql;
|
|
|
146 |
}
|
|
|
147 |
public function execute($sql, $data = array()) {
|
|
|
148 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
149 |
if(!is_array($data)) { $data = array(); }
|
|
|
150 |
$binder = '?';
|
|
|
151 |
if(strpos($sql, $binder) !== false && is_array($data) && count($data)) {
|
|
|
152 |
$tmp = explode($binder, $sql);
|
|
|
153 |
if(!is_array($data)) { $data = array($data); }
|
|
|
154 |
$data = array_values($data);
|
|
|
155 |
if(count($data) >= count($tmp)) { $data = array_slice($data, 0, count($tmp)-1); }
|
|
|
156 |
$sql = $tmp[0];
|
|
|
157 |
foreach($data as $i => $v) {
|
|
|
158 |
$sql .= $this->escape($v) . $tmp[($i + 1)];
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
return $this->real_query($sql);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
public function real_query($sql) {
|
|
|
165 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
166 |
}
|
|
|
167 |
protected function escape($input) {
|
|
|
168 |
if(is_array($input)) {
|
|
|
169 |
foreach($input as $k => $v) {
|
|
|
170 |
$input[$k] = $this->escape($v);
|
|
|
171 |
}
|
|
|
172 |
return implode(',',$input);
|
|
|
173 |
}
|
|
|
174 |
if(is_string($input)) {
|
|
|
175 |
$input = addslashes($input);
|
|
|
176 |
return "'".$input."'";
|
|
|
177 |
}
|
|
|
178 |
if(is_bool($input)) {
|
|
|
179 |
return $input === false ? 0 : 1;
|
|
|
180 |
}
|
|
|
181 |
if(is_null($input)) {
|
|
|
182 |
return 'NULL';
|
|
|
183 |
}
|
|
|
184 |
return $input;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
public function nextr($result) {}
|
|
|
188 |
public function nf($result) {}
|
|
|
189 |
public function af() {}
|
|
|
190 |
public function insert_id() {}
|
|
|
191 |
public function seek($result, $row) {}
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
class Result implements \Iterator, \ArrayAccess, \Countable
|
|
|
195 |
{
|
|
|
196 |
protected $all = null;
|
|
|
197 |
protected $rdy = false;
|
|
|
198 |
protected $rslt = null;
|
|
|
199 |
protected $mode = null;
|
|
|
200 |
protected $fake = null;
|
|
|
201 |
protected $skip = false;
|
|
|
202 |
|
|
|
203 |
protected $fake_key = 0;
|
|
|
204 |
protected $real_key = 0;
|
|
|
205 |
public function __construct(Query $rslt, $key = null, $skip_key = false, $mode = 'assoc') {
|
|
|
206 |
$this->rslt = $rslt;
|
|
|
207 |
$this->mode = $mode;
|
|
|
208 |
$this->fake = $key;
|
|
|
209 |
$this->skip = $skip_key;
|
|
|
210 |
}
|
|
|
211 |
public function count() {
|
|
|
212 |
return $this->rdy ? count($this->all) : $this->rslt->nf();
|
|
|
213 |
}
|
|
|
214 |
public function current() {
|
|
|
215 |
if(!$this->count()) {
|
|
|
216 |
return null;
|
|
|
217 |
}
|
|
|
218 |
if($this->rdy) {
|
|
|
219 |
return current($this->all);
|
|
|
220 |
}
|
|
|
221 |
$tmp = $this->rslt->row();
|
|
|
222 |
$row = array();
|
|
|
223 |
switch($this->mode) {
|
|
|
224 |
case 'num':
|
|
|
225 |
foreach($tmp as $k => $v) {
|
|
|
226 |
if(is_int($k)) {
|
|
|
227 |
$row[$k] = $v;
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
break;
|
|
|
231 |
case 'both':
|
|
|
232 |
$row = $tmp;
|
|
|
233 |
break;
|
|
|
234 |
case 'assoc':
|
|
|
235 |
default:
|
|
|
236 |
foreach($tmp as $k => $v) {
|
|
|
237 |
if(!is_int($k)) {
|
|
|
238 |
$row[$k] = $v;
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
break;
|
|
|
242 |
}
|
|
|
243 |
if($this->fake) {
|
|
|
244 |
$this->fake_key = $row[$this->fake];
|
|
|
245 |
}
|
|
|
246 |
if($this->skip) {
|
|
|
247 |
unset($row[$this->fake]);
|
|
|
248 |
}
|
|
|
249 |
if(is_array($row) && count($row) === 1) {
|
|
|
250 |
$row = current($row);
|
|
|
251 |
}
|
|
|
252 |
return $row;
|
|
|
253 |
}
|
|
|
254 |
public function key() {
|
|
|
255 |
if($this->rdy) {
|
|
|
256 |
return key($this->all);
|
|
|
257 |
}
|
|
|
258 |
return $this->fake ? $this->fake_key : $this->real_key;
|
|
|
259 |
}
|
|
|
260 |
public function next() {
|
|
|
261 |
if($this->rdy) {
|
|
|
262 |
return next($this->all);
|
|
|
263 |
}
|
|
|
264 |
$this->rslt->nextr();
|
|
|
265 |
$this->real_key++;
|
|
|
266 |
}
|
|
|
267 |
public function rewind() {
|
|
|
268 |
if($this->rdy) {
|
|
|
269 |
return reset($this->all);
|
|
|
270 |
}
|
|
|
271 |
if($this->real_key !== null) {
|
|
|
272 |
$this->rslt->seek(($this->real_key = 0));
|
|
|
273 |
}
|
|
|
274 |
$this->rslt->nextr();
|
|
|
275 |
}
|
|
|
276 |
public function valid() {
|
|
|
277 |
if($this->rdy) {
|
|
|
278 |
return current($this->all) !== false;
|
|
|
279 |
}
|
|
|
280 |
return $this->rslt->row() !== false && $this->rslt->row() !== null;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
public function one() {
|
|
|
284 |
$this->rewind();
|
|
|
285 |
return $this->current();
|
|
|
286 |
}
|
|
|
287 |
public function get() {
|
|
|
288 |
if(!$this->rdy) {
|
|
|
289 |
$this->all = array();
|
|
|
290 |
foreach($this as $k => $v) {
|
|
|
291 |
$this->all[$k] = $v;
|
|
|
292 |
}
|
|
|
293 |
$this->rdy = true;
|
|
|
294 |
}
|
|
|
295 |
return $this->all;
|
|
|
296 |
}
|
|
|
297 |
public function offsetExists($offset) {
|
|
|
298 |
if($this->rdy) {
|
|
|
299 |
return isset($this->all[$offset]);
|
|
|
300 |
}
|
|
|
301 |
if($this->fake === null) {
|
|
|
302 |
return $this->rslt->seek(($this->real_key = $offset));
|
|
|
303 |
}
|
|
|
304 |
$this->get();
|
|
|
305 |
return isset($this->all[$offset]);
|
|
|
306 |
}
|
|
|
307 |
public function offsetGet($offset) {
|
|
|
308 |
if($this->rdy) {
|
|
|
309 |
return $this->all[$offset];
|
|
|
310 |
}
|
|
|
311 |
if($this->fake === null) {
|
|
|
312 |
$this->rslt->seek(($this->real_key = $offset));
|
|
|
313 |
$this->rslt->nextr();
|
|
|
314 |
return $this->current();
|
|
|
315 |
}
|
|
|
316 |
$this->get();
|
|
|
317 |
return $this->all[$offset];
|
|
|
318 |
}
|
|
|
319 |
public function offsetSet ($offset, $value ) {
|
|
|
320 |
throw new Exception('Cannot set result');
|
|
|
321 |
}
|
|
|
322 |
public function offsetUnset ($offset) {
|
|
|
323 |
throw new Exception('Cannot unset result');
|
|
|
324 |
}
|
|
|
325 |
public function __sleep() {
|
|
|
326 |
$this->get();
|
|
|
327 |
return array('all', 'rdy', 'mode', 'fake', 'skip');
|
|
|
328 |
}
|
|
|
329 |
public function __toString() {
|
|
|
330 |
return print_r($this->get(), true);
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
class Query
|
|
|
335 |
{
|
|
|
336 |
protected $drv = null;
|
|
|
337 |
protected $sql = null;
|
|
|
338 |
protected $prp = null;
|
|
|
339 |
protected $rsl = null;
|
|
|
340 |
protected $row = null;
|
|
|
341 |
protected $num = null;
|
|
|
342 |
protected $aff = null;
|
|
|
343 |
protected $iid = null;
|
|
|
344 |
|
|
|
345 |
public function __construct(IDriver $drv, $sql) {
|
|
|
346 |
$this->drv = $drv;
|
|
|
347 |
$this->sql = $sql;
|
|
|
348 |
$this->prp = $this->drv->prepare($sql);
|
|
|
349 |
}
|
|
|
350 |
public function execute($vars = array()) {
|
|
|
351 |
$this->rsl = $this->drv->execute($this->prp, $vars);
|
|
|
352 |
$this->num = (is_object($this->rsl) || is_resource($this->rsl)) && is_callable(array($this->drv, 'nf')) ? (int)@$this->drv->nf($this->rsl) : 0;
|
|
|
353 |
$this->aff = $this->drv->af();
|
|
|
354 |
$this->iid = $this->drv->insert_id();
|
|
|
355 |
return $this;
|
|
|
356 |
}
|
|
|
357 |
public function result($key = null, $skip_key = false, $mode = 'assoc') {
|
|
|
358 |
return new Result($this, $key, $skip_key, $mode);
|
|
|
359 |
}
|
|
|
360 |
public function row() {
|
|
|
361 |
return $this->row;
|
|
|
362 |
}
|
|
|
363 |
public function f($field) {
|
|
|
364 |
return $this->row[$field];
|
|
|
365 |
}
|
|
|
366 |
public function nextr() {
|
|
|
367 |
$this->row = $this->drv->nextr($this->rsl);
|
|
|
368 |
return $this->row !== false && $this->row !== null;
|
|
|
369 |
}
|
|
|
370 |
public function seek($offset) {
|
|
|
371 |
return @$this->drv->seek($this->rsl, $offset) ? true : false;
|
|
|
372 |
}
|
|
|
373 |
public function nf() {
|
|
|
374 |
return $this->num;
|
|
|
375 |
}
|
|
|
376 |
public function af() {
|
|
|
377 |
return $this->aff;
|
|
|
378 |
}
|
|
|
379 |
public function insert_id() {
|
|
|
380 |
return $this->iid;
|
|
|
381 |
}
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
class DBC implements IDB
|
|
|
385 |
{
|
|
|
386 |
protected $drv = null;
|
|
|
387 |
protected $que = null;
|
|
|
388 |
|
|
|
389 |
public function __construct($drv = null) {
|
|
|
390 |
if(!$drv && defined('DATABASE')) {
|
|
|
391 |
$drv = DATABASE;
|
|
|
392 |
}
|
|
|
393 |
if(!$drv) {
|
|
|
394 |
$this->error('Could not create database (no settings)');
|
|
|
395 |
}
|
|
|
396 |
if(is_string($drv)) {
|
|
|
397 |
$drv = new \vakata\database\Settings($drv);
|
|
|
398 |
}
|
|
|
399 |
if($drv instanceof Settings) {
|
|
|
400 |
$tmp = '\\vakata\\database\\' . $drv->type . '_driver';
|
|
|
401 |
if(!class_exists($tmp)) {
|
|
|
402 |
$this->error('Could not create database (no driver: '.$drv->type.')');
|
|
|
403 |
}
|
|
|
404 |
$drv = new $tmp($drv);
|
|
|
405 |
}
|
|
|
406 |
if(!($drv instanceof IDriver)) {
|
|
|
407 |
$this->error('Could not create database - wrong driver');
|
|
|
408 |
}
|
|
|
409 |
$this->drv = $drv;
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
public function connect() {
|
|
|
413 |
if(!$this->drv->is_connected()) {
|
|
|
414 |
try {
|
|
|
415 |
$this->drv->connect();
|
|
|
416 |
}
|
|
|
417 |
catch (Exception $e) {
|
|
|
418 |
$this->error($e->getMessage(), 1);
|
|
|
419 |
}
|
|
|
420 |
}
|
|
|
421 |
return true;
|
|
|
422 |
}
|
|
|
423 |
public function disconnect() {
|
|
|
424 |
if($this->drv->is_connected()) {
|
|
|
425 |
$this->drv->disconnect();
|
|
|
426 |
}
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
public function prepare($sql) {
|
|
|
430 |
try {
|
|
|
431 |
$this->que = new Query($this->drv, $sql);
|
|
|
432 |
return $this->que;
|
|
|
433 |
} catch (Exception $e) {
|
|
|
434 |
$this->error($e->getMessage(), 2);
|
|
|
435 |
}
|
|
|
436 |
}
|
|
|
437 |
public function execute($data = array()) {
|
|
|
438 |
try {
|
|
|
439 |
return $this->que->execute($data);
|
|
|
440 |
} catch (Exception $e) {
|
|
|
441 |
$this->error($e->getMessage(), 3);
|
|
|
442 |
}
|
|
|
443 |
}
|
|
|
444 |
public function query($sql, $data = array()) {
|
|
|
445 |
try {
|
|
|
446 |
$this->que = new Query($this->drv, $sql);
|
|
|
447 |
return $this->que->execute($data);
|
|
|
448 |
}
|
|
|
449 |
catch (Exception $e) {
|
|
|
450 |
$this->error($e->getMessage(), 4);
|
|
|
451 |
}
|
|
|
452 |
}
|
|
|
453 |
public function get($sql, $data = array(), $key = null, $skip_key = false, $mode = 'assoc') {
|
|
|
454 |
return $this->query($sql, $data)->result($key, $skip_key, $mode);
|
|
|
455 |
}
|
|
|
456 |
public function all($sql, $data = array(), $key = null, $skip_key = false, $mode = 'assoc') {
|
|
|
457 |
return $this->get($sql, $data, $key, $skip_key, $mode)->get();
|
|
|
458 |
}
|
|
|
459 |
public function one($sql, $data = array(), $mode = 'assoc') {
|
|
|
460 |
return $this->query($sql, $data)->result(null, false, $mode)->one();
|
|
|
461 |
}
|
|
|
462 |
public function raw($sql) {
|
|
|
463 |
return $this->drv->real_query($sql);
|
|
|
464 |
}
|
|
|
465 |
public function get_driver() {
|
|
|
466 |
return $this->drv->get_settings()->type;
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
public function __call($method, $args) {
|
|
|
470 |
if($this->que && is_callable(array($this->que, $method))) {
|
|
|
471 |
try {
|
|
|
472 |
return call_user_func_array(array($this->que, $method), $args);
|
|
|
473 |
} catch (Exception $e) {
|
|
|
474 |
$this->error($e->getMessage(), 5);
|
|
|
475 |
}
|
|
|
476 |
}
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
protected final function error($error = '') {
|
|
|
480 |
$dirnm = defined('LOGROOT') ? LOGROOT : realpath(dirname(__FILE__));
|
|
|
481 |
@file_put_contents(
|
|
|
482 |
$dirnm . DIRECTORY_SEPARATOR . '_errors_sql.log',
|
|
|
483 |
'[' . date('d-M-Y H:i:s') . '] ' . $this->settings->type . ' > ' . preg_replace("@[\s\r\n\t]+@", ' ', $error) . "\n",
|
|
|
484 |
FILE_APPEND
|
|
|
485 |
);
|
|
|
486 |
throw new Exception($error);
|
|
|
487 |
}
|
|
|
488 |
}
|
|
|
489 |
|
|
|
490 |
class DBCCached extends DBC
|
|
|
491 |
{
|
|
|
492 |
protected $cache_inst = null;
|
|
|
493 |
protected $cache_nmsp = null;
|
|
|
494 |
public function __construct($settings = null, \vakata\cache\ICache $c = null) {
|
|
|
495 |
parent::__construct($settings);
|
|
|
496 |
$this->cache_inst = $c;
|
|
|
497 |
$this->cache_nmsp = 'DBCCached_' . md5(serialize($this->drv->get_settings()));
|
|
|
498 |
}
|
|
|
499 |
public function cache($expires, $sql, $data = array(), $key = null, $skip_key = false, $mode = 'assoc') {
|
|
|
500 |
$arg = func_get_args();
|
|
|
501 |
array_shift($arg);
|
|
|
502 |
$key = md5(serialize($arg));
|
|
|
503 |
if(!$this->cache_inst) {
|
|
|
504 |
return call_user_func_array(array($this, 'all'), $arg);
|
|
|
505 |
}
|
|
|
506 |
|
|
|
507 |
$tmp = $this->cache_inst->get($key, $this->cache_nmsp);
|
|
|
508 |
if(!$tmp) {
|
|
|
509 |
$this->cache_inst->prep($key, $this->cache_nmsp);
|
|
|
510 |
$tmp = call_user_func_array(array($this, 'all'), $arg);
|
|
|
511 |
$this->cache_inst->set($key, $tmp, $this->cache_nmsp, $expires);
|
|
|
512 |
}
|
|
|
513 |
return $tmp;
|
|
|
514 |
}
|
|
|
515 |
public function clear() {
|
|
|
516 |
if($this->cache_inst) {
|
|
|
517 |
$this->cache_inst->clear($this->cache_nmsp);
|
|
|
518 |
}
|
|
|
519 |
}
|
|
|
520 |
}
|
|
|
521 |
|
|
|
522 |
class mysqli_driver extends ADriver
|
|
|
523 |
{
|
|
|
524 |
protected $iid = 0;
|
|
|
525 |
protected $aff = 0;
|
|
|
526 |
protected $mnd = false;
|
|
|
527 |
|
|
|
528 |
public function __construct($settings) {
|
|
|
529 |
parent::__construct($settings);
|
|
|
530 |
if(!$this->settings->serverport) { $this->settings->serverport = 3306; }
|
|
|
531 |
$this->mnd = function_exists('mysqli_fetch_all');
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
public function connect() {
|
|
|
535 |
$this->lnk = new \mysqli(
|
|
|
536 |
($this->settings->persist ? 'p:' : '') . $this->settings->servername,
|
|
|
537 |
$this->settings->username,
|
|
|
538 |
$this->settings->password,
|
|
|
539 |
$this->settings->database,
|
|
|
540 |
$this->settings->serverport
|
|
|
541 |
);
|
|
|
542 |
if($this->lnk->connect_errno) {
|
|
|
543 |
throw new Exception('Connect error: ' . $this->lnk->connect_errno);
|
|
|
544 |
}
|
|
|
545 |
if(!$this->lnk->set_charset($this->settings->charset)) {
|
|
|
546 |
throw new Exception('Charset error: ' . $this->lnk->connect_errno);
|
|
|
547 |
}
|
|
|
548 |
if($this->settings->timezone) {
|
|
|
549 |
@$this->lnk->query("SET time_zone = '" . addslashes($this->settings->timezone) . "'");
|
|
|
550 |
}
|
|
|
551 |
return true;
|
|
|
552 |
}
|
|
|
553 |
public function disconnect() {
|
|
|
554 |
if($this->is_connected()) {
|
|
|
555 |
@$this->lnk->close();
|
|
|
556 |
}
|
|
|
557 |
}
|
|
|
558 |
public function real_query($sql) {
|
|
|
559 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
560 |
$temp = $this->lnk->query($sql);
|
|
|
561 |
if(!$temp) {
|
|
|
562 |
throw new Exception('Could not execute query : ' . $this->lnk->error . ' <'.$sql.'>');
|
|
|
563 |
}
|
|
|
564 |
$this->iid = $this->lnk->insert_id;
|
|
|
565 |
$this->aff = $this->lnk->affected_rows;
|
|
|
566 |
return $temp;
|
|
|
567 |
}
|
|
|
568 |
public function nextr($result) {
|
|
|
569 |
if($this->mnd) {
|
|
|
570 |
return $result->fetch_array(MYSQL_BOTH);
|
|
|
571 |
}
|
|
|
572 |
else {
|
|
|
573 |
$ref = $result->result_metadata();
|
|
|
574 |
if(!$ref) { return false; }
|
|
|
575 |
$tmp = mysqli_fetch_fields($ref);
|
|
|
576 |
if(!$tmp) { return false; }
|
|
|
577 |
$ref = array();
|
|
|
578 |
foreach($tmp as $col) { $ref[$col->name] = null; }
|
|
|
579 |
$tmp = array();
|
|
|
580 |
foreach($ref as $k => $v) { $tmp[] =& $ref[$k]; }
|
|
|
581 |
if(!call_user_func_array(array($result, 'bind_result'), $tmp)) { return false; }
|
|
|
582 |
if(!$result->fetch()) { return false; }
|
|
|
583 |
$tmp = array();
|
|
|
584 |
$i = 0;
|
|
|
585 |
foreach($ref as $k => $v) { $tmp[$i++] = $v; $tmp[$k] = $v; }
|
|
|
586 |
return $tmp;
|
|
|
587 |
}
|
|
|
588 |
}
|
|
|
589 |
public function seek($result, $row) {
|
|
|
590 |
$temp = $result->data_seek($row);
|
|
|
591 |
return $temp;
|
|
|
592 |
}
|
|
|
593 |
public function nf($result) {
|
|
|
594 |
return $result->num_rows;
|
|
|
595 |
}
|
|
|
596 |
public function af() {
|
|
|
597 |
return $this->aff;
|
|
|
598 |
}
|
|
|
599 |
public function insert_id() {
|
|
|
600 |
return $this->iid;
|
|
|
601 |
}
|
|
|
602 |
public function prepare($sql) {
|
|
|
603 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
604 |
$temp = $this->lnk->prepare($sql);
|
|
|
605 |
if(!$temp) {
|
|
|
606 |
throw new Exception('Could not prepare : ' . $this->lnk->error . ' <'.$sql.'>');
|
|
|
607 |
}
|
|
|
608 |
return $temp;
|
|
|
609 |
}
|
|
|
610 |
public function execute($sql, $data = array()) {
|
|
|
611 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
612 |
if(!is_array($data)) { $data = array(); }
|
|
|
613 |
if(is_string($sql)) {
|
|
|
614 |
return parent::execute($sql, $data);
|
|
|
615 |
}
|
|
|
616 |
|
|
|
617 |
$data = array_values($data);
|
|
|
618 |
if($sql->param_count) {
|
|
|
619 |
if(count($data) < $sql->param_count) {
|
|
|
620 |
throw new Exception('Prepared execute - not enough parameters.');
|
|
|
621 |
}
|
|
|
622 |
$ref = array('');
|
|
|
623 |
foreach($data as $i => $v) {
|
|
|
624 |
switch(gettype($v)) {
|
|
|
625 |
case "boolean":
|
|
|
626 |
case "integer":
|
|
|
627 |
$data[$i] = (int)$v;
|
|
|
628 |
$ref[0] .= 'i';
|
|
|
629 |
$ref[$i+1] =& $data[$i];
|
|
|
630 |
break;
|
|
|
631 |
case "double":
|
|
|
632 |
$ref[0] .= 'd';
|
|
|
633 |
$ref[$i+1] =& $data[$i];
|
|
|
634 |
break;
|
|
|
635 |
case "array":
|
|
|
636 |
$data[$i] = implode(',',$v);
|
|
|
637 |
$ref[0] .= 's';
|
|
|
638 |
$ref[$i+1] =& $data[$i];
|
|
|
639 |
break;
|
|
|
640 |
case "object":
|
|
|
641 |
case "resource":
|
|
|
642 |
$data[$i] = serialize($data[$i]);
|
|
|
643 |
$ref[0] .= 's';
|
|
|
644 |
$ref[$i+1] =& $data[$i];
|
|
|
645 |
break;
|
|
|
646 |
default:
|
|
|
647 |
$ref[0] .= 's';
|
|
|
648 |
$ref[$i+1] =& $data[$i];
|
|
|
649 |
break;
|
|
|
650 |
}
|
|
|
651 |
}
|
|
|
652 |
call_user_func_array(array($sql, 'bind_param'), $ref);
|
|
|
653 |
}
|
|
|
654 |
$rtrn = $sql->execute();
|
|
|
655 |
if(!$this->mnd) {
|
|
|
656 |
$sql->store_result();
|
|
|
657 |
}
|
|
|
658 |
if(!$rtrn) {
|
|
|
659 |
throw new Exception('Prepared execute error : ' . $this->lnk->error);
|
|
|
660 |
}
|
|
|
661 |
$this->iid = $this->lnk->insert_id;
|
|
|
662 |
$this->aff = $this->lnk->affected_rows;
|
|
|
663 |
if(!$this->mnd) {
|
|
|
664 |
return $sql->field_count ? $sql : $rtrn;
|
|
|
665 |
}
|
|
|
666 |
else {
|
|
|
667 |
return $sql->field_count ? $sql->get_result() : $rtrn;
|
|
|
668 |
}
|
|
|
669 |
}
|
|
|
670 |
|
|
|
671 |
protected function escape($input) {
|
|
|
672 |
if(is_array($input)) {
|
|
|
673 |
foreach($input as $k => $v) {
|
|
|
674 |
$input[$k] = $this->escape($v);
|
|
|
675 |
}
|
|
|
676 |
return implode(',',$input);
|
|
|
677 |
}
|
|
|
678 |
if(is_string($input)) {
|
|
|
679 |
$input = $this->lnk->real_escape_string($input);
|
|
|
680 |
return "'".$input."'";
|
|
|
681 |
}
|
|
|
682 |
if(is_bool($input)) {
|
|
|
683 |
return $input === false ? 0 : 1;
|
|
|
684 |
}
|
|
|
685 |
if(is_null($input)) {
|
|
|
686 |
return 'NULL';
|
|
|
687 |
}
|
|
|
688 |
return $input;
|
|
|
689 |
}
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
class mysql_driver extends ADriver
|
|
|
693 |
{
|
|
|
694 |
protected $iid = 0;
|
|
|
695 |
protected $aff = 0;
|
|
|
696 |
public function __construct($settings) {
|
|
|
697 |
parent::__construct($settings);
|
|
|
698 |
if(!$this->settings->serverport) { $this->settings->serverport = 3306; }
|
|
|
699 |
}
|
|
|
700 |
public function connect() {
|
|
|
701 |
$this->lnk = ($this->settings->persist) ?
|
|
|
702 |
@mysql_pconnect(
|
|
|
703 |
$this->settings->servername.':'.$this->settings->serverport,
|
|
|
704 |
$this->settings->username,
|
|
|
705 |
$this->settings->password
|
|
|
706 |
) :
|
|
|
707 |
@mysql_connect(
|
|
|
708 |
$this->settings->servername.':'.$this->settings->serverport,
|
|
|
709 |
$this->settings->username,
|
|
|
710 |
$this->settings->password
|
|
|
711 |
);
|
|
|
712 |
|
|
|
713 |
if($this->lnk === false || !mysql_select_db($this->settings->database, $this->lnk) || !mysql_query("SET NAMES '".$this->settings->charset."'", $this->lnk)) {
|
|
|
714 |
throw new Exception('Connect error: ' . mysql_error());
|
|
|
715 |
}
|
|
|
716 |
if($this->settings->timezone) {
|
|
|
717 |
@mysql_query("SET time_zone = '" . addslashes($this->settings->timezone) . "'", $this->lnk);
|
|
|
718 |
}
|
|
|
719 |
return true;
|
|
|
720 |
}
|
|
|
721 |
public function disconnect() {
|
|
|
722 |
if(is_resource($this->lnk)) {
|
|
|
723 |
mysql_close($this->lnk);
|
|
|
724 |
}
|
|
|
725 |
}
|
|
|
726 |
|
|
|
727 |
public function real_query($sql) {
|
|
|
728 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
729 |
$temp = mysql_query($sql, $this->lnk);
|
|
|
730 |
if(!$temp) {
|
|
|
731 |
throw new Exception('Could not execute query : ' . mysql_error($this->lnk) . ' <'.$sql.'>');
|
|
|
732 |
}
|
|
|
733 |
$this->iid = mysql_insert_id($this->lnk);
|
|
|
734 |
$this->aff = mysql_affected_rows($this->lnk);
|
|
|
735 |
return $temp;
|
|
|
736 |
}
|
|
|
737 |
public function nextr($result) {
|
|
|
738 |
return mysql_fetch_array($result, MYSQL_BOTH);
|
|
|
739 |
}
|
|
|
740 |
public function seek($result, $row) {
|
|
|
741 |
$temp = @mysql_data_seek($result, $row);
|
|
|
742 |
if(!$temp) {
|
|
|
743 |
//throw new Exception('Could not seek : ' . mysql_error($this->lnk));
|
|
|
744 |
}
|
|
|
745 |
return $temp;
|
|
|
746 |
}
|
|
|
747 |
public function nf($result) {
|
|
|
748 |
return mysql_num_rows($result);
|
|
|
749 |
}
|
|
|
750 |
public function af() {
|
|
|
751 |
return $this->aff;
|
|
|
752 |
}
|
|
|
753 |
public function insert_id() {
|
|
|
754 |
return $this->iid;
|
|
|
755 |
}
|
|
|
756 |
|
|
|
757 |
protected function escape($input) {
|
|
|
758 |
if(is_array($input)) {
|
|
|
759 |
foreach($input as $k => $v) {
|
|
|
760 |
$input[$k] = $this->escape($v);
|
|
|
761 |
}
|
|
|
762 |
return implode(',',$input);
|
|
|
763 |
}
|
|
|
764 |
if(is_string($input)) {
|
|
|
765 |
$input = mysql_real_escape_string($input, $this->lnk);
|
|
|
766 |
return "'".$input."'";
|
|
|
767 |
}
|
|
|
768 |
if(is_bool($input)) {
|
|
|
769 |
return $input === false ? 0 : 1;
|
|
|
770 |
}
|
|
|
771 |
if(is_null($input)) {
|
|
|
772 |
return 'NULL';
|
|
|
773 |
}
|
|
|
774 |
return $input;
|
|
|
775 |
}
|
|
|
776 |
}
|
|
|
777 |
|
|
|
778 |
class postgre_driver extends ADriver
|
|
|
779 |
{
|
|
|
780 |
protected $iid = 0;
|
|
|
781 |
protected $aff = 0;
|
|
|
782 |
public function __construct($settings) {
|
|
|
783 |
parent::__construct($settings);
|
|
|
784 |
if(!$this->settings->serverport) { $this->settings->serverport = 5432; }
|
|
|
785 |
}
|
|
|
786 |
public function connect() {
|
|
|
787 |
$this->lnk = ($this->settings->persist) ?
|
|
|
788 |
@pg_pconnect(
|
|
|
789 |
"host=" . $this->settings->servername . " " .
|
|
|
790 |
"port=" . $this->settings->serverport . " " .
|
|
|
791 |
"user=" . $this->settings->username . " " .
|
|
|
792 |
"password=" . $this->settings->password . " " .
|
|
|
793 |
"dbname=" . $this->settings->database . " " .
|
|
|
794 |
"options='--client_encoding=".strtoupper($this->settings->charset)."' "
|
|
|
795 |
) :
|
|
|
796 |
@pg_connect(
|
|
|
797 |
"host=" . $this->settings->servername . " " .
|
|
|
798 |
"port=" . $this->settings->serverport . " " .
|
|
|
799 |
"user=" . $this->settings->username . " " .
|
|
|
800 |
"password=" . $this->settings->password . " " .
|
|
|
801 |
"dbname=" . $this->settings->database . " " .
|
|
|
802 |
"options='--client_encoding=".strtoupper($this->settings->charset)."' "
|
|
|
803 |
);
|
|
|
804 |
if($this->lnk === false) {
|
|
|
805 |
throw new Exception('Connect error');
|
|
|
806 |
}
|
|
|
807 |
if($this->settings->timezone) {
|
|
|
808 |
@pg_query($this->lnk, "SET TIME ZONE '".addslashes($this->settings->timezone)."'");
|
|
|
809 |
}
|
|
|
810 |
return true;
|
|
|
811 |
}
|
|
|
812 |
public function disconnect() {
|
|
|
813 |
if(is_resource($this->lnk)) {
|
|
|
814 |
pg_close($this->lnk);
|
|
|
815 |
}
|
|
|
816 |
}
|
|
|
817 |
public function real_query($sql) {
|
|
|
818 |
return $this->query($sql);
|
|
|
819 |
}
|
|
|
820 |
public function prepare($sql) {
|
|
|
821 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
822 |
$binder = '?';
|
|
|
823 |
if(strpos($sql, $binder) !== false) {
|
|
|
824 |
$tmp = explode($binder, $sql);
|
|
|
825 |
$sql = $tmp[0];
|
|
|
826 |
foreach($tmp as $i => $v) {
|
|
|
827 |
$sql .= '$' . ($i + 1);
|
|
|
828 |
if(isset($tmp[($i + 1)])) {
|
|
|
829 |
$sql .= $tmp[($i + 1)];
|
|
|
830 |
}
|
|
|
831 |
}
|
|
|
832 |
}
|
|
|
833 |
return $sql;
|
|
|
834 |
}
|
|
|
835 |
public function execute($sql, $data = array()) {
|
|
|
836 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
837 |
if(!is_array($data)) { $data = array(); }
|
|
|
838 |
$temp = (is_array($data) && count($data)) ? pg_query_params($this->lnk, $sql, $data) : pg_query_params($this->lnk, $sql, array());
|
|
|
839 |
if(!$temp) {
|
|
|
840 |
throw new Exception('Could not execute query : ' . pg_last_error($this->lnk) . ' <'.$sql.'>');
|
|
|
841 |
}
|
|
|
842 |
if(preg_match('@^\s*(INSERT|REPLACE)\s+INTO@i', $sql)) {
|
|
|
843 |
$this->iid = pg_query($this->lnk, 'SELECT lastval()');
|
|
|
844 |
$this->aff = pg_affected_rows($temp);
|
|
|
845 |
}
|
|
|
846 |
return $temp;
|
|
|
847 |
}
|
|
|
848 |
|
|
|
849 |
public function nextr($result) {
|
|
|
850 |
return pg_fetch_array($result, NULL, PGSQL_BOTH);
|
|
|
851 |
}
|
|
|
852 |
public function seek($result, $row) {
|
|
|
853 |
$temp = @pg_result_seek($result, $row);
|
|
|
854 |
if(!$temp) {
|
|
|
855 |
//throw new Exception('Could not seek : ' . pg_last_error($this->lnk));
|
|
|
856 |
}
|
|
|
857 |
return $temp;
|
|
|
858 |
}
|
|
|
859 |
public function nf($result) {
|
|
|
860 |
return pg_num_rows($result);
|
|
|
861 |
}
|
|
|
862 |
public function af() {
|
|
|
863 |
return $this->aff;
|
|
|
864 |
}
|
|
|
865 |
public function insert_id() {
|
|
|
866 |
return $this->iid;
|
|
|
867 |
}
|
|
|
868 |
|
|
|
869 |
// Функция mysql_query?
|
|
|
870 |
// - http://okbob.blogspot.com/2009/08/mysql-functions-for-postgresql.html
|
|
|
871 |
// - http://www.xach.com/aolserver/mysql-to-postgresql.html
|
|
|
872 |
// - REPLACE unixtimestamp / limit / curdate
|
|
|
873 |
}
|
|
|
874 |
|
|
|
875 |
class oracle_driver extends ADriver
|
|
|
876 |
{
|
|
|
877 |
protected $iid = 0;
|
|
|
878 |
protected $aff = 0;
|
|
|
879 |
|
|
|
880 |
public function connect() {
|
|
|
881 |
$this->lnk = ($this->settings->persist) ?
|
|
|
882 |
@oci_pconnect($this->settings->username, $this->settings->password, $this->settings->servername, $this->settings->charset) :
|
|
|
883 |
@oci_connect ($this->settings->username, $this->settings->password, $this->settings->servername, $this->settings->charset);
|
|
|
884 |
if($this->lnk === false) {
|
|
|
885 |
throw new Exception('Connect error : ' . oci_error());
|
|
|
886 |
}
|
|
|
887 |
if($this->settings->timezone) {
|
|
|
888 |
$this->real_query("ALTER session SET time_zone = '" . addslashes($this->settings->timezone) . "'");
|
|
|
889 |
}
|
|
|
890 |
return true;
|
|
|
891 |
}
|
|
|
892 |
public function disconnect() {
|
|
|
893 |
if(is_resource($this->lnk)) {
|
|
|
894 |
oci_close($this->lnk);
|
|
|
895 |
}
|
|
|
896 |
}
|
|
|
897 |
public function real_query($sql) {
|
|
|
898 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
899 |
$temp = oci_parse($this->lnk, $sql);
|
|
|
900 |
if(!$temp || !oci_execute($temp)) {
|
|
|
901 |
throw new Exception('Could not execute real query : ' . oci_error($temp));
|
|
|
902 |
}
|
|
|
903 |
$this->aff = oci_num_rows($temp);
|
|
|
904 |
return $temp;
|
|
|
905 |
}
|
|
|
906 |
|
|
|
907 |
public function prepare($sql) {
|
|
|
908 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
909 |
$binder = '?';
|
|
|
910 |
if(strpos($sql, $binder) !== false && $vars !== false) {
|
|
|
911 |
$tmp = explode($this->binder, $sql);
|
|
|
912 |
$sql = $tmp[0];
|
|
|
913 |
foreach($tmp as $i => $v) {
|
|
|
914 |
$sql .= ':f' . $i;
|
|
|
915 |
if(isset($tmp[($i + 1)])) {
|
|
|
916 |
$sql .= $tmp[($i + 1)];
|
|
|
917 |
}
|
|
|
918 |
}
|
|
|
919 |
}
|
|
|
920 |
return oci_parse($this->lnk, $sql);
|
|
|
921 |
}
|
|
|
922 |
public function execute($sql, $data = array()) {
|
|
|
923 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
924 |
if(!is_array($data)) { $data = array(); }
|
|
|
925 |
$data = array_values($data);
|
|
|
926 |
foreach($data as $i => $v) {
|
|
|
927 |
switch(gettype($v)) {
|
|
|
928 |
case "boolean":
|
|
|
929 |
case "integer":
|
|
|
930 |
$data[$i] = (int)$v;
|
|
|
931 |
oci_bind_by_name($sql, 'f'.$i, $data[$i], SQLT_INT);
|
|
|
932 |
break;
|
|
|
933 |
case "array":
|
|
|
934 |
$data[$i] = implode(',',$v);
|
|
|
935 |
oci_bind_by_name($sql, 'f'.$i, $data[$i]);
|
|
|
936 |
break;
|
|
|
937 |
case "object":
|
|
|
938 |
case "resource":
|
|
|
939 |
$data[$i] = serialize($data[$i]);
|
|
|
940 |
oci_bind_by_name($sql, 'f'.$i, $data[$i]);
|
|
|
941 |
break;
|
|
|
942 |
default:
|
|
|
943 |
oci_bind_by_name($sql, 'f'.$i, $data[$i]);
|
|
|
944 |
break;
|
|
|
945 |
}
|
|
|
946 |
}
|
|
|
947 |
$temp = oci_execute($sql);
|
|
|
948 |
if(!$temp) {
|
|
|
949 |
throw new Exception('Could not execute query : ' . oci_error($sql));
|
|
|
950 |
}
|
|
|
951 |
$this->aff = oci_num_rows($sql);
|
|
|
952 |
|
|
|
953 |
/* TO DO: get iid
|
|
|
954 |
if(!$seqname) { return $this->error('INSERT_ID not supported with no sequence.'); }
|
|
|
955 |
$stm = oci_parse($this->link, 'SELECT '.strtoupper(str_replace("'",'',$seqname)).'.CURRVAL FROM DUAL');
|
|
|
956 |
oci_execute($stm, $sql);
|
|
|
957 |
$tmp = oci_fetch_array($stm);
|
|
|
958 |
$tmp = $tmp[0];
|
|
|
959 |
oci_free_statement($stm);
|
|
|
960 |
*/
|
|
|
961 |
return $sql;
|
|
|
962 |
}
|
|
|
963 |
public function nextr($result) {
|
|
|
964 |
return oci_fetch_array($result, OCI_BOTH);
|
|
|
965 |
}
|
|
|
966 |
public function seek($result, $row) {
|
|
|
967 |
$cnt = 0;
|
|
|
968 |
while($cnt < $row) {
|
|
|
969 |
if(oci_fetch_array($result, OCI_BOTH) === false) {
|
|
|
970 |
return false;
|
|
|
971 |
}
|
|
|
972 |
$cnt++;
|
|
|
973 |
}
|
|
|
974 |
return true;
|
|
|
975 |
}
|
|
|
976 |
public function nf($result) {
|
|
|
977 |
return oci_num_rows($result);
|
|
|
978 |
}
|
|
|
979 |
public function af() {
|
|
|
980 |
return $this->aff;
|
|
|
981 |
}
|
|
|
982 |
public function insert_id() {
|
|
|
983 |
return $this->iid;
|
|
|
984 |
}
|
|
|
985 |
}
|
|
|
986 |
|
|
|
987 |
class ibase_driver extends ADriver
|
|
|
988 |
{
|
|
|
989 |
protected $iid = 0;
|
|
|
990 |
protected $aff = 0;
|
|
|
991 |
public function __construct($settings) {
|
|
|
992 |
parent::__construct($settings);
|
|
|
993 |
if(!is_file($this->settings->database) && is_file('/'.$this->settings->database)) {
|
|
|
994 |
$this->settings->database = '/'.$this->settings->database;
|
|
|
995 |
}
|
|
|
996 |
$this->settings->servername = ($this->settings->servername === 'localhost' || $this->settings->servername === '127.0.0.1' || $this->settings->servername === '') ?
|
|
|
997 |
'' :
|
|
|
998 |
$this->settings->servername . ':';
|
|
|
999 |
}
|
|
|
1000 |
public function connect() {
|
|
|
1001 |
$this->lnk = ($this->settings->persist) ?
|
|
|
1002 |
@ibase_pconnect(
|
|
|
1003 |
$this->settings->servername . $this->settings->database,
|
|
|
1004 |
$this->settings->username,
|
|
|
1005 |
$this->settings->password,
|
|
|
1006 |
strtoupper($this->settings->charset)
|
|
|
1007 |
) :
|
|
|
1008 |
@ibase_connect(
|
|
|
1009 |
$this->settings->servername . $this->settings->database,
|
|
|
1010 |
$this->settings->username,
|
|
|
1011 |
$this->settings->password,
|
|
|
1012 |
strtoupper($this->settings->charset)
|
|
|
1013 |
);
|
|
|
1014 |
if($this->lnk === false) {
|
|
|
1015 |
throw new Exception('Connect error: ' . ibase_errmsg());
|
|
|
1016 |
}
|
|
|
1017 |
return true;
|
|
|
1018 |
}
|
|
|
1019 |
public function disconnect() {
|
|
|
1020 |
if(is_resource($this->lnk)) {
|
|
|
1021 |
ibase_close($this->lnk);
|
|
|
1022 |
}
|
|
|
1023 |
}
|
|
|
1024 |
|
|
|
1025 |
public function real_query($sql) {
|
|
|
1026 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
1027 |
$temp = ibase_query($sql, $this->lnk);
|
|
|
1028 |
if(!$temp) {
|
|
|
1029 |
throw new Exception('Could not execute query : ' . ibase_errmsg() . ' <'.$sql.'>');
|
|
|
1030 |
}
|
|
|
1031 |
//$this->iid = mysql_insert_id($this->lnk);
|
|
|
1032 |
$this->aff = ibase_affected_rows($this->lnk);
|
|
|
1033 |
return $temp;
|
|
|
1034 |
}
|
|
|
1035 |
public function prepare($sql) {
|
|
|
1036 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
1037 |
return ibase_prepare($this->lnk, $sql);
|
|
|
1038 |
}
|
|
|
1039 |
public function execute($sql, $data = array()) {
|
|
|
1040 |
if(!$this->is_connected()) { $this->connect(); }
|
|
|
1041 |
if(!is_array($data)) { $data = array(); }
|
|
|
1042 |
$data = array_values($data);
|
|
|
1043 |
foreach($data as $i => $v) {
|
|
|
1044 |
switch(gettype($v)) {
|
|
|
1045 |
case "boolean":
|
|
|
1046 |
case "integer":
|
|
|
1047 |
$data[$i] = (int)$v;
|
|
|
1048 |
break;
|
|
|
1049 |
case "array":
|
|
|
1050 |
$data[$i] = implode(',',$v);
|
|
|
1051 |
break;
|
|
|
1052 |
case "object":
|
|
|
1053 |
case "resource":
|
|
|
1054 |
$data[$i] = serialize($data[$i]);
|
|
|
1055 |
break;
|
|
|
1056 |
}
|
|
|
1057 |
}
|
|
|
1058 |
array_unshift($data, $sql);
|
|
|
1059 |
$temp = call_user_func_array("ibase_execute", $data);
|
|
|
1060 |
if(!$temp) {
|
|
|
1061 |
throw new Exception('Could not execute query : ' . ibase_errmsg() . ' <'.$sql.'>');
|
|
|
1062 |
}
|
|
|
1063 |
$this->aff = ibase_affected_rows($this->lnk);
|
|
|
1064 |
return $temp;
|
|
|
1065 |
}
|
|
|
1066 |
public function nextr($result) {
|
|
|
1067 |
return ibase_fetch_assoc($result, IBASE_TEXT);
|
|
|
1068 |
}
|
|
|
1069 |
public function seek($result, $row) {
|
|
|
1070 |
return false;
|
|
|
1071 |
}
|
|
|
1072 |
public function nf($result) {
|
|
|
1073 |
return false;
|
|
|
1074 |
}
|
|
|
1075 |
public function af() {
|
|
|
1076 |
return $this->aff;
|
|
|
1077 |
}
|
|
|
1078 |
public function insert_id() {
|
|
|
1079 |
return $this->iid;
|
|
|
1080 |
}
|
|
|
1081 |
}
|
|
|
1082 |
}
|