Skip to content

Commit 1d23519

Browse files
author
Gabriel Tadra Mainginski
committed
Now offset works, but it is slow.
2 parents 3e1cc8b + aaba2f9 commit 1d23519

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

Database/SybaseConnection.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,9 @@ protected function getDoctrineDriver()
8282
return new DoctrineDriver;
8383
}
8484
private function compileForSelect(Builder $builder, $bindings) {
85-
86-
if(count($bindings)==0){
87-
return [];
88-
}
89-
$bindings = $this->prepareBindings($bindings);
9085

86+
87+
9188
$arrTables = [];
9289
array_push($arrTables, $builder->from);
9390
if(!empty($builder->joins)){
@@ -145,7 +142,6 @@ private function compileForSelect(Builder $builder, $bindings) {
145142
*/
146143
private function compileBindings($query, $bindings)
147144
{
148-
149145

150146
if(count($bindings)==0){
151147
return [];
@@ -154,7 +150,7 @@ private function compileBindings($query, $bindings)
154150
$bindings = $this->prepareBindings($bindings);
155151
$new_format = [];
156152

157-
switch(\explode(' ', $query)[0]){
153+
switch(explode(' ', $query)[0]){
158154
case "select":
159155
return $this->compileForSelect($this->queryGrammar->getBuilder(), $bindings);
160156
case "insert":
@@ -270,8 +266,33 @@ public function select($query, $bindings = array(), $useReadPdo = true)
270266
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
271267
{
272268
if ($me->pretending()) return array();
273-
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->fetchAll($me->getFetchMode());
274-
});
269+
$offset = $this->queryGrammar->getBuilder()->offset;
270+
$limit = $this->queryGrammar->getBuilder()->limit;
271+
if($offset>0){
272+
if(!isset($limit)){
273+
$limit = 999999999999999999999999999;
274+
}
275+
$primarys = $this->getPdo()->query("SELECT index_col(object_name(i.id), i.indid, c.colid) AS primary_key
276+
FROM sysindexes i, syscolumns c WHERE i.id = c.id AND c.colid <= i.keycnt AND i.id = object_id('precos')")->fetchAll($me->getFetchMode());
277+
foreach($primarys as $primary)
278+
{
279+
$new_arr[] = $primary->primary_key.'+0 AS '.$primary->primary_key;
280+
$where_arr[] = "#tmpPaginate.".$primary->primary_key.' = #tmpTable.'.$primary->primary_key;
281+
}
282+
$res_primarys = implode(', ',$new_arr);
283+
$where_primarys = implode(' AND ',$where_arr);
284+
285+
//Offset operation
286+
$this->getPdo()->query(str_replace(" from ", " into #tmpPaginate from ", $this->compileNewQuery($query, $bindings)));
287+
$this->getPdo()->query("SELECT ".$res_primarys.", idTmp=identity(18) INTO #tmpTable FROM #tmpPaginate");
288+
return $this->getPdo()->query("SELECT #tmpPaginate.*, #tmpTable.idTmp FROM #tmpTable INNER JOIN #tmpPaginate ON ".$where_primarys." WHERE #tmpTable.idTmp "
289+
. "BETWEEN ".($offset+1) ." AND ". ($offset+$limit)
290+
." ORDER BY #tmpTable.idTmp ASC")->fetchAll($me->getFetchMode());
291+
}else{
292+
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->fetchAll($me->getFetchMode());
293+
}
294+
295+
});
275296
}
276297

277298
/**
@@ -295,4 +316,4 @@ public function affectingStatement($query, $bindings = array())
295316
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->rowCount();
296317
});
297318
}
298-
}
319+
}

0 commit comments

Comments
 (0)