Test Failed
Push — master ( ccf43a...ee4cc3 )
by Jean-Christophe
17:42
created

Database::getInsertValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Database implementation
5
 */
6
namespace Ubiquity\db;
7
8
use Ubiquity\exceptions\CacheException;
9
use Ubiquity\db\traits\DatabaseOperationsTrait;
10
use Ubiquity\exceptions\DBException;
11
use Ubiquity\db\traits\DatabaseTransactionsTrait;
12
use Ubiquity\controllers\Startup;
13
use Ubiquity\db\traits\DatabaseMetadatas;
14
use Ubiquity\cache\database\DbCache;
15
16
/**
17
 * Ubiquity Generic database class.
18
 * Ubiquity\db$Database
19
 * This class is part of Ubiquity
20
 *
21
 * @author jcheron <[email protected]>
22
 * @version 1.1.4
23
 *
24
 */
25
class Database {
26
	use DatabaseOperationsTrait,DatabaseTransactionsTrait,DatabaseMetadatas;
27
	public static $wrappers = [ 'pdo' => \Ubiquity\db\providers\pdo\PDOWrapper::class,'tarantool' => '\Ubiquity\db\providers\tarantool\TarantoolWrapper','mysqli' => '\Ubiquity\db\providers\mysqli\MysqliWrapper','swoole' => '\Ubiquity\db\providers\swoole\SwooleWrapper' ];
28
	private $dbType;
29
	private $serverName;
30
	private $port;
31
	private $dbName;
32
	private $user;
33
	private $password;
34
	private $cache;
35
	private $options;
36
	public $quote;
37
38
	/**
39
	 *
40
	 * @var \Ubiquity\db\providers\AbstractDbWrapper
41
	 */
42
	protected $wrapperObject;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param string $dbWrapperClass
48
	 * @param string $dbName
49
	 * @param string $serverName
50
	 * @param string $port
51
	 * @param string $user
52
	 * @param string $password
53
	 * @param array $options
54
	 * @param boolean|string $cache
55
	 * @param mixed $pool
56 104
	 */
57 104
	public function __construct($dbWrapperClass, $dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [ ], $cache = false, $pool = null) {
58 104
		$this->setDbWrapperClass ( $dbWrapperClass, $dbType );
59 104
		$this->dbName = $dbName;
60 104
		$this->serverName = $serverName;
61 104
		$this->port = $port;
62 104
		$this->user = $user;
63 104
		$this->password = $password;
64 104
		$this->options = $options;
65 18
		if ($cache !== false) {
66 1
			if ($cache instanceof \Closure) {
67
				$this->cache = $cache ();
68 18
			} else {
69 18
				if (\class_exists ( $cache )) {
70
					$this->cache = new $cache ();
71 1
				} else {
72
					throw new CacheException ( $cache . " is not a valid value for database cache" );
73
				}
74
			}
75 104
		}
76
		if ($pool && (\method_exists ( $this->wrapperObject, 'pool' ))) {
77
			$this->wrapperObject->setPool ( $pool );
78 104
		}
79
	}
80 104
81 104
	private function setDbWrapperClass($dbWrapperClass, $dbType) {
82 104
		$this->wrapperObject = new $dbWrapperClass ( $this->dbType = $dbType );
83 104
	}
84
85
	/**
86
	 * Creates the Db instance and realize a safe connection.
87
	 *
88
	 * @throws DBException
89
	 * @return boolean
90
	 */
91 94
	public function connect() {
92
		try {
93 94
			$this->_connect ();
94 94
			$this->quote = $this->wrapperObject->quote;
95 1
			return true;
96 1
		} catch ( \Exception $e ) {
97
			throw new DBException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
98
		}
99
	}
100 1
101 1
	public function getDSN() {
102
		return $this->wrapperObject->getDSN ( $this->serverName, $this->port, $this->dbName, $this->dbType );
103
	}
104
105
	/**
106
	 *
107
	 * @return string
108
	 * @codeCoverageIgnore
109
	 */
110
	public function getServerName() {
111
		return $this->serverName;
112
	}
113 3
114 3
	public function setServerName($serverName) {
115 3
		$this->serverName = $serverName;
116
	}
117 3
118 3
	public function setDbType($dbType) {
119 3
		$this->dbType = $dbType;
120
		return $this;
121
	}
122
123
	/**
124
	 *
125
	 * @return string
126
	 * @codeCoverageIgnore
127
	 */
128
	public function getPort() {
129
		return $this->port;
130
	}
131
132
	/**
133
	 *
134
	 * @return string
135
	 * @codeCoverageIgnore
136
	 */
137
	public function getDbName() {
138
		return $this->dbName;
139
	}
140
141
	/**
142
	 *
143
	 * @return string
144
	 * @codeCoverageIgnore
145
	 */
146
	public function getUser() {
147
		return $this->user;
148
	}
149 3
150 3
	public static function getAvailableDrivers($dbWrapperClass = \Ubiquity\db\providers\pdo\PDOWrapper::class) {
151
		return \call_user_func ( $dbWrapperClass . '::getAvailableDrivers' );
152
	}
153
154
	/**
155
	 *
156
	 * @return mixed
157
	 * @codeCoverageIgnore
158
	 */
159
	public function getDbType() {
160
		return $this->dbType;
161
	}
162
163
	/**
164
	 *
165
	 * @return string
166
	 * @codeCoverageIgnore
167
	 */
168
	public function getPassword() {
169
		return $this->password;
170
	}
171
172
	/**
173
	 *
174
	 * @return array
175
	 * @codeCoverageIgnore
176
	 */
177
	public function getOptions() {
178
		return $this->options;
179
	}
180
181
	/**
182
	 *
183
	 * @param string $port
184 2
	 */
185 2
	public function setPort($port) {
186 2
		$this->port = $port;
187
	}
188
189
	/**
190
	 *
191
	 * @param string $dbName
192 1
	 */
193 1
	public function setDbName($dbName) {
194 1
		$this->dbName = $dbName;
195
	}
196
197
	/**
198
	 *
199
	 * @param string $user
200 2
	 */
201 2
	public function setUser($user) {
202 2
		$this->user = $user;
203
	}
204
205
	/**
206
	 *
207
	 * @param string $password
208 1
	 */
209 1
	public function setPassword($password) {
210 1
		$this->password = $password;
211
	}
212
213
	/**
214
	 *
215
	 * @param array $options
216 1
	 */
217 1
	public function setOptions($options) {
218 1
		$this->options = $options;
219
	}
220
221
	/**
222
	 * Closes the active connection
223 29
	 */
224 29
	public function close() {
225 29
		$this->wrapperObject->close ();
226
	}
227
228
	/**
229
	 * Starts and returns a database instance corresponding to an offset in config
230
	 *
231
	 * @param string $offset
232
	 * @param array $config Ubiquity config file content
233
	 * @return \Ubiquity\db\Database|NULL
234
	 */
235
	public static function start(string $offset = null, ?array $config = null): ?self {
236
		$config ??= Startup::$config;
237
		$db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ?? [ ]);
238
		if ($db ['dbName'] !== '') {
239
			$database = new Database ( $db ['wrapper'] ?? \Ubiquity\db\providers\pdo\PDOWrapper::class, $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false);
240
			$database->connect ();
241
			return $database;
242
		}
243
		return null;
244
	}
