Completed
Push — resets ( d2f77b )
by Paul
02:06
created

QueryFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 0 Features 3
Metric Value
wmc 12
c 11
b 0
f 3
lcom 1
cbo 1
dl 0
loc 231
ccs 44
cts 44
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setLastInsertIdNames() 0 4 1
A newSelect() 0 4 1
A newInsert() 0 6 1
A newUpdate() 0 4 1
A newDelete() 0 4 1
A newInstance() 0 15 2
A getQuoter() 0 11 2
A newSeqBindPrefix() 0 10 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 387
    public function __construct(
111
        $db,
112
        $common = null
113
    ) {
114 387
        $this->db = ucfirst(strtolower($db));
115 387
        $this->common = ($common === self::COMMON);
116 387
        $this->quote_name_prefix = $this->quotes[$this->db][0];
117 387
        $this->quote_name_suffix = $this->quotes[$this->db][1];
118 387
    }
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 66
    public function setLastInsertIdNames(array $last_insert_id_names)
131
    {
132 66
        $this->last_insert_id_names = $last_insert_id_names;
133 66
    }
134
135
    /**
136
     *
137
     * Returns a new SELECT object.
138
     *
139
     * @return Common\SelectInterface
140
     *
141
     */
142 250
    public function newSelect()
143
    {
144 250
        return $this->newInstance('Select');
145
    }
146
147
    /**
148
     *
149
     * Returns a new INSERT object.
150
     *
151
     * @return Common\InsertInterface
152
     *
153
     */
154 76
    public function newInsert()
155
    {
156 76
        $insert = $this->newInstance('Insert');
157 76
        $insert->setLastInsertIdNames($this->last_insert_id_names);
158 76
        return $insert;
159
    }
160
161
    /**
162
     *
163
     * Returns a new UPDATE object.
164
     *
165
     * @return Common\UpdateInterface
166
     *
167
     */
168 33
    public function newUpdate()
169
    {
170 33
        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 387
    protected function newInstance($query)
195
    {
196 387
        if ($this->common) {
197 20
            $class = "Aura\SqlQuery\Common";
198 20
        } else {
199 367
            $class = "Aura\SqlQuery\\{$this->db}";
200
        }
201
202 387
        $class .= "\\{$query}";
203
204 387
        return new $class(
205 387
            $this->getQuoter(),
206 387
            $this->newSeqBindPrefix()
207 387
        );
208
    }
209
210
    /**
211
     *
212
     * Returns the Quoter object for queries; creates one if needed.
213
     *
214
     * @return Quoter
215
     *
216
     */
217 387
    protected function getQuoter()
218
    {
219 387
        if (! $this->quoter) {
220 387
            $this->quoter = new Quoter(
221 387
                $this->quote_name_prefix,
222 387
                $this->quote_name_suffix
223 387
            );
224 387
        }
225
226 387
        return $this->quoter;
227
    }
228
229
    /**
230
     *
231
     * Returns a new sequential-placeholder prefix for a query object.
232
     *
233
     * We need these to deconflict between bound values in subselect queries.
234
     *
235
     * @return string
236
     *
237
     */
238 387
    protected function newSeqBindPrefix()
239
    {
240 387
        $seq_bind_prefix = '';
241 387
        if ($this->instance_count) {
242 36
            $seq_bind_prefix = '_' . $this->instance_count;
243 36
        }
244
245 387
        $this->instance_count ++;
246 387
        return $seq_bind_prefix;
247
    }
248
}
249