Skip to content

Commit 3e1cc8b

Browse files
author
Gabriel Tadra Mainginski
committed
Fix #4 and #5.
1 parent 61f00cd commit 3e1cc8b

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

Database/SybaseConnection.php

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php namespace Uepg\LaravelSybase\Database;
2-
32
use Closure;
43
use Exception;
54
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as DoctrineDriver;
@@ -8,7 +7,6 @@
87
use Uepg\LaravelSybase\Database\Schema\SybaseGrammar as SchemaGrammar;
98
use Illuminate\Database\Connection;
109
use Illuminate\Database\Query\Builder;
11-
1210
class SybaseConnection extends Connection {
1311

1412

@@ -28,32 +26,25 @@ public function transaction(Closure $callback)
2826
{
2927
return parent::transaction($callback);
3028
}
31-
3229
$this->pdo->exec('BEGIN TRAN');
33-
3430
// We'll simply execute the given callback within a try / catch block
3531
// and if we catch any exception we can rollback the transaction
3632
// so that none of the changes are persisted to the database.
3733
try
3834
{
3935
$result = $callback($this);
40-
4136
$this->pdo->exec('COMMIT TRAN');
4237
}
43-
4438
// If we catch an exception, we will roll back so nothing gets messed
4539
// up in the database. Then we'll re-throw the exception so it can
4640
// be handled how the developer sees fit for their applications.
4741
catch (Exception $e)
4842
{
4943
$this->pdo->exec('ROLLBACK TRAN');
50-
5144
throw $e;
5245
}
53-
5446
return $result;
5547
}
56-
5748
/**
5849
* Get the default query grammar instance.
5950
*
@@ -63,7 +54,6 @@ protected function getDefaultQueryGrammar()
6354
{
6455
return $this->withTablePrefix(new QueryGrammar);
6556
}
66-
6757
/**
6858
* Get the default schema grammar instance.
6959
*
@@ -73,7 +63,6 @@ protected function getDefaultSchemaGrammar()
7363
{
7464
return $this->withTablePrefix(new SchemaGrammar);
7565
}
76-
7766
/**
7867
* Get the default post processor instance.
7968
*
@@ -83,7 +72,6 @@ protected function getDefaultPostProcessor()
8372
{
8473
return new SqlServerProcessor;
8574
}
86-
8775
/**
8876
* Get the Doctrine DBAL Driver.
8977
*
@@ -93,39 +81,50 @@ protected function getDoctrineDriver()
9381
{
9482
return new DoctrineDriver;
9583
}
96-
9784
private function compileForSelect(Builder $builder, $bindings) {
9885

9986
if(count($bindings)==0){
10087
return [];
10188
}
102-
10389
$bindings = $this->prepareBindings($bindings);
104-
10590

10691
$arrTables = [];
10792
array_push($arrTables, $builder->from);
10893
if(!empty($builder->joins)){
10994
foreach($builder->joins as $join){
95+
11096
array_push($arrTables, $join->table);
11197
}
11298
}
11399
$new_format = [];
114100
foreach($arrTables as $tables){
115-
116-
$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."'");
117-
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
101+
preg_match("/(?:(?'table'.*)(?: as )(?'alias'.*))|(?'tables'.*)/", $tables, $alias);
102+
if(empty($alias['alias'])){
103+
$tables = $alias['tables'];
104+
}else{
105+
$tables = $alias['table'];
106+
}
118107

108+
$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."'");
109+
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
110+
119111
foreach ($types[$tables] as &$row) {
120-
$tipos[$row['name']] = $row['type'];
121-
$tipos[$tables.'.'.$row['name']] = $row['type'];
112+
$tipos[$row['name']] = $row['type'];
113+
$tipos[$tables.'.'.$row['name']] = $row['type'];
114+
if(!empty($alias['alias'])){
115+
$tipos[$alias['alias'].'.'.$row['name']] = $row['type'];
116+
}
122117
}
123118

124119
$new_format[$tables] = [];
125-
126120
}
127121
$wheres = (array)$builder->wheres;
128122
for($ind = 0; $ind < count($wheres); $ind++ ){
123+
if(!isset($wheres[$ind]['value'])){
124+
$ind++;
125+
unset($wheres[$ind]);
126+
break;
127+
}
129128

130129
if(in_array(strtolower($tipos[$wheres[$ind]['column']]), $this->without_quotes)){
131130
$new_binds[$ind] = $bindings[$ind]/1;
@@ -167,14 +166,10 @@ private function compileBindings($query, $bindings)
167166
case "delete":
168167
preg_match("/(?'tables'.*) where (?'attributes'.*)/i" ,$query, $matches);
169168
break;
170-
default:
171-
return [];
172169
}
173170

174171
$desQuery = array_intersect_key($matches, array_flip(array_filter(array_keys($matches), 'is_string')));
175172

176-
177-
178173
if(is_array($desQuery['tables'])){
179174
$desQuery['tables'] = implode($desQuery['tables'], ' ');
180175
}
@@ -292,7 +287,6 @@ public function statement($query, $bindings = array()) {
292287
return $this->getPdo()->query($this->compileNewQuery($query, $bindings));
293288
});
294289
}
295-
296290
public function affectingStatement($query, $bindings = array())
297291
{
298292
return $this->run($query, $bindings, function($me, $query, $bindings)
@@ -301,4 +295,4 @@ public function affectingStatement($query, $bindings = array())
301295
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->rowCount();
302296
});
303297
}
304-
}
298+
}

0 commit comments

Comments
 (0)