Skip to content

Commit

Permalink
Fix select when raw where is passed, fix #52
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomazer authored Jun 21, 2019
1 parent 7e1c0d9 commit 17dc5dc
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function getDoctrineDriver()
}

/**
* Compile for select.
* Compile the bindings for select.
*
* @param \Illuminate\Database\Query\Builder $builder
* @param array $bindings
Expand Down Expand Up @@ -148,15 +148,13 @@ private function compileForSelect(Builder $builder, $bindings) {
$tables = $alias['table'];
}

// TODO: cache this query
$queryString = $this->queryStringForSelect($tables);

$queryRes = $this->getPdo()->query($queryString);

$types[$tables] = $queryRes->fetchAll(PDO::FETCH_NAMED);

foreach ($types[$tables] as &$row) {
$tipos[strtolower($row['name'])] = $row['type'];

$tipos[strtolower($tables . '.' . $row['name'])] = $row['type'];

if (!empty($alias['alias'])) {
Expand All @@ -170,7 +168,7 @@ private function compileForSelect(Builder $builder, $bindings) {

foreach ($builder->wheres as $w) {
switch ($w['type']) {
case 'Nested':
case "Nested":
$wheres += $w['query']->wheres;
break;
default:
Expand All @@ -180,11 +178,13 @@ private function compileForSelect(Builder $builder, $bindings) {
}

$i = 0;

$wheresCount = count($wheres);

for ($ind = 0; $ind < $wheresCount; $ind++) {
if (
if ($wheres[$ind]['type'] == 'raw') {
$newBinds[] = $bindings[$i];
$i++;
} else if (
isset($wheres[$ind]['value']) &&
isset($tipos[strtolower($wheres[$ind]['column'])])
) {
Expand All @@ -205,7 +205,6 @@ private function compileForSelect(Builder $builder, $bindings) {
} else {
$newBinds[$i] = (string) $bindings[$i];
}

$i++;
}
}
Expand All @@ -214,14 +213,22 @@ private function compileForSelect(Builder $builder, $bindings) {
$newFormat[$tables] = [];
}

$wheres = (array) $builder->wheres;

/**
* Is this block duplicated? Need more tests will keep it
* commented to remember that a possible error could be related to
* this
*/
/**
$wheres = (array) $builder->wheres;
$i = 0;

$wheresCount = count($wheres);
for ($ind = 0; $ind < $wheresCount; $ind++) {
if (isset($wheres[$ind]['value'])) {
if ($wheres[$ind]['type'] == 'raw') {
$newBinds[] = $bindings[$i];
$i++;
} else if (isset($wheres[$ind]['value'])) {
if (is_object($wheres[$ind]['value']) === false) {
if (
in_array(
Expand All @@ -239,11 +246,11 @@ private function compileForSelect(Builder $builder, $bindings) {
} else {
$newBinds[$i] = (string) $bindings[$i];
}

$i++;
}
}
}
*/

return $newBinds;
}
Expand Down

0 comments on commit 17dc5dc

Please sign in to comment.