| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* TOracleCommandBuilder class file.
|
|
|
5 |
*
|
|
|
6 |
* @author Marcos Nobre <marconobre[at]gmail[dot]com>
|
|
|
7 |
* @link http://www.pradosoft.com/
|
|
|
8 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
9 |
* @license http://www.pradosoft.com/license/
|
|
|
10 |
* @version $Id: TOracleCommandBuilder.php 2482 2008-07-30 02:07:13Z knut $
|
|
|
11 |
* @package System.Data.Common
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
Prado :: using('System.Data.Common.TDbCommandBuilder');
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* TOracleCommandBuilder provides specifics methods to create limit/offset query commands
|
|
|
18 |
* for Oracle database.
|
|
|
19 |
*
|
|
|
20 |
* @author Marcos Nobre <marconobre[at]gmail[dot]com>
|
|
|
21 |
* @version $Id: TOracleCommandBuilder.php 2482 2008-07-30 02:07:13Z knut $
|
|
|
22 |
* @package System.Data.Common
|
|
|
23 |
* @since 3.1
|
|
|
24 |
*/
|
|
|
25 |
class TOracleCommandBuilder extends TDbCommandBuilder {
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Overrides parent implementation. Only column of type text or character (and its variants)
|
|
|
29 |
* accepts the LIKE criteria.
|
|
|
30 |
* @param array list of column id for potential search condition.
|
|
|
31 |
* @param string string of keywords
|
|
|
32 |
* @return string SQL search condition matching on a set of columns.
|
|
|
33 |
*/
|
|
|
34 |
public function getSearchExpression($fields, $keywords) {
|
|
|
35 |
$columns = array ();
|
|
|
36 |
foreach ($fields as $field) {
|
|
|
37 |
if ($this->isSearchableColumn($this->getTableInfo()->getColumn($field)))
|
|
|
38 |
$columns[] = $field;
|
|
|
39 |
}
|
|
|
40 |
return parent :: getSearchExpression($columns, $keywords);
|
|
|
41 |
}
|
|
|
42 |
/**
|
|
|
43 |
*
|
|
|
44 |
* @return boolean true if column can be used for LIKE searching.
|
|
|
45 |
*/
|
|
|
46 |
protected function isSearchableColumn($column) {
|
|
|
47 |
$type = strtolower($column->getDbType());
|
|
|
48 |
return $type === 'character varying' || $type === 'varchar2' || $type === 'character' || $type === 'char' || $type === 'text';
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Overrides parent implementation to use PostgreSQL's ILIKE instead of LIKE (case-sensitive).
|
|
|
53 |
* @param string column name.
|
|
|
54 |
* @param array keywords
|
|
|
55 |
* @return string search condition for all words in one column.
|
|
|
56 |
*/
|
|
|
57 |
/*
|
|
|
58 |
*
|
|
|
59 |
* how Oracle don't implements ILIKE, this method won't be overrided
|
|
|
60 |
*
|
|
|
61 |
protected function getSearchCondition($column, $words)
|
|
|
62 |
{
|
|
|
63 |
$conditions=array();
|
|
|
64 |
foreach($words as $word)
|
|
|
65 |
$conditions[] = $column.' LIKE '.$this->getDbConnection()->quoteString('%'.$word.'%');
|
|
|
66 |
return '('.implode(' AND ', $conditions).')';
|
|
|
67 |
}
|
|
|
68 |
*/
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Overrides parent implementation to use Oracle way of get paginated RecordSet instead of using LIMIT sql clause.
|
|
|
72 |
* @param string SQL query string.
|
|
|
73 |
* @param integer maximum number of rows, -1 to ignore limit.
|
|
|
74 |
* @param integer row offset, -1 to ignore offset.
|
|
|
75 |
* @return string SQL with limit and offset in Oracle way.
|
|
|
76 |
*/
|
|
|
77 |
public function applyLimitOffset($sql, $limit = -1, $offset = -1) {
|
|
|
78 |
if ((int) $limit <= 0 && (int) $offset <= 0)
|
|
|
79 |
return $sql;
|
|
|
80 |
|
|
|
81 |
$pradoNUMLIN = 'pradoNUMLIN';
|
|
|
82 |
$fieldsALIAS = 'xyz';
|
|
|
83 |
|
|
|
84 |
$nfimDaSQL = strlen($sql);
|
|
|
85 |
$nfimDoWhere = (strpos($sql, 'ORDER') !== false ? strpos($sql, 'ORDER') : $nfimDaSQL);
|
|
|
86 |
$niniDoSelect = strpos($sql, 'SELECT') + 6;
|
|
|
87 |
$nfimDoSelect = (strpos($sql, 'FROM') !== false ? strpos($sql, 'FROM') : $nfimDaSQL);
|
|
|
88 |
|
|
|
89 |
$niniDoWhere = strpos($sql, 'WHERE') + 5;
|
|
|
90 |
|
|
|
91 |
$WhereConstraint = substr($sql, $niniDoWhere, $nfimDoWhere - $niniDoWhere);
|
|
|
92 |
|
|
|
93 |
$WhereInSubSelect = "";
|
|
|
94 |
if (trim($WhereConstraint) !== "") {
|
|
|
95 |
$WhereInSubSelect = "WHERE " . $WhereConstraint;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$sORDERBY = '';
|
|
|
99 |
if (stripos($sql, 'ORDER') !== false) {
|
|
|
100 |
$p = stripos($sql, 'ORDER');
|
|
|
101 |
$sORDERBY = substr($sql, $p +8, 10000);
|
|
|
102 |
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
$fields = substr($sql, 0, $nfimDoSelect);
|
|
|
106 |
$fields = trim(substr($fields, $niniDoSelect));
|
|
|
107 |
$aliasedFields = ', ';
|
|
|
108 |
|
|
|
109 |
if (trim($fields) == '*') {
|
|
|
110 |
$aliasedFields = ", {$fieldsALIAS}.{$fields}";
|
|
|
111 |
$fields = '';
|
|
|
112 |
$arr = $this->getTableInfo()->getColumns();
|
|
|
113 |
foreach ($arr as $field) {
|
|
|
114 |
$fields .= strtolower($field->getColumnName()) . ', ';
|
|
|
115 |
}
|
|
|
116 |
$fields = str_replace('"', '', $fields);
|
|
|
117 |
$fields = trim($fields);
|
|
|
118 |
$fields = substr($fields, 0, strlen($fields) - 1);
|
|
|
119 |
} else {
|
|
|
120 |
if (strpos($fields, ',') !== false) {
|
|
|
121 |
$arr = $this->getTableInfo()->getColumns();
|
|
|
122 |
foreach ($arr as $field) {
|
|
|
123 |
$field = strtolower($field);
|
|
|
124 |
$existAS = str_ireplace(' as ', '-as-', $field);
|
|
|
125 |
if (strpos($existAS, '-as-') === false)
|
|
|
126 |
$aliasedFields .= "{$fieldsALIAS}." . trim($field) . ", ";
|
|
|
127 |
else
|
|
|
128 |
$aliasedFields .= "{$field}, ";
|
|
|
129 |
}
|
|
|
130 |
$aliasedFields = trim($aliasedFields);
|
|
|
131 |
$aliasedFields = substr($aliasedFields, 0, strlen($aliasedFields) - 1);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
if ($aliasedFields == ', ')
|
|
|
135 |
$aliasedFields = " , $fieldsALIAS.* ";
|
|
|
136 |
|
|
|
137 |
/* ************************
|
|
|
138 |
$newSql = " SELECT $fields FROM ".
|
|
|
139 |
"( ".
|
|
|
140 |
" SELECT rownum as {$pradoNUMLIN} {$aliasedFields} FROM ".
|
|
|
141 |
" ($sql) {$fieldsALIAS} WHERE rownum <= {$limit} ".
|
|
|
142 |
") WHERE {$pradoNUMLIN} >= {$offset} ";
|
|
|
143 |
|
|
|
144 |
************************* */
|
|
|
145 |
$toReg = $offset + $limit -1;
|
|
|
146 |
$fullTableName = $this->getTableInfo()->getTableFullName();
|
|
|
147 |
if (empty ($sORDERBY)) {
|
|
|
148 |
$newSql = " SELECT $fields FROM " .
|
|
|
149 |
"( " .
|
|
|
150 |
" SELECT ROW_NUMBER() OVER ( ORDER BY ROWNUM ) as {$pradoNUMLIN} {$aliasedFields} " .
|
|
|
151 |
" FROM {$fullTableName} {$fieldsALIAS}" .
|
|
|
152 |
") nn " .
|
|
|
153 |
" WHERE nn.{$pradoNUMLIN} >= {$offset} AND nn.{$pradoNUMLIN} <= {$toReg} ";
|
|
|
154 |
} else {
|
|
|
155 |
$newSql = " SELECT $fields FROM " .
|
|
|
156 |
"( " .
|
|
|
157 |
" SELECT ROW_NUMBER() OVER ( ORDER BY {$sORDERBY} ) as {$pradoNUMLIN} {$aliasedFields} " .
|
|
|
158 |
" FROM {$fullTableName} {$fieldsALIAS} $WhereInSubSelect" .
|
|
|
159 |
") nn " .
|
|
|
160 |
" WHERE nn.{$pradoNUMLIN} >= {$offset} AND nn.{$pradoNUMLIN} <= {$toReg} ";
|
|
|
161 |
}
|
|
|
162 |
//echo $newSql."\n<br>\n";
|
|
|
163 |
return $newSql;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
}
|
|
|
167 |
?>
|