summaryrefslogtreecommitdiff
path: root/src/Query/FirebirdBuilder.php
blob: cac265e7dabb8998d3aa2759457ee0b5038dce03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

namespace Xgrz\Firebird\Query;

use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Collection;

class FirebirdBuilder extends QueryBuilder
{
    /**
     * Determine if any rows exist for the current query.
     *
     * @return bool
     */
    public function exists()
    {
        return parent::count() > 0;
    }

    /**
     * Add a from stored procedure clause to the query builder.
     *
     * @param string $procedure
     * @param array  $values
     * @return \Illuminate\Database\Query\Builder|static
     */
    public function fromProcedure(string $procedure, array $values = [])
    {
        $compiledProcedure = $this->grammar->compileProcedure($this, $procedure, $values);

        // Remove any expressions from the values array, as they will have
        // already been evaluated by the grammar's parameterize() function.
        $values = array_filter($values, function($value) {
            return ! $this->grammar->isExpression($value);
        });

        $this->fromRaw($compiledProcedure, array_values($values));

        return $this;
    }

    /**
     * Insert new records into the database.
     *
     * @return bool
     */
    public function insert(array $values)
    {
		// Handle multi-row inserts by looping
		if (count($values) > 1 && is_array(reset($values))) {
			$results = [];
			foreach ($values as $row) {
				$results[] = parent::insert($row);
			}
			return end($results);
    }
    
    // Single row or already formatted correctly
    return parent::insert($values);
    }
    public function where($column, $operator = NULL, $value = NULL, $boolean = 'and')
    {
        // Not sure what this was intended to fix, but target hidden for now
        if (! str($operator)->contains('hide', true)) {
            return parent::where($column, $operator, $value, $boolean);
        }

        // when is search covert to upper case column and value at database level
        $wrapped = $this->grammar->wrap($column);
        return $boolean === 'and'
            ? parent::whereRaw("UPPER($wrapped) LIKE UPPER(?)", [$value])
            : parent::orWhereRaw("UPPER($wrapped) LIKE UPPER(?)", [$value]);
    }

    /**
     * Retrieve column values from rows represented as objects.
     *
     * @param  array  $queryResult
     * @param  string  $column
     * @param  string  $key
     * @return Collection
     */
    protected function pluckFromObjectColumn($queryResult, $column, $key)
    {
        $results = [];
		$column = str_replace('"', '', $column);
		foreach ($queryResult as $item) {
			if (is_null($key)) {
				foreach ($queryResult as $row) {
					$results[] = $row->$column;
				}
			} else {
				foreach ($queryResult as $row) {
					$results[$row->$key] = $row->$column;
				}
			}
		}

        return new Collection($results);
    }

    /**
     * Retrieve column values from rows represented as arrays.
     *
     * @param  array  $queryResult
     * @param  string  $column
     * @param  string  $key
     * @return Collection
     */
    protected function pluckFromArrayColumn($queryResult, $column, $key)
    {
        $results = [];

        if (is_null($key)) {
            foreach ($queryResult as $row) {
                $results[] = $row[$column];
            }
        } else {
            foreach ($queryResult as $row) {
                $results[$row[$key]] = $row[$column];
            }
        }

        return new Collection($results);
    }
}