diff --git a/libs/picodb/lib/PicoDb/Driver/Base.php b/libs/picodb/lib/PicoDb/Driver/Base.php index 790cd6236..238c13cfe 100644 --- a/libs/picodb/lib/PicoDb/Driver/Base.php +++ b/libs/picodb/lib/PicoDb/Driver/Base.php @@ -30,6 +30,14 @@ abstract class Base */ protected $pdo = null; + /** + * use TOP or LIMIT for returning a subset of rows + * + * @access public + * @var bool + */ + public bool $useTop; + /** * Create a new PDO connection * @@ -128,6 +136,7 @@ abstract class Base $this->createConnection($settings); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->useTop = false; } /** @@ -231,4 +240,5 @@ abstract class Base { return $this->getConnection()->query('SELECT VERSION()')->fetchColumn(); } + } diff --git a/libs/picodb/lib/PicoDb/Driver/Mssql.php b/libs/picodb/lib/PicoDb/Driver/Mssql.php index 1c4521280..04d93529d 100644 --- a/libs/picodb/lib/PicoDb/Driver/Mssql.php +++ b/libs/picodb/lib/PicoDb/Driver/Mssql.php @@ -25,6 +25,18 @@ class Mssql extends Base 'database', ); + /** + * Constructor + * + * @access public + * @param array $settings + */ + public function __construct(array $settings) + { + parent::__construct($settings); + $this->useTop = true; + } + /** * Table to store the schema version * @@ -180,4 +192,5 @@ class Mssql extends Base $this->getConnection()->exec('SET SHOWPLAN_ALL ON'); return $this->getConnection()->query($this->getSqlFromPreparedStatement($sql, $values))->fetchAll(PDO::FETCH_ASSOC); } + } diff --git a/libs/picodb/lib/PicoDb/Table.php b/libs/picodb/lib/PicoDb/Table.php index dd0af8293..874c401df 100644 --- a/libs/picodb/lib/PicoDb/Table.php +++ b/libs/picodb/lib/PicoDb/Table.php @@ -119,6 +119,14 @@ class Table */ private $sqlSelect = ''; + /** + * SQL TOP clause + * + * @access private + * @var string + */ + private $sqlTop = ''; + /** * SQL joins * @@ -578,7 +586,11 @@ class Table public function limit($value) { if (! is_null($value)) { - $this->sqlLimit = ' LIMIT '.(int) $value; + if ($this->db->getDriver()->useTop) { + $this->sqlTop = ' TOP ('.(int) $value.') '; + } else { + $this->sqlLimit = ' LIMIT '.(int) $value; + } } return $this; @@ -693,7 +705,8 @@ class Table $this->groupBy = $this->db->escapeIdentifierList($this->groupBy); return trim(sprintf( - 'SELECT %s FROM %s %s %s %s %s %s %s', + 'SELECT %s %s FROM %s %s %s %s %s %s %s', + $this->sqlTop, $this->sqlSelect, $this->db->escapeIdentifier($this->name), implode(' ', $this->joins),