* @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2007 PradoSoft * @license http://www.pradosoft.com/license/ * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ * @package System.Data.Common */ Prado::using('System.Data.Common.TDbCommandBuilder'); /** * TMssqlCommandBuilder provides specifics methods to create limit/offset query commands * for MSSQL servers. * * @author Wei Zhuo * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ * @package System.Data.Common * @since 3.1 */ class TMssqlCommandBuilder extends TDbCommandBuilder { /** * Overrides parent implementation. Uses "SELECT @@Identity". * @return integer last insert id, null if none is found. */ public function getLastInsertID() { foreach($this->getTableInfo()->getColumns() as $column) { if($column->hasSequence()) { $command = $this->getDbConnection()->createCommand('SELECT @@Identity'); return intval($command->queryScalar()); } } } /** * Overrides parent implementation. Alters the sql to apply $limit and $offset. * The idea for limit with offset is done by modifying the sql on the fly * with numerous assumptions on the structure of the sql string. * The modification is done with reference to the notes from * http://troels.arvin.dk/db/rdbms/#select-limit-offset * * * SELECT * FROM ( * SELECT TOP n * FROM ( * SELECT TOP z columns -- (z=n+skip) * FROM tablename * ORDER BY key ASC * ) AS FOO ORDER BY key DESC -- ('FOO' may be anything) * ) AS BAR ORDER BY key ASC -- ('BAR' may be anything) * * * Regular expressions are used to alter the SQL query. The resulting SQL query * may be malformed for complex queries. The following restrictions apply * *