Query::quoteIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Sql;
5
6
use Atlas\Pdo\Connection;
7
use Sirius\Sql\Component\Flags;
8
9
abstract class Query
10
{
11
    protected $bindings;
12
13
    protected $connection;
14
15
    protected $flags;
16
17
    protected $quoter;
18
19
    protected $indent = '';
20
21 20
    public function __construct(Connection $connection, Bindings $bindings = null, $indent = '')
22
    {
23 20
        $this->connection = $connection;
24 20
        if (! $bindings) {
25 20
            $bindings = new Bindings();
26
        }
27 20
        $this->bindings = $bindings;
28
29
        $quoter       = 'Sirius\Sql\\Quoter\\'
30 20
                        . ucfirst($this->connection->getDriverName())
31 20
                        . 'Quoter';
32 20
        $this->quoter = new $quoter();
33
34 20
        $this->indent = $indent;
35
36 20
        $this->reset();
37 20
    }
38
39
    /**
40
     * Perform the query at it's current state (conditions and bind values)
41
     *
42
     * @return \PDOStatement
43
     */
44
    public function perform()
45
    {
46
        return $this->connection->perform(
47
            $this->getStatement(),
48
            $this->getBindValues()
49
        );
50
    }
51
52
    /**
53
     * Creates a numbered binding placeholder to be added to the statement
54
     * like :__102__, :__3__ and adds the value to the list of bind values
55
     *
56
     * @param $value
57
     * @param int $type
58
     *
59
     * @return string
60
     */
61 1
    public function bindInline($value, int $type = -1)
62
    {
63 1
        return $this->bindings->inline($value, $type);
64
    }
65
66
    /**
67
     * Creates a string where %s is replaced by numbered binding placeholders
68
     * and adds the values to the list of bind values
69
     *
70
     * @param string $format
71
     * @param mixed ...$values
72
     *
73
     * @return string
74
     * @example $format = "date between %s and %s", returns "date between :__4__ and :__5__"
75
     *
76
     */
77 1
    public function bindSprintf(string $format, ...$values): string
78
    {
79 1
        return $this->bindings->sprintf($format, ...$values);
80
    }
81
82
    /**
83
     * Creates a named binding placedholder and adds the value to the bind values
84
     *
85
     * @param string $key
86
     * @param $value
87
     * @param int $type
88
     *
89
     * @return $this
90
     * @example $key = 'title', returns ':title'
91
     *
92
     */
93 2
    public function bindValue(string $key, $value, int $type = -1)
94
    {
95 2
        $this->bindings->value($key, $value, $type);
96
97 2
        return $this;
98
    }
99
100
    /**
101
     * Binds a set of key-value pairs to the statement
102
     *
103
     * @param array $values
104
     *
105
     * @return $this
106
     */
107 1
    public function bindValues(array $values)
108
    {
109 1
        $this->bindings->values($values);
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * Returns a RAW value that will be used as-is in the statement
116
     *
117
     * @param $value
118
     *
119
     * @return Raw
120
     */
121 3
    public function raw($value): Raw
122
    {
123 3
        return new Raw($value);
124
    }
125
126 11
    public function getBindValues(): array
127
    {
128 11
        return $this->bindings->getArrayCopy();
129
    }
130
131
    /**
132
     * Sets a statement flag like IGNORE for INSERT or DISTINCT for SELECT
133
     *
134
     * @param string $flag
135
     * @param bool $enable
136
     */
137 2
    public function setFlag(string $flag, bool $enable = true): void
138
    {
139 2
        $this->flags->set($flag, $enable);
140 2
    }
141
142
    /**
143
     * Resets the query (conditions, columns etc)
144
     * @return $this
145
     */
146 20
    public function reset()
147
    {
148 20
        foreach (get_class_methods($this) as $method) {
149 20
            if (substr($method, 0, 5) == 'reset' && $method != 'reset') {
150 20
                $this->$method();
151
            }
152
        }
153
154 20
        return $this;
155
    }
156
157 20
    public function resetFlags()
158
    {
159 20
        $this->flags = new Flags();
160
161 20
        return $this;
162
    }
163
164
    public function quoteIdentifier(string $name): string
165
    {
166
        return $this->quoter->quoteIdentifier($name);
167
    }
168
169
    /**
170
     * Returns the statement to be executed on the connection
171
     * @return string
172
     */
173
    abstract public function getStatement(): string;
174
175
    /**
176
     * Returns a compiled version of the query.
177
     * To be used only for debugging purposes.
178
     * All lines are prepended with # so you don't accidentally run it
179
     *
180
     * @return string
181
     */
182 1
    public function __toString()
183
    {
184 1
        $bindings = $this->getBindValues();
185 1
        $sql      = $this->getStatement();
186 1
        foreach ($bindings as $k => $v) {
187 1
            $value = $v[0];
188 1
            if ($v[1] == \PDO::PARAM_STR) {
189 1
                $value = "'" . $value . "'";
190
            }
191 1
            $sql = str_replace(':' . $k, $value, $sql);
192
        }
193 1
        $sql = preg_replace('/:[0-9a-z_]+/', 'NULL', $sql);
194
195 1
        return "# " . str_replace(PHP_EOL, PHP_EOL . '# ', $sql);
196
    }
197
}
198