Completed
Push — 3.x ( e5eb1c...76cecd )
by Paul
9s
created

QueryFactory::newSeqBindPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\SqlQuery;
10
11
/**
12
 *
13
 * Creates query statement objects.
14
 *
15
 * @package Aura.SqlQuery
16
 *
17
 */
18
class QueryFactory
19
{
20
    const COMMON = 'common';
21
22
    /**
23
     *
24
     * What database are we building for?
25
     *
26
     * @param string
27
     *
28
     */
29
    protected $db;
30
31
    /**
32
     *
33
     * Build "common" query objects regardless of database type?
34
     *
35
     * @param bool
36
     *
37
     */
38
    protected $common = false;
39
40
    /**
41
     *
42
     * The quote prefix/suffix to use for each type.
43
     *
44
     * @param array
45
     *
46
     */
47
    protected $quotes = array(
48
        'Common' => array('"', '"'),
49
        'Mysql'  => array('`', '`'),
50
        'Pgsql'  => array('"', '"'),
51
        'Sqlite' => array('"', '"'),
52
        'Sqlsrv' => array('[', ']'),
53
    );
54
55
    /**
56
     *
57
     * The quote name prefix extracted from `$quotes`.
58
     *
59
     * @var string
60
     *
61
     */
62
    protected $quote_name_prefix;
63
64
    /**
65
     *
66
     * The quote name suffix extracted from `$quotes`.
67
     *
68
     * @var string
69
     *
70
     */
71
    protected $quote_name_suffix;
72
73
    /**
74
     *
75
     * A map of `table.col` names to last-insert-id names.
76
     *
77
     * @var array
78
     *
79
     */
80
    protected $last_insert_id_names = array();
81
82
    /**
83
     *
84
     * A Quoter for identifiers.
85
     *
86
     * @param Quoter
87
     *
88
     */
89
    protected $quoter;
90
91
    /**
92
     *
93
     * A count of Query instances, used for determining $seq_bind_prefix.
94
     *
95
     * @var int
96
     *
97
     */
98
    protected $instance_count = 0;
99
100
    /**
101
     *
102
     * Constructor.
103
     *
104
     * @param string $db The database type.
105
     *
106
     * @param string $common Pass the constant self::COMMON to force common
107
     * query objects instead of db-specific ones.
108
     *
109
     */
110 408
    public function __construct(
111
        $db,
112
        $common = null
113
    ) {
114 408
        $this->db = ucfirst(strtolower($db));
115 408
        $this->common = ($common === self::COMMON);
116 408
        $this->quote_name_prefix = $this->quotes[$this->db][0];
117 408
        $this->quote_name_suffix = $this->quotes[$this->db][1];
118 408
    }
119
120
    /**
121
     *
122
     * Sets the last-insert-id names to be used for Insert queries..
123
     *
124
     * @param array $last_insert_id_names A map of `table.col` names to
125
     * last-insert-id names.
126
     *
127
     * @return null
128
     *
129
     */
130 67
    public function setLastInsertIdNames(array $last_insert_id_names)
131
    {
132 67
        $this->last_insert_id_names = $last_insert_id_names;
133 67
    }
134
135
    /**
136
     *
137
     * Returns a new SELECT object.
138
     *
139
     * @return Common\SelectInterface
140
     *
141
     */
142 265
    public function newSelect()
143
    {
144 265
        return $this->newInstance('Select');
145
    }
146
147
    /**
148
     *
149
     * Returns a new INSERT object.
150
     *
151
     * @return Common\InsertInterface
152
     *
153
     */
154 77
    public function newInsert()
155
    {
156 77
        $insert = $this->newInstance('Insert');
157 77
        $insert->setLastInsertIdNames($this->last_insert_id_names);
158 77
        return $insert;
159
    }
160
161
    /**
162
     *
163
     * Returns a new UPDATE object.
164
     *
165
     * @return Common\UpdateInterface
166
     *
167
     */
168 38
    public function newUpdate()
169
    {
170 38
        return $this->newInstance('Update');
171
    }
172
173
    /**
174
     *
175
     * Returns a new DELETE object.
176
     *
177
     * @return Common\DeleteInterface
178
     *
179
     */
180 28
    public function newDelete()
181
    {
182 28
        return $this->newInstance('Delete');
183
    }
184
185
    /**
186
     *
187
     * Returns a new query object.
188
     *
189
     * @param string $query The query object type.
190
     *
191
     * @return AbstractQuery
192
     *
193
     */
194 408
    protected function newInstance($query)
195
    {
196 408
        $queryClass = "Aura\SqlQuery\\{$this->db}\\{$query}";
197 408
        if ($this->common) {
198 20
            $queryClass = "Aura\SqlQuery\Common\\{$query}";
199
        }
200
201 408
        $builderClass = "Aura\SqlQuery\\{$this->db}\\{$query}Builder";
202 408
        if ($this->common || ! class_exists($builderClass)) {
203 249
            $builderClass = "Aura\SqlQuery\Common\\{$query}Builder";
204
        }
205
206 408
        return new $queryClass(
207 408
            $this->getQuoter(),
208 408
            new $builderClass(),
209 408
            $this->newSeqBindPrefix()
210
        );
211
    }
212
213
    /**
214
     *
215
     * Returns the Quoter object for queries; creates one if needed.
216
     *
217
     * @return Quoter
218
     *
219
     */
220 408
    protected function getQuoter()
221
    {
222 408
        if (! $this->quoter) {
223 408
            $this->quoter = new Quoter(
224 408
                $this->quote_name_prefix,
225 408
                $this->quote_name_suffix
226
            );
227
        }
228
229 408
        return $this->quoter;
230
    }
231
232
    /**
233
     *
234
     * Returns a new sequential-placeholder prefix for a query object.
235
     *
236
     * We need these to deconflict between bound values in subselect queries.
237
     *
238
     * @return string
239
     *
240
     */
241 408
    protected function newSeqBindPrefix()
242
    {
243 408
        $seq_bind_prefix = '';
244 408
        if ($this->instance_count) {
245 21
            $seq_bind_prefix = '_' . $this->instance_count;
246
        }
247
248 408
        $this->instance_count ++;
249 408
        return $seq_bind_prefix;
250
    }
251
}
252