Repository navigation
Expand file tree
/
Copy pathFBQuery.class.php
More file actions
274 lines (234 loc) · 8.87 KB
/
Copy pathFBQuery.class.php
File metadata and controls
274 lines (234 loc) · 8.87 KB
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
<?php
// Firebird Query Class
//
// Author:
// Sergey Mereutsa <serj@dqteam.com>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/*
Wrapper object for management queries to Firebird SQl server
Last revision at 2 Mar 2012 by Sergey Mereutsa <serj[at]dqteam[dot]com>.
To Do: change all internal variables to comply internal naming conversion
*/
require_once(__DIR__ . '/FBTransaction.class.php');
// Some helper classes
// class helper due the lack of enums - query parameters type (IN, OUT, BOth, NONE)
class QRPType
{
const qrNULL = 0; // no parameters for query
const qrIN = 1; // query has only IN parameters
const qrOUT = 2; // query has only OUT parameters
const qrIN_OUT = 3; // query has both IN and OUT parameters
}
class QueryParameter
{
protected $prData = NULL; // parameter data pointer
protected $prType = NULL; // field type (SQL type)
protected $prName = NULL; // parameter (field) name, not always available
protected $prRelation = NULL; // relation object, usually table, but can be selectable procedure
protected $prNull = false; // is it null now
public function isNull()
{
return $prNull;
}
// deprecated function
public function isBlob()
{
if ($this->prType == 'BLOB')
{
return true;
}
else
return false;
}
// deprecated, compatibility only - remove in future versions
public function asText()
{
}
}
// class implementation itself
class FBQuery
{
protected $dbHandle = NULL; // Database(s) handle for query
protected $trHandle = NULL; // Internal transaction handle for query (for lazzy programmers)
protected $qrStatementHandle = NULL; // (query) statement handle, filled by ibase_prepare
protected $qrHandle = NULL; // internal query handle, filled by last call of ibase_execute
private $qrIsPrepared = false; // is query already prepared?
private $qrIsBinded = false; // is query binded?
private $qrIsDescribed = false; // is query described?
private $qrPType = NULL; // query parameters type - can be only one from defined in QRPType
private $qrSQLText = ''; // SQL query text itself
private $qrInParamCount = 0; // input parameters count
private $qrOutParamCount = 0; // output params count
private $qrParams = array(); //!< store object parameters for input
private $qrFields = array(); //!< store object parameters for output
// Class constructor
public function __construct($sqlText = NULL)
{
$this->qrSQLText = $sqlText;
}
// Class destructor
public function __destruct()
{
self::Unprepare();
}
public function setSQL($sqlText)
{
// unprepare if prepared and cleanup all handles
self::Unprepare();
$this->qrSQLText = $sqlText;
}
// assign database and transaction for query, transaction can be omited - we will use internal instead
// not used any more, deprecated
public function Assign($FBDb, $FBTr = NULL)
{
}
// Assign Transaction for query. If transaction is not assigned - we will use our internal RO transaction
public function AssignTR(FBTransaction $FBTr)
{
$this->dbHandle = $FBTr->getDbHandle();
$this->trHandle = $FBTr->getHandle();
}
// Assign Database for query -
// not used any more, deprecated
public function AssignDB($FBDb)
{
}
//! Prepare DSQL statement - statement is allocated automatically
//! Preapare() call can be omited, statement will be prepared automatically
public function Prepare()
{
if (!$this->qrIsPrepared)
{
$this->qrStatementHandle = @ibase_prepare($this->dbHandle, $this->trHandle, $this->qrSQLText);
if (FALSE == $this->qrStatementHandle)
{
throw new Exception(ibase_errmsg(), ibase_errcode());
} else
{
$this->qrIsPrepared = true;
}
// getting info about expected parameters and doing coercion if needed
$this->qrInParamCount = @ibase_num_params($this->qrStatementHandle);
// getting info about fields (output)
$this->qrOutParamCount = @ibase_num_fields($this->qrStatementHandle);
}
}
//! Execute SQL query
public function Execute($sqlParams = array(), $fetchOne = false)
{
// ToDo: check params count
// check if statement was prepared before
if (!$this->qrIsPrepared)
{
self::Prepare();
}
if (is_scalar($sqlParams)) // convert scalar value to an array
{
$sqlParams = array($sqlParams);
}
$this->qrParams = $sqlParams; // saving query parameters data
array_unshift($sqlParams, $this->qrStatementHandle);
$this->qrHandle = @call_user_func_array('ibase_execute', $sqlParams);
if (FALSE === $this->qrHandle)
{
throw new Exception(ibase_errmsg(), ibase_errcode());
}
if (TRUE === $this->qrHandle) // query succesful, but not returned any row
{
$res = array();
for ($i = 0; $i < $this->qrOutParamCount; $i++)
{
$this->qrFields[$i] = NULL;
}
return $this->qrFields;
}
// Check if we need to fetch one record from cursor
if ($fetchOne)
{
$res = @ibase_fetch_row($this->qrHandle);
return $res;
}
}
//! Fetch row from opened cursor as number-indexed array a[0]...a[n]
//! fetchFlag is a combination of the constants IBASE_TEXT and IBASE_UNIXTIME ORed together.
//! Passing IBASE_TEXT will cause this function to return BLOB contents instead of BLOB ids.
//! Passing IBASE_UNIXTIME will cause this function to return date/time values as Unix timestamps
//! instead of as formatted strings.
public function Fetch($fetchFlag = 0)
{
@$res = @ibase_fetch_row($this->qrHandle, $fetchFlag); // inhibit PHP warnings
if ((FALSE === $res) && (ibase_errcode() != FALSE)) // failed to execute or fetch
{
throw new Exception(ibase_errmsg(), ibase_errcode());
}
return $res;
}
//! Fetch row from opened cursor as an associative (hash) array a['F1']...a['Fn']
public function FetchHashed($fetchFlag = 0)
{
@$res = ibase_fetch_assoc($this->qrHandle, $fetchFlag);
if ((FALSE === $res) && (ibase_errcode() != FALSE)) // failed to execute or fetch
{
throw new Exception(ibase_errmsg(), ibase_errcode());
}
return $res;
}
// Close opened cursor, but do not unprepare statement
public function Close()
{
if ($this->qrHandle) // if resultset is not empty
{
if (FALSE === @ibase_free_result($this->qrHandle))
{
throw new Exception(ibase_errmsg(), ibase_errcode());
}
$this->qrHandle = NULL;
}
}
// Drop opened cursor and unprepare statement
public function Drop()
{
self::Unprepare();
}
//! Free resultset and unprepare statement
public function Unprepare()
{
if ($this->qrIsPrepared)
{
if (TRUE === $this->qrHandle) // if resultset is not empty
{
if (FALSE === @ibase_free_result($this->qrHandle))
{
throw new Exception(ibase_errmsg(), ibase_errcode());
}
}
if (FALSE === @ibase_free_query($this->qrStatementHandle)) // if call was not successful...
{
throw new Exception(ibase_errmsg(), ibase_errcode());
}
$this->qrIsPrepared = false;
$this->qrIsBinded = false;
$this->qrIsDescribed = false;
$this->qrHandle = NULL;
}
}
}
?>