| 1 |
<?php |
|---|
| 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 |
class DBMSSQL extends DBAdapter { |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
* This method is used to ignore case. |
|---|
| 34 |
* |
|---|
| 35 |
* @param in The string to transform to upper case. |
|---|
| 36 |
* @return The upper case string. |
|---|
| 37 |
*/ |
|---|
| 38 |
public function toUpperCase($in) |
|---|
| 39 |
{ |
|---|
| 40 |
return "UPPER(" . $in . ")"; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* This method is used to ignore case. |
|---|
| 45 |
* |
|---|
| 46 |
* @param in The string whose case to ignore. |
|---|
| 47 |
* @return The string in a case that can be ignored. |
|---|
| 48 |
*/ |
|---|
| 49 |
public function ignoreCase($in) |
|---|
| 50 |
{ |
|---|
| 51 |
return "UPPER(" . $in . ")"; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
* Returns SQL which concatenates the second string to the first. |
|---|
| 56 |
* |
|---|
| 57 |
* @param string String to concatenate. |
|---|
| 58 |
* @param string String to append. |
|---|
| 59 |
* @return string |
|---|
| 60 |
*/ |
|---|
| 61 |
public function concatString($s1, $s2) |
|---|
| 62 |
{ |
|---|
| 63 |
return "($s1 + $s2)"; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Returns SQL which extracts a substring. |
|---|
| 68 |
* |
|---|
| 69 |
* @param string String to extract from. |
|---|
| 70 |
* @param int Offset to start from. |
|---|
| 71 |
* @param int Number of characters to extract. |
|---|
| 72 |
* @return string |
|---|
| 73 |
*/ |
|---|
| 74 |
public function subString($s, $pos, $len) |
|---|
| 75 |
{ |
|---|
| 76 |
return "SUBSTRING($s, $pos, $len)"; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
* Returns SQL which calculates the length (in chars) of a string. |
|---|
| 81 |
* |
|---|
| 82 |
* @param string String to calculate length of. |
|---|
| 83 |
* @return string |
|---|
| 84 |
*/ |
|---|
| 85 |
public function strLength($s) |
|---|
| 86 |
{ |
|---|
| 87 |
return "LEN($s)"; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
* @see DBAdapter::quoteIdentifier() |
|---|
| 92 |
*/ |
|---|
| 93 |
public function quoteIdentifier($text) |
|---|
| 94 |
{ |
|---|
| 95 |
return '[' . $text . ']'; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
* @see DBAdapter::random() |
|---|
| 100 |
*/ |
|---|
| 101 |
public function random($seed = null) |
|---|
| 102 |
{ |
|---|
| 103 |
return 'rand('.((int) $seed).')'; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
* Simulated Limit/Offset |
|---|
| 108 |
* This rewrites the $sql query to apply the offset and limit. |
|---|
| 109 |
* @see DBAdapter::applyLimit() |
|---|
| 110 |
* @author Justin Carlson <justin.carlson@gmail.com> |
|---|
| 111 |
*/ |
|---|
| 112 |
public function applyLimit(&$sql, $offset, $limit) |
|---|
| 113 |
{ |
|---|
| 114 |
|
|---|
| 115 |
if (!is_numeric($offset) || !is_numeric($limit)){ |
|---|
| 116 |
throw new Exception("DBMSSQL ::applyLimit() expects a number for argument 2 and 3"); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
preg_match('/\A(.*)select(.*)from/si',$sql,$select_segment); |
|---|
| 121 |
if (count($select_segment)>0) |
|---|
| 122 |
{ |
|---|
| 123 |
$original_select = $select_segment[0]; |
|---|
| 124 |
} else { |
|---|
| 125 |
throw new Exception("DBMSSQL ::applyLimit() could not locate the select statement at the start of the query. "); |
|---|
| 126 |
} |
|---|
| 127 |
$modified_select = substr_replace($original_select, null, stristr($original_select,'select') , 6 ); |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
preg_match('/order by(.*)\Z/si',$sql,$order_segment); |
|---|
| 131 |
if (count($order_segment)>0) |
|---|
| 132 |
{ |
|---|
| 133 |
$order_by = $order_segment[0]; |
|---|
| 134 |
} else { |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
$select_items = split(',',$modified_select); |
|---|
| 138 |
if (count($select_items)>0) |
|---|
| 139 |
{ |
|---|
| 140 |
$item_number = 0; |
|---|
| 141 |
$order_by = null; |
|---|
| 142 |
while ($order_by === null && $item_number<count($select_items)) |
|---|
| 143 |
{ |
|---|
| 144 |
if ($select_items[$item_number]!='*' && !strstr($select_items[$item_number],'(')) |
|---|
| 145 |
{ |
|---|
| 146 |
$order_by = 'order by ' . $select_items[0] . ' asc'; |
|---|
| 147 |
} |
|---|
| 148 |
$item_number++; |
|---|
| 149 |
} |
|---|
| 150 |
} |
|---|
| 151 |
if ($order_by === null) |
|---|
| 152 |
{ |
|---|
| 153 |
throw new Exception("DBMSSQL ::applyLimit() could not locate the order by statement at the end of your query or any columns at the start of your query. "); |
|---|
| 154 |
} else { |
|---|
| 155 |
$sql.= ' ' . $order_by; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
$sql = str_replace($original_select , null, $sql); |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
$inverted_order = ''; |
|---|
| 165 |
$order_columns = split(',',str_ireplace('order by ','',$order_by)); |
|---|
| 166 |
$original_order_by = $order_by; |
|---|
| 167 |
$order_by = ''; |
|---|
| 168 |
foreach ($order_columns as $column) |
|---|
| 169 |
{ |
|---|
| 170 |
|
|---|
| 171 |
$column = array_reverse(split("\.",$column)); |
|---|
| 172 |
$column = $column[0]; |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
if (strlen($inverted_order)>0){ |
|---|
| 176 |
$order_by.= ', '; |
|---|
| 177 |
$inverted_order.=', '; |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
if (stristr($column,' desc')) |
|---|
| 182 |
{ |
|---|
| 183 |
$order_by .= $column; |
|---|
| 184 |
$inverted_order .= str_ireplace(' desc',' asc',$column); |
|---|
| 185 |
} elseif (stristr($column,' asc')) { |
|---|
| 186 |
$order_by .= $column; |
|---|
| 187 |
$inverted_order .= str_ireplace(' asc',' desc',$column); |
|---|
| 188 |
} else { |
|---|
| 189 |
$order_by .= $column; |
|---|
| 190 |
$inverted_order .= $column .' desc'; |
|---|
| 191 |
} |
|---|
| 192 |
} |
|---|
| 193 |
$order_by = 'order by ' . $order_by; |
|---|
| 194 |
$inverted_order = 'order by ' . $inverted_order; |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
$offset = ($limit+$offset); |
|---|
| 198 |
$modified_sql = 'select * from ('; |
|---|
| 199 |
$modified_sql.= 'select top '.$limit.' * from ('; |
|---|
| 200 |
$modified_sql.= 'select top '.$offset.' '.$modified_select.$sql; |
|---|
| 201 |
$modified_sql.= ') deriveda '.$inverted_order.') derivedb '.$order_by; |
|---|
| 202 |
$sql = $modified_sql; |
|---|
| 203 |
|
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
} |
|---|
| 207 |
|
|---|