Skip to content

Commit af2fb2d

Browse files
author
Gabriel Tadra Mainginski
committed
Merge branch 'dev'
2 parents e1298bc + f4329ae commit af2fb2d

File tree

1 file changed

+60
-33
lines changed

1 file changed

+60
-33
lines changed

Database/SybaseConnection.php

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ private function compileForSelect(Builder $builder, $bindings) {
102102
$tables = $alias['table'];
103103
}
104104

105-
$queryRes = $this->getPdo()->query("select a.name, b.name AS type FROM syscolumns a noholdlock JOIN systypes b noholdlock ON a.usertype = b.usertype and object_name(a.id) = '".$tables."'");
105+
$explicitDB = explode('..', $tables);
106+
if(isset($explicitDB[1])){
107+
$queryRes = $this->getPdo()->query("select a.name, b.name AS type FROM ".$explicitDB[0]."..syscolumns a noholdlock JOIN ".$explicitDB[0]."..systypes b noholdlock ON a.usertype = b.usertype and object_name(a.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
108+
}else{
109+
$queryRes = $this->getPdo()->query("select a.name, b.name AS type FROM syscolumns a noholdlock JOIN systypes b noholdlock ON a.usertype = b.usertype and object_name(a.id) = '".$tables."'");
110+
}
111+
106112
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
107113

108114
foreach ($types[$tables] as &$row) {
@@ -165,6 +171,9 @@ private function compileBindings($query, $bindings)
165171
case "delete":
166172
preg_match("/(?'tables'.*) where (?'attributes'.*)/i" ,$query, $matches);
167173
break;
174+
default:
175+
return $bindings;
176+
break;
168177
}
169178

170179
$desQuery = array_intersect_key($matches, array_flip(array_filter(array_keys($matches), 'is_string')));
@@ -179,7 +188,7 @@ private function compileBindings($query, $bindings)
179188
unset($matches);
180189
unset($query_type);
181190
preg_match_all("/\[([^\]]*)\]/", $desQuery['attributes'], $arrQuery);
182-
preg_match_all("/\[([^\]]*)\]/", $desQuery['tables'], $arrTables);
191+
preg_match_all("/\[([^\]]*)\]/", str_replace( "].[].[", '..' , $desQuery['tables']), $arrTables);
183192

184193
$arrQuery = $arrQuery[1];
185194
$arrTables = $arrTables[1];
@@ -200,7 +209,12 @@ private function compileBindings($query, $bindings)
200209
$table = $campos;
201210
}
202211
if(!array_key_exists($table, $new_format)){
203-
$queryRes = $this->getPdo()->query("select a.name, b.name AS type FROM syscolumns a noholdlock JOIN systypes b noholdlock ON a.usertype = b.usertype and object_name(a.id) = '".$table."'");
212+
$explicitDB = explode('..', $table);
213+
if(isset($explicitDB[1])){
214+
$queryRes = $this->getPdo()->query("select a.name, b.name AS type FROM ".$explicitDB[0]."..syscolumns a noholdlock JOIN ".$explicitDB[0]."..systypes b noholdlock ON a.usertype = b.usertype and object_name(a.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
215+
}else{
216+
$queryRes = $this->getPdo()->query("select a.name, b.name AS type FROM syscolumns a noholdlock JOIN systypes b noholdlock ON a.usertype = b.usertype and object_name(a.id) = '".$table."'");
217+
}
204218
$types[$table] = $queryRes->fetchAll(\PDO::FETCH_ASSOC);
205219
for($k = 0; $k < count($types[$table]); $k++){
206220
$types[$table][$types[$table][$k]['name']] = $types[$table][$k];
@@ -209,7 +223,7 @@ private function compileBindings($query, $bindings)
209223
$new_format[$table] = [];
210224
}
211225
}
212-
226+
echo $table;
213227
if(!$itsTable){
214228
if(count($bindings)>$ind){
215229
array_push($new_format[$table], ['campo' => $campos, 'binding' => $ind]);
@@ -253,39 +267,29 @@ private function compileNewQuery($query, $bindings)
253267
}
254268
}
255269
}
270+
$newQuery = str_replace( "[]", '' ,$newQuery);
256271
return $newQuery;
257272
}
258273

259-
/**
260-
* Run a select statement against the database.
261-
*
262-
* @param string $query
263-
* @param array $bindings
264-
* @param bool $useReadPdo
265-
* @return array
266-
*/
267-
public function select($query, $bindings = array(), $useReadPdo = true)
268-
{
269-
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
270-
{
271-
if ($me->pretending()) return array();
272-
273-
if($this->queryGrammar->getBuilder() != NULL){
274-
275-
$offset = $this->queryGrammar->getBuilder()->offset;
274+
public function compileOffset($offset, $query, $bindings = array(), $me){
275+
276276
$limit = $this->queryGrammar->getBuilder()->limit;
277277
$from = explode(" ", $this->queryGrammar->getBuilder()->from)[0];
278-
}else{
279-
$offset = 0;
280-
}
281-
if($offset>0){
282278
if(!isset($limit)){
283279
$limit = 999999999999999999999999999;
284280
}
285-
$indentity = $this->getPdo()->query("select name as 'column' from syscolumns where status & 128 = 128 AND object_name(id)='".$from."'")->fetchAll($me->getFetchMode())[0];
286-
287-
if(count($indentity) == 0){
288-
$primaries = $this->getPdo()->query("SELECT index_col(object_name(i.id), i.indid, c.colid) AS primary_key FROM sysindexes i, syscolumns c WHERE i.id = c.id AND c.colid <= i.keycnt AND i.id = object_id('".$from."')")->fetchAll($me->getFetchMode());
281+
$explicitDB = explode('..', $from);
282+
if(isset($explicitDB[1])){
283+
$identity = $this->getPdo()->query("select b.name as 'column' from ".$explicitDB[0]."..syscolumns AS b INNER JOIN ".$explicitDB[0]."..sysobjects AS a ON a.id = b.id WHERE status & 128 = 128 AND a.name ='".$explicitDB[1]."'")->fetchAll($me->getFetchMode())[0];
284+
}else{
285+
$identity = $this->getPdo()->query("select name as 'column' from syscolumns where status & 128 = 128 AND object_name(id)='".$from."'")->fetchAll($me->getFetchMode())[0];
286+
}
287+
if(count($identity) == 0){
288+
if(isset($explicitDB[1])){
289+
$primaries = $this->getPdo()->query("SELECT index_col(".$from.", i.indid, c.colid) AS primary_key FROM ".$explicitDB[0]."..sysindexes i, ".$explicitDB[0]."..syscolumns c WHERE i.id = c.id AND c.colid <= i.keycnt AND i.id = object_id('".$from."')")->fetchAll($me->getFetchMode());
290+
}else{
291+
$primaries = $this->getPdo()->query("SELECT index_col(".$from.", i.indid, c.colid) AS primary_key FROM sysindexes i, syscolumns c WHERE i.id = c.id AND c.colid <= i.keycnt AND i.id = object_id('".$from."')")->fetchAll($me->getFetchMode());
292+
}
289293
foreach($primaries as $primary)
290294
{
291295
$new_arr[] = $primary->primary_key.'+0 AS '.$primary->primary_key;
@@ -294,8 +298,8 @@ public function select($query, $bindings = array(), $useReadPdo = true)
294298
$res_primaries = implode(', ',$new_arr);
295299
$where_primaries = implode(' AND ',$where_arr);
296300
}else{
297-
$res_primaries = $indentity->column.'+0 AS '.$indentity->column;
298-
$where_primaries = "#tmpPaginate.".$indentity->column.' = #tmpTable.'.$indentity->column;
301+
$res_primaries = $identity->column.'+0 AS '.$identity->column;
302+
$where_primaries = "#tmpPaginate.".$identity->column.' = #tmpTable.'.$identity->column;
299303
}
300304

301305
//Offset operation
@@ -304,10 +308,33 @@ public function select($query, $bindings = array(), $useReadPdo = true)
304308
return $this->getPdo()->query("SELECT #tmpPaginate.*, #tmpTable.idTmp FROM #tmpTable INNER JOIN #tmpPaginate ON ".$where_primaries." WHERE #tmpTable.idTmp "
305309
. "BETWEEN ".($offset+1) ." AND ". ($offset+$limit)
306310
." ORDER BY #tmpTable.idTmp ASC")->fetchAll($me->getFetchMode());
311+
312+
}
313+
/**
314+
* Run a select statement against the database.
315+
*
316+
* @param string $query
317+
* @param array $bindings
318+
* @param bool $useReadPdo
319+
* @return array
320+
*/
321+
public function select($query, $bindings = array(), $useReadPdo = true)
322+
{
323+
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
324+
{
325+
if ($me->pretending()) return array();
326+
327+
if($this->queryGrammar->getBuilder() != NULL){
328+
$offset = $this->queryGrammar->getBuilder()->offset;
307329
}else{
308-
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->fetchAll($me->getFetchMode());
330+
$offset = 0;
331+
}
332+
333+
if($offset>0){
334+
return $this->compileOffset($offset, $query, $bindings, $me);
335+
}else{
336+
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->fetchAll($me->getFetchMode());
309337
}
310-
311338
});
312339
}
313340

0 commit comments

Comments
 (0)