5
* @version $Id: oracle.php,v 1.50 2007/10/05 14:36:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
19
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
22
* Oracle Database Abstraction Layer
25
class dbal_oracle extends dbal
27
var $last_query_text = '';
32
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
34
$this->persistency = $persistency;
35
$this->user = $sqluser;
36
$this->server = $sqlserver . (($port) ? ':' . $port : '');
37
$this->dbname = $database;
41
// support for "easy connect naming"
42
if ($sqlserver !== '' && $sqlserver !== '/')
44
if (substr($sqlserver, -1, 1) == '/')
46
$sqlserver == substr($sqlserver, 0, -1);
48
$connect = $sqlserver . (($port) ? ':' . $port : '') . '/' . $database;
51
$this->db_connect_id = ($new_link) ? @ocinlogon($this->user, $sqlpassword, $connect, 'UTF8') : (($this->persistency) ? @ociplogon($this->user, $sqlpassword, $connect, 'UTF8') : @ocilogon($this->user, $sqlpassword, $connect, 'UTF8'));
53
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
57
* Version information about used database
59
function sql_server_info()
61
return @ociserverversion($this->db_connect_id);
68
function _sql_transaction($status = 'begin')
77
return @ocicommit($this->db_connect_id);
81
return @ocirollback($this->db_connect_id);
89
* Oracle specific code to handle the fact that it does not compare columns properly
92
function _rewrite_col_compare($args)
94
if (sizeof($args) == 4)
98
return '(' . $args[0] . ' OR (' . $args[1] . ' is NULL AND ' . $args[3] . ' is NULL))';
100
else if ($args[2] == '<>')
102
// really just a fancy way of saying foo <> bar or (foo is NULL XOR bar is NULL) but SQL has no XOR :P
103
return '(' . $args[0] . ' OR ((' . $args[1] . ' is NULL AND ' . $args[3] . ' is NOT NULL) OR (' . $args[1] . ' is NOT NULL AND ' . $args[3] . ' is NULL)))';
108
return $this->_rewrite_where($args[0]);
113
* Oracle specific code to handle it's lack of sanity
116
function _rewrite_where($where_clause)
118
preg_match_all('/\s*(AND|OR)?\s*([\w_.]++)\s*(?:(=|<[=>]?|>=?)\s*((?>\'(?>[^\']++|\'\')*+\'|[\d-.]+))|((NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))/', $where_clause, $result, PREG_SET_ORDER);
120
foreach ($result as $val)
124
if ($val[4] !== "''")
130
$out .= ' ' . $val[1] . ' ' . $val[2];
135
else if ($val[3] == '<>')
137
$out .= ' is NOT NULL';
143
$in_clause = array();
144
$sub_exp = substr($val[5], strpos($val[5], '(') + 1, -1);
146
preg_match_all('/\'(?>[^\']++|\'\')*+\'|[\d-.]++/', $sub_exp, $sub_vals, PREG_PATTERN_ORDER);
148
foreach ($sub_vals[0] as $sub_val)
151
// 1) This determines if an empty string was in the IN clausing, making us turn it into a NULL comparison
152
// 2) This fixes the 1000 list limit that Oracle has (ORA-01795)
153
if ($sub_val !== "''")
155
$in_clause[(int) $i++/1000][] = $sub_val;
162
if (!$extra && $i < 1000)
168
$out .= ' ' . $val[1] . '(';
171
// constuct each IN() clause
172
foreach ($in_clause as $in_values)
174
$in_array[] = $val[2] . ' ' . (isset($val[6]) ? $val[6] : '') . 'IN(' . implode(', ', $in_values) . ')';
177
// Join the IN() clauses against a few ORs (IN is just a nicer OR anyway)
178
$out .= implode(' OR ', $in_array);
180
// handle the empty string case
183
$out .= ' OR ' . $val[2] . ' is ' . (isset($val[6]) ? $val[6] : '') . 'NULL';
187
unset($in_array, $in_clause);
198
* @param string $query Contains the SQL query which shall be executed
199
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
200
* @return mixed When casted to bool the returned value returns true on success and false on failure
204
function sql_query($query = '', $cache_ttl = 0)
210
// EXPLAIN only in extra debug mode
211
if (defined('DEBUG_EXTRA'))
213
$this->sql_report('start', $query);
216
$this->last_query_text = $query;
217
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
218
$this->sql_add_num_queries($this->query_result);
220
if ($this->query_result === false)
222
$in_transaction = false;
223
if (!$this->transaction)
225
$this->sql_transaction('begin');
229
$in_transaction = true;
234
// We overcome Oracle's 4000 char limit by binding vars
235
if (strlen($query) > 4000)
237
if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
239
if (strlen($regs[3]) > 4000)
241
$cols = explode(', ', $regs[2]);
242
preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
247
foreach ($inserts as $key => $value)
249
if (!empty($value) && $value[0] === "'" && strlen($value) > 4002) // check to see if this thing is greater than the max + 'x2
251
$inserts[$key] = ':' . strtoupper($cols[$key]);
252
$array[$inserts[$key]] = str_replace("''", "'", substr($value, 1, -1));
256
$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
259
else if (preg_match_all('/^(UPDATE [\\w_]++\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data, PREG_SET_ORDER))
261
if (strlen($data[0][2]) > 4000)
263
$update = $data[0][1];
264
$where = $data[0][3];
265
preg_match_all('/([\\w_]++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[0][2], $temp, PREG_SET_ORDER);
269
foreach ($temp as $value)
271
if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 4002) // check to see if this thing is greater than the max + 'x2
273
$cols[] = $value[1] . '=:' . strtoupper($value[1]);
274
$array[$value[1]] = str_replace("''", "'", substr($value[2], 1, -1));
278
$cols[] = $value[1] . '=' . $value[2];
282
$query = $update . implode(', ', $cols) . ' ' . $where;
288
switch (substr($query, 0, 6))
291
if (preg_match('/^(DELETE FROM [\w_]++ WHERE)((?:\s*(?:AND|OR)?\s*[\w_]+\s*(?:(?:=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]+)|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))*+)$/', $query, $regs))
293
$query = $regs[1] . $this->_rewrite_where($regs[2]);
299
if (preg_match('/^(UPDATE [\\w_]++\\s+SET [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++)(?:, [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++))*+\\s+WHERE)(.*)$/s', $query, $regs))
301
$query = $regs[1] . $this->_rewrite_where($regs[2]);
307
$query = preg_replace_callback('/([\w_.]++)\s*(?:(=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]++|([\w_.]++))|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]++,? ?)*+\))/', array($this, '_rewrite_col_compare'), $query);
311
$this->query_result = @ociparse($this->db_connect_id, $query);
313
foreach ($array as $key => $value)
315
@ocibindbyname($this->query_result, $key, $array[$key], -1);
318
$success = @ociexecute($this->query_result, OCI_DEFAULT);
322
$this->sql_error($query);
323
$this->query_result = false;
327
if (!$in_transaction)
329
$this->sql_transaction('commit');
333
if (defined('DEBUG_EXTRA'))
335
$this->sql_report('stop', $query);
338
if ($cache_ttl && method_exists($cache, 'sql_save'))
340
$this->open_queries[(int) $this->query_result] = $this->query_result;
341
$cache->sql_save($query, $this->query_result, $cache_ttl);
343
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
345
$this->open_queries[(int) $this->query_result] = $this->query_result;
348
else if (defined('DEBUG_EXTRA'))
350
$this->sql_report('fromcache', $query);
358
return ($this->query_result) ? $this->query_result : false;
364
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
366
$this->query_result = false;
368
$query = 'SELECT * FROM (SELECT /*+ FIRST_ROWS */ rownum AS xrownum, a.* FROM (' . $query . ') a WHERE rownum <= ' . ($offset + $total) . ') WHERE xrownum >= ' . $offset;
370
return $this->sql_query($query, $cache_ttl);
374
* Return number of affected rows
376
function sql_affectedrows()
378
return ($this->query_result) ? @ocirowcount($this->query_result) : false;
384
function sql_fetchrow($query_id = false)
388
if ($query_id === false)
390
$query_id = $this->query_result;
393
if (isset($cache->sql_rowset[$query_id]))
395
return $cache->sql_fetchrow($query_id);
398
if ($query_id !== false)
401
$result = @ocifetchinto($query_id, $row, OCI_ASSOC + OCI_RETURN_NULLS);
403
if (!$result || !$row)
408
$result_row = array();
409
foreach ($row as $key => $value)
411
// Oracle treats empty strings as null
418
if (is_object($value))
420
$value = $value->load();
423
$result_row[strtolower($key)] = $value;
433
* Seek to given row number
434
* rownum is zero-based
436
function sql_rowseek($rownum, &$query_id)
440
if ($query_id === false)
442
$query_id = $this->query_result;
445
if (isset($cache->sql_rowset[$query_id]))
447
return $cache->sql_rowseek($rownum, $query_id);
450
if ($query_id === false)
455
// Reset internal pointer
456
@ociexecute($query_id, OCI_DEFAULT);
458
// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
459
for ($i = 0; $i < $rownum; $i++)
461
if (!$this->sql_fetchrow($query_id))
471
* Get last inserted id after insert statement
473
function sql_nextid()
475
$query_id = $this->query_result;
477
if ($query_id !== false && $this->last_query_text != '')
479
if (preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
481
$query = 'SELECT ' . $tablename[1] . '_seq.currval FROM DUAL';
482
$stmt = @ociparse($this->db_connect_id, $query);
483
@ociexecute($stmt, OCI_DEFAULT);
485
$temp_result = @ocifetchinto($stmt, $temp_array, OCI_ASSOC + OCI_RETURN_NULLS);
486
@ocifreestatement($stmt);
490
return $temp_array['CURRVAL'];
505
function sql_freeresult($query_id = false)
509
if ($query_id === false)
511
$query_id = $this->query_result;
514
if (isset($cache->sql_rowset[$query_id]))
516
return $cache->sql_freeresult($query_id);
519
if (isset($this->open_queries[(int) $query_id]))
521
unset($this->open_queries[(int) $query_id]);
522
return @ocifreestatement($query_id);
529
* Escape string used in sql query
531
function sql_escape($msg)
533
return str_replace("'", "''", $msg);
537
* Build LIKE expression
540
function _sql_like_expression($expression)
542
return $expression . " ESCAPE '\\'";
545
function _sql_custom_build($stage, $data)
551
* return sql error array
554
function _sql_error()
556
$error = @ocierror();
557
$error = (!$error) ? @ocierror($this->query_result) : $error;
558
$error = (!$error) ? @ocierror($this->db_connect_id) : $error;
562
$this->last_error_result = $error;
566
$error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
573
* Close sql connection
576
function _sql_close()
578
return @ocilogoff($this->db_connect_id);
582
* Build db-specific report
585
function _sql_report($mode, $query = '')
593
// Grab a plan table, any will do
594
$sql = "SELECT table_name
596
WHERE table_name LIKE '%PLAN_TABLE%'";
597
$stmt = ociparse($this->db_connect_id, $sql);
601
if (ocifetchinto($stmt, $result, OCI_ASSOC + OCI_RETURN_NULLS))
603
$table = $result['TABLE_NAME'];
605
// This is the statement_id that will allow us to track the plan
606
$statement_id = substr(md5($query), 0, 30);
608
// Remove any stale plans
609
$stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
611
ocifreestatement($stmt2);
615
SET STATEMENT_ID = '$statement_id'
617
$stmt2 = ociparse($this->db_connect_id, $sql);
619
ocifreestatement($stmt2);
621
// Get the data from the plan
622
$sql = "SELECT operation, options, object_name, object_type, cardinality, cost
624
START WITH id = 0 AND statement_id = '$statement_id'
625
CONNECT BY PRIOR id = parent_id
626
AND statement_id = '$statement_id'";
627
$stmt2 = ociparse($this->db_connect_id, $sql);
631
while (ocifetchinto($stmt2, $row, OCI_ASSOC + OCI_RETURN_NULLS))
633
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
636
ocifreestatement($stmt2);
638
// Remove the plan we just made, we delete them on request anyway
639
$stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
641
ocifreestatement($stmt2);
644
ocifreestatement($stmt);
648
$this->html_hold .= '</table>';
654
$endtime = explode(' ', microtime());
655
$endtime = $endtime[0] + $endtime[1];
657
$result = @ociparse($this->db_connect_id, $query);
658
$success = @ociexecute($result, OCI_DEFAULT);
661
while (@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS))
663
// Take the time spent on parsing rows into account
665
@ocifreestatement($result);
667
$splittime = explode(' ', microtime());
668
$splittime = $splittime[0] + $splittime[1];
670
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
b'\\ No newline at end of file'