forked from taq/pdooci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatement.php
632 lines (592 loc) · 16.1 KB
/
statement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?php
/**
* PDOCI
*
* PHP version 5.3
*
* @category PDOOCI
* @package PDOOCI
* @author Eustáquio Rangel <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @link http://github.com/taq/pdooci
*/
namespace PDOOCI;
/**
* State,emt class of PDOOCI
*
* PHP version 5.3
*
* @category Statement
* @package PDOOCI
* @author Eustáquio Rangel <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @link http://github.com/taq/pdooci
*/
class PDOOCIStatement implements \Iterator
{
private $_pdooci = null;
private $_con = null;
private $_statement = null;
private $_stmt = null;
private $_fetch_sty = null;
private $_current = null;
private $_pos = 0;
private $_binds = array();
public $queryString= "";
/**
* Constructor
*
* @param resource $pdooci PDOOCI connection
* @param string $statement sql statement
*
* @return PDOOCI\Statement $statement created
*/
public function __construct($pdooci, $statement)
{
try {
$this->_pdooci = $pdooci;
$this->_con = $pdooci->getConnection();
$this->_statement = PDOOCIStatement::insertMarks($statement);
$this->_stmt = \oci_parse($this->_con, $this->_statement);
$this->_fetch_sty = \PDO::FETCH_BOTH;
$this->queryString = $this->_statement;
} catch (Exception $e) {
throw new \PDOException($e->getMessage());
}
}
/**
* Binds a value
*
* @param mixed $param param (column)
* @param mixed $value value for param
* @param mixed $type optional data type
*
* @return bool bound
*/
public function bindValue($param, $value, $type=null)
{
$ok = false;
try {
$param = $this->_getBindVar($param);
$ok = \oci_bind_by_name($this->_stmt, $param, $value);
$this->_binds[$param] = $value;
} catch (Exception $e) {
throw new \PDOException($e->getMessage());
}
return $ok;
}
/**
* Binds a param
*
* @param mixed $param param (column)
* @param mixed &$value value for param
* @param mixed $type optional data type
* @param mixed $leng optional length
*
* @return bool bound
*/
public function bindParam($param, &$value, $type=null, $leng=null)
{
$ok = false;
try {
$param = $this->_getBindVar($param);
$ok = \oci_bind_by_name($this->_stmt, $param, $value);
$this->_binds[$param] = $value;
} catch (Exception $e) {
throw new \PDOException($e->getMessage());
}
return $ok;
}
/**
* Get the variable name for binding
*
* @param mixed $val variable value
*
* @return string corrent name for binding
*/
private function _getBindVar($val)
{
if (preg_match('/^\d+$/', $val)) {
$val = ":pdooci_m".(intval($val)-1);
}
return $val;
}
/**
* Execute statement
*
* @param mixed $values optional values
*
* @return this object
*/
public function execute($values=null)
{
$ok = false;
set_error_handler(array($this->_pdooci,"errorHandler"));
try {
$this->_pdooci->getAutoCommit();
$auto = $this->_pdooci->getAutoCommit() ? \OCI_COMMIT_ON_SUCCESS : \OCI_NO_AUTO_COMMIT;
if ($values && sizeof($values)>0) {
foreach ($values as $key => $val) {
$parm = $key;
if (preg_match('/^\d+$/', $key)) {
$parm ++;
}
$this->bindValue($parm, $values[$key]);
$this->_pdooci->setError();
}
}
$ok = \oci_execute($this->_stmt, $auto);
if (!$ok) {
$this->_pdooci->setError($this->_stmt);
$error = $this->_pdooci->errorInfo();
throw new \PDOException($error[2]);
}
} catch (Exception $e) {
throw new \PDOException($e->getMessage());
}
restore_error_handler();
return $ok;
}
/**
* Get the number of affected rows
*
* @return int number of rows
*/
public function rowCount()
{
set_error_handler(array($this->_pdooci,"errorHandler"));
$rows = null;
try {
$rows = \oci_num_rows($this->_stmt);
} catch (\Exception $e) {
throw new \PDOException($e->getMessage());
}
restore_error_handler();
return $rows;
}
/**
* Close the current cursor
*
* @return null
*/
public function closeCursor()
{
set_error_handler(array($this->_pdooci,"errorHandler"));
try {
\oci_free_statement($this->_stmt);
} catch (\Exception $e) {
throw new \PDOException($e->getMessage());
}
restore_error_handler();
$this->_stmt = null;
}
/**
* Fetch a value
*
* @param int $style to fetch values
*
* @return mixed
*/
public function fetch($style=null)
{
set_error_handler(array($this->_pdooci,"errorHandler"));
try {
$style = !$style ? $this->_fetch_sty : $style;
$this->_fetch_sty = $style;
$rst = null;
switch ($style)
{
case \PDO::FETCH_BOTH:
case \PDO::FETCH_BOUND:
$rst = \oci_fetch_array($this->_stmt, \OCI_BOTH + \OCI_RETURN_NULLS);
break;
case \PDO::FETCH_ASSOC:
$rst = \oci_fetch_array($this->_stmt, \OCI_ASSOC + \OCI_RETURN_NULLS);
break;
case \PDO::FETCH_NUM:
$rst = \oci_fetch_array($this->_stmt, \OCI_NUM + OCI_RETURN_NULLS);
break;
case \PDO::FETCH_OBJ:
$rst = \oci_fetch_object($this->_stmt);
break;
}
$this->_current = $rst;
$this->_checkBinds();
} catch (\Exception $e) {
throw new \PDOException($e->getMessage());
}
restore_error_handler();
return $rst;
}
/**
* Fetch all
*
* @param mixed $style fetch style
* @param mixed $argument fetch argument
*
* @return mixed results
*/
public function fetchAll($style=null, $argument=null)
{
$style = is_null($style) ? \PDO::FETCH_BOTH : $style;
$rst = null;
try {
switch ($style)
{
case \PDO::FETCH_ASSOC:
\oci_fetch_all($this->_stmt, $rst, 0, -1, \OCI_FETCHSTATEMENT_BY_ROW + \OCI_ASSOC);
break;
case \PDO::FETCH_BOTH:
\oci_fetch_all($this->_stmt, $rst, 0, -1, \OCI_FETCHSTATEMENT_BY_ROW + \OCI_NUM + \OCI_ASSOC);
break;
case \PDO::FETCH_COLUMN:
\oci_fetch_all($this->_stmt, $rst, 0, -1, \OCI_FETCHSTATEMENT_BY_ROW + \OCI_NUM);
$rst = array_map(
function ($vals) use ($argument) {
return $vals[intval($argument)];
}, $rst
);
break;
case \PDO::FETCH_COLUMN|\PDO::FETCH_GROUP:
\oci_fetch_all($this->_stmt, $rst, 0, -1, \OCI_FETCHSTATEMENT_BY_ROW + \OCI_NUM);
$temp = array();
foreach ($rst as $key => $value) {
if (!array_key_exists($value[0], $temp)) {
$temp[$value[0]] = array();
}
array_push($temp[$value[0]], $value[1]);
}
$rst = $temp;
break;
case \PDO::FETCH_CLASS:
\oci_fetch_all($this->_stmt, $rst, 0, -1, \OCI_FETCHSTATEMENT_BY_ROW + \OCI_ASSOC);
$temp = array();
foreach ($rst as $idx => $data) {
array_push($temp, $this->_createObjectFromData($argument, $data));
}
$rst = $temp;
break;
case \PDO::FETCH_FUNC:
if (!function_exists($argument)) {
throw new \PDOException("Function $argument does not exists");
}
$ref = new \ReflectionFunction($argument);
$args = $ref->getNumberOfParameters();
if ($args<1) {
throw new \PDOException("Function $argument can't receive parameters");
}
\oci_fetch_all($this->_stmt, $rst, 0, -1, \OCI_FETCHSTATEMENT_BY_ROW + \OCI_NUM);
foreach ($rst as $idx => $value) {
$temp = array();
foreach ($value as $key => $data) {
array_push($temp, $data);
}
call_user_func_array($argument, $temp);
}
break;
}
} catch (Exception $e) {
throw new \PDOException($e->getMessage());
}
return $rst;
}
/**
* Fetch column
*
* @param int $colnum optional column number
*
* @return mixed column value
*/
public function fetchColumn($colnum=0)
{
$rst = $this->fetch(\PDO::FETCH_NUM);
return $rst[$colnum];
}
/**
* Fetch data and create an object
*
* @param string $name class name
*
* @return mixed object
*/
public function fetchObject($name='stdClass')
{
try {
$data = $this->fetch(\PDO::FETCH_ASSOC);
return $this->_createObjectFromData($name, $data);
} catch (\Exception $e) {
return null;
}
}
/**
* Create a new object from data
*
* @param string $name class name
* @param mixed $data data to use on class
*
* @return new object
*/
private function _createObjectFromData($name, $data)
{
try {
$cls = new $name();
foreach ($data as $key => $value) {
if ($name !== 'stdClass' && !array_key_exists(strtolower($key), get_object_vars($cls))) {
var_dump(get_object_vars($cls));
continue;
}
$key = strtolower($key);
$cls->$key = $value;
}
return $cls;
} catch (Exception $e) {
return null;
}
}
/**
* Convert a query to use bind marks
*
* @param string $query to insert bind marks
*
* @return query with bind marks
*/
public static function insertMarks($query)
{
preg_match_all('/\?/', $query, $marks);
if (sizeof($marks[0])<1) {
return $query;
}
foreach ($marks[0] as $idx => $mark) {
$query = preg_replace("/\?/", ":pdooci_m$idx", $query, 1);
}
return $query;
}
/**
* Return the current statement
*
* @return string statement
*/
public function getStatement()
{
return $this->_statement;
}
/**
* Return the current value
*
* @return null
*/
public function current()
{
if (!$this->_current) {
$this->next();
if (!$this->_current) {
$this->_pos = -1;
$this->closeCursor();
}
}
return $this->_current;
}
/**
* Return the current key/position
*
* @return null
*/
public function key()
{
return $this->_pos;
}
/**
* Return the next value
*
* @return null
*/
public function next()
{
$this->_current = $this->fetch(\PDO::FETCH_ASSOC);
if (!$this->_current) {
$this->_pos = -1;
}
$this->_checkBinds();
}
/**
* Rewind
*
* @return null
*/
public function rewind()
{
$this->_pos = 0;
}
/**
* Check if the current value is valid
*
* @return null
*/
public function valid()
{
$valid = $this->_pos >= 0;
return $valid;
}
/**
* Set the fetch mode
*
* @param int $mode fetch mode
* @param mixed $p1 first optional parameter
* @param mixed $p2 second optional parameter
*
* @return null
*/
public function setFetchMode($mode, $p1=null, $p2=null)
{
$this->_fetch_sty = $mode;
}
/**
* Return the fetch mode
*
* @return int mode
*/
public function getFetchMode()
{
return $this->_fetch_sty;
}
/**
* Bind column
*
* @param mixed $column as index (1-based) or name
* @param mixed &$param variable
* @param int $type type
* @param int $maxlen max length
* @param mixed $driver data
*
* @return bool if was bound
*/
public function bindColumn($column, &$param, $type=null, $maxlen=null, $driver=null)
{
$column = is_numeric($column) ? $column : strtoupper($column);
$this->_binds[$column] = &$param;
}
/**
* Check what binds are needed
*
* @return null
*/
private function _checkBinds()
{
if ($this->_fetch_sty!=\PDO::FETCH_BOUND) {
return;
}
foreach ($this->_binds as $key => &$value) {
if (is_numeric($key)) {
$key --;
} else {
$key = strtoupper($key);
}
if (!array_key_exists($key, $this->_current)) {
continue;
}
$value = $this->_current[$key];
}
}
/**
* Column count
*
* @return int column count or zero if not executed
*/
public function columnCount()
{
if (!$this->_stmt) {
return 0;
}
try {
return \oci_num_fields($this->_stmt);
} catch (Exception $e) {
throw new \PDOException($e->getMessage());
}
return 0;
}
/**
* Debug dump params
*
* @return string params
*/
public function debugDumpParams()
{
$str = "SQL: [".strlen($this->_statement)."] ".$this->_statement."\n";
$str .= "Params: ".sizeof($this->_binds)."\n";
foreach ($this->_binds as $key => $value) {
$str .= "Key: Name: [".strlen($key)."] $key\n";
$str .= "name=[".strlen($key)."] \"$key\"\n";
$str .= "is_param=1\n";
}
echo substr($str, 0, strlen($str)-1);
}
/**
* Return error code
*
* @return mixed error code
*/
public function errorCode()
{
return $this->_pdooci->errorCode();
}
/**
* Return error info
*
* @return mixed error info
*/
public function errorInfo()
{
return $this->_pdooci->errorInfo();
}
/**
* Set an attribute
*
* @param int $attr attribute
* @param mixed $value value
*
* @return true if setted
*/
public function setAttribute($attr, $value)
{
// nothing to see here
}
/**
* Get an attribute
*
* @param int $attr attribute
*
* @return mixed value
*/
public function getAttribute($attr)
{
// nothing to see here
}
/**
* Get column meta data
*
* @param int $colnum column number
*
* @return mixed column meta data
*/
public function getColumnMeta($colnum=0)
{
if (!$this->_stmt) {
return null;
}
$name = \oci_field_name($this->_stmt, $colnum+1);
$len = \oci_field_size($this->_stmt, $colnum+1);
$prec = \oci_field_scale($this->_stmt, $colnum+1);
$type = \oci_field_type($this->_stmt, $colnum+1);
return array("name"=>$name, "len"=>$len, "precision"=>$prec, "driver:decl_type"=>$type);
}
/**
* Dummy method for nextRowSet
*
* @return bool
*/
public function nextRowSet()
{
// TODO: insert some code here if needed
}
}
?>