1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Nip\Database\Connections;
|
4
|
|
|
|
5
|
|
|
use Exception;
|
6
|
|
|
use Nip\Database\Adapters\AbstractAdapter;
|
7
|
|
|
use Nip\Database\Metadata\Manager as MetadataManager;
|
8
|
|
|
use Nip\Database\Query\AbstractQuery as AbstractQuery;
|
9
|
|
|
use Nip\Database\Query\Delete as DeleteQuery;
|
10
|
|
|
use Nip\Database\Query\Insert as InsertQuery;
|
11
|
|
|
use Nip\Database\Query\Select as SelectQuery;
|
12
|
|
|
use Nip\Database\Query\Update as UpdateQuery;
|
13
|
|
|
use Nip\Database\Result;
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* Class Connection
|
17
|
|
|
* @package Nip\Database
|
18
|
|
|
*/
|
19
|
|
|
class Connection
|
20
|
|
|
{
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* The active PDO connection.
|
24
|
|
|
*
|
25
|
|
|
* @var PDO
|
26
|
|
|
*/
|
27
|
|
|
protected $pdo;
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* The name of the connected database.
|
31
|
|
|
*
|
32
|
|
|
* @var string
|
33
|
|
|
*/
|
34
|
|
|
protected $database;
|
35
|
|
|
/**
|
36
|
|
|
* The table prefix for the connection.
|
37
|
|
|
*
|
38
|
|
|
* @var string
|
39
|
|
|
*/
|
40
|
|
|
protected $tablePrefix = '';
|
41
|
|
|
|
42
|
|
|
/**
|
43
|
|
|
* The database connection configuration options.
|
44
|
|
|
*
|
45
|
|
|
* @var array
|
46
|
|
|
*/
|
47
|
|
|
protected $config = [];
|
48
|
|
|
|
49
|
|
|
protected $_adapter = null;
|
50
|
|
|
|
51
|
|
|
protected $metadata;
|
52
|
|
|
|
53
|
|
|
protected $_query;
|
54
|
|
|
|
55
|
|
|
protected $_queries = [];
|
56
|
|
|
|
57
|
|
|
/**
|
58
|
|
|
* Create a new database connection instance.
|
59
|
|
|
*
|
60
|
|
|
* @param \PDO|\Closure $pdo
|
61
|
|
|
* @param string $database
|
62
|
|
|
* @param string $tablePrefix
|
63
|
|
|
* @param array $config
|
64
|
|
|
*/
|
65
|
|
|
public function __construct($pdo, $database = '', $tablePrefix = '', $config = [])
|
66
|
|
|
{
|
67
|
|
|
$this->pdo = $pdo;
|
|
|
|
|
68
|
|
|
|
69
|
|
|
// First we will setup the default properties. We keep track of the DB
|
70
|
|
|
// name we are connected to since it is needed when some reflective
|
71
|
|
|
// type commands are run such as checking whether a table exists.
|
72
|
|
|
$this->database = $database;
|
73
|
|
|
|
74
|
|
|
$this->tablePrefix = $tablePrefix;
|
75
|
|
|
$this->config = $config;
|
76
|
|
|
|
77
|
|
|
// We need to initialize a query grammar and the query post processors
|
78
|
|
|
// which are both very important parts of the database abstractions
|
79
|
|
|
// so we initialize these to their default values while starting.
|
80
|
|
|
// $this->useDefaultQueryGrammar();
|
81
|
|
|
// $this->useDefaultPostProcessor();
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
/**
|
85
|
|
|
* Connects to SQL server
|
86
|
|
|
*
|
87
|
|
|
* @param string $host
|
88
|
|
|
* @param string $user
|
89
|
|
|
* @param string $password
|
90
|
|
|
* @param string $database
|
91
|
|
|
* @param bool $newLink
|
92
|
|
|
* @return static
|
93
|
|
|
*/
|
94
|
|
|
public function connect($host, $user, $password, $database, $newLink = false)
|
95
|
|
|
{
|
96
|
|
|
if (!$this->pdo) {
|
97
|
|
|
try {
|
98
|
|
|
$this->pdo = $this->getAdapter()->connect($host, $user, $password, $database, $newLink);
|
|
|
|
|
99
|
|
|
$this->setDatabase($database);
|
100
|
|
|
} catch (Exception $e) {
|
101
|
|
|
$e->log();
|
|
|
|
|
102
|
|
|
}
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
return $this;
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* @return AbstractAdapter
|
110
|
|
|
*/
|
111
|
|
|
public function getAdapter()
|
112
|
|
|
{
|
113
|
|
|
if ($this->_adapter == null) {
|
114
|
|
|
$this->initAdapter();
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
return $this->_adapter;
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* @param $adapter
|
122
|
|
|
*/
|
123
|
|
|
public function setAdapter($adapter)
|
124
|
|
|
{
|
125
|
|
|
$this->_adapter = $adapter;
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
public function initAdapter()
|
129
|
|
|
{
|
130
|
|
|
$this->setAdapterName('MySQLi');
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* @param $name
|
135
|
|
|
*/
|
136
|
|
|
public function setAdapterName($name)
|
137
|
|
|
{
|
138
|
|
|
$this->setAdapter($this->newAdapter($name));
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
/**
|
142
|
|
|
* @param $name
|
143
|
|
|
* @return AbstractAdapter
|
144
|
|
|
*/
|
145
|
|
|
public function newAdapter($name)
|
146
|
|
|
{
|
147
|
|
|
$class = static::getAdapterClass($name);
|
148
|
|
|
|
149
|
|
|
return new $class();
|
150
|
|
|
}
|
151
|
|
|
|
152
|
|
|
/**
|
153
|
|
|
* @param $name
|
154
|
|
|
* @return string
|
155
|
|
|
*/
|
156
|
|
|
public static function getAdapterClass($name)
|
157
|
|
|
{
|
158
|
|
|
return '\Nip\Database\Adapters\\' . $name;
|
159
|
|
|
}
|
160
|
|
|
|
161
|
|
|
/**
|
162
|
|
|
* @return string
|
163
|
|
|
*/
|
164
|
|
|
public function getDatabase()
|
165
|
|
|
{
|
166
|
|
|
return $this->database;
|
167
|
|
|
}
|
168
|
|
|
|
169
|
|
|
/**
|
170
|
|
|
* @param mixed $database
|
171
|
|
|
*/
|
172
|
|
|
public function setDatabase($database)
|
173
|
|
|
{
|
174
|
|
|
$this->database = $database;
|
175
|
|
|
}
|
176
|
|
|
|
177
|
|
|
/**
|
178
|
|
|
* @return MetadataManager
|
179
|
|
|
*/
|
180
|
|
|
public function getMetadata()
|
181
|
|
|
{
|
182
|
|
|
if (!$this->metadata) {
|
183
|
|
|
$this->metadata = new MetadataManager();
|
184
|
|
|
$this->metadata->setConnection($this);
|
185
|
|
|
}
|
186
|
|
|
|
187
|
|
|
return $this->metadata;
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* Prefixes table names
|
192
|
|
|
*
|
193
|
|
|
* @param string $table
|
194
|
|
|
* @return string
|
195
|
|
|
*/
|
196
|
|
|
public function tableName($table)
|
197
|
|
|
{
|
198
|
|
|
return $table;
|
199
|
|
|
}
|
200
|
|
|
|
201
|
|
|
/**
|
202
|
|
|
* @return SelectQuery
|
203
|
|
|
*/
|
204
|
|
|
public function newSelect()
|
205
|
|
|
{
|
206
|
|
|
return $this->newQuery('select');
|
207
|
|
|
}
|
208
|
|
|
|
209
|
|
|
/**
|
210
|
|
|
* @param string $type optional
|
211
|
|
|
* @return AbstractQuery|SelectQuery|UpdateQuery|InsertQuery|DeleteQuery
|
212
|
|
|
*/
|
213
|
|
|
public function newQuery($type = "select")
|
214
|
|
|
{
|
215
|
|
|
$className = '\Nip\Database\Query\\' . inflector()->camelize($type);
|
216
|
|
|
$query = new $className();
|
217
|
|
|
/** @var AbstractQuery $query */
|
218
|
|
|
$query->setManager($this);
|
219
|
|
|
|
220
|
|
|
return $query;
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
/**
|
224
|
|
|
* @return InsertQuery
|
225
|
|
|
*/
|
226
|
|
|
public function newInsert()
|
227
|
|
|
{
|
228
|
|
|
return $this->newQuery('insert');
|
229
|
|
|
}
|
230
|
|
|
|
231
|
|
|
/**
|
232
|
|
|
* @return UpdateQuery
|
233
|
|
|
*/
|
234
|
|
|
public function newUpdate()
|
235
|
|
|
{
|
236
|
|
|
return $this->newQuery('update');
|
237
|
|
|
}
|
238
|
|
|
|
239
|
|
|
/**
|
240
|
|
|
* @return DeleteQuery
|
241
|
|
|
*/
|
242
|
|
|
public function newDelete()
|
243
|
|
|
{
|
244
|
|
|
return $this->newQuery('delete');
|
245
|
|
|
}
|
246
|
|
|
|
247
|
|
|
/**
|
248
|
|
|
* Executes SQL query
|
249
|
|
|
*
|
250
|
|
|
* @param mixed|AbstractQuery $query
|
251
|
|
|
* @return Result
|
252
|
|
|
*/
|
253
|
|
|
public function execute($query)
|
254
|
|
|
{
|
255
|
|
|
$this->_queries[] = $query;
|
256
|
|
|
|
257
|
|
|
$sql = is_string($query) ? $query : $query->getString();
|
258
|
|
|
|
259
|
|
|
$resultSQL = $this->getAdapter()->execute($sql);
|
260
|
|
|
$result = new Result($resultSQL, $this->getAdapter());
|
261
|
|
|
$result->setQuery($query);
|
262
|
|
|
|
263
|
|
|
return $result;
|
264
|
|
|
}
|
265
|
|
|
|
266
|
|
|
/**
|
267
|
|
|
* Gets the ID of the last inserted record
|
268
|
|
|
* @return int
|
269
|
|
|
*/
|
270
|
|
|
public function lastInsertID()
|
271
|
|
|
{
|
272
|
|
|
return $this->getAdapter()->lastInsertID();
|
273
|
|
|
}
|
274
|
|
|
|
275
|
|
|
/**
|
276
|
|
|
* Gets the number of rows affected by the last operation
|
277
|
|
|
* @return int
|
278
|
|
|
*/
|
279
|
|
|
public function affectedRows()
|
280
|
|
|
{
|
281
|
|
|
return $this->getAdapter()->affectedRows();
|
282
|
|
|
}
|
283
|
|
|
|
284
|
|
|
/**
|
285
|
|
|
* Disconnects from server
|
286
|
|
|
*/
|
287
|
|
|
public function disconnect()
|
288
|
|
|
{
|
289
|
|
|
if ($this->pdo) {
|
290
|
|
|
try {
|
291
|
|
|
$this->getAdapter()->disconnect();
|
292
|
|
|
} catch (Exception $e) {
|
293
|
|
|
$e->log();
|
|
|
|
|
294
|
|
|
}
|
295
|
|
|
}
|
296
|
|
|
}
|
297
|
|
|
|
298
|
|
|
/**
|
299
|
|
|
* @param $table
|
300
|
|
|
* @return mixed
|
301
|
|
|
*/
|
302
|
|
|
public function describeTable($table)
|
303
|
|
|
{
|
304
|
|
|
return $this->getAdapter()->describeTable($this->protect($table));
|
305
|
|
|
}
|
306
|
|
|
|
307
|
|
|
/**
|
308
|
|
|
* Adds backticks to input
|
309
|
|
|
*
|
310
|
|
|
* @param string $input
|
311
|
|
|
* @return string
|
312
|
|
|
*/
|
313
|
|
|
public function protect($input)
|
314
|
|
|
{
|
315
|
|
|
return str_replace("`*`", "*", '`' . str_replace('.', '`.`', $input) . '`');
|
316
|
|
|
}
|
317
|
|
|
|
318
|
|
|
/**
|
319
|
|
|
* @return array
|
320
|
|
|
*/
|
321
|
|
|
public function getQueries()
|
322
|
|
|
{
|
323
|
|
|
return $this->_queries;
|
324
|
|
|
}
|
325
|
|
|
}
|
326
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..