2
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3
// +----------------------------------------------------------------------+
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2004 The PHP Group |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license, |
9
// | that is bundled with this package in the file LICENSE, and is |
10
// | available at through the world-wide-web at |
11
// | http://www.php.net/license/2_02.txt. |
12
// | If you did not receive a copy of the PHP license and are unable to |
13
// | obtain it through the world-wide-web, please send a note to |
14
// | license@php.net so we can mail you a copy immediately. |
15
// +----------------------------------------------------------------------+
16
// | Author: Frank M. Kromann <frank@frontbase.com> |
17
// | Maintainer: Daniel Convissor <danielc@php.net> |
18
// +----------------------------------------------------------------------+
20
// $Id: fbsql.php 6184 2008-08-22 10:33:41Z vargenau $
25
// XXX ERRORMSG: The error message from the fbsql function should
26
// be registered here.
33
require_once 'DB/common.php';
36
* Database independent query interface definition for PHP's FrontBase
40
* @version $Id: fbsql.php 6184 2008-08-22 10:33:41Z vargenau $
42
* @author Frank M. Kromann <frank@frontbase.com>
44
class DB_fbsql extends DB_common
49
var $phptype, $dbsyntax;
50
var $prepare_tokens = array();
51
var $prepare_types = array();
52
var $num_rows = array();
53
var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */
59
* DB_fbsql constructor.
66
$this->phptype = 'fbsql';
67
$this->dbsyntax = 'fbsql';
68
$this->features = array(
71
'transactions' => true,
74
$this->errorcode_map = array(
75
1004 => DB_ERROR_CANNOT_CREATE,
76
1005 => DB_ERROR_CANNOT_CREATE,
77
1006 => DB_ERROR_CANNOT_CREATE,
78
1007 => DB_ERROR_ALREADY_EXISTS,
79
1008 => DB_ERROR_CANNOT_DROP,
80
1046 => DB_ERROR_NODBSELECTED,
81
1050 => DB_ERROR_ALREADY_EXISTS,
82
1051 => DB_ERROR_NOSUCHTABLE,
83
1054 => DB_ERROR_NOSUCHFIELD,
84
1062 => DB_ERROR_ALREADY_EXISTS,
85
1064 => DB_ERROR_SYNTAX,
86
1100 => DB_ERROR_NOT_LOCKED,
87
1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
88
1146 => DB_ERROR_NOSUCHTABLE,
96
* Connect to a database and log in as the specified user.
98
* @param $dsn the data source name (see DB::parseDSN for syntax)
99
* @param $persistent (optional) whether the connection should
102
* @return int DB_OK on success, a DB error on failure
104
function connect($dsninfo, $persistent = false)
106
if (!DB::assertExtension('fbsql')) {
107
return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
110
$this->dsn = $dsninfo;
111
$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
114
$connect_function = $persistent ? 'fbsql_pconnect' : 'fbsql_connect';
116
if ($dbhost && $dsninfo['username'] && $dsninfo['password']) {
117
$conn = @$connect_function($dbhost, $dsninfo['username'],
118
$dsninfo['password']);
119
} elseif ($dbhost && $dsninfo['username']) {
120
$conn = @$connect_function($dbhost, $dsninfo['username']);
122
$conn = @$connect_function($dbhost);
127
if (empty($php_errormsg)) {
128
return $this->raiseError(DB_ERROR_CONNECT_FAILED);
130
return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
131
null, $php_errormsg);
135
if ($dsninfo['database']) {
136
if (!fbsql_select_db($dsninfo['database'], $conn)) {
137
return $this->fbsqlRaiseError();
141
$this->connection = $conn;
149
* Log out and disconnect from the database.
153
* @return bool true on success, false if not connected.
155
function disconnect()
157
$ret = @fbsql_close($this->connection);
158
$this->connection = null;
166
* Send a query to fbsql and return the results as a fbsql resource
169
* @param the SQL query
173
* @return mixed returns a valid fbsql result for successful SELECT
174
* queries, DB_OK for other successful queries. A DB error is
175
* returned on failure.
177
function simpleQuery($query)
179
$this->last_query = $query;
180
$query = $this->modifyQuery($query);
181
$result = @fbsql_query("$query;", $this->connection);
183
return $this->fbsqlRaiseError();
185
// Determine which queries that should return data, and which
186
// should return an error code only.
187
if (DB::isManip($query)) {
190
$numrows = $this->numrows($result);
191
if (is_object($numrows)) {
194
$this->num_rows[$result] = $numrows;
202
* Move the internal fbsql result pointer to the next available result
204
* @param a valid fbsql result resource
208
* @return true if a result is available otherwise return false
210
function nextResult($result)
212
return @fbsql_next_result($result);
219
* Fetch a row and insert the data into an existing array.
221
* Formating of the array and the data therein are configurable.
222
* See DB_result::fetchInto() for more information.
224
* @param resource $result query result identifier
225
* @param array $arr (reference) array where data from the row
227
* @param int $fetchmode how the resulting array should be indexed
228
* @param int $rownum the row number to fetch
230
* @return mixed DB_OK on success, null when end of result set is
231
* reached or on failure
233
* @see DB_result::fetchInto()
236
function fetchInto($result, &$arr, $fetchmode, $rownum=null)
238
if ($rownum !== null) {
239
if (!@fbsql_data_seek($result, $rownum)) {
243
if ($fetchmode & DB_FETCHMODE_ASSOC) {
244
$arr = @fbsql_fetch_array($result, FBSQL_ASSOC);
245
if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
246
$arr = array_change_key_case($arr, CASE_LOWER);
249
$arr = @fbsql_fetch_row($result);
252
$errno = @fbsql_errno($this->connection);
256
return $this->fbsqlRaiseError($errno);
258
if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
259
$this->_rtrimArrayValues($arr);
261
if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
262
$this->_convertNullArrayValuesToEmpty($arr);
271
* Free the internal resources associated with $result.
273
* @param $result fbsql result identifier
277
* @return bool true on success, false if $result is invalid
279
function freeResult($result)
281
return @fbsql_free_result($result);
287
function autoCommit($onoff=false)
290
$this->query("SET COMMIT TRUE");
292
$this->query("SET COMMIT FALSE");
316
* Get the number of columns in a result set.
318
* @param $result fbsql result identifier
322
* @return int the number of columns per row in $result
324
function numCols($result)
326
$cols = @fbsql_num_fields($result);
329
return $this->fbsqlRaiseError();
339
* Get the number of rows in a result set.
341
* @param $result fbsql result identifier
345
* @return int the number of rows in $result
347
function numRows($result)
349
$rows = @fbsql_num_rows($result);
350
if ($rows === null) {
351
return $this->fbsqlRaiseError();
357
// {{{ affectedRows()
360
* Gets the number of rows affected by the data manipulation
361
* query. For other queries, this function returns 0.
363
* @return number of rows affected by the last query
365
function affectedRows()
367
if (DB::isManip($this->last_query)) {
368
$result = @fbsql_affected_rows($this->connection);
379
* Get the native error code of the last error (if any) that
380
* occured on the current connection.
384
* @return int native fbsql error code
386
function errorNative()
388
return @fbsql_errno($this->connection);
395
* Returns the next free id in a sequence
397
* @param string $seq_name name of the sequence
398
* @param boolean $ondemand when true, the seqence is automatically
399
* created if it does not exist
401
* @return int the next id number in the sequence. DB_Error if problem.
404
* @see DB_common::nextID()
407
function nextId($seq_name, $ondemand = true)
409
$seqname = $this->getSequenceName($seq_name);
412
$result = $this->query("INSERT INTO ${seqname} VALUES(NULL)");
413
if ($ondemand && DB::isError($result) &&
414
$result->getCode() == DB_ERROR_NOSUCHTABLE) {
416
$result = $this->createSequence($seq_name);
417
if (DB::isError($result)) {
424
if (DB::isError($result)) {
427
return @fbsql_insert_id($this->connection);
431
* Creates a new sequence
433
* @param string $seq_name name of the new sequence
435
* @return int DB_OK on success. A DB_Error object is returned if
439
* @see DB_common::createSequence()
442
function createSequence($seq_name)
444
$seqname = $this->getSequenceName($seq_name);
445
return $this->query("CREATE TABLE ${seqname} ".
446
'(id INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,'.
447
' PRIMARY KEY(id))');
451
// {{{ dropSequence()
456
* @param string $seq_name name of the sequence to be deleted
458
* @return int DB_OK on success. DB_Error if problems.
461
* @see DB_common::dropSequence()
464
function dropSequence($seq_name)
466
$seqname = $this->getSequenceName($seq_name);
467
return $this->query("DROP TABLE ${seqname} RESTRICT");
473
function modifyQuery($query)
475
if ($this->options['portability'] & DB_PORTABILITY_DELETE_COUNT) {
476
// "DELETE FROM table" gives 0 affected rows in fbsql.
477
// This little hack lets you know how many rows were deleted.
478
if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
479
$query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
480
'DELETE FROM \1 WHERE 1=1', $query);
490
* Format input so it can be safely used in a query
492
* @param mixed $in data to be quoted
494
* @return mixed Submitted variable's type = returned value:
495
* + null = the string <samp>NULL</samp>
496
* + boolean = string <samp>TRUE</samp> or <samp>FALSE</samp>
497
* + integer or double = the unquoted number
498
* + other (including strings and numeric strings) =
499
* the data escaped according to MySQL's settings
500
* then encapsulated between single quotes
504
function quoteSmart($in)
506
if (is_int($in) || is_double($in)) {
508
} elseif (is_bool($in)) {
509
return $in ? 'TRUE' : 'FALSE';
510
} elseif (is_null($in)) {
513
return "'" . $this->escapeSimple($in) . "'";
518
// {{{ fbsqlRaiseError()
521
* Gather information about an error, then use that info to create a
522
* DB error object and finally return that object.
524
* @param integer $errno PEAR error number (usually a DB constant) if
525
* manually raising an error
526
* @return object DB error object
527
* @see DB_common::errorCode()
528
* @see DB_common::raiseError()
530
function fbsqlRaiseError($errno = null)
532
if ($errno === null) {
533
$errno = $this->errorCode(fbsql_errno($this->connection));
535
return $this->raiseError($errno, null, null, null,
536
@fbsql_error($this->connection));
543
* Returns information about a table or a result set.
545
* @param object|string $result DB_result object from a query or a
546
* string containing the name of a table
547
* @param int $mode a valid tableInfo mode
548
* @return array an associative array with the information requested
549
* or an error object if something is wrong
552
* @see DB_common::tableInfo()
554
function tableInfo($result, $mode = null) {
555
if (isset($result->result)) {
557
* Probably received a result object.
558
* Extract the result resource identifier.
560
$id = $result->result;
562
} elseif (is_string($result)) {
564
* Probably received a table name.
565
* Create a result resource identifier.
567
$id = @fbsql_list_fields($this->dsn['database'],
568
$result, $this->connection);
572
* Probably received a result resource identifier.
574
* Depricated. Here for compatibility only.
580
if (!is_resource($id)) {
581
return $this->fbsqlRaiseError(DB_ERROR_NEED_MORE_DATA);
584
if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
585
$case_func = 'strtolower';
587
$case_func = 'strval';
590
$count = @fbsql_num_fields($id);
592
// made this IF due to performance (one if is faster than $count if's)
594
for ($i=0; $i<$count; $i++) {
595
$res[$i]['table'] = $case_func(@fbsql_field_table($id, $i));
596
$res[$i]['name'] = $case_func(@fbsql_field_name($id, $i));
597
$res[$i]['type'] = @fbsql_field_type($id, $i);
598
$res[$i]['len'] = @fbsql_field_len($id, $i);
599
$res[$i]['flags'] = @fbsql_field_flags($id, $i);
602
$res["num_fields"]= $count;
604
for ($i=0; $i<$count; $i++) {
605
$res[$i]['table'] = $case_func(@fbsql_field_table($id, $i));
606
$res[$i]['name'] = $case_func(@fbsql_field_name($id, $i));
607
$res[$i]['type'] = @fbsql_field_type($id, $i);
608
$res[$i]['len'] = @fbsql_field_len($id, $i);
609
$res[$i]['flags'] = @fbsql_field_flags($id, $i);
611
if ($mode & DB_TABLEINFO_ORDER) {
612
$res['order'][$res[$i]['name']] = $i;
614
if ($mode & DB_TABLEINFO_ORDERTABLE) {
615
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
620
// free the result only if we were called on a table
622
@fbsql_free_result($id);
628
// {{{ getSpecialQuery()
631
* Returns the query needed to get some backend info
632
* @param string $type What kind of info you want to retrieve
633
* @return string The SQL query string
635
function getSpecialQuery($type)
639
return 'select "table_name" from information_schema.tables';