Commit b8033f8e authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-203] SQLServerPlatform Refactoring

parent 60346341
...@@ -13,3 +13,60 @@ Doctrine\DBAL\Connection#executeQuery() got a new last parameter "QueryCacheProf ...@@ -13,3 +13,60 @@ Doctrine\DBAL\Connection#executeQuery() got a new last parameter "QueryCacheProf
The Driver statement was split into a ResultStatement and the normal statement extending from it. The Driver statement was split into a ResultStatement and the normal statement extending from it.
This seperates the configuration and the retrieval API from a statement. This seperates the configuration and the retrieval API from a statement.
## MsSql Platform/SchemaManager renamed
The MsSqlPlatform was renamed to SQLServerPlatform, the MsSqlSchemaManager was renamed
to SQLServerSchemaManager.
## Cleanup SQLServer Platform version mess
DBAL 2.1 and before were actually only compatible to SQL Server 2008, not earlier versions.
Still other parts of the platform did use old features instead of newly introced datatypes
in SQL Server 2005. Starting with DBAL 2.2 you can pick the Doctrine abstraction exactly
matching your SQL Server version.
The PDO SqlSrv driver now uses the new `SQLServer2008Platform` as default platform.
This platform uses new features of SQL Server as of version 2008. This also includes a switch
in the used fields for "text" and "blob" field types to:
"text" => "VARCHAR(MAX)"
"blob" => "VARBINARY(MAX)"
Additionally `SQLServerPlatform` in DBAL 2.1 and before used "DATE", "TIME" and "DATETIME2" for dates.
This types are only available since version 2008 and the introduction of an explicit
SQLServer 2008 platform makes this dependency explicit.
An `SQLServer2005Platform` was also introduced to differentiate the features between
versions 2003, earlier and 2005.
With this change the `SQLServerPlatform` now throws an exception for using limit queries
with an offset, since SQLServer 2003 and lower do not support this feature.
To use the old SQL Server Platform, because you are using SQL Server 2003 and below use
the following configuration code:
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Platforms\SQLServer2005Platform;
// You are using SQL Server 2003 or earlier
$conn = DriverManager::getConnection(array(
'driver' => 'pdo_sqlsrv',
'platform' => new SQLServerPlatform()
// .. additional parameters
));
// You are using SQL Server 2005
$conn = DriverManager::getConnection(array(
'driver' => 'pdo_sqlsrv',
'platform' => new SQLServer2005Platform()
// .. additional parameters
));
// You are using SQL Server 2008
$conn = DriverManager::getConnection(array(
'driver' => 'pdo_sqlsrv',
// 2008 is default platform
// .. additional parameters
));
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -65,12 +63,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -65,12 +63,12 @@ class Driver implements \Doctrine\DBAL\Driver
public function getDatabasePlatform() public function getDatabasePlatform()
{ {
return new \Doctrine\DBAL\Platforms\MsSqlPlatform(); return new \Doctrine\DBAL\Platforms\SQLServer2005Platform();
} }
public function getSchemaManager(\Doctrine\DBAL\Connection $conn) public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
{ {
return new \Doctrine\DBAL\Schema\MsSqlSchemaManager($conn); return new \Doctrine\DBAL\Schema\SQLServerSchemaManager($conn);
} }
public function getName() public function getName()
...@@ -83,4 +81,4 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -83,4 +81,4 @@ class Driver implements \Doctrine\DBAL\Driver
$params = $conn->getParams(); $params = $conn->getParams();
return $params['dbname']; return $params['dbname'];
} }
} }
\ No newline at end of file
...@@ -2454,6 +2454,13 @@ abstract class AbstractPlatform ...@@ -2454,6 +2454,13 @@ abstract class AbstractPlatform
if ( $offset !== null) { if ( $offset !== null) {
$offset = (int)$offset; $offset = (int)$offset;
if ($offset < 0) {
throw new DBALException("LIMIT argument offset=$offset is not valid");
}
if ( $offset > 0 && ! $this->supportsLimitOffset()) {
throw new DBALException(sprintf("Platform %s does not support offset values in limit queries.", $this->getName()));
}
} }
return $this->doModifyLimitQuery($query, $limit, $offset); return $this->doModifyLimitQuery($query, $limit, $offset);
...@@ -2478,6 +2485,16 @@ abstract class AbstractPlatform ...@@ -2478,6 +2485,16 @@ abstract class AbstractPlatform
return $query; return $query;
} }
/**
* Does the database platform support offsets in modify limit clauses?
*
* @return bool
*/
public function supportsLimitOffset()
{
return true;
}
/** /**
* Gets the character casing of a column in an SQL result set of this platform. * Gets the character casing of a column in an SQL result set of this platform.
* *
......
...@@ -20,15 +20,20 @@ ...@@ -20,15 +20,20 @@
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
/** /**
* Platform to ensure compatibility of Doctrine with the old SQLServer2005 version. * Platform to ensure compatibility of Doctrine with SQLServer2005 version and
* higher.
* *
* Differences to SQL Server 2008 are: * Differences to SQL Server 2008 are:
* *
* - DATETIME2 datatype does not exist, only DATETIME which has a precision of * - DATETIME2 datatype does not exist, only DATETIME which has a precision of
* 3. This is not supported by PHP DateTime, so we are emulating it by * 3. This is not supported by PHP DateTime, so we are emulating it by
* setting .000 manually. * setting .000 manually.
* - Starting with SQLServer2005 VARCHAR(MAX), VARBINARY(MAX) and
* NVARCHAR(max) replace the old TEXT, NTEXT and IMAGE types. See
* {@link http://www.sql-server-helper.com/faq/sql-server-2005-varchar-max-p01.aspx}
* for more information.
*/ */
class SQLServer2005Platform extends MsSqlPlatform class SQLServer2005Platform extends SQLServerPlatform
{ {
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
{ {
...@@ -44,5 +49,23 @@ class SQLServer2005Platform extends MsSqlPlatform ...@@ -44,5 +49,23 @@ class SQLServer2005Platform extends MsSqlPlatform
{ {
return 'Y-m-d H:i:s.000'; return 'Y-m-d H:i:s.000';
} }
/**
*/
protected function initializeDoctrineTypeMappings()
{
parent::initializeDoctrineTypeMappings();
$this->doctrineTypeMapping = array(
);
}
/**
* @override
*/
public function supportsLimitOffset()
{
return true;
}
} }
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Platforms;
/**
* Platform to ensure compatibility of Doctrine with SQLServer2008 version.
*
* Differences to SQL Server 2005 and before are that a new DATETIME2 type was
* introduced that has a higher precision.
*/
class SQLServer2008Platform extends SQLServer2005Platform
{
/**
* @override
*/
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
{
// 3 - microseconds precision length
// http://msdn.microsoft.com/en-us/library/ms187819.aspx
return 'DATETIME2(6)';
}
/**
* @override
*/
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
{
return 'DATE';
}
/**
* @override
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
{
return 'TIME(0)';
}
/**
* Adding Datetime2 Type
*/
protected function initializeDoctrineTypeMappings()
{
parent::initializeDoctrineTypeMappings();
$this->doctrineTypeMapping = array(
'datetime2' => 'datetime',
);
}
}
...@@ -26,16 +26,15 @@ use Doctrine\DBAL\Schema\Index, ...@@ -26,16 +26,15 @@ use Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table; Doctrine\DBAL\Schema\Table;
/** /**
* The MsSqlPlatform provides the behavior, features and SQL dialect of the * The SQLServerPlatform provides the behavior, features and SQL dialect of the
* MySQL database platform. * Microsoft SQL Server database platform.
* *
* @since 2.0 * @since 2.0
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
* @todo Rename: MsSQLPlatform
*/ */
class MsSqlPlatform extends AbstractPlatform class SQLServerPlatform extends AbstractPlatform
{ {
/** /**
* {@inheritDoc} * {@inheritDoc}
...@@ -599,8 +598,7 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -599,8 +598,7 @@ class MsSqlPlatform extends AbstractPlatform
*/ */
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
{ {
// 6 - microseconds precision length return 'DATETIME';
return 'DATETIME2(6)';
} }
/** /**
...@@ -608,7 +606,7 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -608,7 +606,7 @@ class MsSqlPlatform extends AbstractPlatform
*/ */
public function getDateTypeDeclarationSQL(array $fieldDeclaration) public function getDateTypeDeclarationSQL(array $fieldDeclaration)
{ {
return 'DATE'; return 'DATETIME';
} }
/** /**
...@@ -616,7 +614,7 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -616,7 +614,7 @@ class MsSqlPlatform extends AbstractPlatform
*/ */
public function getTimeTypeDeclarationSQL(array $fieldDeclaration) public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
{ {
return 'TIME(0)'; return 'DATETIME';
} }
/** /**
...@@ -631,23 +629,16 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -631,23 +629,16 @@ class MsSqlPlatform extends AbstractPlatform
* Adds an adapter-specific LIMIT clause to the SELECT statement. * Adds an adapter-specific LIMIT clause to the SELECT statement.
* *
* @param string $query * @param string $query
* @param mixed $limit * @param integer $limit
* @param mixed $offset * @param integer $offset
* @link http://lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html * @link http://lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html
* @return string * @return string
*/ */
protected function doModifyLimitQuery($query, $limit, $offset = null) protected function doModifyLimitQuery($query, $limit, $offset = null)
{ {
if ($limit > 0) { if ($limit > 0) {
$count = intval($limit);
$offset = intval($offset);
if ($offset < 0) {
throw new DBALException("LIMIT argument offset=$offset is not valid");
}
if ($offset == 0) { if ($offset == 0) {
$query = preg_replace('/^(SELECT\s(DISTINCT\s)?)/i', '\1TOP ' . $count . ' ', $query); $query = preg_replace('/^(SELECT\s(DISTINCT\s)?)/i', '\1TOP ' . $limit . ' ', $query);
} else { } else {
$orderby = stristr($query, 'ORDER BY'); $orderby = stristr($query, 'ORDER BY');
...@@ -662,7 +653,7 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -662,7 +653,7 @@ class MsSqlPlatform extends AbstractPlatform
$query = preg_replace('/^SELECT\s/', '', $query); $query = preg_replace('/^SELECT\s/', '', $query);
$start = $offset + 1; $start = $offset + 1;
$end = $offset + $count; $end = $offset + $limit;
$query = "SELECT * FROM (SELECT ROW_NUMBER() OVER ($over) AS \"doctrine_rownum\", $query) AS doctrine_tbl WHERE \"doctrine_rownum\" BETWEEN $start AND $end"; $query = "SELECT * FROM (SELECT ROW_NUMBER() OVER ($over) AS \"doctrine_rownum\", $query) AS doctrine_tbl WHERE \"doctrine_rownum\" BETWEEN $start AND $end";
} }
...@@ -671,6 +662,14 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -671,6 +662,14 @@ class MsSqlPlatform extends AbstractPlatform
return $query; return $query;
} }
/**
* @override
*/
public function supportsLimitOffset()
{
return false;
}
/** /**
* @override * @override
*/ */
...@@ -753,7 +752,6 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -753,7 +752,6 @@ class MsSqlPlatform extends AbstractPlatform
'double precision' => 'float', 'double precision' => 'float',
'date' => 'date', 'date' => 'date',
'datetimeoffset' => 'datetimetz', 'datetimeoffset' => 'datetimetz',
'datetime2' => 'datetime',
'smalldatetime' => 'datetime', 'smalldatetime' => 'datetime',
'datetime' => 'datetime', 'datetime' => 'datetime',
'time' => 'time', 'time' => 'time',
......
...@@ -23,18 +23,16 @@ use Doctrine\DBAL\Events; ...@@ -23,18 +23,16 @@ use Doctrine\DBAL\Events;
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs; use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
/** /**
* xxx * SQL Server Schema Manager
* *
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
* @author Juozas Kaziukenas <juozas@juokaz.com> * @author Juozas Kaziukenas <juozas@juokaz.com>
* @version $Revision$
* @since 2.0 * @since 2.0
*/ */
class MsSqlSchemaManager extends AbstractSchemaManager class SQLServerSchemaManager extends AbstractSchemaManager
{ {
/** /**
* @override * @override
*/ */
...@@ -215,4 +213,4 @@ class MsSqlSchemaManager extends AbstractSchemaManager ...@@ -215,4 +213,4 @@ class MsSqlSchemaManager extends AbstractSchemaManager
return $this->_getPortableTableIndexesList($tableIndexes, $table); return $this->_getPortableTableIndexesList($tableIndexes, $table);
} }
} }
\ No newline at end of file
...@@ -4,9 +4,7 @@ namespace Doctrine\Tests\DBAL\Functional\Schema; ...@@ -4,9 +4,7 @@ namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Schema; use Doctrine\DBAL\Schema;
require_once __DIR__ . '/../../../TestInit.php'; class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
class MsSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
} }
\ No newline at end of file
...@@ -2,17 +2,14 @@ ...@@ -2,17 +2,14 @@
namespace Doctrine\Tests\DBAL\Platforms; namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MsSqlPlatform; use Doctrine\DBAL\Platforms\SQLServer2008Platform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php'; class SQLServerPlatformTest extends AbstractPlatformTestCase
class MsSqlPlatformTest extends AbstractPlatformTestCase
{ {
public function createPlatform() public function createPlatform()
{ {
return new MsSqlPlatform; return new SQLServer2008Platform;
} }
public function getGenerateTableSql() public function getGenerateTableSql()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment