Skip to content

Commit abe9884

Browse files
author
Gabriel Tadra Mainginski
committed
Fix #13
1 parent 93f11d7 commit abe9884

File tree

1 file changed

+109
-83
lines changed

1 file changed

+109
-83
lines changed

Database/SybaseConnection.php

Lines changed: 109 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,10 @@ protected function getDoctrineDriver()
8282
return new DoctrineDriver;
8383
}
8484
private function compileForSelect(Builder $builder, $bindings) {
85-
86-
87-
8885
$arrTables = [];
8986
array_push($arrTables, $builder->from);
9087
if(!empty($builder->joins)){
9188
foreach($builder->joins as $join){
92-
9389
array_push($arrTables, $join->table);
9490
}
9591
}
@@ -104,9 +100,25 @@ private function compileForSelect(Builder $builder, $bindings) {
104100

105101
$explicitDB = explode('..', $tables);
106102
if(isset($explicitDB[1])){
107-
$queryRes = $this->getPdo()->query("SELECT s0.name,(SELECT name FROM ".$explicitDB[0]."..systypes s2 noholdlock WHERE s2.usertype=(SELECT min( s3.usertype) FROM ".$explicitDB[0]."..systypes s3 noholdlock WHERE s3.hierarchy=s1.hierarchy)) AS type FROM ".$explicitDB[0]."..systypes s1 noholdlock, ".$explicitDB[0]."..syscolumns s0 noholdlock WHERE s0.usertype = s1.usertype AND object_name(s0.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
108-
}else{
109-
$queryRes = $this->getPdo()->query("SELECT s0.name,(SELECT name FROM systypes s2 noholdlock WHERE s2.usertype=(SELECT min( s3.usertype) FROM systypes s3 noholdlock WHERE s3.hierarchy=s1.hierarchy)) AS type FROM systypes s1 noholdlock, syscolumns s0 noholdlock WHERE s0.usertype = s1.usertype AND object_name(s0.id) = '".$tables."'");
103+
$queryRes = $this->getPdo()->query("select a.name,
104+
b.name AS customtype,
105+
st.name as type
106+
FROM ".$explicitDB[0]."..syscolumns a, ".$explicitDB[0]."..systypes b, ".$explicitDB[0]."..systypes s, ".$explicitDB[0]."..systypes st
107+
WHERE a.usertype = b.usertype
108+
AND s.usertype = a.usertype
109+
AND s.type = st.type
110+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
111+
AND st.usertype < 100
112+
AND object_name(a.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
113+
}else{
114+
$queryRes = $this->getPdo()->query("select a.name, st.name as type
115+
FROM syscolumns a, systypes b, systypes s, systypes st
116+
WHERE a.usertype = b.usertype
117+
AND s.usertype = a.usertype
118+
AND s.type = st.type
119+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
120+
AND st.usertype < 100
121+
AND object_name(a.id) = '".$tables."'");
110122
}
111123

112124
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
@@ -126,11 +138,11 @@ private function compileForSelect(Builder $builder, $bindings) {
126138
for($ind = 0; $ind < count($wheres); $ind++ ){
127139
if(isset($wheres[$ind]['value'])){
128140
if(in_array(strtolower($tipos[$wheres[$ind]['column']]), $this->without_quotes)){
129-
if(!is_null($bindings[$ind])){
130-
$new_binds[$ind] = $bindings[$ind]/1;
131-
}else{
132-
$new_binds[$ind] = null;
133-
}
141+
if(!is_null($bindings[$ind])){
142+
$new_binds[$ind] = $bindings[$ind]/1;
143+
}else{
144+
$new_binds[$ind] = null;
145+
}
134146
}else{
135147
$new_binds[$ind] = (string)$bindings[$ind];
136148
}
@@ -215,10 +227,27 @@ private function compileBindings($query, $bindings)
215227
if(!array_key_exists($table, $new_format)){
216228
$explicitDB = explode('..', $table);
217229
if(isset($explicitDB[1])){
218-
$queryRes = $this->getPdo()->query("SELECT s0.name,(SELECT name FROM ".$explicitDB[0]."..systypes s2 noholdlock WHERE s2.usertype=(SELECT min( s3.usertype) FROM ".$explicitDB[0]."..systypes s3 noholdlock WHERE s3.hierarchy=s1.hierarchy)) AS type FROM ".$explicitDB[0]."..systypes s1 noholdlock, ".$explicitDB[0]."..syscolumns s0 noholdlock WHERE s0.usertype = s1.usertype AND object_name(s0.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
230+
$queryRes = $this->getPdo()->query("select a.name,
231+
b.name AS customtype,
232+
st.name as type
233+
FROM ".$explicitDB[0]."..syscolumns a, ".$explicitDB[0]."..systypes b, ".$explicitDB[0]."..systypes s, ".$explicitDB[0]."..systypes st
234+
WHERE a.usertype = b.usertype
235+
AND s.usertype = a.usertype
236+
AND s.type = st.type
237+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
238+
AND st.usertype < 100
239+
AND object_name(a.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
240+
219241
}else{
220-
$queryRes = $this->getPdo()->query("SELECT s0.name,(SELECT name FROM systypes s2 noholdlock WHERE s2.usertype=(SELECT min( s3.usertype) FROM systypes s3 noholdlock WHERE s3.hierarchy=s1.hierarchy)) AS type FROM systypes s1 noholdlock, syscolumns s0 noholdlock WHERE s0.usertype = s1.usertype AND object_name(s0.id) = '".$table."'");
221-
}
242+
$queryRes = $this->getPdo()->query("select a.name, st.name as type
243+
FROM syscolumns a, systypes b, systypes s, systypes st
244+
WHERE a.usertype = b.usertype
245+
AND s.usertype = a.usertype
246+
AND s.type = st.type
247+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
248+
AND st.usertype < 100
249+
AND object_name(a.id) = '".$table."'");
250+
}
222251
$types[$table] = $queryRes->fetchAll(\PDO::FETCH_ASSOC);
223252
for($k = 0; $k < count($types[$table]); $k++){
224253
$types[$table][$types[$table][$k]['name']] = $types[$table][$k];
@@ -232,10 +261,10 @@ private function compileBindings($query, $bindings)
232261
array_push($new_format[$table], ['campo' => $campos, 'binding' => $ind]);
233262
if(in_array(strtolower($types[$table][$campos]['type']), $this->without_quotes)){
234263
if(!is_null($bindings[$ind])){
235-
$new_binds[$ind] = $bindings[$ind]/1;
236-
}else{
237-
$new_binds[$ind] = null;
238-
}
264+
$new_binds[$ind] = $bindings[$ind]/1;
265+
}else{
266+
$new_binds[$ind] = null;
267+
}
239268
}else{
240269
$new_binds[$ind] = (string)$bindings[$ind];
241270
}
@@ -264,63 +293,60 @@ private function compileNewQuery($query, $bindings)
264293
$bindings = $this->compileBindings($query, $bindings);
265294
$partQuery = explode("?", $query);
266295
for($i = 0; $i<count($partQuery); $i++){
267-
$newQuery .= $partQuery[$i];
268-
if($i<count($bindings)){
269-
if(is_string($bindings[$i])){
270-
$bindings[$i] = str_replace( "'", "''", $bindings[$i] );
271-
$newQuery .= "'".$bindings[$i]."'";
296+
$newQuery .= $partQuery[$i];
297+
if($i<count($bindings)){
298+
if(is_string($bindings[$i])){
299+
$bindings[$i] = str_replace( "'", "''", $bindings[$i] );
300+
$newQuery .= "'".$bindings[$i]."'";
301+
}else{
302+
if(!is_null($bindings[$i])){
303+
$newQuery .= $bindings[$i];
272304
}else{
273-
if(!is_null($bindings[$i])){
274-
$newQuery .= $bindings[$i];
275-
}else{
276-
$newQuery .= 'null';
277-
}
278-
305+
$newQuery .= 'null';
279306
}
280307
}
308+
}
281309
}
282310
$newQuery = str_replace( "[]", '' ,$newQuery);
283-
var_dump($newQuery);
284311
return $newQuery;
285312
}
286313

287314
public function compileOffset($offset, $query, $bindings = array(), $me){
288-
289-
$limit = $this->queryGrammar->getBuilder()->limit;
290-
$from = explode(" ", $this->queryGrammar->getBuilder()->from)[0];
291-
if(!isset($limit)){
292-
$limit = 999999999999999999999999999;
293-
}
294-
$explicitDB = explode('..', $from);
295-
if(isset($explicitDB[1])){
296-
$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];
297-
}else{
298-
$identity = $this->getPdo()->query("select name as 'column' from syscolumns where status & 128 = 128 AND object_name(id)='".$from."'")->fetchAll($me->getFetchMode())[0];
299-
}
300-
if(count($identity) == 0){
301-
if(isset($explicitDB[1])){
302-
$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());
303-
}else{
304-
$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());
305-
}
306-
foreach($primaries as $primary)
307-
{
308-
$new_arr[] = $primary->primary_key.'+0 AS '.$primary->primary_key;
309-
$where_arr[] = "#tmpPaginate.".$primary->primary_key.' = #tmpTable.'.$primary->primary_key;
310-
}
311-
$res_primaries = implode(', ',$new_arr);
312-
$where_primaries = implode(' AND ',$where_arr);
313-
}else{
314-
$res_primaries = $identity->column.'+0 AS '.$identity->column;
315-
$where_primaries = "#tmpPaginate.".$identity->column.' = #tmpTable.'.$identity->column;
316-
}
317-
318-
//Offset operation
319-
$this->getPdo()->query(str_replace(" from ", " into #tmpPaginate from ", $this->compileNewQuery($query, $bindings)));
320-
$this->getPdo()->query("SELECT ".$res_primaries.", idTmp=identity(18) INTO #tmpTable FROM #tmpPaginate");
321-
return $this->getPdo()->query("SELECT #tmpPaginate.*, #tmpTable.idTmp FROM #tmpTable INNER JOIN #tmpPaginate ON ".$where_primaries." WHERE #tmpTable.idTmp "
322-
. "BETWEEN ".($offset+1) ." AND ". ($offset+$limit)
323-
." ORDER BY #tmpTable.idTmp ASC")->fetchAll($me->getFetchMode());
315+
$limit = $this->queryGrammar->getBuilder()->limit;
316+
$from = explode(" ", $this->queryGrammar->getBuilder()->from)[0];
317+
if(!isset($limit)){
318+
$limit = 999999999999999999999999999;
319+
}
320+
$explicitDB = explode('..', $from);
321+
if(isset($explicitDB[1])){
322+
$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];
323+
}else{
324+
$identity = $this->getPdo()->query("select name as 'column' from syscolumns where status & 128 = 128 AND object_name(id)='".$from."'")->fetchAll($me->getFetchMode())[0];
325+
}
326+
if(count($identity) == 0){
327+
if(isset($explicitDB[1])){
328+
$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());
329+
}else{
330+
$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());
331+
}
332+
foreach($primaries as $primary)
333+
{
334+
$new_arr[] = $primary->primary_key.'+0 AS '.$primary->primary_key;
335+
$where_arr[] = "#tmpPaginate.".$primary->primary_key.' = #tmpTable.'.$primary->primary_key;
336+
}
337+
$res_primaries = implode(', ',$new_arr);
338+
$where_primaries = implode(' AND ',$where_arr);
339+
}else{
340+
$res_primaries = $identity->column.'+0 AS '.$identity->column;
341+
$where_primaries = "#tmpPaginate.".$identity->column.' = #tmpTable.'.$identity->column;
342+
}
343+
344+
//Offset operation
345+
$this->getPdo()->query(str_replace(" from ", " into #tmpPaginate from ", $this->compileNewQuery($query, $bindings)));
346+
$this->getPdo()->query("SELECT ".$res_primaries.", idTmp=identity(18) INTO #tmpTable FROM #tmpPaginate");
347+
return $this->getPdo()->query("SELECT #tmpPaginate.*, #tmpTable.idTmp FROM #tmpTable INNER JOIN #tmpPaginate ON ".$where_primaries." WHERE #tmpTable.idTmp "
348+
. "BETWEEN ".($offset+1) ." AND ". ($offset+$limit)
349+
." ORDER BY #tmpTable.idTmp ASC")->fetchAll($me->getFetchMode());
324350

325351
}
326352
/**
@@ -333,22 +359,22 @@ public function compileOffset($offset, $query, $bindings = array(), $me){
333359
*/
334360
public function select($query, $bindings = array(), $useReadPdo = true)
335361
{
336-
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
337-
{
338-
if ($me->pretending()) return array();
339-
340-
if($this->queryGrammar->getBuilder() != NULL){
341-
$offset = $this->queryGrammar->getBuilder()->offset;
342-
}else{
343-
$offset = 0;
344-
}
345-
346-
if($offset>0){
347-
return $this->compileOffset($offset, $query, $bindings, $me);
348-
}else{
349-
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->fetchAll($me->getFetchMode());
350-
}
351-
});
362+
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
363+
{
364+
if ($me->pretending()) return array();
365+
366+
if($this->queryGrammar->getBuilder() != NULL){
367+
$offset = $this->queryGrammar->getBuilder()->offset;
368+
}else{
369+
$offset = 0;
370+
}
371+
372+
if($offset>0){
373+
return $this->compileOffset($offset, $query, $bindings, $me);
374+
}else{
375+
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->fetchAll($me->getFetchMode());
376+
}
377+
});
352378
}
353379

354380
/**

0 commit comments

Comments
 (0)