/usr/share/php/propel/adapter/DBMSSQL.php is in php-propel-runtime 1.6.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | <?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
/**
* This is used to connect to a MSSQL database.
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @version $Revision$
* @package propel.runtime.adapter
*/
class DBMSSQL extends DBAdapter
{
/**
* MS SQL Server does not support SET NAMES
*
* @see DBAdapter::setCharset()
*
* @param PDO $con
* @param string $charset
*/
public function setCharset(PDO $con, $charset)
{
}
/**
* This method is used to ignore case.
*
* @param string $in The string to transform to upper case.
* @return string The upper case string.
*/
public function toUpperCase($in)
{
return $this->ignoreCase($in);
}
/**
* This method is used to ignore case.
*
* @param string $in The string whose case to ignore.
* @return string The string in a case that can be ignored.
*/
public function ignoreCase($in)
{
return 'UPPER(' . $in . ')';
}
/**
* Returns SQL which concatenates the second string to the first.
*
* @param string $s1 String to concatenate.
* @param string $s2 String to append.
*
* @return string
*/
public function concatString($s1, $s2)
{
return '(' . $s1 . ' + ' . $s2 . ')';
}
/**
* Returns SQL which extracts a substring.
*
* @param string $s String to extract from.
* @param integer $pos Offset to start from.
* @param integer $len Number of characters to extract.
*
* @return string
*/
public function subString($s, $pos, $len)
{
return 'SUBSTRING(' . $s . ', ' . $pos . ', ' . $len . ')';
}
/**
* Returns SQL which calculates the length (in chars) of a string.
*
* @param string $s String to calculate length of.
* @return string
*/
public function strLength($s)
{
return 'LEN(' . $s . ')';
}
/**
* @see DBAdapter::quoteIdentifier()
*
* @param string $text
* @return string
*/
public function quoteIdentifier($text)
{
return '[' . $text . ']';
}
/**
* @see DBAdapter::quoteIdentifierTable()
*
* @param string $table
* @return string
*/
public function quoteIdentifierTable($table)
{
// e.g. 'database.table alias' should be escaped as '[database].[table] [alias]'
return '[' . strtr($table, array('.' => '].[', ' ' => '] [')) . ']';
}
/**
* @see DBAdapter::random()
*
* @param string $seed
* @return string
*/
public function random($seed = null)
{
return 'RAND(' . ((int) $seed) . ')';
}
/**
* Simulated Limit/Offset
*
* This rewrites the $sql query to apply the offset and limit.
* some of the ORDER BY logic borrowed from Doctrine MsSqlPlatform
*
* @see DBAdapter::applyLimit()
* @author Benjamin Runnels <kraven@kraven.org>
*
* @param string $sql
* @param integer $offset
* @param integer $limit
*
* @return void
*
* @throws PropelException
* @throws Exception
*/
public function applyLimit(&$sql, $offset, $limit)
{
// make sure offset and limit are numeric
if (! is_numeric($offset) || ! is_numeric($limit)) {
throw new PropelException('DBMSSQL::applyLimit() expects a number for argument 2 and 3');
}
//split the select and from clauses out of the original query
$selectSegment = array();
$selectText = 'SELECT ';
preg_match('/\Aselect(.*)from(.*)/si', $sql, $selectSegment);
if (count($selectSegment) == 3) {
$selectStatement = trim($selectSegment[1]);
$fromStatement = trim($selectSegment[2]);
} else {
throw new Exception('DBMSSQL::applyLimit() could not locate the select statement at the start of the query.');
}
if (preg_match('/\Aselect(\s+)distinct/i', $sql)) {
$selectText .= 'DISTINCT ';
$selectStatement = str_ireplace('distinct ', '', $selectStatement);
}
// if we're starting at offset 0 then theres no need to simulate limit,
// just grab the top $limit number of rows
if ($offset == 0) {
$sql = $selectText . 'TOP ' . $limit . ' ' . $selectStatement . ' FROM ' . $fromStatement;
return;
}
//get the ORDER BY clause if present
$orderStatement = stristr($fromStatement, 'ORDER BY');
$orders = '';
if ($orderStatement !== false) {
//remove order statement from the from statement
$fromStatement = trim(str_replace($orderStatement, '', $fromStatement));
$order = str_ireplace('ORDER BY', '', $orderStatement);
$orders = explode(',', $order);
for ($i = 0; $i < count($orders); $i ++) {
$orderArr[trim(preg_replace('/\s+(ASC|DESC)$/i', '', $orders[$i]))] = array(
'sort' => (stripos($orders[$i], ' DESC') !== false) ? 'DESC' : 'ASC',
'key' => $i
);
}
}
//setup inner and outer select selects
$innerSelect = '';
$outerSelect = '';
foreach (explode(', ', $selectStatement) as $selCol) {
$selColArr = explode(' ', $selCol);
$selColCount = count($selColArr) - 1;
//make sure the current column isn't * or an aggregate
if ($selColArr[0] != '*' && ! strstr($selColArr[0], '(')) {
if (isset($orderArr[$selColArr[0]])) {
$orders[$orderArr[$selColArr[0]]['key']] = $selColArr[0] . ' ' . $orderArr[$selColArr[0]]['sort'];
}
//use the alias if one was present otherwise use the column name
$alias = (! stristr($selCol, ' AS ')) ? $selColArr[0] : $selColArr[$selColCount];
//don't quote the identifier if it is already quoted
if($alias[0] != '[') $alias = $this->quoteIdentifier($alias);
//save the first non-aggregate column for use in ROW_NUMBER() if required
if (! isset($firstColumnOrderStatement)) {
$firstColumnOrderStatement = 'ORDER BY ' . $selColArr[0];
}
//add an alias to the inner select so all columns will be unique
$innerSelect .= $selColArr[0] . ' AS ' . $alias . ', ';
$outerSelect .= $alias . ', ';
} else {
//agregate columns must always have an alias clause
if (! stristr($selCol, ' AS ')) {
throw new Exception('DBMSSQL::applyLimit() requires aggregate columns to have an Alias clause');
}
//aggregate column alias can't be used as the count column you must use the entire aggregate statement
if (isset($orderArr[$selColArr[$selColCount]])) {
$orders[$orderArr[$selColArr[$selColCount]]['key']] = str_replace($selColArr[$selColCount - 1] . ' ' . $selColArr[$selColCount], '', $selCol) . $orderArr[$selColArr[$selColCount]]['sort'];
}
//quote the alias
$alias = $selColArr[$selColCount];
//don't quote the identifier if it is already quoted
if($alias[0] != '[') $alias = $this->quoteIdentifier($alias);
$innerSelect .= str_replace($selColArr[$selColCount], $alias, $selCol) . ', ';
$outerSelect .= $alias . ', ';
}
}
if (is_array($orders)) {
$orderStatement = 'ORDER BY ' . implode(', ', $orders);
} else {
//use the first non aggregate column in our select statement if no ORDER BY clause present
if (isset($firstColumnOrderStatement)) {
$orderStatement = $firstColumnOrderStatement;
} else {
throw new Exception('DBMSSQL::applyLimit() unable to find column to use with ROW_NUMBER()');
}
}
//substring the select strings to get rid of the last comma and add our FROM and SELECT clauses
$innerSelect = $selectText . 'ROW_NUMBER() OVER(' . $orderStatement . ') AS [RowNumber], ' . substr($innerSelect, 0, - 2) . ' FROM';
//outer select can't use * because of the RowNumber column
$outerSelect = 'SELECT ' . substr($outerSelect, 0, - 2) . ' FROM';
//ROW_NUMBER() starts at 1 not 0
$sql = $outerSelect . ' (' . $innerSelect . ' ' . $fromStatement . ') AS derivedb WHERE RowNumber BETWEEN ' . ($offset + 1) . ' AND ' . ($limit + $offset);
}
/**
* @see parent::cleanupSQL()
*
* @param string $sql
* @param array $params
* @param Criteria $values
* @param DatabaseMap $dbMap
*/
public function cleanupSQL(&$sql, array &$params, Criteria $values, DatabaseMap $dbMap)
{
$i = 1;
$paramCols = array();
foreach ($params as $param) {
if (null !== $param['table']) {
$column = $dbMap->getTable($param['table'])->getColumn($param['column']);
/* MSSQL pdo_dblib and pdo_mssql blob values must be converted to hex and then the hex added
* to the query string directly. If it goes through PDOStatement::bindValue quotes will cause
* an error with the insert or update.
*/
if (is_resource($param['value']) && $column->isLob()) {
// we always need to make sure that the stream is rewound, otherwise nothing will
// get written to database.
rewind($param['value']);
$hexArr = unpack('H*hex', stream_get_contents($param['value']));
$sql = str_replace(":p$i", '0x' . $hexArr['hex'], $sql);
unset($hexArr);
fclose($param['value']);
} else {
$paramCols[] = $param;
}
}
$i++;
}
//if we made changes re-number the params
if ($params != $paramCols) {
$params = $paramCols;
unset($paramCols);
preg_match_all('/:p\d/', $sql, $matches);
foreach ($matches[0] as $key => $match) {
$sql = str_replace($match, ':p'.($key+1), $sql);
}
}
}
}
|