Skip to content

Commit 122dd1b

Browse files
committed
Merge branch 'master' into dev
2 parents 863055b + c760494 commit 122dd1b

File tree

2 files changed

+122
-86
lines changed

2 files changed

+122
-86
lines changed

Database/SybaseConnection.php

Lines changed: 121 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SybaseConnection extends Connection {
1111

1212

1313
// All types without quotes in Sybase's query
14-
private $without_quotes = ['int' , 'numeric', 'bigint', 'integer' , 'smallint', 'tinyint', 'decimal', 'double', 'float', 'real', 'bit', 'binary', 'varbinary', 'timestamp'];
14+
private $without_quotes = ['int' , 'numeric', 'bigint', 'integer' , 'smallint', 'tinyint', 'decimal', 'double', 'float', 'real', 'bit', 'binary', 'varbinary', 'timestamp', 'money'];
1515
/**
1616
* Execute a Closure within a transaction.
1717
*
@@ -20,7 +20,7 @@ class SybaseConnection extends Connection {
2020
*
2121
* @throws \Exception
2222
*/
23-
public function transaction(Closure $callback)
23+
public function transaction(Closure $callback, $attempts = 1)
2424
{
2525
if ($this->getDriverName() == 'sqlsrv')
2626
{
@@ -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,18 +100,34 @@ private function compileForSelect(Builder $builder, $bindings) {
104100

105101
$explicitDB = explode('..', $tables);
106102
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."'");
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);
113125

114126
foreach ($types[$tables] as &$row) {
115-
$tipos[$row['name']] = $row['type'];
116-
$tipos[$tables.'.'.$row['name']] = $row['type'];
127+
$tipos[strtolower($row['name'])] = $row['type'];
128+
$tipos[strtolower($tables.'.'.$row['name'])] = $row['type'];
117129
if(!empty($alias['alias'])){
118-
$tipos[$alias['alias'].'.'.$row['name']] = $row['type'];
130+
$tipos[strtolower($alias['alias'].'.'.$row['name'])] = $row['type'];
119131
}
120132
}
121133

@@ -125,8 +137,12 @@ private function compileForSelect(Builder $builder, $bindings) {
125137
$i = 0;
126138
for($ind = 0; $ind < count($wheres); $ind++ ){
127139
if(isset($wheres[$ind]['value'])){
128-
if(in_array(strtolower($tipos[$wheres[$ind]['column']]), $this->without_quotes)){
129-
$new_binds[$i] = $bindings[$i]/1;
140+
if(in_array(strtolower($tipos[strtolower($wheres[$ind]['column'])]), $this->without_quotes)){
141+
if(!is_null($bindings[$i])){
142+
$new_binds[$i] = $bindings[$i]/1;
143+
}else{
144+
$new_binds[$i] = null;
145+
}
130146
}else{
131147
$new_binds[$i] = (string)$bindings[$i];
132148
}
@@ -211,9 +227,26 @@ private function compileBindings($query, $bindings)
211227
if(!array_key_exists($table, $new_format)){
212228
$explicitDB = explode('..', $table);
213229
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."'");
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+
241+
}else{
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."'");
217250
}
218251
$types[$table] = $queryRes->fetchAll(\PDO::FETCH_ASSOC);
219252
for($k = 0; $k < count($types[$table]); $k++){
@@ -223,12 +256,15 @@ private function compileBindings($query, $bindings)
223256
$new_format[$table] = [];
224257
}
225258
}
226-
echo $table;
227259
if(!$itsTable){
228260
if(count($bindings)>$ind){
229261
array_push($new_format[$table], ['campo' => $campos, 'binding' => $ind]);
230262
if(in_array(strtolower($types[$table][$campos]['type']), $this->without_quotes)){
231-
$new_binds[$ind] = $bindings[$ind]/1;
263+
if(!is_null($bindings[$ind])){
264+
$new_binds[$ind] = $bindings[$ind]/1;
265+
}else{
266+
$new_binds[$ind] = null;
267+
}
232268
}else{
233269
$new_binds[$ind] = (string)$bindings[$ind];
234270
}
@@ -257,57 +293,60 @@ private function compileNewQuery($query, $bindings)
257293
$bindings = $this->compileBindings($query, $bindings);
258294
$partQuery = explode("?", $query);
259295
for($i = 0; $i<count($partQuery); $i++){
260-
$newQuery .= $partQuery[$i];
261-
if($i<count($bindings)){
262-
if(is_string($bindings[$i])){
263-
$bindings[$i] = str_replace( "'", "''", $bindings[$i] );
264-
$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];
265304
}else{
266-
$newQuery .= $bindings[$i];
305+
$newQuery .= 'null';
267306
}
268307
}
308+
}
269309
}
270310
$newQuery = str_replace( "[]", '' ,$newQuery);
271311
return $newQuery;
272312
}
273313

274314
public function compileOffset($offset, $query, $bindings = array(), $me){
275-
276-
$limit = $this->queryGrammar->getBuilder()->limit;
277-
$from = explode(" ", $this->queryGrammar->getBuilder()->from)[0];
278-
if(!isset($limit)){
279-
$limit = 999999999999999999999999999;
280-
}
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-
}
293-
foreach($primaries as $primary)
294-
{
295-
$new_arr[] = $primary->primary_key.'+0 AS '.$primary->primary_key;
296-
$where_arr[] = "#tmpPaginate.".$primary->primary_key.' = #tmpTable.'.$primary->primary_key;
297-
}
298-
$res_primaries = implode(', ',$new_arr);
299-
$where_primaries = implode(' AND ',$where_arr);
300-
}else{
301-
$res_primaries = $identity->column.'+0 AS '.$identity->column;
302-
$where_primaries = "#tmpPaginate.".$identity->column.' = #tmpTable.'.$identity->column;
303-
}
304-
305-
//Offset operation
306-
$this->getPdo()->query(str_replace(" from ", " into #tmpPaginate from ", $this->compileNewQuery($query, $bindings)));
307-
$this->getPdo()->query("SELECT ".$res_primaries.", idTmp=identity(18) INTO #tmpTable FROM #tmpPaginate");
308-
return $this->getPdo()->query("SELECT #tmpPaginate.*, #tmpTable.idTmp FROM #tmpTable INNER JOIN #tmpPaginate ON ".$where_primaries." WHERE #tmpTable.idTmp "
309-
. "BETWEEN ".($offset+1) ." AND ". ($offset+$limit)
310-
." 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());
311350

312351
}
313352
/**
@@ -320,22 +359,27 @@ public function compileOffset($offset, $query, $bindings = array(), $me){
320359
*/
321360
public function select($query, $bindings = array(), $useReadPdo = true)
322361
{
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;
329-
}else{
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());
337-
}
338-
});
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+
$result = [];
376+
$statement = $this->getPdo()->query($this->compileNewQuery($query, $bindings));
377+
do {
378+
$result+= $statement->fetchAll($me->getFetchMode());
379+
} while ($statement->nextRowset());
380+
return $result;
381+
}
382+
});
339383
}
340384

341385
/**

Database/SybaseServiceProvider.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@ class SybaseServiceProvider extends ServiceProvider
1212
*/
1313
public function register()
1414
{
15-
// Register the MySql connection class as a singleton
16-
// because we only want to have one, and only one,
17-
// MySql database connection at the same time.
18-
$this->app->singleton('db.connection.sqlsrv', function ($app, $parameters) {
19-
// First, we list the passes parameters into single
20-
// variables. I do this because it is far easier
21-
// to read than using it as eg $parameters[0].
15+
$this->app->bind('db.connection.sqlsrv', function ($app, $parameters) {
2216
list($connection, $database, $prefix, $config) = $parameters;
23-
24-
// Next we can initialize the connection.
2517
return new SybaseConnection($connection, $database, $prefix, $config);
2618
});
2719
}

0 commit comments

Comments
 (0)