Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SQL Server 2005+ library with support for ROW_NUMBER() in limit…
… functionality
- Loading branch information
Jeremiah Peschka
authored and
Jeremiah Peschka
committed
Dec 7, 2009
1 parent
b248056
commit 809c6dd
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @package Prosper | ||
*/ | ||
namespace Prosper; | ||
|
||
class MSSql2005Adapter extends BaseAdapter { | ||
|
||
/** | ||
* Microsoft T-SQL sucks and can't easily do limit offset like every other | ||
* reasonable rdbms, this creates a complex windowing function to do something | ||
* that everyone else has built-in. | ||
* @param string $sql sql statement | ||
* @param int $limit how many records to return | ||
* @param int $offset where to start returning | ||
* @return string modified sql statement | ||
*/ | ||
function limit($sql, $limit, $offset) { | ||
$pos = strripos(@sql, "select"); | ||
// TODO: Modify to use ORDER BY from $sql | ||
if ($pos !== false) { | ||
$pos += 6; | ||
|
||
|
||
$orderpos = strripos($sql, "order by"); | ||
|
||
$orderpos = strripos($sql, "order by"); | ||
|
||
if ($orderpos === false) { | ||
$order = "SELECT 1)) AS row, "; | ||
} else { | ||
// I think this accounts for multiple ORDER BY directions, but I could be wrong. It definitely needs tested. | ||
$order = substr($sql, $orderpos); | ||
} | ||
|
||
$sql = substr($sql, 0, $pos) . " ROW_NUMBER() OVER (ORDER BY (" . $order . substr($sql, $pos); | ||
} | ||
|
||
$sql = "SELECT * FROM (" . $sql . ") WHERE row >= " . $offset . " AND row <= " . ($limit + $offset); | ||
|
||
return $sql; | ||
} | ||
} | ||
?> |