diff --git a/src/Dibi/Fluent.php b/src/Dibi/Fluent.php index 2cc68e49..46cec1e0 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -319,11 +319,10 @@ class Fluent implements IDataSource public function fetch() { if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { - $result = $this->query($this->limit(1)->_export())->fetch(); - $this->removeClause('LIMIT'); - return $result; + return $this->query($this->_export(NULL, ['%lmt', 1]))->fetch(); + } else { + return $this->query($this->_export())->fetch(); } - return $this->query($this->_export())->fetch(); } @@ -334,11 +333,10 @@ class Fluent implements IDataSource public function fetchSingle() { if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { - $result = $this->query($this->limit(1)->_export())->fetchSingle(); - $this->removeClause('LIMIT'); - return $result; + return $this->query($this->_export(NULL, ['%lmt', 1]))->fetchSingle(); + } else { + return $this->query($this->_export())->fetchSingle(); } - return $this->query($this->_export())->fetchSingle(); } @@ -459,6 +457,10 @@ class Fluent implements IDataSource { if ($clause === NULL) { $data = $this->clauses; + if ($this->command === 'SELECT' && ($data['LIMIT'] || $data['OFFSET'])) { + $args = array_merge(['%lmt %ofs', $data['LIMIT'][0], $data['OFFSET'][0]], $args); + unset($data['LIMIT'], $data['OFFSET']); + } } else { $clause = self::$normalizer->$clause; diff --git a/tests/dibi/Fluent.fetch.limit.mssql.phpt b/tests/dibi/Fluent.fetch.limit.mssql.phpt new file mode 100644 index 00000000..8a49075a --- /dev/null +++ b/tests/dibi/Fluent.fetch.limit.mssql.phpt @@ -0,0 +1,101 @@ +select('*') + ->from('customers') + ->limit(1) + ->orderBy('customer_id'); + +Assert::same( + reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + (string) $fluent +); + + +$fluent->fetch(); +Assert::same( + 'SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T ', + dibi::$sql +); +$fluent->fetchSingle(); +Assert::same( + reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + dibi::$sql +); +$fluent->fetchAll(0, 3); +Assert::same( + reformat('SELECT TOP 3 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + dibi::$sql +); +Assert::same( + reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + (string) $fluent +); + + +$fluent->limit(0); +$fluent->fetch(); +Assert::same( + reformat('SELECT TOP 0 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + dibi::$sql +); +$fluent->fetchSingle(); +Assert::same( + reformat('SELECT TOP 0 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + dibi::$sql +); +Assert::same( + reformat('SELECT TOP 0 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + (string) $fluent +); + + +$fluent->removeClause('limit'); +$fluent->removeClause('offset'); +$fluent->fetch(); +Assert::same( + reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + dibi::$sql +); +$fluent->fetchSingle(); +Assert::same( + reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '), + dibi::$sql +); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id]'), + (string) $fluent +); diff --git a/tests/dibi/Fluent.fetch.limit.phpt b/tests/dibi/Fluent.fetch.limit.phpt index 9b111c02..70e4b609 100644 --- a/tests/dibi/Fluent.fetch.limit.phpt +++ b/tests/dibi/Fluent.fetch.limit.phpt @@ -1,7 +1,7 @@ select('*') ->orderBy('customer_id'); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), (string) $fluent ); $fluent->fetch(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), dibi::$sql ); $fluent->fetchSingle(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + dibi::$sql +); +$fluent->fetchAll(2, 3); +Assert::same( + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 3 OFFSET 2'), dibi::$sql ); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), (string) $fluent ); @@ -44,16 +49,16 @@ Assert::same( $fluent->limit(0); $fluent->fetch(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), dibi::$sql ); $fluent->fetchSingle(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), dibi::$sql ); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), (string) $fluent ); @@ -61,16 +66,16 @@ Assert::same( $fluent->removeClause('limit'); $fluent->fetch(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), dibi::$sql ); $fluent->fetchSingle(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), dibi::$sql ); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] OFFSET 3'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 18446744073709551615 OFFSET 3'), (string) $fluent ); @@ -78,12 +83,12 @@ Assert::same( $fluent->removeClause('offset'); $fluent->fetch(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), dibi::$sql ); $fluent->fetchSingle(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), dibi::$sql ); Assert::same( diff --git a/tests/dibi/Fluent.select.phpt b/tests/dibi/Fluent.select.phpt index a9257362..119c17df 100644 --- a/tests/dibi/Fluent.select.phpt +++ b/tests/dibi/Fluent.select.phpt @@ -85,7 +85,7 @@ try { } catch (Exception $e) { } Assert::same( - reformat('SELECT * FROM [table] LIMIT 1'), + reformat(' SELECT * FROM [table] LIMIT 1'), dibi::$sql ); @@ -102,7 +102,7 @@ $fluent = $conn->select('*') ->offset(0); Assert::same( - reformat('SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 LIMIT 1 OFFSET 0'), + reformat(' SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 LIMIT 1'), (string) $fluent ); @@ -141,3 +141,13 @@ Assert::same( reformat('SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = \'a\') AND (b) AND (c)'), (string) $fluent ); + + +$fluent = $conn->select('*') + ->limit(' 1; DROP TABLE users') + ->offset(' 1; DROP TABLE users'); + +Assert::same( + reformat(' SELECT * LIMIT 1 OFFSET 1'), + (string) $fluent +);