Skip to content

Commit b628b35

Browse files
authored
Merge pull request #30 from uepg/dev
Fix #29
2 parents 90c4af8 + c179730 commit b628b35

File tree

1 file changed

+67
-54
lines changed

1 file changed

+67
-54
lines changed

Database/SybaseConnection.php

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -120,58 +120,71 @@ private function compileForSelect(Builder $builder, $bindings) {
120120
}
121121
$queryString = $this->queryStringForSelect($tables);
122122
$queryRes = $this->getPdo()->query($queryString);
123-
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
123+
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
124124

125125
foreach ($types[$tables] as &$row) {
126126
$tipos[strtolower($row['name'])] = $row['type'];
127127
$tipos[strtolower($tables.'.'.$row['name'])] = $row['type'];
128+
128129
if (!empty($alias['alias'])) {
129130
$tipos[strtolower($alias['alias'].'.'.$row['name'])] = $row['type'];
130-
}
131-
}
132-
$wheres = [];
133-
foreach($builder->wheres as $w){
134-
switch($w['type']){
135-
default:
136-
array_push($wheres, $w);
137-
break;
138-
case "Nested":
139-
$wheres += $w['query']->wheres;
140-
break;
141-
}
142-
}
131+
}
132+
}
133+
134+
$wheres = [];
135+
136+
foreach($builder->wheres as $w){
137+
switch($w['type']){
138+
default:
139+
array_push($wheres, $w);
140+
break;
141+
case "Nested":
142+
$wheres += $w['query']->wheres;
143+
break;
144+
}
145+
}
146+
143147
$i = 0;
144-
for($ind = 0; $ind < count($wheres); $ind++ ){
148+
$wheresCount = count($wheres);
149+
150+
for($ind = 0; $ind < $wheresCount; $ind++ ){
145151
if(isset($wheres[$ind]['value']) && isset($tipos[strtolower($wheres[$ind]['column'])])){
146-
if(in_array(strtolower($tipos[strtolower($wheres[$ind]['column'])]), $this->without_quotes)){
147-
if(!is_null($bindings[$i])){
152+
if (is_object($wheres[$ind]['value']) === false) {
153+
if(in_array(strtolower($tipos[strtolower($wheres[$ind]['column'])]), $this->without_quotes)){
154+
if(!is_null($bindings[$i])){
148155
$new_binds[$i] = $bindings[$i]/1;
149-
}else{
156+
}else{
150157
$new_binds[$i] = null;
158+
}
159+
}else{
160+
$new_binds[$i] = (string)$bindings[$i];
151161
}
152-
}else{
153-
$new_binds[$i] = (string)$bindings[$i];
162+
$i++;
154163
}
155-
$i++;
156164
}
157165
}
158166

159167
$new_format[$tables] = [];
160168
}
169+
161170
$wheres = (array)$builder->wheres;
162171
$i = 0;
163-
for ($ind = 0; $ind < count($wheres); $ind++ ) {
172+
$wheresCount = count($wheres);
173+
174+
for ($ind = 0; $ind < $wheresCount; $ind++ ) {
164175
if (isset($wheres[$ind]['value'])) {
165-
if (in_array(strtolower($tipos[strtolower($wheres[$ind]['column'])]), $this->without_quotes)) {
166-
if (!is_null($bindings[$i])) {
167-
$new_binds[$i] = $bindings[$i]/1;
176+
if (is_object($wheres[$ind]['value']) === false) {
177+
if (in_array(strtolower($tipos[strtolower($wheres[$ind]['column'])]), $this->without_quotes)) {
178+
if (!is_null($bindings[$i])) {
179+
$new_binds[$i] = $bindings[$i]/1;
180+
} else {
181+
$new_binds[$i] = null;
182+
}
168183
} else {
169-
$new_binds[$i] = null;
184+
$new_binds[$i] = (string)$bindings[$i];
170185
}
171-
} else {
172-
$new_binds[$i] = (string)$bindings[$i];
186+
$i++;
173187
}
174-
$i++;
175188
}
176189
}
177190

@@ -185,32 +198,32 @@ private function queryStringForSelect($tables)
185198
return <<<QUERY
186199
select a.name,
187200
b.name AS customtype,
188-
st.name as type
201+
st.name as type
189202
FROM {$explicitDB[0]}..syscolumns a, {$explicitDB[0]}..systypes b, {$explicitDB[0]}..systypes s, {$explicitDB[0]}..systypes st
190-
WHERE a.usertype = b.usertype
203+
WHERE a.usertype = b.usertype
191204
AND s.usertype = a.usertype
192205
AND s.type = st.type
193206
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
194-
AND st.usertype < 100
207+
AND st.usertype < 100
195208
AND object_name(a.id, db_id('{$explicitDB[0]}')) = '{$explicitDB[1]}'
196209
QUERY;
197210
} else {
198211
return <<<QUERY
199212
select a.name, st.name as type
200213
FROM syscolumns a, systypes b, systypes s, systypes st
201-
WHERE a.usertype = b.usertype
214+
WHERE a.usertype = b.usertype
202215
AND s.usertype = a.usertype
203216
AND s.type = st.type
204217
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
205-
AND st.usertype < 100
218+
AND st.usertype < 100
206219
AND object_name(a.id) = '{$tables}'
207220
QUERY;
208221
}
209222
}
210223

211224
/**
212225
* Set new bindings with specified column types to Sybase
213-
*
226+
*
214227
* @param string $query
215228
* @param array $bindings
216229
* @return mixed $new_binds
@@ -243,7 +256,7 @@ private function compileBindings($query, $bindings)
243256
break;
244257
default:
245258
return $bindings;
246-
break;
259+
break;
247260
}
248261

249262
$desQuery = array_intersect_key($matches, array_flip(array_filter(array_keys($matches), 'is_string')));
@@ -316,34 +329,34 @@ private function queryStringForCompileBindings($table)
316329
$explicitDB = explode('..', $table);
317330
if (isset($explicitDB[1])) {
318331
return <<<QUERY
319-
select a.name,
332+
select a.name,
320333
b.name AS customtype,
321-
st.name as type
334+
st.name as type
322335
FROM {$explicitDB[0]}..syscolumns a, {$explicitDB[0]}..systypes b, {$explicitDB[0]}..systypes s, {$explicitDB[0]}..systypes st
323-
WHERE a.usertype = b.usertype
336+
WHERE a.usertype = b.usertype
324337
AND s.usertype = a.usertype
325338
AND s.type = st.type
326339
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
327-
AND st.usertype < 100
340+
AND st.usertype < 100
328341
AND object_name(a.id, db_id('{$explicitDB[0]}')) = '{$explicitDB[1]}'
329342
QUERY;
330343
} else {
331344
return <<<QUERY
332345
select a.name, st.name as type
333346
FROM syscolumns a, systypes b, systypes s, systypes st
334-
WHERE a.usertype = b.usertype
347+
WHERE a.usertype = b.usertype
335348
AND s.usertype = a.usertype
336349
AND s.type = st.type
337350
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
338-
AND st.usertype < 100
351+
AND st.usertype < 100
339352
AND object_name(a.id) = '{$table}'
340353
QUERY;
341354
}
342355
}
343356

344357
/**
345358
* Set new bindings with specified column types to Sybase
346-
*
359+
*
347360
* @param string $query
348361
* @param array $bindings
349362
* @return string $query
@@ -353,7 +366,7 @@ private function queryStringForCompileBindings($table)
353366
// Detalhes: http://stackoverflow.com/questions/2718628/pdoparam-for-type-decimal
354367
private function compileNewQuery($query, $bindings)
355368
{
356-
$newQuery = "";
369+
$newQuery = "";
357370
$bindings = $this->compileBindings($query, $bindings);
358371
$partQuery = explode("?", $query);
359372
for ($i = 0; $i<count($partQuery); $i++) {
@@ -372,13 +385,13 @@ private function compileNewQuery($query, $bindings)
372385
}
373386
}
374387
$newQuery = str_replace( "[]", '' ,$newQuery);
375-
return $newQuery;
388+
return $newQuery;
376389
}
377390

378391
public function compileOffset($offset, $query, $bindings = array(), $me)
379392
{
380393
$limit = $this->queryGrammar->getBuilder()->limit;
381-
$from = explode(" ", $this->queryGrammar->getBuilder()->from)[0];
394+
$from = explode(" ", $this->queryGrammar->getBuilder()->from)[0];
382395
if (!isset($limit)) {
383396
$limit = 999999999999999999999999999;
384397
}
@@ -401,11 +414,11 @@ public function compileOffset($offset, $query, $bindings = array(), $me)
401414
$this->getPdo()->query(str_replace(" from ", " into #tmpPaginate from ", $this->compileNewQuery($query, $bindings)));
402415
$this->getPdo()->query("SELECT ".$res_primaries.", idTmp=identity(18) INTO #tmpTable FROM #tmpPaginate");
403416
return $this->getPdo()->query("SELECT #tmpPaginate.*, #tmpTable.idTmp FROM #tmpTable INNER JOIN #tmpPaginate ON ".$where_primaries." WHERE #tmpTable.idTmp "
404-
. "BETWEEN ".($offset+1) ." AND ". ($offset+$limit)
417+
. "BETWEEN ".($offset+1) ." AND ". ($offset+$limit)
405418
." ORDER BY #tmpTable.idTmp ASC")->fetchAll($me->getFetchMode());
406-
419+
407420
}
408-
}
421+
}
409422
private function queryStringForIdentity($from)
410423
{
411424
$explicitDB = explode('..', $from);
@@ -432,7 +445,7 @@ private function queryStringForPrimaries($from)
432445
WHERE i.id = c.id AND c.colid <= i.keycnt AND i.id = object_id('".$from."')";
433446
}
434447
}
435-
448+
436449
/**
437450
* Run a select statement against the database.
438451
*
@@ -455,9 +468,9 @@ public function select($query, $bindings = array(), $useReadPdo = true)
455468
}
456469
if ($offset > 0) {
457470
return $this->compileOffset($offset, $query, $bindings, $me);
458-
} else {
471+
} else {
459472
$result = [];
460-
$statement = $this->getPdo()->query($this->compileNewQuery($query, $bindings));
473+
$statement = $this->getPdo()->query($this->compileNewQuery($query, $bindings));
461474
do {
462475
$result+= $statement->fetchAll($me->getFetchMode());
463476
} while ($statement->nextRowset());
@@ -467,7 +480,7 @@ public function select($query, $bindings = array(), $useReadPdo = true)
467480
}
468481

469482

470-
/**
483+
/**
471484
* @param string $query
472485
* @param mixed array $bindings
473486
* @return bool
@@ -484,7 +497,7 @@ public function statement($query, $bindings = array())
484497
}
485498

486499
public function affectingStatement($query, $bindings = array())
487-
{
500+
{
488501
return $this->run($query, $bindings, function($me, $query, $bindings)
489502
{
490503
if ($me->pretending()) {

0 commit comments

Comments
 (0)