Skip to content

Commit 15cc18e

Browse files
author
Simon Champion
committed
Refactored several if() blocks building alternate query strings into separate functions.
1 parent 7c6caa7 commit 15cc18e

File tree

1 file changed

+105
-60
lines changed

1 file changed

+105
-60
lines changed

Database/SybaseConnection.php

Lines changed: 105 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,9 @@ private function compileForSelect(Builder $builder, $bindings) {
118118
} else {
119119
$tables = $alias['table'];
120120
}
121-
122-
$explicitDB = explode('..', $tables);
123-
if (isset($explicitDB[1])) {
124-
$queryRes = $this->getPdo()->query("select a.name,
125-
b.name AS customtype,
126-
st.name as type
127-
FROM ".$explicitDB[0]."..syscolumns a, ".$explicitDB[0]."..systypes b, ".$explicitDB[0]."..systypes s, ".$explicitDB[0]."..systypes st
128-
WHERE a.usertype = b.usertype
129-
AND s.usertype = a.usertype
130-
AND s.type = st.type
131-
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
132-
AND st.usertype < 100
133-
AND object_name(a.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
134-
}else{
135-
$queryRes = $this->getPdo()->query("select a.name, st.name as type
136-
FROM syscolumns a, systypes b, systypes s, systypes st
137-
WHERE a.usertype = b.usertype
138-
AND s.usertype = a.usertype
139-
AND s.type = st.type
140-
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
141-
AND st.usertype < 100
142-
AND object_name(a.id) = '".$tables."'");
143-
}
144121

122+
$queryString = $this->queryStringForSelect($tables);
123+
$queryRes = $this->getPdo()->query($queryString);
145124
$types[$tables] = $queryRes->fetchAll(\PDO::FETCH_NAMED);
146125

147126
foreach ($types[$tables] as &$row) {
@@ -151,7 +130,7 @@ private function compileForSelect(Builder $builder, $bindings) {
151130
$tipos[strtolower($alias['alias'].'.'.$row['name'])] = $row['type'];
152131
}
153132
}
154-
133+
155134
$new_format[$tables] = [];
156135
}
157136
$wheres = (array)$builder->wheres;
@@ -173,7 +152,37 @@ private function compileForSelect(Builder $builder, $bindings) {
173152

174153
return $new_binds;
175154
}
176-
155+
156+
private function queryStringForSelect($tables)
157+
{
158+
$explicitDB = explode('..', $tables);
159+
if (isset($explicitDB[1])) {
160+
return <<<QUERY
161+
select a.name,
162+
b.name AS customtype,
163+
st.name as type
164+
FROM {$explicitDB[0]}..syscolumns a, {$explicitDB[0]}..systypes b, {$explicitDB[0]}..systypes s, {$explicitDB[0]}..systypes st
165+
WHERE a.usertype = b.usertype
166+
AND s.usertype = a.usertype
167+
AND s.type = st.type
168+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
169+
AND st.usertype < 100
170+
AND object_name(a.id, db_id('{$explicitDB[0]}')) = '{$explicitDB[1]}'
171+
QUERY;
172+
} else {
173+
return <<<QUERY
174+
select a.name, st.name as type
175+
FROM syscolumns a, systypes b, systypes s, systypes st
176+
WHERE a.usertype = b.usertype
177+
AND s.usertype = a.usertype
178+
AND s.type = st.type
179+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
180+
AND st.usertype < 100
181+
AND object_name(a.id) = '{$tables}'
182+
QUERY;
183+
}
184+
}
185+
177186
/**
178187
* Set new bindings with specified column types to Sybase
179188
*
@@ -245,28 +254,7 @@ private function compileBindings($query, $bindings)
245254
$table = $campos;
246255
}
247256
if (!array_key_exists($table, $new_format)) {
248-
$explicitDB = explode('..', $table);
249-
if (isset($explicitDB[1])) {
250-
$queryRes = $this->getPdo()->query("select a.name,
251-
b.name AS customtype,
252-
st.name as type
253-
FROM ".$explicitDB[0]."..syscolumns a, ".$explicitDB[0]."..systypes b, ".$explicitDB[0]."..systypes s, ".$explicitDB[0]."..systypes st
254-
WHERE a.usertype = b.usertype
255-
AND s.usertype = a.usertype
256-
AND s.type = st.type
257-
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
258-
AND st.usertype < 100
259-
AND object_name(a.id, db_id('".$explicitDB[0]."')) = '".$explicitDB[1]."'");
260-
} else {
261-
$queryRes = $this->getPdo()->query("select a.name, st.name as type
262-
FROM syscolumns a, systypes b, systypes s, systypes st
263-
WHERE a.usertype = b.usertype
264-
AND s.usertype = a.usertype
265-
AND s.type = st.type
266-
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
267-
AND st.usertype < 100
268-
AND object_name(a.id) = '".$table."'");
269-
}
257+
$queryRes = $this->getPdo()->query($this->queryStringForCompileBindings($table));
270258
$types[$table] = $queryRes->fetchAll(\PDO::FETCH_ASSOC);
271259
for ($k = 0; $k < count($types[$table]); $k++) {
272260
$types[$table][$types[$table][$k]['name']] = $types[$table][$k];
@@ -275,6 +263,7 @@ private function compileBindings($query, $bindings)
275263
$new_format[$table] = [];
276264
}
277265
}
266+
278267
if (!$itsTable) {
279268
if (count($bindings)>$ind) {
280269
array_push($new_format[$table], ['campo' => $campos, 'binding' => $ind]);
@@ -297,6 +286,36 @@ private function compileBindings($query, $bindings)
297286
return $new_binds;
298287
}
299288

289+
private function queryStringForCompileBindings($table)
290+
{
291+
$explicitDB = explode('..', $table);
292+
if (isset($explicitDB[1])) {
293+
return <<<QUERY
294+
select a.name,
295+
b.name AS customtype,
296+
st.name as type
297+
FROM {$explicitDB[0]}..syscolumns a, {$explicitDB[0]}..systypes b, {$explicitDB[0]}..systypes s, {$explicitDB[0]}..systypes st
298+
WHERE a.usertype = b.usertype
299+
AND s.usertype = a.usertype
300+
AND s.type = st.type
301+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
302+
AND st.usertype < 100
303+
AND object_name(a.id, db_id('{$explicitDB[0]}')) = '{$explicitDB[1]}'
304+
QUERY;
305+
} else {
306+
return <<<QUERY
307+
select a.name, st.name as type
308+
FROM syscolumns a, systypes b, systypes s, systypes st
309+
WHERE a.usertype = b.usertype
310+
AND s.usertype = a.usertype
311+
AND s.type = st.type
312+
AND st.name not in ('timestamp', 'sysname', 'longsysname', 'nchar', 'nvarchar')
313+
AND st.usertype < 100
314+
AND object_name(a.id) = '{$table}'
315+
QUERY;
316+
}
317+
}
318+
300319
/**
301320
* Set new bindings with specified column types to Sybase
302321
*
@@ -338,18 +357,12 @@ public function compileOffset($offset, $query, $bindings = array(), $me)
338357
if (!isset($limit)) {
339358
$limit = 999999999999999999999999999;
340359
}
341-
$explicitDB = explode('..', $from);
342-
if (isset($explicitDB[1])) {
343-
$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];
344-
} else {
345-
$identity = $this->getPdo()->query("select name as 'column' from syscolumns where status & 128 = 128 AND object_name(id)='".$from."'")->fetchAll($me->getFetchMode())[0];
346-
}
360+
$queryString = $this->queryStringForIdentity($from);
361+
$identity = $this->getPdo()->query($queryString)->fetchAll($me->getFetchMode())[0];
362+
347363
if (count($identity) === 0) {
348-
if (isset($explicitDB[1])) {
349-
$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());
350-
} else {
351-
$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());
352-
}
364+
$queryString = $this->queryStringForPrimaries($from);
365+
$primaries = $this->getPdo()->query($queryString)->fetchAll($me->getFetchMode());
353366
foreach ($primaries as $primary) {
354367
$new_arr[] = $primary->primary_key.'+0 AS '.$primary->primary_key;
355368
$where_arr[] = "#tmpPaginate.".$primary->primary_key.' = #tmpTable.'.$primary->primary_key;
@@ -369,6 +382,34 @@ public function compileOffset($offset, $query, $bindings = array(), $me)
369382
." ORDER BY #tmpTable.idTmp ASC")->fetchAll($me->getFetchMode());
370383

371384
}
385+
386+
private function queryStringForIdentity($from)
387+
{
388+
$explicitDB = explode('..', $from);
389+
if (isset($explicitDB[1])) {
390+
return "select b.name as 'column'
391+
from ".$explicitDB[0]."..syscolumns AS b INNER JOIN ".$explicitDB[0]."..sysobjects AS a
392+
ON a.id = b.id WHERE status & 128 = 128 AND a.name ='".$explicitDB[1]."'";
393+
} else {
394+
return "select name as 'column' from syscolumns
395+
where status & 128 = 128 AND object_name(id)='".$from."'";
396+
}
397+
}
398+
399+
private function queryStringForPrimaries($from)
400+
{
401+
$explicitDB = explode('..', $from);
402+
if (isset($explicitDB[1])) {
403+
return "SELECT index_col(".$from.", i.indid, c.colid) AS primary_key
404+
FROM ".$explicitDB[0]."..sysindexes i, ".$explicitDB[0]."..syscolumns c
405+
WHERE i.id = c.id AND c.colid <= i.keycnt AND i.id = object_id('".$from."')";
406+
} else {
407+
return "SELECT index_col(".$from.", i.indid, c.colid) AS primary_key
408+
FROM sysindexes i, syscolumns c
409+
WHERE i.id = c.id AND c.colid <= i.keycnt AND i.id = object_id('".$from."')";
410+
}
411+
}
412+
372413
/**
373414
* Run a select statement against the database.
374415
*
@@ -413,7 +454,9 @@ public function statement($query, $bindings = array())
413454
{
414455
return $this->run($query, $bindings, function($me, $query, $bindings)
415456
{
416-
if ($me->pretending()) return true;
457+
if ($me->pretending()) {
458+
return true;
459+
}
417460
return $this->getPdo()->query($this->compileNewQuery($query, $bindings));
418461
});
419462
}
@@ -422,7 +465,9 @@ public function affectingStatement($query, $bindings = array())
422465
{
423466
return $this->run($query, $bindings, function($me, $query, $bindings)
424467
{
425-
if ($me->pretending()) return 0;
468+
if ($me->pretending()) {
469+
return 0;
470+
}
426471
return $this->getPdo()->query($this->compileNewQuery($query, $bindings))->rowCount();
427472
});
428473
}

0 commit comments

Comments
 (0)