Skip to content

Commit 39f888a

Browse files
author
Gabriel Tadra Mainginski
committed
Almost there! Few details to fix #5 and #4.
1 parent 2131c95 commit 39f888a

File tree

1 file changed

+84
-51
lines changed

1 file changed

+84
-51
lines changed

Database/SybaseConnection.php

Lines changed: 84 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Uepg\LaravelSybase\Database\Query\SybaseGrammar as QueryGrammar;
88
use Uepg\LaravelSybase\Database\Schema\SybaseGrammar as SchemaGrammar;
99
use Illuminate\Database\Connection;
10+
use Illuminate\Database\Query\Builder;
1011

1112
class SybaseConnection extends Connection {
1213

@@ -93,6 +94,50 @@ protected function getDoctrineDriver()
9394
return new DoctrineDriver;
9495
}
9596

97+
private function compileForSelect(Builder $builder, $bindings) {
98+
99+
if(count($bindings)==0){
100+
return [];
101+
}
102+
103+
$bindings = $this->prepareBindings($bindings);
104+
105+
106+
$arrTables = [];
107+
array_push($arrTables, $builder->from);
108+
//var_dump($builder);
109+
if(!empty($builder->joins)){
110+
foreach($builder->joins as $join){
111+
array_push($arrTables, $join->table);
112+
}
113+
}
114+
$new_format = [];
115+
foreach($arrTables as $tables){
116+
117+
$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."'");
118+
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
119+
120+
foreach ($types[$tables] as &$row) {
121+
$tipos[$row['name']] = $row['type'];
122+
$tipos[$tables.'.'.$row['name']] = $row['type'];
123+
}
124+
125+
$new_format[$tables] = [];
126+
127+
}
128+
$wheres = (array)$builder->wheres;
129+
var_dump($wheres);
130+
for($ind = 0; $ind < count($wheres); $ind++ ){
131+
132+
if(in_array(strtolower($tipos[$wheres[$ind]['column']]), $this->without_quotes)){
133+
$new_binds[$ind] = $bindings[$ind]/1;
134+
}else{
135+
$new_binds[$ind] = (string)$bindings[$ind];
136+
}
137+
}
138+
139+
return $new_binds;
140+
}
96141

