Base_PDO::query()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 8
nop 4
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Provides database connectivity for PDO
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Database
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\database;
17
18
/**
19
 * Provides database connectivity for PDO
20
 *
21
 * @category  Core
22
 * @package   Database
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Base_PDO extends \csphere\core\database\Base
30
{
31
    /**
32
     * Stores the database connection
33
     **/
34
    protected $con = null;
35
36
    /**
37
     * Returns a formatted array with statistics
38
     *
39
     * @return array
40
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
41
42
    public function info()
43
    {
44
        $info = parent::info();
45
46
        $info['client']  = $this->con->getAttribute(\PDO::ATTR_CLIENT_VERSION);
47
        $info['server']  = $this->con->getAttribute(\PDO::ATTR_SERVER_VERSION);
48
        $info['version'] = phpversion($info['driver']);
49
50
        return $info;
51
    }
52
53
    /**
54
     * Handle database driver specific transactions
55
     *
56
     * @param string $command One of these strings: begin, commit, rollback
57
     *
58
     * @return boolean
59
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
60
61
    public function transaction($command)
62
    {
63
        // Execute requested command
64
        if ($command == 'commit') {
65
66
            $result = $this->con->commit();
67
68
        } elseif ($command == 'rollback') {
69
70
            $result = $this->con->rollBack();
71
72
        } else {
73
74
            $result = $this->con->beginTransaction();
75
        }
76
77
        return $result;
78
    }
79
80
    /**
81
     * Sends a command to the database and gets the affected rows
82
     *
83
     * @param string  $prepare  Prepared query string with placeholders
84
     * @param array   $assoc    Array with columns and values
85
     * @param boolean $replace  If more than {pre} needs to be replaced
86
     * @param boolean $insertid Return the last insert id instead of a rowcount
87
     * @param boolean $log      Defaults to true which enables log files if used
88
     *
89
     * @return integer
90
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
91
92
    public function exec(
93
        $prepare, array $assoc, $replace = false, $insertid = false, $log = true
94
    ) {
95
        // Apply replaces if required
96
        if (!empty($replace)) {
97
98
            $prepare = $this->replace($prepare);
99
        }
100
101
        $statement = $this->_execute($prepare, $assoc, $log);
102
103
        // Determine what to return
104
        if (empty($insertid)) {
105
106
            $result = $statement->rowCount();
107
108
        } else {
109
110
            $result = $this->insertID();
111
        }
112
113
        return (int)$result;
114
    }
115
116
    /**
117
     * Returns the ID of the last insert query
118
     *
119
     * @return integer
120
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
121
122
    protected function insertId()
123
    {
124
        return (int)$this->con->lastInsertId();
125
    }
126
127
    /**
128
     * Sends a query to the database and fetches the result
129
     *
130
     * @param string  $prepare Prepared query string with placeholders
131
     * @param array   $assoc   Array with columns and values
132
     * @param integer $first   Number of the first dataset to show
133
     * @param integer $max     Number of datasets to show from first on
134
     *
135
     * @return array
136
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
137
138
    public function query($prepare, array $assoc, $first = 0, $max = 1)
139
    {
140
        // Attach limit vars first and max
141
        if ($first != 0 || $max != 0) {
142
143
            $prepare .= ' ' . $this->limits($first, $max);
144
        }
145
146
        $statement = $this->_execute($prepare, $assoc, false);
147
148
        // Determine what to return
149
        if ($max == 1) {
150
151
            $result = $statement->fetch(\PDO::FETCH_ASSOC);
152
153
            if (!is_array($result)) {
154
155
                if ($result === false) {
156
157
                    $result = [];
158
159
                } else {
160
161
                    $result = [$result];
162
                }
163
            }
164
165
        } else {
166
167
            $result = $statement->fetchAll(\PDO::FETCH_ASSOC);
168
        }
169
170
        return (array)$result;
171
    }
172
173
    /**
174
     * Executes a query
175
     *
176
     * @param string  $prepare Prepared query string with placeholders
177
     * @param array   $assoc   Array with columns and values
178
     * @param boolean $log     Save query info to log files
179
     *
180
     * @return \PDOStatement
181
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
182
183
    private function _execute($prepare, array $assoc, $log)
184
    {
185
        // Rewrite assoc array to use named placeholders
186
        $data = [];
187
188
        foreach ($assoc AS $key => $value) {
189
190
            $data[':' . $key] = $value;
191
        }
192
193
        $prepare = str_replace('{pre}', $this->prefix . '_', $prepare);
194
195
        // Log executed query
196
        $this->log($prepare, $data, $log);
197
198
        // Prepare and execute the statement
199
        $statement = $this->con->prepare($prepare);
200
201
        if (is_object($statement)) {
202
203
            $statement->execute($data);
204
205
        } else {
206
207
            $info = $this->con->errorInfo();
208
209
            if (isset($info[2])) {
210
211
                $info = $info[2];
212
            }
213
214
            $this->error($prepare, $assoc, $info);
215
        }
216
217
        return $statement;
218
    }
219
220
    /**
221
     * Builds the query string part for limit and offset
222
     *
223
     * @param integer $first Number of the first dataset to show
224
     * @param integer $max   Number of datasets to show from first on
225
     *
226
     * @return string
227
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
228
229
    abstract protected function limits($first, $max);
230
231
    /**
232
     * Replaces driver specific query placeholders
233
     *
234
     * @param string $replace The string to use for replaces
235
     *
236
     * @return string
237
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
238
239
    abstract protected function replace($replace);
240
}
241