Completed
Push — 3.x-named-placeholders-only ( b88cbe...93047d )
by Paul
02:34 queued 42s
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
     * A map of `table.col` names to last-insert-id names.
43
     *
44
     * @var array
45
     *
46
     */
47
    protected $last_insert_id_names = array();
48
49
    /**
50
     *
51
     * A Quoter for identifiers.
52
     *
53
     * @param QuoterInterface
54
     *
55
     */
56
    protected $quoter;
57
58
    /**
59
     *
60
     * Constructor.
61
     *
62
     * @param string $db The database type.
63
     *
64
     * @param string $common Pass the constant self::COMMON to force common
65
     * query objects instead of db-specific ones.
66
     *
67
     */
68 407
    public function __construct($db, $common = null)
69
    {
70 407
        $this->db = ucfirst(strtolower($db));
71 407
        $this->common = ($common === self::COMMON);
72 407
    }
73
74
    /**
75
     *
76
     * Sets the last-insert-id names to be used for Insert queries..
77
     *
78
     * @param array $last_insert_id_names A map of `table.col` names to
79
     * last-insert-id names.
80
     *
81
     * @return null
82
     *
83
     */
84 67
    public function setLastInsertIdNames(array $last_insert_id_names)
85
    {
86 67
        $this->last_insert_id_names = $last_insert_id_names;
87 67
    }
88
89
    /**
90
     *
91
     * Returns a new SELECT object.
92
     *
93
     * @return Common\SelectInterface
94
     *
95
     */
96 264
    public function newSelect()
97
    {
98 264
        return $this->newInstance('Select');
99
    }
100
101
    /**
102
     *
103
     * Returns a new INSERT object.
104
     *
105
     * @return Common\InsertInterface
106
     *
107
     */
108 77
    public function newInsert()
109
    {
110 77
        $insert = $this->newInstance('Insert');
111 77
        $insert->setLastInsertIdNames($this->last_insert_id_names);
112 77
        return $insert;
113
    }
114
115
    /**
116
     *
117
     * Returns a new UPDATE object.
118
     *
119
     * @return Common\UpdateInterface
120
     *
121
     */
122 38
    public function newUpdate()
123
    {
124 38
        return $this->newInstance('Update');
125
    }
126
127
    /**
128
     *
129
     * Returns a new DELETE object.
130
     *
131
     * @return Common\DeleteInterface
132
     *
133
     */
134 28
    public function newDelete()
135
    {
136 28
        return $this->newInstance('Delete');
137
    }
138
139
    /**
140
     *
141
     * Returns a new query object.
142
     *
143
     * @param string $query The query object type.
144
     *
145
     * @return AbstractQuery
146
     *
147
     */
148 407
    protected function newInstance($query)
149
    {
150 407
        $queryClass = "Aura\SqlQuery\\{$this->db}\\{$query}";
151 407
        if ($this->common) {
152 20
            $queryClass = "Aura\SqlQuery\Common\\{$query}";
153 20
        }
154
155 407
        $builderClass = "Aura\SqlQuery\\{$this->db}\\{$query}Builder";
156 407
        if ($this->common || ! class_exists($builderClass)) {
157 248
            $builderClass = "Aura\SqlQuery\Common\\{$query}Builder";
0 ignored issues
show
Unused Code introduced by
$builderClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158 248
        }
159
160 407
        return new $queryClass(
161 407
            $this->getQuoter(),
162 407
            $this->newBuilder($query)
163 407
        );
164
    }
165
166 407
    protected function newBuilder($query)
167
    {
168 407
        $builderClass = "Aura\SqlQuery\\{$this->db}\\{$query}Builder";
169 407
        if ($this->common || ! class_exists($builderClass)) {
170 248
            $builderClass = "Aura\SqlQuery\Common\\{$query}Builder";
171 248
        }
172 407
        return new $builderClass();
173
    }
174
175
    /**
176
     *
177
     * Returns the Quoter object for queries; creates one if needed.
178
     *
179
     * @return Quoter
180
     *
181
     */
182 407
    protected function getQuoter()
183
    {
184 407
        if (! $this->quoter) {
185 407
            $this->quoter = $this->newQuoter();
186 407
        }
187 407
        return $this->quoter;
188
    }
189
190 407
    protected function newQuoter()
191
    {
192 407
        $quoterClass = "Aura\SqlQuery\\{$this->db}\Quoter";
193 407
        if ($this->common || ! class_exists($quoterClass)) {
194 176
            $quoterClass = "Aura\SqlQuery\Common\Quoter";
195 176
        }
196 407
        return new $quoterClass();
197
    }
198
}
199