import v1.1.0_RC2 | 2009-09-20
This commit is contained in:
@ -15,9 +15,9 @@
|
||||
* @category Zend
|
||||
* @package Zend_Db
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Mssql.php 12960 2008-11-30 11:04:38Z mikaelkael $
|
||||
* @version $Id: Mssql.php 17792 2009-08-24 16:18:02Z ralph $
|
||||
*/
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
|
||||
* @category Zend
|
||||
* @package Zend_Db
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
|
||||
@ -85,6 +85,8 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
|
||||
// don't pass the username and password in the DSN
|
||||
unset($dsn['username']);
|
||||
unset($dsn['password']);
|
||||
unset($dsn['options']);
|
||||
unset($dsn['persistent']);
|
||||
unset($dsn['driver_options']);
|
||||
|
||||
if (isset($dsn['port'])) {
|
||||
@ -218,10 +220,20 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
|
||||
*/
|
||||
public function describeTable($tableName, $schemaName = null)
|
||||
{
|
||||
if ($schemaName != null) {
|
||||
if (strpos($schemaName, '.') !== false) {
|
||||
$result = explode('.', $schemaName);
|
||||
$schemaName = $result[1];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Discover metadata information about this table.
|
||||
*/
|
||||
$sql = "exec sp_columns @table_name = " . $this->quoteIdentifier($tableName, true);
|
||||
if ($schemaName != null) {
|
||||
$sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
|
||||
}
|
||||
|
||||
$stmt = $this->query($sql);
|
||||
$result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
|
||||
|
||||
@ -239,6 +251,10 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
|
||||
* Discover primary key column(s) for this table.
|
||||
*/
|
||||
$sql = "exec sp_pkeys @table_name = " . $this->quoteIdentifier($tableName, true);
|
||||
if ($schemaName != null) {
|
||||
$sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
|
||||
}
|
||||
|
||||
$stmt = $this->query($sql);
|
||||
$primaryKeysResult = $stmt->fetchAll(Zend_Db::FETCH_NUM);
|
||||
$primaryKeyColumn = array();
|
||||
@ -314,25 +330,51 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
|
||||
throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
|
||||
}
|
||||
|
||||
$orderby = stristr($sql, 'ORDER BY');
|
||||
if ($orderby !== false) {
|
||||
$sort = (stripos($orderby, ' desc') !== false) ? 'desc' : 'asc';
|
||||
$order = str_ireplace('ORDER BY', '', $orderby);
|
||||
$order = trim(preg_replace('/\bASC\b|\bDESC\b/i', '', $order));
|
||||
$sql = preg_replace(
|
||||
'/^SELECT\s+(DISTINCT\s)?/i',
|
||||
'SELECT $1TOP ' . ($count+$offset) . ' ',
|
||||
$sql
|
||||
);
|
||||
|
||||
if ($offset > 0) {
|
||||
$orderby = stristr($sql, 'ORDER BY');
|
||||
|
||||
if ($orderby !== false) {
|
||||
$orderParts = explode(',', substr($orderby, 8));
|
||||
$pregReplaceCount = null;
|
||||
$orderbyInverseParts = array();
|
||||
foreach ($orderParts as $orderPart) {
|
||||
$orderPart = rtrim($orderPart);
|
||||
$inv = preg_replace('/\s+desc$/i', ' ASC', $orderPart, 1, $pregReplaceCount);
|
||||
if ($pregReplaceCount) {
|
||||
$orderbyInverseParts[] = $inv;
|
||||
continue;
|
||||
}
|
||||
$inv = preg_replace('/\s+asc$/i', ' DESC', $orderPart, 1, $pregReplaceCount);
|
||||
if ($pregReplaceCount) {
|
||||
$orderbyInverseParts[] = $inv;
|
||||
continue;
|
||||
} else {
|
||||
$orderbyInverseParts[] = $orderPart . ' DESC';
|
||||
}
|
||||
}
|
||||
|
||||
$orderbyInverse = 'ORDER BY ' . implode(', ', $orderbyInverseParts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
$sql = 'SELECT * FROM (SELECT TOP ' . $count . ' * FROM (' . $sql . ') AS inner_tbl';
|
||||
if ($orderby !== false) {
|
||||
$sql .= ' ' . $orderbyInverse . ' ';
|
||||
}
|
||||
$sql .= ') AS outer_tbl';
|
||||
if ($orderby !== false) {
|
||||
$sql .= ' ' . $orderby;
|
||||
}
|
||||
}
|
||||
|
||||
$sql = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . ($count+$offset) . ' ', $sql);
|
||||
|
||||
$sql = 'SELECT * FROM (SELECT TOP ' . $count . ' * FROM (' . $sql . ') AS inner_tbl';
|
||||
if ($orderby !== false) {
|
||||
$sql .= ' ORDER BY ' . $order . ' ';
|
||||
$sql .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
|
||||
}
|
||||
$sql .= ') AS outer_tbl';
|
||||
if ($orderby !== false) {
|
||||
$sql .= ' ORDER BY ' . $order . ' ' . $sort;
|
||||
}
|
||||
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user