QueryBuilder   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 165
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPDO() 0 4 1
A select() 0 10 2
A insert() 0 10 2
A update() 0 10 2
A delete() 0 10 2
A raw() 0 10 2
A beginTransaction() 0 4 1
A commit() 0 4 1
A rollBack() 0 4 1
A inTransaction() 0 4 1
A lastInsertId() 0 4 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace JAQB;
13
14
use PDO;
15
16
class QueryBuilder
17
{
18
    /**
19
     * @var PDO
20
     */
21
    protected $pdo;
22
23
    /**
24
     * @param PDO $pdo
25
     */
26
    public function __construct(PDO $pdo = null)
27
    {
28
        $this->pdo = $pdo;
29
    }
30
31
    /**
32
     * Returns the PDO instance.
33
     *
34
     * @return PDO
35
     */
36
    public function getPDO()
37
    {
38
        return $this->pdo;
39
    }
40
41
    /**
42
     * Creates a SELECT query.
43
     *
44
     * @param string|array $fields select fields
45
     *
46
     * @return \JAQB\Query\SelectQuery
47
     */
48
    public function select($fields = '*')
49
    {
50
        $query = new Query\SelectQuery();
51
52
        if ($this->pdo) {
53
            $query->setPDO($this->pdo);
54
        }
55
56
        return $query->select($fields);
57
    }
58
59
    /**
60
     * Creates an INSERT query.
61
     *
62
     * @param array $values insert values
63
     *
64
     * @return \JAQB\Query\InsertQuery
65
     */
66
    public function insert(array $values)
67
    {
68
        $query = new Query\InsertQuery();
69
70
        if ($this->pdo) {
71
            $query->setPDO($this->pdo);
72
        }
73
74
        return $query->values($values);
75
    }
76
77
    /**
78
     * Creates an UPDATE query.
79
     *
80
     * @param string $table update table
81
     *
82
     * @return \JAQB\Query\UpdateQuery
83
     */
84
    public function update($table)
85
    {
86
        $query = new Query\UpdateQuery();
87
88
        if ($this->pdo) {
89
            $query->setPDO($this->pdo);
90
        }
91
92
        return $query->table($table);
93
    }
94
95
    /**
96
     * Creates a DELETE query.
97
     *
98
     * @param string $from delete table
99
     *
100
     * @return \JAQB\Query\DeleteQuery
101
     */
102
    public function delete($from)
103
    {
104
        $query = new Query\DeleteQuery();
105
106
        if ($this->pdo) {
107
            $query->setPDO($this->pdo);
108
        }
109
110
        return $query->from($from);
111
    }
112
113
    /**
114
     * Creates a raw SQL query.
115
     *
116
     * @param string $sql SQL statement
117
     *
118
     * @return \JAQB\Query\SqlQuery
119
     */
120
    public function raw($sql)
121
    {
122
        $query = new Query\SqlQuery();
123
124
        if ($this->pdo) {
125
            $query->setPDO($this->pdo);
126
        }
127
128
        return $query->raw($sql);
129
    }
130
131
    /**
132
     * Starts a transaction.
133
     *
134
     * @return bool
135
     */
136
    public function beginTransaction()
137
    {
138
        return $this->pdo->beginTransaction();
139
    }
140
141
    /**
142
     * Commits the transaction.
143
     *
144
     * @return bool
145
     */
146
    public function commit()
147
    {
148
        return $this->pdo->commit();
149
    }
150
151
    /**
152
     * Rolls back the transaction.
153
     *
154
     * @return bool
155
     */
156
    public function rollBack()
157
    {
158
        return $this->pdo->rollBack();
159
    }
160
161
    /**
162
     * Checks if the query is in a transaction.
163
     *
164
     * @return bool
165
     */
166
    public function inTransaction()
167
    {
168
        return $this->pdo->inTransaction();
169
    }
170
171
    /**
172
     * Gets the last inserted ID.
173
     *
174
     * @return mixed
175
     */
176
    public function lastInsertId()
177
    {
178
        return $this->pdo->lastInsertId();
179
    }
180
}
181