97142
/**
98143
* Set new bindings with specified column types to Sybase
@@ -103,30 +148,18 @@ protected function getDoctrineDriver()
103148
*/
104149
private function compileBindings($query, $bindings)
105150
{
106-
var_dump($query);
151+
152+
107153
if(count($bindings)==0){
108154
return [];
109155
}
110156

111157
$bindings = $this->prepareBindings($bindings);
112158
$new_format = [];
113159

114-
switch(explode(' ', $query)[0]){
160+
switch(\explode(' ', $query)[0]){
115161
case "select":
116-
preg_match_all("/(?:from |join )(?'tables'.*?)(?: (?:on(?:(?!join ).)*|)where(?'attributes'.*)| on|$)/i" ,$query, $matches);
117-
$selects = explode('from', $query)[0];
118-
$selects = explode(',', $selects);
119-
120-
foreach($selects as $ind->$arrSelect){
121-
$arrSelects[$ind] = trim($arrSelect);
122-
if($arrSelect = '*'){
123-
break;
124-
}else{
125-
126-
}
127-
}
128-
var_dump($selects);
129-
break;
162+
return $this->compileForSelect($this->queryGrammar->getBuilder(), $bindings);
130163
case "insert":
131164
preg_match("/(?'tables'.*) \((?'attributes'.*)\) values/i" ,$query, $matches);
132165
break;
@@ -136,20 +169,23 @@ private function compileBindings($query, $bindings)
136169
case "delete":
137170
preg_match("/(?'tables'.*) where (?'attributes'.*)/i" ,$query, $matches);
138171
break;
172+
default:
173+
return [];
139174
}
140-
if(is_array($matches['tables'])){
141-
$desQuery['tables'] = implode($matches['tables'], ' ');
142-
}else if(isset($matches['tables'])){
143-
$desQuery['tables'] = $matches['tables'];
144-
}
145175

146-
if(is_array($matches['attributes'])){
147-
$desQuery['attributes'] = implode($matches['attributes'], ' ');
148-
}else if(isset($matches['attributes'])){
149-
$desQuery['attributes'] = $matches['attributes'];
176+
$desQuery = array_intersect_key($matches, array_flip(array_filter(array_keys($matches), 'is_string')));
177+
178+
179+
180+
if(is_array($desQuery['tables'])){
181+
$desQuery['tables'] = implode($desQuery['tables'], ' ');
182+
}
183+
if(is_array($desQuery['attributes'])){
184+
$desQuery['attributes'] = implode($desQuery['attributes'], ' ');
150185
}
151186

152187
unset($matches);
188+
unset($query_type);
153189
preg_match_all("/\[([^\]]*)\]/", $desQuery['attributes'], $arrQuery);
154190
preg_match_all("/\[([^\]]*)\]/", $desQuery['tables'], $arrTables);
155191

@@ -158,35 +194,31 @@ private function compileBindings($query, $bindings)
158194
$ind = 0;
159195
$numTables = count($arrTables);
160196

161-
if($numTables == 0){
197+
if($numTables == 1){
198+
$table = $arrTables[0];
199+
}else if($numTables == 0){
162200
return $bindings;
163201
}
164202

165-
166-
foreach($arrTables as $tables){
167-
$table = $tables;
168-
169-
if(!array_key_exists($table, $new_format)){
170-
$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."'");
171-
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
172-
foreach ($types[$tables] as &$row) {
173-
$types[$tables][$row['name']] = &$row;
203+
foreach($arrQuery as $key=>$campos){
204+
$itsTable = in_array($campos, $arrTables);
205+
206+
if($itsTable || ($numTables == 1 && isset($table) && $key == 0)){
207+
if($numTables > 1){
208+
$table = $campos;
209+
}
210+
if(!array_key_exists($table, $new_format)){
211+
$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+
$types[$table] = $queryRes->fetchAll(\PDO::FETCH_ASSOC);
213+
for($k = 0; $k < count($types[$table]); $k++){
214+
$types[$table][$types[$table][$k]['name']] = $types[$table][$k];
215+
unset($types[$table][$k]);
216+
}
217+
$new_format[$table] = [];
174218
}
175-
176-
$new_format[$tables] = [];
177-
}
178-
}
179-
180-
foreach($arrQuery as $key->$campos){
181-
if(isset($arrQuery[$key-1]) && in_array($arrQuery[$key-1], $arrTables)){
182-
$table = $arrQuery[$key-1];
183-
continue;
184-
}else{
185-
186219
}
187-
if(in_array($campos, $arrTables)){
188-
if($campos!=$table) $table = $campos;
189-
}else{
220+
221+
if(!$itsTable){
190222
if(count($bindings)>$ind){
191223
array_push($new_format[$table], ['campo' => $campos, 'binding' => $ind]);
192224
if(in_array(strtolower($types[$table][$campos]['type']), $this->without_quotes)){
@@ -200,6 +232,7 @@ private function compileBindings($query, $bindings)
200232
$ind++;
201233
}
202234
}
235+
203236
return $new_binds;
204237
}
205238
/**
@@ -215,9 +248,8 @@ private function compileBindings($query, $bindings)
215248
private function compileNewQuery($query, $bindings)
216249
{
217250
$time_start = microtime(true);
218-
$bindings = $this->compileBindings($query, $bindings);
219-
220251
$newQuery = "";
252+
$bindings = $this->compileBindings($query, $bindings);
221253
$partQuery = explode("?", $query);
222254
for($i = 0; $i<count($partQuery); $i++){
223255
$newQuery .= $partQuery[$i];
@@ -231,6 +263,7 @@ private function compileNewQuery($query, $bindings)
231263
}
232264
}
233265
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start).'<br>';
266+
var_dump($newQuery);
234267
return $newQuery;
235268
}
236269

0 commit comments

Comments
 (0)