| 1 |
efrain |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Firebird driver.
|
|
|
4 |
*
|
|
|
5 |
* Requires firebird client. Works on Windows and Unix.
|
|
|
6 |
*
|
|
|
7 |
* This file is part of ADOdb, a Database Abstraction Layer library for PHP.
|
|
|
8 |
*
|
|
|
9 |
* @package ADOdb
|
|
|
10 |
* @link https://adodb.org Project's web site and documentation
|
|
|
11 |
* @link https://github.com/ADOdb/ADOdb Source code and issue tracker
|
|
|
12 |
*
|
|
|
13 |
* The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
|
|
|
14 |
* and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
|
|
|
15 |
* any later version. This means you can use it in proprietary products.
|
|
|
16 |
* See the LICENSE.md file distributed with this source code for details.
|
|
|
17 |
* @license BSD-3-Clause
|
|
|
18 |
* @license LGPL-2.1-or-later
|
|
|
19 |
*
|
|
|
20 |
* @copyright 2000-2013 John Lim
|
|
|
21 |
* @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
|
|
|
22 |
*
|
|
|
23 |
* Driver was cloned from Interbase, so there's quite a lot of duplicated code
|
|
|
24 |
* @noinspection DuplicatedCode
|
|
|
25 |
* @noinspection PhpUnused
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
// security - hide paths
|
|
|
29 |
if (!defined('ADODB_DIR')) die();
|
|
|
30 |
|
|
|
31 |
class ADODB_firebird extends ADOConnection {
|
|
|
32 |
var $databaseType = "firebird";
|
|
|
33 |
var $dataProvider = "firebird";
|
|
|
34 |
var $replaceQuote = "''"; // string to use to replace quotes
|
|
|
35 |
var $fbird_datefmt = '%Y-%m-%d'; // For hours,mins,secs change to '%Y-%m-%d %H:%M:%S';
|
|
|
36 |
var $fmtDate = "'Y-m-d'";
|
|
|
37 |
var $fbird_timestampfmt = "%Y-%m-%d %H:%M:%S";
|
|
|
38 |
var $fbird_timefmt = "%H:%M:%S";
|
|
|
39 |
var $fmtTimeStamp = "'Y-m-d, H:i:s'";
|
|
|
40 |
var $concat_operator='||';
|
|
|
41 |
var $_transactionID;
|
|
|
42 |
|
|
|
43 |
public $metaTablesSQL = "SELECT LOWER(rdb\$relation_name) FROM rdb\$relations";
|
|
|
44 |
//OPN STUFF start
|
|
|
45 |
|
|
|
46 |
var $metaColumnsSQL = "select lower(a.rdb\$field_name), a.rdb\$null_flag, a.rdb\$default_source, b.rdb\$field_length, b.rdb\$field_scale, b.rdb\$field_sub_type, b.rdb\$field_precision, b.rdb\$field_type from rdb\$relation_fields a, rdb\$fields b where a.rdb\$field_source = b.rdb\$field_name and a.rdb\$relation_name = '%s' order by a.rdb\$field_position asc";
|
|
|
47 |
//OPN STUFF end
|
|
|
48 |
|
|
|
49 |
public $_genSeqSQL = "CREATE SEQUENCE %s START WITH %s";
|
|
|
50 |
|
|
|
51 |
public $_dropSeqSQL = "DROP SEQUENCE %s";
|
|
|
52 |
|
|
|
53 |
var $hasGenID = true;
|
|
|
54 |
var $_bindInputArray = true;
|
|
|
55 |
var $sysDate = "cast('TODAY' as timestamp)";
|
|
|
56 |
var $sysTimeStamp = "CURRENT_TIMESTAMP"; //"cast('NOW' as timestamp)";
|
|
|
57 |
var $ansiOuter = true;
|
|
|
58 |
var $hasAffectedRows = true;
|
|
|
59 |
var $poorAffectedRows = false;
|
|
|
60 |
var $blobEncodeType = 'C';
|
|
|
61 |
/*
|
|
|
62 |
* firebird custom optionally specifies the user role
|
|
|
63 |
*/
|
|
|
64 |
public $role = false;
|
|
|
65 |
/*
|
|
|
66 |
* firebird custom optionally specifies the connection buffers
|
|
|
67 |
*/
|
|
|
68 |
public $buffers = 0;
|
|
|
69 |
|
|
|
70 |
/*
|
|
|
71 |
* firebird custom optionally specifies database dialect
|
|
|
72 |
*/
|
|
|
73 |
public $dialect = 3;
|
|
|
74 |
|
|
|
75 |
var $nameQuote = ''; /// string to use to quote identifiers and names
|
|
|
76 |
|
|
|
77 |
function __construct()
|
|
|
78 |
{
|
|
|
79 |
parent::__construct();
|
|
|
80 |
$this->setTransactionMode('');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Sets the isolation level of a transaction.
|
|
|
85 |
*
|
|
|
86 |
* The default behavior is a more practical IBASE_WAIT | IBASE_REC_VERSION | IBASE_COMMITTED
|
|
|
87 |
* instead of IBASE_DEFAULT
|
|
|
88 |
*
|
|
|
89 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:settransactionmode
|
|
|
90 |
*
|
|
|
91 |
* @param string $transaction_mode The transaction mode to set.
|
|
|
92 |
*
|
|
|
93 |
* @return void
|
|
|
94 |
*/
|
|
|
95 |
public function setTransactionMode($transaction_mode)
|
|
|
96 |
{
|
|
|
97 |
$this->_transmode = $transaction_mode;
|
|
|
98 |
|
|
|
99 |
if (empty($transaction_mode)) {
|
|
|
100 |
$this->_transmode = IBASE_WAIT | IBASE_REC_VERSION | IBASE_COMMITTED;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Connect to a database.
|
|
|
107 |
*
|
|
|
108 |
* @todo add: parameter int $port, parameter string $socket
|
|
|
109 |
*
|
|
|
110 |
* @param string|null $argHostname (Optional) The host to connect to.
|
|
|
111 |
* @param string|null $argUsername (Optional) The username to connect as.
|
|
|
112 |
* @param string|null $argPassword (Optional) The password to connect with.
|
|
|
113 |
* @param string|null $argDatabasename (Optional) The name of the database to start in when connected.
|
|
|
114 |
* @param bool $persist (Optional) Whether or not to use a persistent connection.
|
|
|
115 |
*
|
|
|
116 |
* @return bool|null True if connected successfully, false if connection failed, or null if the mysqli extension
|
|
|
117 |
* isn't currently loaded.
|
|
|
118 |
*/
|
|
|
119 |
public function _connect($argHostname, $argUsername, $argPassword, $argDatabasename,$persist=false)
|
|
|
120 |
{
|
|
|
121 |
if (!function_exists('fbird_pconnect'))
|
|
|
122 |
return null;
|
|
|
123 |
|
|
|
124 |
if ($argDatabasename)
|
|
|
125 |
$argHostname .= ':'.$argDatabasename;
|
|
|
126 |
|
|
|
127 |
$fn = ($persist) ? 'fbird_pconnect':'fbird_connect';
|
|
|
128 |
|
|
|
129 |
/*
|
|
|
130 |
* Now merge in the standard connection parameters setting
|
|
|
131 |
*/
|
|
|
132 |
foreach ($this->connectionParameters as $options)
|
|
|
133 |
{
|
|
|
134 |
foreach($options as $k=>$v)
|
|
|
135 |
{
|
|
|
136 |
switch($k){
|
|
|
137 |
case 'role':
|
|
|
138 |
$this->role = $v;
|
|
|
139 |
break;
|
|
|
140 |
case 'dialect':
|
|
|
141 |
$this->dialect = $v;
|
|
|
142 |
break;
|
|
|
143 |
case 'buffers':
|
|
|
144 |
$this->buffers = $v;
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
if ($this->role)
|
|
|
150 |
$this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
|
|
|
151 |
$this->charSet,$this->buffers,$this->dialect,$this->role);
|
|
|
152 |
else
|
|
|
153 |
$this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
|
|
|
154 |
$this->charSet,$this->buffers,$this->dialect);
|
|
|
155 |
|
|
|
156 |
if ($this->dialect == 1) { // http://www.ibphoenix.com/ibp_60_del_id_ds.html
|
|
|
157 |
$this->replaceQuote = "";
|
|
|
158 |
}
|
|
|
159 |
if ($this->_connectionID === false) {
|
|
|
160 |
$this->_handleError();
|
|
|
161 |
return false;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
ini_set("ibase.timestampformat", $this->fbird_timestampfmt);
|
|
|
165 |
ini_set("ibase.dateformat", $this->fbird_datefmt);
|
|
|
166 |
ini_set("ibase.timeformat", $this->fbird_timefmt);
|
|
|
167 |
|
|
|
168 |
return true;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Connect to a database with a persistent connection.
|
|
|
173 |
*
|
|
|
174 |
* @param string|null $argHostname The host to connect to.
|
|
|
175 |
* @param string|null $argUsername The username to connect as.
|
|
|
176 |
* @param string|null $argPassword The password to connect with.
|
|
|
177 |
* @param string|null $argDatabasename The name of the database to start in when connected.
|
|
|
178 |
*
|
|
|
179 |
* @return bool|null True if connected successfully, false if connection failed, or null if the mysqli extension
|
|
|
180 |
* isn't currently loaded.
|
|
|
181 |
*/
|
|
|
182 |
function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
|
|
|
183 |
{
|
|
|
184 |
return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename,true);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
public function metaPrimaryKeys($table,$owner_notused=false,$internalKey=false)
|
|
|
189 |
{
|
|
|
190 |
if ($internalKey) {
|
|
|
191 |
return array('RDB$DB_KEY');
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
$table = strtoupper($table);
|
|
|
195 |
|
|
|
196 |
$sql = 'SELECT S.RDB$FIELD_NAME AFIELDNAME
|
|
|
197 |
FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME
|
|
|
198 |
WHERE I.RDB$RELATION_NAME=\''.$table.'\' and I.RDB$INDEX_NAME like \'RDB$PRIMARY%\'
|
|
|
199 |
ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
|
|
|
200 |
|
|
|
201 |
$a = $this->GetCol($sql,false,true);
|
|
|
202 |
if ($a && sizeof($a)>0) return $a;
|
|
|
203 |
return false;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Get information about the current Firebird server.
|
|
|
208 |
*
|
|
|
209 |
* @return array
|
|
|
210 |
*/
|
|
|
211 |
public function serverInfo()
|
|
|
212 |
{
|
|
|
213 |
$arr['dialect'] = $this->dialect;
|
|
|
214 |
switch($arr['dialect']) {
|
|
|
215 |
case '':
|
|
|
216 |
case '1':
|
|
|
217 |
$s = 'Firebird Dialect 1';
|
|
|
218 |
break;
|
|
|
219 |
case '2':
|
|
|
220 |
$s = 'Firebird Dialect 2';
|
|
|
221 |
break;
|
|
|
222 |
default:
|
|
|
223 |
case '3':
|
|
|
224 |
$s = 'Firebird Dialect 3';
|
|
|
225 |
break;
|
|
|
226 |
}
|
|
|
227 |
$arr['version'] = ADOConnection::_findvers($s);
|
|
|
228 |
$arr['description'] = $s;
|
|
|
229 |
return $arr;
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* Begin a Transaction. Must be followed by CommitTrans() or RollbackTrans().
|
|
|
234 |
*
|
|
|
235 |
* @return bool true if succeeded or false if database does not support transactions
|
|
|
236 |
*/
|
|
|
237 |
public function beginTrans()
|
|
|
238 |
{
|
|
|
239 |
if ($this->transOff) return true;
|
|
|
240 |
$this->transCnt += 1;
|
|
|
241 |
$this->autoCommit = false;
|
|
|
242 |
/*
|
|
|
243 |
* We manage the transaction mode via fbird_trans
|
|
|
244 |
*/
|
|
|
245 |
$this->_transactionID = fbird_trans( $this->_transmode, $this->_connectionID );
|
|
|
246 |
return $this->_transactionID;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Commits a transaction.
|
|
|
252 |
*
|
|
|
253 |
* @param bool $ok false to rollback transaction, true to commit
|
|
|
254 |
*
|
|
|
255 |
* @return bool
|
|
|
256 |
*/
|
|
|
257 |
public function commitTrans($ok=true)
|
|
|
258 |
{
|
|
|
259 |
if (!$ok) {
|
|
|
260 |
return $this->RollbackTrans();
|
|
|
261 |
}
|
|
|
262 |
if ($this->transOff) {
|
|
|
263 |
return true;
|
|
|
264 |
}
|
|
|
265 |
if ($this->transCnt) {
|
|
|
266 |
$this->transCnt -= 1;
|
|
|
267 |
}
|
|
|
268 |
$ret = false;
|
|
|
269 |
$this->autoCommit = true;
|
|
|
270 |
if ($this->_transactionID) {
|
|
|
271 |
$ret = fbird_commit($this->_transactionID);
|
|
|
272 |
}
|
|
|
273 |
$this->_transactionID = false;
|
|
|
274 |
return $ret;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
function _affectedrows()
|
|
|
278 |
{
|
|
|
279 |
return fbird_affected_rows($this->_transactionID ?: $this->_connectionID);
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* Rollback a smart transaction.
|
|
|
284 |
*
|
|
|
285 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:rollbacktrans
|
|
|
286 |
*
|
|
|
287 |
* @return bool
|
|
|
288 |
*/
|
|
|
289 |
public function rollbackTrans()
|
|
|
290 |
{
|
|
|
291 |
if ($this->transOff)
|
|
|
292 |
return true;
|
|
|
293 |
if ($this->transCnt)
|
|
|
294 |
$this->transCnt -= 1;
|
|
|
295 |
|
|
|
296 |
$ret = false;
|
|
|
297 |
$this->autoCommit = true;
|
|
|
298 |
|
|
|
299 |
if ($this->_transactionID) {
|
|
|
300 |
$ret = fbird_rollback($this->_transactionID);
|
|
|
301 |
}
|
|
|
302 |
$this->_transactionID = false;
|
|
|
303 |
|
|
|
304 |
return $ret;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* Get a list of indexes on the specified table.
|
|
|
309 |
*
|
|
|
310 |
* @param string $table The name of the table to get indexes for.
|
|
|
311 |
* @param bool $primary (Optional) Whether or not to include the primary key.
|
|
|
312 |
* @param bool $owner (Optional) Unused.
|
|
|
313 |
*
|
|
|
314 |
* @return array|bool An array of the indexes, or false if the query to get the indexes failed.
|
|
|
315 |
*/
|
|
|
316 |
public function metaIndexes($table, $primary = false, $owner = false)
|
|
|
317 |
{
|
|
|
318 |
// save old fetch mode
|
|
|
319 |
global $ADODB_FETCH_MODE;
|
|
|
320 |
$save = $ADODB_FETCH_MODE;
|
|
|
321 |
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
|
|
|
322 |
|
|
|
323 |
if ($this->fetchMode !== FALSE) {
|
|
|
324 |
$savem = $this->SetFetchMode(FALSE);
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
$table = strtoupper($table);
|
|
|
328 |
$sql = "SELECT * FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = '".$table."'";
|
|
|
329 |
if (!$primary) {
|
|
|
330 |
$sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$%'";
|
|
|
331 |
} else {
|
|
|
332 |
$sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$FOREIGN%'";
|
|
|
333 |
}
|
|
|
334 |
// get index details
|
|
|
335 |
$rs = $this->execute($sql);
|
|
|
336 |
if (!is_object($rs)) {
|
|
|
337 |
// restore fetchmode
|
|
|
338 |
if (isset($savem)) {
|
|
|
339 |
$this->SetFetchMode($savem);
|
|
|
340 |
}
|
|
|
341 |
$ADODB_FETCH_MODE = $save;
|
|
|
342 |
return false;
|
|
|
343 |
}
|
|
|
344 |
$indexes = array();
|
|
|
345 |
while ($row = $rs->FetchRow()) {
|
|
|
346 |
|
|
|
347 |
$index = trim($row[0]);
|
|
|
348 |
if (!isset($indexes[$index])) {
|
|
|
349 |
if (is_null($row[3])) {
|
|
|
350 |
$row[3] = 0;
|
|
|
351 |
}
|
|
|
352 |
$indexes[$index] = array(
|
|
|
353 |
'unique' => ($row[3] == 1),
|
|
|
354 |
'columns' => array()
|
|
|
355 |
);
|
|
|
356 |
}
|
|
|
357 |
$sql = sprintf("SELECT * FROM RDB\$INDEX_SEGMENTS WHERE RDB\$INDEX_NAME = '%s' ORDER BY RDB\$FIELD_POSITION ASC",$index);
|
|
|
358 |
$rs1 = $this->execute($sql);
|
|
|
359 |
while ($row1 = $rs1->FetchRow()) {
|
|
|
360 |
$indexes[$index]['columns'][$row1[2]] = trim($row1[1]);
|
|
|
361 |
}
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
// restore fetchmode
|
|
|
365 |
if (isset($savem)) {
|
|
|
366 |
$this->SetFetchMode($savem);
|
|
|
367 |
}
|
|
|
368 |
$ADODB_FETCH_MODE = $save;
|
|
|
369 |
|
|
|
370 |
return $indexes;
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
/**
|
|
|
374 |
* Lock a table row for a duration of a transaction.
|
|
|
375 |
*
|
|
|
376 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:rowlock
|
|
|
377 |
* @link https://firebirdsql.org/refdocs/langrefupd21-notes-withlock.html
|
|
|
378 |
*
|
|
|
379 |
* @param string $table The table(s) to lock rows for.
|
|
|
380 |
* @param string $where (Optional) The WHERE clause to use to determine which rows to lock.
|
|
|
381 |
* @param string $col (Optional) The columns to select.
|
|
|
382 |
*
|
|
|
383 |
* @return bool True if the locking SQL statement executed successfully, otherwise false.
|
|
|
384 |
*/
|
|
|
385 |
public function rowLock($table,$where,$col=false)
|
|
|
386 |
{
|
|
|
387 |
if ($this->transCnt==0)
|
|
|
388 |
$this->beginTrans();
|
|
|
389 |
|
|
|
390 |
if ($where) $where = ' where '.$where;
|
|
|
391 |
$rs = $this->execute("SELECT $col FROM $table $where FOR UPDATE WITH LOCK");
|
|
|
392 |
return !empty($rs);
|
|
|
393 |
}
|
|
|
394 |
|
|
|
395 |
/**
|
|
|
396 |
* Creates a sequence in the database.
|
|
|
397 |
*
|
|
|
398 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:createsequence
|
|
|
399 |
*
|
|
|
400 |
* @param string $seqname The sequence name.
|
|
|
401 |
* @param int $startID The start id.
|
|
|
402 |
*
|
|
|
403 |
* @return ADORecordSet|bool A record set if executed successfully, otherwise false.
|
|
|
404 |
*/
|
|
|
405 |
public function createSequence($seqname='adodbseq', $startID = 1)
|
|
|
406 |
{
|
|
|
407 |
$sql = sprintf($this->_genSeqSQL,$seqname,$startID);
|
|
|
408 |
return $this->execute($sql);
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
/**
|
|
|
412 |
* A portable method of creating sequence numbers.
|
|
|
413 |
*
|
|
|
414 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:genid
|
|
|
415 |
*
|
|
|
416 |
* @param string $seqname (Optional) The name of the sequence to use.
|
|
|
417 |
* @param int $startID (Optional) The point to start at in the sequence.
|
|
|
418 |
*
|
|
|
419 |
* @return int
|
|
|
420 |
*/
|
|
|
421 |
public function genID($seqname='adodbseq',$startID=1)
|
|
|
422 |
{
|
|
|
423 |
$getnext = ("SELECT Gen_ID($seqname,1) FROM RDB\$DATABASE");
|
|
|
424 |
$rs = @$this->Execute($getnext);
|
|
|
425 |
if (!$rs) {
|
|
|
426 |
$this->Execute("CREATE SEQUENCE $seqname START WITH $startID");
|
|
|
427 |
$rs = $this->Execute($getnext);
|
|
|
428 |
}
|
|
|
429 |
if ($rs && !$rs->EOF) {
|
|
|
430 |
$this->genID = (integer) reset($rs->fields);
|
|
|
431 |
}
|
|
|
432 |
else {
|
|
|
433 |
$this->genID = 0; // false
|
|
|
434 |
}
|
|
|
435 |
|
|
|
436 |
if ($rs) {
|
|
|
437 |
$rs->Close();
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
return $this->genID;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
function selectDB($dbName)
|
|
|
444 |
{
|
|
|
445 |
return false;
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
function _handleError()
|
|
|
449 |
{
|
|
|
450 |
$this->_errorCode = fbird_errcode();
|
|
|
451 |
$this->_errorMsg = fbird_errmsg();
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
|
|
|
455 |
public function errorNo()
|
|
|
456 |
{
|
|
|
457 |
return (integer) $this->_errorCode;
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
function errorMsg()
|
|
|
461 |
{
|
|
|
462 |
return $this->_errorMsg;
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
/**
|
|
|
466 |
* Prepares an SQL statement and returns a handle to use.
|
|
|
467 |
* This is not used by bound parameters anymore
|
|
|
468 |
*
|
|
|
469 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:prepare
|
|
|
470 |
* @todo update this function to handle prepared statements correctly
|
|
|
471 |
*
|
|
|
472 |
* @param string $sql The SQL to prepare.
|
|
|
473 |
*
|
|
|
474 |
* @return bool|array The SQL that was provided and the prepared parameters,
|
|
|
475 |
* or false if the preparation fails
|
|
|
476 |
*/
|
|
|
477 |
public function prepare($sql)
|
|
|
478 |
{
|
|
|
479 |
$stmt = fbird_prepare($this->_connectionID,$sql);
|
|
|
480 |
if (!$stmt)
|
|
|
481 |
return false;
|
|
|
482 |
return array($sql,$stmt);
|
|
|
483 |
}
|
|
|
484 |
|
|
|
485 |
/**
|
|
|
486 |
* Execute a query.
|
|
|
487 |
*
|
|
|
488 |
* @param string|array $sql Query to execute.
|
|
|
489 |
* @param array $inputarr An optional array of parameters.
|
|
|
490 |
*
|
|
|
491 |
* @return object|bool Query identifier or true if execution successful, false if failed.
|
|
|
492 |
*/
|
|
|
493 |
function _query($sql, $inputarr = false)
|
|
|
494 |
{
|
|
|
495 |
if (!$this->isConnected()) {
|
|
|
496 |
return false;
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
if (!$this->autoCommit && $this->_transactionID) {
|
|
|
500 |
$conn = $this->_transactionID;
|
|
|
501 |
$docommit = false;
|
|
|
502 |
} else {
|
|
|
503 |
$conn = $this->_connectionID;
|
|
|
504 |
$docommit = true;
|
|
|
505 |
}
|
|
|
506 |
|
|
|
507 |
if (is_array($sql)) {
|
|
|
508 |
// Prepared statement
|
|
|
509 |
$fn = 'fbird_execute';
|
|
|
510 |
$args = [$sql[1]];
|
|
|
511 |
} else {
|
|
|
512 |
$fn = 'fbird_query';
|
|
|
513 |
$args = [$conn, $sql];
|
|
|
514 |
}
|
|
|
515 |
if (is_array($inputarr)) {
|
|
|
516 |
$args = array_merge($args, $inputarr);
|
|
|
517 |
}
|
|
|
518 |
$ret = call_user_func_array($fn, $args);
|
|
|
519 |
|
|
|
520 |
// fbird_query() and fbird_execute() return number of affected rows
|
|
|
521 |
// ADOConnection::_Execute() expects true for INSERT/UPDATE/DELETE
|
|
|
522 |
if (is_numeric($ret)) {
|
|
|
523 |
$ret = true;
|
|
|
524 |
}
|
|
|
525 |
|
|
|
526 |
if ($docommit && $ret === true) {
|
|
|
527 |
fbird_commit($this->_connectionID);
|
|
|
528 |
}
|
|
|
529 |
|
|
|
530 |
$this->_handleError();
|
|
|
531 |
return $ret;
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
// returns true or false
|
|
|
535 |
function _close()
|
|
|
536 |
{
|
|
|
537 |
if (!$this->autoCommit) {
|
|
|
538 |
@fbird_rollback($this->_connectionID);
|
|
|
539 |
}
|
|
|
540 |
return @fbird_close($this->_connectionID);
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
//OPN STUFF start
|
|
|
544 |
function _ConvertFieldType(&$fld, $ftype, $flen, $fscale, $fsubtype, $fprecision, $dialect3)
|
|
|
545 |
{
|
|
|
546 |
$fscale = abs($fscale);
|
|
|
547 |
$fld->max_length = $flen;
|
|
|
548 |
$fld->scale = null;
|
|
|
549 |
switch($ftype){
|
|
|
550 |
case 7:
|
|
|
551 |
case 8:
|
|
|
552 |
if ($dialect3) {
|
|
|
553 |
switch($fsubtype){
|
|
|
554 |
case 0:
|
|
|
555 |
$fld->type = ($ftype == 7 ? 'smallint' : 'integer');
|
|
|
556 |
break;
|
|
|
557 |
case 1:
|
|
|
558 |
$fld->type = 'numeric';
|
|
|
559 |
$fld->max_length = $fprecision;
|
|
|
560 |
$fld->scale = $fscale;
|
|
|
561 |
break;
|
|
|
562 |
case 2:
|
|
|
563 |
$fld->type = 'decimal';
|
|
|
564 |
$fld->max_length = $fprecision;
|
|
|
565 |
$fld->scale = $fscale;
|
|
|
566 |
break;
|
|
|
567 |
} // switch
|
|
|
568 |
} else {
|
|
|
569 |
if ($fscale !=0) {
|
|
|
570 |
$fld->type = 'decimal';
|
|
|
571 |
$fld->scale = $fscale;
|
|
|
572 |
$fld->max_length = ($ftype == 7 ? 4 : 9);
|
|
|
573 |
} else {
|
|
|
574 |
$fld->type = ($ftype == 7 ? 'smallint' : 'integer');
|
|
|
575 |
}
|
|
|
576 |
}
|
|
|
577 |
break;
|
|
|
578 |
case 16:
|
|
|
579 |
if ($dialect3) {
|
|
|
580 |
switch($fsubtype){
|
|
|
581 |
case 0:
|
|
|
582 |
$fld->type = 'decimal';
|
|
|
583 |
$fld->max_length = 18;
|
|
|
584 |
$fld->scale = 0;
|
|
|
585 |
break;
|
|
|
586 |
case 1:
|
|
|
587 |
$fld->type = 'numeric';
|
|
|
588 |
$fld->max_length = $fprecision;
|
|
|
589 |
$fld->scale = $fscale;
|
|
|
590 |
break;
|
|
|
591 |
case 2:
|
|
|
592 |
$fld->type = 'decimal';
|
|
|
593 |
$fld->max_length = $fprecision;
|
|
|
594 |
$fld->scale = $fscale;
|
|
|
595 |
break;
|
|
|
596 |
} // switch
|
|
|
597 |
}
|
|
|
598 |
break;
|
|
|
599 |
case 10:
|
|
|
600 |
$fld->type = 'float';
|
|
|
601 |
break;
|
|
|
602 |
case 14:
|
|
|
603 |
$fld->type = 'char';
|
|
|
604 |
break;
|
|
|
605 |
case 27:
|
|
|
606 |
if ($fscale !=0) {
|
|
|
607 |
$fld->type = 'decimal';
|
|
|
608 |
$fld->max_length = 15;
|
|
|
609 |
$fld->scale = 5;
|
|
|
610 |
} else {
|
|
|
611 |
$fld->type = 'double';
|
|
|
612 |
}
|
|
|
613 |
break;
|
|
|
614 |
case 35:
|
|
|
615 |
if ($dialect3) {
|
|
|
616 |
$fld->type = 'timestamp';
|
|
|
617 |
} else {
|
|
|
618 |
$fld->type = 'date';
|
|
|
619 |
}
|
|
|
620 |
break;
|
|
|
621 |
case 12:
|
|
|
622 |
$fld->type = 'date';
|
|
|
623 |
break;
|
|
|
624 |
case 13:
|
|
|
625 |
$fld->type = 'time';
|
|
|
626 |
break;
|
|
|
627 |
case 37:
|
|
|
628 |
$fld->type = 'varchar';
|
|
|
629 |
break;
|
|
|
630 |
case 40:
|
|
|
631 |
$fld->type = 'cstring';
|
|
|
632 |
break;
|
|
|
633 |
case 261:
|
|
|
634 |
$fld->type = 'blob';
|
|
|
635 |
$fld->max_length = -1;
|
|
|
636 |
break;
|
|
|
637 |
} // switch
|
|
|
638 |
}
|
|
|
639 |
//OPN STUFF end
|
|
|
640 |
|
|
|
641 |
/**
|
|
|
642 |
* Return an array of information about a table's columns.
|
|
|
643 |
*
|
|
|
644 |
* @param string $table The name of the table to get the column info for.
|
|
|
645 |
* @param bool $normalize (Optional) Unused.
|
|
|
646 |
*
|
|
|
647 |
* @return ADOFieldObject[]|bool An array of info for each column,
|
|
|
648 |
* or false if it could not determine the info.
|
|
|
649 |
*/
|
|
|
650 |
public function metaColumns($table, $normalize = true)
|
|
|
651 |
{
|
|
|
652 |
|
|
|
653 |
global $ADODB_FETCH_MODE;
|
|
|
654 |
|
|
|
655 |
$save = $ADODB_FETCH_MODE;
|
|
|
656 |
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
|
|
|
657 |
|
|
|
658 |
$rs = $this->execute(sprintf($this->metaColumnsSQL,strtoupper($table)));
|
|
|
659 |
|
|
|
660 |
$ADODB_FETCH_MODE = $save;
|
|
|
661 |
|
|
|
662 |
if ($rs === false) {
|
|
|
663 |
return false;
|
|
|
664 |
}
|
|
|
665 |
|
|
|
666 |
$retarr = array();
|
|
|
667 |
//OPN STUFF start
|
|
|
668 |
$dialect3 = $this->dialect == 3;
|
|
|
669 |
//OPN STUFF end
|
|
|
670 |
while (!$rs->EOF) { //print_r($rs->fields);
|
|
|
671 |
$fld = new ADOFieldObject();
|
|
|
672 |
$fld->name = trim($rs->fields[0]);
|
|
|
673 |
//OPN STUFF start
|
|
|
674 |
//print_r($rs->fields);
|
|
|
675 |
$this->_ConvertFieldType(
|
|
|
676 |
$fld, $rs->fields[7], $rs->fields[3], $rs->fields[4], $rs->fields[5], $rs->fields[6], $dialect3);
|
|
|
677 |
if (isset($rs->fields[1]) && $rs->fields[1]) {
|
|
|
678 |
$fld->not_null = true;
|
|
|
679 |
}
|
|
|
680 |
if (isset($rs->fields[2])) {
|
|
|
681 |
|
|
|
682 |
$fld->has_default = true;
|
|
|
683 |
$d = substr($rs->fields[2],strlen('default '));
|
|
|
684 |
switch ($fld->type) {
|
|
|
685 |
case 'smallint':
|
|
|
686 |
case 'integer':
|
|
|
687 |
$fld->default_value = (int)$d;
|
|
|
688 |
break;
|
|
|
689 |
case 'char':
|
|
|
690 |
case 'blob':
|
|
|
691 |
case 'text':
|
|
|
692 |
case 'varchar':
|
|
|
693 |
$fld->default_value = (string)substr($d, 1, strlen($d) - 2);
|
|
|
694 |
break;
|
|
|
695 |
case 'double':
|
|
|
696 |
case 'float':
|
|
|
697 |
$fld->default_value = (float)$d;
|
|
|
698 |
break;
|
|
|
699 |
default:
|
|
|
700 |
$fld->default_value = $d;
|
|
|
701 |
break;
|
|
|
702 |
}
|
|
|
703 |
// case 35:$tt = 'TIMESTAMP'; break;
|
|
|
704 |
}
|
|
|
705 |
if ((isset($rs->fields[5])) && ($fld->type == 'blob')) {
|
|
|
706 |
$fld->sub_type = $rs->fields[5];
|
|
|
707 |
} else {
|
|
|
708 |
$fld->sub_type = null;
|
|
|
709 |
}
|
|
|
710 |
//OPN STUFF end
|
|
|
711 |
if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
|
|
|
712 |
else $retarr[strtoupper($fld->name)] = $fld;
|
|
|
713 |
|
|
|
714 |
$rs->MoveNext();
|
|
|
715 |
}
|
|
|
716 |
$rs->Close();
|
|
|
717 |
if ( empty($retarr))
|
|
|
718 |
return false;
|
|
|
719 |
else return $retarr;
|
|
|
720 |
}
|
|
|
721 |
|
|
|
722 |
/**
|
|
|
723 |
* Retrieves a list of tables based on given criteria
|
|
|
724 |
*
|
|
|
725 |
* @param string|bool $ttype (Optional) Table type = 'TABLE', 'VIEW' or false=both (default)
|
|
|
726 |
* @param string|bool $showSchema (Optional) schema name, false = current schema (default)
|
|
|
727 |
* @param string|bool $mask (Optional) filters the table by name
|
|
|
728 |
*
|
|
|
729 |
* @return array list of tables
|
|
|
730 |
*/
|
|
|
731 |
public function metaTables($ttype = false, $showSchema = false, $mask = false)
|
|
|
732 |
{
|
|
|
733 |
$save = $this->metaTablesSQL;
|
|
|
734 |
if (!$showSchema) {
|
|
|
735 |
$this->metaTablesSQL .= " WHERE (rdb\$relation_name NOT LIKE 'RDB\$%' AND rdb\$relation_name NOT LIKE 'MON\$%' AND rdb\$relation_name NOT LIKE 'SEC\$%')";
|
|
|
736 |
} elseif (is_string($showSchema)) {
|
|
|
737 |
$this->metaTablesSQL .= $this->qstr($showSchema);
|
|
|
738 |
}
|
|
|
739 |
|
|
|
740 |
if ($mask) {
|
|
|
741 |
$mask = $this->qstr($mask);
|
|
|
742 |
$this->metaTablesSQL .= " AND table_name LIKE $mask";
|
|
|
743 |
}
|
|
|
744 |
$ret = ADOConnection::metaTables($ttype,$showSchema);
|
|
|
745 |
|
|
|
746 |
$this->metaTablesSQL = $save;
|
|
|
747 |
return $ret;
|
|
|
748 |
}
|
|
|
749 |
|
|
|
750 |
/**
|
|
|
751 |
* Encodes a blob, then assigns an id ready to be used
|
|
|
752 |
*
|
|
|
753 |
* @param string $blob The blob to be encoded
|
|
|
754 |
*
|
|
|
755 |
* @return bool success
|
|
|
756 |
*/
|
|
|
757 |
public function blobEncode( $blob )
|
|
|
758 |
{
|
|
|
759 |
$blobid = fbird_blob_create( $this->_connectionID);
|
|
|
760 |
fbird_blob_add( $blobid, $blob );
|
|
|
761 |
return fbird_blob_close( $blobid );
|
|
|
762 |
}
|
|
|
763 |
|
|
|
764 |
/**
|
|
|
765 |
* Manually decode a blob
|
|
|
766 |
*
|
|
|
767 |
* since we auto-decode all blob's since 2.42,
|
|
|
768 |
* BlobDecode should not do any transforms
|
|
|
769 |
*
|
|
|
770 |
* @param string $blob
|
|
|
771 |
*
|
|
|
772 |
* @return string the same blob
|
|
|
773 |
*/
|
|
|
774 |
public function blobDecode($blob)
|
|
|
775 |
{
|
|
|
776 |
return $blob;
|
|
|
777 |
}
|
|
|
778 |
|
|
|
779 |
/**
|
|
|
780 |
* Auto function called on read of blob to decode
|
|
|
781 |
*
|
|
|
782 |
* @param string $blob Value to decode
|
|
|
783 |
*
|
|
|
784 |
* @return string Decoded blob
|
|
|
785 |
*/
|
|
|
786 |
public function _blobDecode($blob)
|
|
|
787 |
{
|
|
|
788 |
if ($blob === null) {
|
|
|
789 |
return '';
|
|
|
790 |
}
|
|
|
791 |
|
|
|
792 |
$blob_data = fbird_blob_info($this->_connectionID, $blob);
|
|
|
793 |
$blobId = fbird_blob_open($this->_connectionID, $blob);
|
|
|
794 |
|
|
|
795 |
if ($blob_data[0] > $this->maxblobsize) {
|
|
|
796 |
$realBlob = fbird_blob_get($blobId, $this->maxblobsize);
|
|
|
797 |
while ($string = fbird_blob_get($blobId, 8192)) {
|
|
|
798 |
$realBlob .= $string;
|
|
|
799 |
}
|
|
|
800 |
} else {
|
|
|
801 |
$realBlob = fbird_blob_get($blobId, $blob_data[0]);
|
|
|
802 |
}
|
|
|
803 |
|
|
|
804 |
fbird_blob_close($blobId);
|
|
|
805 |
return $realBlob;
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
/**
|
|
|
809 |
* Insert blob data into a database column directly
|
|
|
810 |
* from file
|
|
|
811 |
*
|
|
|
812 |
* @param string $table table to insert
|
|
|
813 |
* @param string $column column to insert
|
|
|
814 |
* @param string $path physical file name
|
|
|
815 |
* @param string $where string to find unique record
|
|
|
816 |
* @param string $blobtype BLOB or CLOB
|
|
|
817 |
*
|
|
|
818 |
* @return bool success
|
|
|
819 |
*/
|
|
|
820 |
public function updateBlobFile($table,$column,$path,$where,$blobtype='BLOB')
|
|
|
821 |
{
|
|
|
822 |
$fd = fopen($path,'rb');
|
|
|
823 |
if ($fd === false)
|
|
|
824 |
return false;
|
|
|
825 |
|
|
|
826 |
$blob_id = fbird_blob_create($this->_connectionID);
|
|
|
827 |
|
|
|
828 |
/* fill with data */
|
|
|
829 |
|
|
|
830 |
while ($val = fread($fd,32768)){
|
|
|
831 |
fbird_blob_add($blob_id, $val);
|
|
|
832 |
}
|
|
|
833 |
|
|
|
834 |
/* close and get $blob_id_str for inserting into table */
|
|
|
835 |
$blob_id_str = fbird_blob_close($blob_id);
|
|
|
836 |
|
|
|
837 |
fclose($fd);
|
|
|
838 |
return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
|
|
|
839 |
}
|
|
|
840 |
|
|
|
841 |
/**
|
|
|
842 |
* Insert blob data into a database column
|
|
|
843 |
*
|
|
|
844 |
* @param string $table table to insert
|
|
|
845 |
* @param string $column column to insert
|
|
|
846 |
* @param string $val value to insert
|
|
|
847 |
* @param string $where string to find unique record
|
|
|
848 |
* @param string $blobtype BLOB or CLOB
|
|
|
849 |
*
|
|
|
850 |
* @return bool success
|
|
|
851 |
*/
|
|
|
852 |
public function updateBlob($table,$column,$val,$where,$blobtype='BLOB')
|
|
|
853 |
{
|
|
|
854 |
$blob_id = fbird_blob_create($this->_connectionID);
|
|
|
855 |
|
|
|
856 |
// fbird_blob_add($blob_id, $val);
|
|
|
857 |
|
|
|
858 |
// replacement that solves the problem by which only the first modulus 64K /
|
|
|
859 |
// of $val are stored at the blob field ////////////////////////////////////
|
|
|
860 |
// Thx Abel Berenstein aberenstein#afip.gov.ar
|
|
|
861 |
$len = strlen($val);
|
|
|
862 |
$chunk_size = 32768;
|
|
|
863 |
$tail_size = $len % $chunk_size;
|
|
|
864 |
$n_chunks = ($len - $tail_size) / $chunk_size;
|
|
|
865 |
|
|
|
866 |
for ($n = 0; $n < $n_chunks; $n++) {
|
|
|
867 |
$start = $n * $chunk_size;
|
|
|
868 |
$data = substr($val, $start, $chunk_size);
|
|
|
869 |
fbird_blob_add($blob_id, $data);
|
|
|
870 |
}
|
|
|
871 |
|
|
|
872 |
if ($tail_size) {
|
|
|
873 |
$start = $n_chunks * $chunk_size;
|
|
|
874 |
$data = substr($val, $start, $tail_size);
|
|
|
875 |
fbird_blob_add($blob_id, $data);
|
|
|
876 |
}
|
|
|
877 |
// end replacement /////////////////////////////////////////////////////////
|
|
|
878 |
|
|
|
879 |
$blob_id_str = fbird_blob_close($blob_id);
|
|
|
880 |
|
|
|
881 |
return $this->execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
|
|
|
882 |
|
|
|
883 |
}
|
|
|
884 |
|
|
|
885 |
|
|
|
886 |
/**
|
|
|
887 |
* Returns a portably-formatted date string from a timestamp database column.
|
|
|
888 |
*
|
|
|
889 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:sqldate
|
|
|
890 |
*
|
|
|
891 |
* Firebird does not support an AM/PM format, so the A indicator always shows AM
|
|
|
892 |
*
|
|
|
893 |
* @param string $fmt The date format to use.
|
|
|
894 |
* @param string|bool $col (Optional) The table column to date format, or if false, use NOW().
|
|
|
895 |
*
|
|
|
896 |
* @return string The SQL DATE_FORMAT() string, or empty if the provided date format was empty.
|
|
|
897 |
*/
|
|
|
898 |
public function sqlDate($fmt, $col=false)
|
|
|
899 |
{
|
|
|
900 |
if (!$col)
|
|
|
901 |
$col = 'CURRENT_TIMESTAMP';
|
|
|
902 |
|
|
|
903 |
$s = '';
|
|
|
904 |
|
|
|
905 |
$len = strlen($fmt);
|
|
|
906 |
for ($i=0; $i < $len; $i++) {
|
|
|
907 |
if ($s) $s .= '||';
|
|
|
908 |
$ch = $fmt[$i];
|
|
|
909 |
$choice = strtoupper($ch);
|
|
|
910 |
switch($choice) {
|
|
|
911 |
case 'Y':
|
|
|
912 |
$s .= "EXTRACT(YEAR FROM $col)";
|
|
|
913 |
break;
|
|
|
914 |
case 'M':
|
|
|
915 |
$s .= "RIGHT('0' || TRIM(EXTRACT(MONTH FROM $col)),2)";
|
|
|
916 |
break;
|
|
|
917 |
case 'W':
|
|
|
918 |
// The more accurate way of doing this is with a stored procedure
|
|
|
919 |
// See http://wiki.firebirdsql.org/wiki/index.php?page=DATE+Handling+Functions for details
|
|
|
920 |
$s .= "((EXTRACT(YEARDAY FROM $col) - EXTRACT(WEEKDAY FROM $col - 1) + 7) / 7)";
|
|
|
921 |
break;
|
|
|
922 |
case 'Q':
|
|
|
923 |
$s .= "CAST(((EXTRACT(MONTH FROM $col)+2) / 3) AS INTEGER)";
|
|
|
924 |
break;
|
|
|
925 |
case 'D':
|
|
|
926 |
$s .= "RIGHT('0' || TRIM(EXTRACT(DAY FROM $col)),2)";
|
|
|
927 |
break;
|
|
|
928 |
case 'H':
|
|
|
929 |
$s .= "RIGHT('0' || TRIM(EXTRACT(HOUR FROM $col)),2)";
|
|
|
930 |
break;
|
|
|
931 |
case 'I':
|
|
|
932 |
$s .= "RIGHT('0' || TRIM(EXTRACT(MINUTE FROM $col)),2)";
|
|
|
933 |
break;
|
|
|
934 |
case 'S':
|
|
|
935 |
//$s .= "CAST((EXTRACT(SECOND FROM $col)) AS INTEGER)";
|
|
|
936 |
$s .= "RIGHT('0' || TRIM(EXTRACT(SECOND FROM $col)),2)";
|
|
|
937 |
break;
|
|
|
938 |
case 'A':
|
|
|
939 |
$s .= $this->qstr('AM');
|
|
|
940 |
break;
|
|
|
941 |
default:
|
|
|
942 |
if ($ch == '\\') {
|
|
|
943 |
$i++;
|
|
|
944 |
$ch = substr($fmt,$i,1);
|
|
|
945 |
}
|
|
|
946 |
$s .= $this->qstr($ch);
|
|
|
947 |
break;
|
|
|
948 |
}
|
|
|
949 |
}
|
|
|
950 |
return $s;
|
|
|
951 |
}
|
|
|
952 |
|
|
|
953 |
/**
|
|
|
954 |
* Creates a portable date offset field, for use in SQL statements.
|
|
|
955 |
*
|
|
|
956 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:offsetdate
|
|
|
957 |
*
|
|
|
958 |
* @param float $dayFraction A day in floating point
|
|
|
959 |
* @param string|bool $date (Optional) The date to offset. If false, uses CURDATE()
|
|
|
960 |
*
|
|
|
961 |
* @return string
|
|
|
962 |
*/
|
|
|
963 |
public function offsetDate($dayFraction, $date=false)
|
|
|
964 |
{
|
|
|
965 |
if (!$date)
|
|
|
966 |
$date = $this->sysTimeStamp;
|
|
|
967 |
|
|
|
968 |
$fraction = $dayFraction * 24 * 3600;
|
|
|
969 |
return sprintf("DATEADD (second, %s, %s) FROM RDB\$DATABASE",$fraction,$date);
|
|
|
970 |
}
|
|
|
971 |
|
|
|
972 |
|
|
|
973 |
// Note that Interbase 6.5 uses this ROWS instead - don't you love forking wars!
|
|
|
974 |
// SELECT col1, col2 FROM table ROWS 5 -- get 5 rows
|
|
|
975 |
// SELECT col1, col2 FROM TABLE ORDER BY col1 ROWS 3 TO 7 -- first 5 skip 2
|
|
|
976 |
/**
|
|
|
977 |
* Executes a provided SQL statement and returns a handle to the result, with the ability to supply a starting
|
|
|
978 |
* offset and record count.
|
|
|
979 |
*
|
|
|
980 |
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:selectlimit
|
|
|
981 |
*
|
|
|
982 |
* @param string $sql The SQL to execute.
|
|
|
983 |
* @param int $nrows (Optional) The limit for the number of records you want returned. By default, all results.
|
|
|
984 |
* @param int $offset (Optional) The offset to use when selecting the results. By default, no offset.
|
|
|
985 |
* @param array|bool $inputarr (Optional) Any parameter values required by the SQL statement, or false if none.
|
|
|
986 |
* @param int $secs2cache (Optional) If greater than 0, perform a cached execute. By default, normal execution.
|
|
|
987 |
*
|
|
|
988 |
* @return ADORecordSet|false The query results, or false if the query failed to execute.
|
|
|
989 |
*/
|
|
|
990 |
public function selectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false, $secs2cache=0)
|
|
|
991 |
{
|
|
|
992 |
$nrows = (integer) $nrows;
|
|
|
993 |
$offset = (integer) $offset;
|
|
|
994 |
$str = 'SELECT ';
|
|
|
995 |
if ($nrows >= 0) $str .= "FIRST $nrows ";
|
|
|
996 |
$str .=($offset>=0) ? "SKIP $offset " : '';
|
|
|
997 |
|
|
|
998 |
$sql = preg_replace('/^[ \t]*select/i',$str,$sql);
|
|
|
999 |
if ($secs2cache)
|
|
|
1000 |
$rs = $this->cacheExecute($secs2cache,$sql,$inputarr);
|
|
|
1001 |
else
|
|
|
1002 |
$rs = $this->execute($sql,$inputarr);
|
|
|
1003 |
|
|
|
1004 |
return $rs;
|
|
|
1005 |
}
|
|
|
1006 |
|
|
|
1007 |
}
|
|
|
1008 |
|
|
|
1009 |
/**
|
|
|
1010 |
* Class ADORecordset_firebird
|
|
|
1011 |
*/
|
|
|
1012 |
class ADORecordset_firebird extends ADORecordSet
|
|
|
1013 |
{
|
|
|
1014 |
var $databaseType = "firebird";
|
|
|
1015 |
var $bind = false;
|
|
|
1016 |
|
|
|
1017 |
/**
|
|
|
1018 |
* @var ADOFieldObject[] Holds a cached version of the metadata
|
|
|
1019 |
*/
|
|
|
1020 |
private $fieldObjects = false;
|
|
|
1021 |
|
|
|
1022 |
/**
|
|
|
1023 |
* @var bool Flags if we have retrieved the metadata
|
|
|
1024 |
*/
|
|
|
1025 |
private $fieldObjectsRetrieved = false;
|
|
|
1026 |
|
|
|
1027 |
/**
|
|
|
1028 |
* @var array Cross-reference the objects by name for easy access
|
|
|
1029 |
*/
|
|
|
1030 |
private $fieldObjectsIndex = array();
|
|
|
1031 |
|
|
|
1032 |
/**
|
|
|
1033 |
* @var bool Flag to indicate if the result has a blob
|
|
|
1034 |
*/
|
|
|
1035 |
private $fieldObjectsHaveBlob = false;
|
|
|
1036 |
|
|
|
1037 |
function __construct($id, $mode = false)
|
|
|
1038 |
{
|
|
|
1039 |
global $ADODB_FETCH_MODE;
|
|
|
1040 |
|
|
|
1041 |
$this->fetchMode = ($mode === false) ? $ADODB_FETCH_MODE : $mode;
|
|
|
1042 |
parent::__construct($id);
|
|
|
1043 |
}
|
|
|
1044 |
|
|
|
1045 |
|
|
|
1046 |
/**
|
|
|
1047 |
* Returns: an object containing field information.
|
|
|
1048 |
*
|
|
|
1049 |
* Get column information in the Recordset object. fetchField()
|
|
|
1050 |
* can be used in order to obtain information about fields in a
|
|
|
1051 |
* certain query result. If the field offset isn't specified,
|
|
|
1052 |
* the next field that wasn't yet retrieved by fetchField()
|
|
|
1053 |
* is retrieved.
|
|
|
1054 |
*
|
|
|
1055 |
* $param int $fieldOffset (optional default=-1 for all
|
|
|
1056 |
* @return mixed an ADOFieldObject, or array of objects
|
|
|
1057 |
*/
|
|
|
1058 |
private function _fetchField($fieldOffset = -1)
|
|
|
1059 |
{
|
|
|
1060 |
if ($this->fieldObjectsRetrieved) {
|
|
|
1061 |
if ($this->fieldObjects) {
|
|
|
1062 |
// Already got the information
|
|
|
1063 |
if ($fieldOffset == -1) {
|
|
|
1064 |
return $this->fieldObjects;
|
|
|
1065 |
} else {
|
|
|
1066 |
return $this->fieldObjects[$fieldOffset];
|
|
|
1067 |
}
|
|
|
1068 |
} else {
|
|
|
1069 |
// No metadata available
|
|
|
1070 |
return false;
|
|
|
1071 |
}
|
|
|
1072 |
}
|
|
|
1073 |
|
|
|
1074 |
// Populate the field objects cache
|
|
|
1075 |
$this->fieldObjectsRetrieved = true;
|
|
|
1076 |
$this->fieldObjectsHaveBlob = false;
|
|
|
1077 |
$this->_numOfFields = fbird_num_fields($this->_queryID);
|
|
|
1078 |
for ($fieldIndex = 0; $fieldIndex < $this->_numOfFields; $fieldIndex++) {
|
|
|
1079 |
$fld = new ADOFieldObject;
|
|
|
1080 |
$ibf = fbird_field_info($this->_queryID, $fieldIndex);
|
|
|
1081 |
|
|
|
1082 |
$name = empty($ibf['alias']) ? $ibf['name'] : $ibf['alias'];
|
|
|
1083 |
|
|
|
1084 |
switch (ADODB_ASSOC_CASE) {
|
|
|
1085 |
case ADODB_ASSOC_CASE_UPPER:
|
|
|
1086 |
$fld->name = strtoupper($name);
|
|
|
1087 |
break;
|
|
|
1088 |
case ADODB_ASSOC_CASE_LOWER:
|
|
|
1089 |
$fld->name = strtolower($name);
|
|
|
1090 |
break;
|
|
|
1091 |
case ADODB_ASSOC_CASE_NATIVE:
|
|
|
1092 |
default:
|
|
|
1093 |
$fld->name = $name;
|
|
|
1094 |
break;
|
|
|
1095 |
}
|
|
|
1096 |
|
|
|
1097 |
$fld->type = $ibf['type'];
|
|
|
1098 |
$fld->max_length = $ibf['length'];
|
|
|
1099 |
|
|
|
1100 |
// This needs to be populated from the metadata
|
|
|
1101 |
$fld->not_null = false;
|
|
|
1102 |
$fld->has_default = false;
|
|
|
1103 |
$fld->default_value = 'null';
|
|
|
1104 |
|
|
|
1105 |
$this->fieldObjects[$fieldIndex] = $fld;
|
|
|
1106 |
$this->fieldObjectsIndex[$fld->name] = $fieldIndex;
|
|
|
1107 |
|
|
|
1108 |
if ($fld->type == 'BLOB') {
|
|
|
1109 |
$this->fieldObjectsHaveBlob = true;
|
|
|
1110 |
}
|
|
|
1111 |
}
|
|
|
1112 |
|
|
|
1113 |
if ($fieldOffset == -1) {
|
|
|
1114 |
return $this->fieldObjects;
|
|
|
1115 |
}
|
|
|
1116 |
|
|
|
1117 |
return $this->fieldObjects[$fieldOffset];
|
|
|
1118 |
}
|
|
|
1119 |
|
|
|
1120 |
/**
|
|
|
1121 |
* Fetchfield copies the oracle method, it loads the field information
|
|
|
1122 |
* into the _fieldobjs array once, to save multiple calls to the
|
|
|
1123 |
* fbird_ function
|
|
|
1124 |
*
|
|
|
1125 |
* @param int $fieldOffset (optional)
|
|
|
1126 |
*
|
|
|
1127 |
* @return adoFieldObject|false
|
|
|
1128 |
*/
|
|
|
1129 |
public function fetchField($fieldOffset = -1)
|
|
|
1130 |
{
|
|
|
1131 |
if ($fieldOffset == -1) {
|
|
|
1132 |
return $this->fieldObjects;
|
|
|
1133 |
}
|
|
|
1134 |
|
|
|
1135 |
return $this->fieldObjects[$fieldOffset];
|
|
|
1136 |
}
|
|
|
1137 |
|
|
|
1138 |
function _initrs()
|
|
|
1139 |
{
|
|
|
1140 |
$this->_numOfRows = -1;
|
|
|
1141 |
|
|
|
1142 |
/*
|
|
|
1143 |
* Retrieve all of the column information first. We copy
|
|
|
1144 |
* this method from oracle
|
|
|
1145 |
*/
|
|
|
1146 |
$this->_fetchField();
|
|
|
1147 |
|
|
|
1148 |
}
|
|
|
1149 |
|
|
|
1150 |
function _seek($row)
|
|
|
1151 |
{
|
|
|
1152 |
return false;
|
|
|
1153 |
}
|
|
|
1154 |
|
|
|
1155 |
public function _fetch()
|
|
|
1156 |
{
|
|
|
1157 |
// Case conversion function for use in Closure defined below
|
|
|
1158 |
$localFnCaseConv = null;
|
|
|
1159 |
|
|
|
1160 |
if ($this->fetchMode & ADODB_FETCH_ASSOC) {
|
|
|
1161 |
// Handle either associative or fetch both
|
|
|
1162 |
$localNumeric = false;
|
|
|
1163 |
|
|
|
1164 |
$f = @fbird_fetch_assoc($this->_queryID);
|
|
|
1165 |
if (is_array($f)) {
|
|
|
1166 |
// Optimally do the case_upper or case_lower
|
|
|
1167 |
if (ADODB_ASSOC_CASE == ADODB_ASSOC_CASE_LOWER) {
|
|
|
1168 |
$f = array_change_key_case($f, CASE_LOWER);
|
|
|
1169 |
$localFnCaseConv = 'strtolower';
|
|
|
1170 |
} elseif (ADODB_ASSOC_CASE == ADODB_ASSOC_CASE_UPPER) {
|
|
|
1171 |
$f = array_change_key_case($f, CASE_UPPER);
|
|
|
1172 |
$localFnCaseConv = 'strtoupper';
|
|
|
1173 |
}
|
|
|
1174 |
}
|
|
|
1175 |
} else {
|
|
|
1176 |
// Numeric fetch mode
|
|
|
1177 |
$localNumeric = true;
|
|
|
1178 |
$f = @fbird_fetch_row($this->_queryID);
|
|
|
1179 |
}
|
|
|
1180 |
|
|
|
1181 |
if ($f === false) {
|
|
|
1182 |
$this->fields = false;
|
|
|
1183 |
return false;
|
|
|
1184 |
}
|
|
|
1185 |
|
|
|
1186 |
// OPN stuff start - optimized
|
|
|
1187 |
// fix missing nulls and decode blobs automatically
|
|
|
1188 |
global $ADODB_ANSI_PADDING_OFF;
|
|
|
1189 |
$rtrim = !empty($ADODB_ANSI_PADDING_OFF);
|
|
|
1190 |
|
|
|
1191 |
// For optimal performance, only process if there is a possibility of something to do
|
|
|
1192 |
if ($this->fieldObjectsHaveBlob || $rtrim) {
|
|
|
1193 |
$localFieldObjects = $this->fieldObjects;
|
|
|
1194 |
$localFieldObjectIndex = $this->fieldObjectsIndex;
|
|
|
1195 |
/** @var ADODB_firebird $localConnection */
|
|
|
1196 |
$localConnection = &$this->connection;
|
|
|
1197 |
|
|
|
1198 |
/**
|
|
|
1199 |
* Closure for an efficient method of iterating over the elements.
|
|
|
1200 |
* @param mixed $value
|
|
|
1201 |
* @param string|int $key
|
|
|
1202 |
* @return void
|
|
|
1203 |
*/
|
|
|
1204 |
$rowTransform = function ($value, $key) use (
|
|
|
1205 |
&$f,
|
|
|
1206 |
$rtrim,
|
|
|
1207 |
$localFieldObjects,
|
|
|
1208 |
$localConnection,
|
|
|
1209 |
$localNumeric,
|
|
|
1210 |
$localFnCaseConv,
|
|
|
1211 |
$localFieldObjectIndex
|
|
|
1212 |
) {
|
|
|
1213 |
if ($localNumeric) {
|
|
|
1214 |
$localKey = $key;
|
|
|
1215 |
} else {
|
|
|
1216 |
// Cross-reference the associative key back to numeric
|
|
|
1217 |
// with appropriate case conversion
|
|
|
1218 |
$index = $localFnCaseConv ? $localFnCaseConv($key) : $key;
|
|
|
1219 |
$localKey = $localFieldObjectIndex[$index];
|
|
|
1220 |
}
|
|
|
1221 |
|
|
|
1222 |
// As we iterate the elements check for blobs and padding
|
|
|
1223 |
if ($localFieldObjects[$localKey]->type == 'BLOB') {
|
|
|
1224 |
$f[$key] = $localConnection->_BlobDecode($value);
|
|
|
1225 |
} else {
|
|
|
1226 |
if ($rtrim && is_string($value)) {
|
|
|
1227 |
$f[$key] = rtrim($value);
|
|
|
1228 |
}
|
|
|
1229 |
}
|
|
|
1230 |
|
|
|
1231 |
};
|
|
|
1232 |
|
|
|
1233 |
// Walk the array, applying the above closure
|
|
|
1234 |
array_walk($f, $rowTransform);
|
|
|
1235 |
}
|
|
|
1236 |
|
|
|
1237 |
if (!$localNumeric && $this->fetchMode & ADODB_FETCH_NUM) {
|
|
|
1238 |
// Creates a fetch both
|
|
|
1239 |
$fNum = array_values($f);
|
|
|
1240 |
$f = array_merge($f, $fNum);
|
|
|
1241 |
}
|
|
|
1242 |
|
|
|
1243 |
$this->fields = $f;
|
|
|
1244 |
|
|
|
1245 |
return true;
|
|
|
1246 |
}
|
|
|
1247 |
|
|
|
1248 |
/**
|
|
|
1249 |
* Get the value of a field in the current row by column name.
|
|
|
1250 |
* Will not work if ADODB_FETCH_MODE is set to ADODB_FETCH_NUM.
|
|
|
1251 |
*
|
|
|
1252 |
* @param string $colname is the field to access
|
|
|
1253 |
*
|
|
|
1254 |
* @return mixed the value of $colname column
|
|
|
1255 |
*/
|
|
|
1256 |
public function fields($colname)
|
|
|
1257 |
{
|
|
|
1258 |
if ($this->fetchMode & ADODB_FETCH_ASSOC) {
|
|
|
1259 |
return $this->fields[$colname];
|
|
|
1260 |
}
|
|
|
1261 |
|
|
|
1262 |
if (!$this->bind) {
|
|
|
1263 |
// fieldsObjectIndex populated by the recordset load
|
|
|
1264 |
$this->bind = array_change_key_case($this->fieldObjectsIndex, CASE_UPPER);
|
|
|
1265 |
}
|
|
|
1266 |
|
|
|
1267 |
return $this->fields[$this->bind[strtoupper($colname)]];
|
|
|
1268 |
}
|
|
|
1269 |
|
|
|
1270 |
|
|
|
1271 |
function _close()
|
|
|
1272 |
{
|
|
|
1273 |
return @fbird_free_result($this->_queryID);
|
|
|
1274 |
}
|
|
|
1275 |
|
|
|
1276 |
public function metaType($t, $len = -1, $fieldObj = false)
|
|
|
1277 |
{
|
|
|
1278 |
if (is_object($t)) {
|
|
|
1279 |
$fieldObj = $t;
|
|
|
1280 |
$t = $fieldObj->type;
|
|
|
1281 |
$len = $fieldObj->max_length;
|
|
|
1282 |
}
|
|
|
1283 |
|
|
|
1284 |
$t = strtoupper($t);
|
|
|
1285 |
|
|
|
1286 |
if (array_key_exists($t, $this->connection->customActualTypes)) {
|
|
|
1287 |
return $this->connection->customActualTypes[$t];
|
|
|
1288 |
}
|
|
|
1289 |
|
|
|
1290 |
switch ($t) {
|
|
|
1291 |
case 'CHAR':
|
|
|
1292 |
return 'C';
|
|
|
1293 |
|
|
|
1294 |
case 'TEXT':
|
|
|
1295 |
case 'VARCHAR':
|
|
|
1296 |
case 'VARYING':
|
|
|
1297 |
if ($len <= $this->blobSize) {
|
|
|
1298 |
return 'C';
|
|
|
1299 |
}
|
|
|
1300 |
return 'X';
|
|
|
1301 |
case 'BLOB':
|
|
|
1302 |
return 'B';
|
|
|
1303 |
|
|
|
1304 |
case 'TIMESTAMP':
|
|
|
1305 |
case 'DATE':
|
|
|
1306 |
return 'D';
|
|
|
1307 |
case 'TIME':
|
|
|
1308 |
return 'T';
|
|
|
1309 |
//case 'T': return 'T';
|
|
|
1310 |
|
|
|
1311 |
//case 'L': return 'L';
|
|
|
1312 |
case 'INT':
|
|
|
1313 |
case 'SHORT':
|
|
|
1314 |
case 'INTEGER':
|
|
|
1315 |
return 'I';
|
|
|
1316 |
default:
|
|
|
1317 |
return ADODB_DEFAULT_METATYPE;
|
|
|
1318 |
}
|
|
|
1319 |
}
|
|
|
1320 |
|
|
|
1321 |
}
|
|
|
1322 |
|