forked from arildb/tiki-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pdo.php
127 lines (107 loc) · 2.97 KB
/
Pdo.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
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id: Pdo.php 46037 2013-05-24 18:07:14Z lphuberdeau $
class TikiDb_Pdo_Result
{
var $result;
var $numrows;
function __construct ($result)
{
$this->result = &$result;
$this->numrows = count($this->result);
}
function fetchRow()
{
return is_array($this->result) ? array_shift($this->result) : 0;
}
function numRows()
{
return $this->numrows;
}
}
class TikiDb_Pdo extends TikiDb
{
private $db;
function __construct( $db ) // {{{
{
if (!$db) {
die ("Invalid db object passed to TikiDB constructor");
}
$this->db=$db;
$this->setServerType($db->getAttribute(PDO::ATTR_DRIVER_NAME));
} // }}}
function qstr( $str ) // {{{
{
return $this->db->quote($str);
} // }}}
private function _query($query, $values = null, $numrows = -1, $offset = -1) // {{{
{
global $num_queries;
$num_queries++;
$numrows = intval($numrows);
$offset = intval($offset);
if ( $query == null ) {
$query = $this->getQuery();
}
$this->convertQueryTablePrefixes($query);
if ( $offset != -1 && $numrows != -1 )
$query .= " LIMIT $numrows OFFSET $offset";
elseif ( $numrows != -1 )
$query .= " LIMIT $numrows";
$starttime=$this->startTimer();
$result = false;
if ($values) {
if ( @ $pq = $this->db->prepare($query) ) {
if (!is_array($values)) {
$values = array($values);
}
$result = $pq->execute($values);
}
} else {
$result = @ $this->db->query($query);
}
$this->stopTimer($starttime);
if ( $result === false) {
if ( !$values || ! $pq) { // Query preparation or query failed
$tmp = $this->db->errorInfo();
} else { // Prepared query failed to execute
$tmp = $pq->errorInfo();
$pq->closeCursor();
}
$this->setErrorMessage($tmp[2]);
return false;
} else {
$this->setErrorMessage("");
if (($values && !$pq->columnCount()) || (!$values && !$result->columnCount())) {
return array(); // Return empty result set for statements of manipulation
} elseif ( !$values) {
return $result->fetchAll(PDO::FETCH_ASSOC);
} else {
return $pq->fetchAll(PDO::FETCH_ASSOC);
}
}
} // }}}
function fetchAll($query = null, $values = null, $numrows = -1, $offset = -1, $reporterrors = true ) // {{{
{
$result = $this->_query($query, $values, $numrows, $offset);
if (! is_array($result) ) {
if ($reporterrors) {
$this->handleQueryError($query, $values, $result);
}
}
return $result;
} // }}}
function query($query = null, $values = null, $numrows = -1, $offset = -1, $reporterrors = true ) // {{{
{
$result = $this->_query($query, $values, $numrows, $offset);
if ( $result === false ) {
if ($reporterrors) {
$this->handleQueryError($query, $values, $result);
}
}
return new TikiDb_Pdo_Result($result);
} // }}}
}