245
246
	public function quoteValue($value, $type = 2) {
247
		return $this->wrapperObject->quoteValue ( ( string ) $value, $type );
248
	}
249
250
	public function getUpdateFieldsKeyAndValues($keyAndValues, $fields) {
251
		$ret = array ();
252
		foreach ( $fields as $field ) {
253
			$ret [] = $this->quote . $field . $this->quote . ' = ' . $this->quoteValue ( $keyAndValues [$field] );
254
		}
255
		return \implode ( ',', $ret );
256
	}
257
258
	public function getInsertValues($keyAndValues) {
259
		$ret = array ();
260
		foreach ( $keyAndValues as $value ) {
261
			$ret [] = $this->quoteValue ( $value );
262
		}
263
		return \implode ( ',', $ret );
264
	}
265
266
	public function getCondition(array $keyValues, $separator = ' AND ') {
267
		$retArray = array ();
268
		foreach ( $keyValues as $key => $value ) {
269
			$retArray [] = $this->quote . $key . $this->quote . " = " . $this->quoteValue ( $value );
270
		}
271
		return \implode ( $separator, $retArray );
272
	}
273
274
	/**
275
	 * For databases with Connection pool (retrieve a new dbInstance from pool wrapper)
276
	 */
277
	public function pool() {
278
		return $this->wrapperObject->pool ();
279
	}
280
281
	/**
282
	 * For databases with Connection pool (put a dbInstance in pool wrapper)
283 2
	 */
284 2
	public function freePool($db) {
285 2
		$this->wrapperObject->freePool ( $db );
286 2
	}
287 2
288
	public function setPool($pool) {
289
		$this->wrapperObject->setPool ( $pool );
290 2
	}
291
292
	public static function getAvailableWrappers() {
293 24
		$wrappers = [ ];
294 24
		foreach ( self::$wrappers as $k => $wrapper ) {
295 24
			if (\class_exists ( $wrapper, true )) {
296 23
				$wrappers [$k] = $wrapper;
297 24
			}
298 24
		}
299
		return $wrappers;
300
	}
301
302
	public function getSpecificSQL($key, ?array $params = null) {
303
		switch ($key) {
304
			case 'groupconcat' :
305
				return $this->wrapperObject->groupConcat ( $params [0], $params [1] ?? ',');
306
			case 'tostring' :
307
				return $this->wrapperObject->toStringOperator ();
308
		}
309
	}
310
311
	public function setCacheInstance(DbCache $cache) {
312
		$this->cache = $cache;
313
	}
314
315
	public function getCacheInstance() {
316
		return $this->cache;
317
	}
318
}
319