Completed
Push — 7.4 ( abf332 )
by Nikolaos
16:13
created

AbstractQuery::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 11
cts 11
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AtlasPHP
12
 *
13
 * @link    https://github.com/atlasphp/Atlas.Query
14
 * @license https://github.com/atlasphp/Atlas.Query/blob/1.x/LICENSE.md
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\DataMapper\Query;
20
21
use PDO;
22
use PDOStatement;
23
use Phalcon\DataMapper\Pdo\Connection;
24
25
/**
26
 * Class AbstractQuery
27
 *
28
 * @property Bind       $bind
29
 * @property Connection $connection
30
 * @property array      $store
31
 */
32
abstract class AbstractQuery
33
{
34
    /**
35
     * @var Bind
36
     */
37
    protected $bind;
38
39
    /**
40
     * @var Connection
41
     */
42
    protected $connection;
43
44
    /**
45
     * @var array
46
     */
47
    protected $store = [];
48
49
    /**
50
     * AbstractQuery constructor.
51
     *
52
     * @param Connection $connection
53
     * @param Bind       $bind
54
     */
55 188
    public function __construct(Connection $connection, Bind $bind)
56
    {
57 188
        $this->bind           = $bind;
58 188
        $this->connection     = $connection;
59 188
        $this->store["UNION"] = [];
60
61 188
        $this->reset();
62 188
    }
63
64
    /**
65
     * Binds a value inline
66
     *
67
     * @param mixed $value
68
     * @param int   $type
69
     *
70
     * @return string
71
     */
72 24
    public function bindInline($value, int $type = -1): string
73
    {
74 24
        return $this->bind->bindInline($value, $type);
75
    }
76
77
    /**
78
     * Binds a value - auto-detects the type if necessary
79
     *
80
     * @param string $key
81
     * @param mixed  $value
82
     * @param int    $type
83
     *
84
     * @return AbstractQuery
85
     */
86 20
    public function bindValue(
87
        string $key,
88
        $value,
89
        int $type = -1
90
    ): AbstractQuery {
91 20
        $this->bind->setValue($key, $value, $type);
92
93 20
        return $this;
94
    }
95
96
    /**
97
     * Binds an array of values
98
     *
99
     * @param array $values
100
     *
101
     * @return AbstractQuery
102
     */
103 24
    public function bindValues(array $values): AbstractQuery
104
    {
105 24
        $this->bind->setValues($values);
106
107 24
        return $this;
108
    }
109
110
    /**
111
     * Returns all the bound values
112
     *
113
     * @return array
114
     */
115 56
    public function getBindValues(): array
116
    {
117 56
        return $this->bind->toArray();
118
    }
119
120
    /**
121
     * Return the generated statement
122
     *
123
     * @return string
124
     */
125
    abstract public function getStatement(): string;
126
127
    /**
128
     * Performs a statement in the connection
129
     *
130
     * @return PDOStatement
131
     */
132 8
    public function perform()
133
    {
134 8
        return $this->connection->perform(
135 8
            $this->getStatement(),
136 8
            $this->getBindValues()
137
        );
138
    }
139
140
    /**
141
     * Sets a flag for the query such as "DISTINCT"
142
     *
143
     * @param string $flag
144
     * @param bool   $enable
145
     */
146 16
    public function setFlag(string $flag, bool $enable = true): void
147
    {
148 16
        if ($enable) {
149 16
            $this->store["FLAGS"][$flag] = true;
150
        } else {
151 4
            $flags = $this->store["FLAGS"];
152
153 4
            unset($flags[$flag]);
154
155 4
            $this->store["FLAGS"] = $flags;
156
        }
157 16
    }
158
159
    /**
160
     * Quotes the identifier
161
     *
162
     * @param string $name
163
     * @param int    $type
164
     *
165
     * @return string
166
     */
167 16
    public function quoteIdentifier(
168
        string $name,
169
        int $type = PDO::PARAM_STR
170
    ): string {
171 16
        return $this->connection->quote($name, $type);
172
    }
173
174
    /**
175
     * Resets the internal array
176
     */
177 188
    public function reset()
178
    {
179 188
        $this->store["COLUMNS"] = [];
180 188
        $this->store["FLAGS"]   = [];
181 188
        $this->store["FROM"]    = [];
182 188
        $this->store["GROUP"]   = [];
183 188
        $this->store["HAVING"]  = [];
184 188
        $this->store["LIMIT"]   = 0;
185 188
        $this->store["ORDER"]   = [];
186 188
        $this->store["OFFSET"]  = 0;
187 188
        $this->store["WHERE"]   = [];
188 188
    }
189
190
    /**
191
     * Builds the flags statement(s)
192
     *
193
     * @return string
194
     */
195 136
    protected function buildFlags()
196
    {
197 136
        if (empty($this->store["FLAGS"])) {
198 124
            return "";
199
        }
200
201 12
        return " " . implode(" ", array_keys($this->store["FLAGS"]));
202
    }
203
204
    /**
205
     * Builds the `RETURNING` clause
206
     *
207
     * @return string
208
     */
209 16
    protected function buildReturning(): string
210
    {
211 16
        if (empty($this->store["RETURNING"])) {
212 8
            return "";
213
        }
214
215 12
        return " RETURNING" . $this->indent($this->store["RETURNING"], ",");
216
    }
217
218
    /**
219
     * Indents a collection
220
     *
221
     * @param array  $collection
222
     * @param string $glue
223
     *
224
     * @return string
225
     */
226 136
    protected function indent(array $collection, string $glue = ""): string
227
    {
228 136
        if (empty($collection)) {
229 96
            return "";
230
        }
231
232 136
        return " " . implode($glue . " ", $collection);
233
    }
234
}
235