Passed
Pull Request — master (#78)
by Jean-Christophe
12:38
created

PDOWrapper::getPrimaryKeys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ubiquity\db\providers\pdo;
4
5
use Ubiquity\db\providers\AbstractDbWrapper;
6
7
/**
8
 * Ubiquity\db\providers$PDOWrapper
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 * @property \PDO $dbInstance
14
 *
15
 */
16
class PDOWrapper extends AbstractDbWrapper {
17
	protected static $savepointsDrivers = [ 'pgsql' => true,'mysql' => true,'sqlite' => true ];
18
	private static $quotes = [ 'mysql' => '`','sqlite' => '"','pgsql' => '' ];
19
	protected $transactionLevel = 0;
20
	protected $dbType;
21
22 80
	public function __construct($dbType = 'mysql') {
23 80
		$this->quote = self::$quotes [$dbType] ?? '';
24 80
		$this->dbType = $dbType;
25 80
	}
26
27
	public function fetchAllColumn($statement, array $values = null, string $column = null) {
28
		$result = false;
29
		if ($statement->execute ( $values )) {
30
			$result = $statement->fetchAll ( \PDO::FETCH_COLUMN, $column );
31
		}
32
		$statement->closeCursor ();
33
		return $result;
34
	}
35
36 10
	public function lastInsertId() {
37 10
		return $this->dbInstance->lastInsertId ();
38
	}
39
40 23
	public function fetchAll($statement, array $values = null, $mode = null) {
41 23
		$result = false;
42 23
		if ($statement->execute ( $values )) {
43 23
			$result = $statement->fetchAll ( $mode );
44
		}
45 23
		$statement->closeCursor ();
46 23
		return $result;
47
	}
48
49
	public function fetchOne($statement, array $values = null, $mode = null) {
50
		$result = false;
51
		if ($statement->execute ( $values )) {
52
			$result = $statement->fetch ( $mode );
53
		}
54
		$statement->closeCursor ();
55
		return $result;
56
	}
57
58 2
	public static function getAvailableDrivers() {
59 2
		return \PDO::getAvailableDrivers ();
60
	}
61
62 7
	public function prepareStatement(string $sql) {
63 7
		return $this->dbInstance->prepare ( $sql );
64
	}
65
66
	public function fetchColumn($statement, array $values = null, int $columnNumber = null) {
67
		if ($statement->execute ( $values )) {
68
			return $statement->fetchColumn ( $columnNumber );
69
		}
70
		return false;
71
	}
72
73 68
	public function getStatement($sql) {
74 68
		$st = $this->dbInstance->prepare ( $sql );
75 68
		$st->setFetchMode ( \PDO::FETCH_ASSOC );
76 68
		return $st;
77
	}
78
79
	public function execute($sql) {
80
		return $this->dbInstance->exec ( $sql );
81
	}
82
83 75
	public function connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options) {
84 75
		$options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
85 75
		$this->dbInstance = new \PDO ( $this->getDSN ( $serverName, $port, $dbName, $dbType ), $user, $password, $options );
86 75
	}
87
88 75
	public function getDSN(string $serverName, string $port, string $dbName, string $dbType = 'mysql') {
89 75
		$charsetString = [ 'mysql' => 'charset=UTF8','pgsql' => 'options=\'--client_encoding=UTF8\'','sqlite' => '' ] [$dbType] ?? 'charset=UTF8';
90 75
		return $dbType . ":dbname={$dbName};host={$serverName};{$charsetString};port=" . $port;
91
	}
92
93
	public function bindValueFromStatement($statement, $parameter, $value) {
94
		return $statement->bindValue ( ":" . $parameter, $value );
95
	}
96
97
	public function query(string $sql) {
98
		return $this->dbInstance->query ( $sql );
99
	}
100
101
	public function queryAll(string $sql, int $fetchStyle = null) {
102
		return $this->dbInstance->query ( $sql )->fetchAll ( $fetchStyle );
103
	}
104
105 7
	public function queryColumn(string $sql, int $columnNumber = null) {
106 7
		return $this->dbInstance->query ( $sql )->fetchColumn ( $columnNumber );
107
	}
108
109
	public function executeStatement($statement, array $values = null) {
110
		return $statement->execute ( $values );
111
	}
112
113 3
	public function getTablesName() {
114 3
		$query = $this->dbInstance->query ( 'SHOW TABLES' );
115 3
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
116
	}
117
118
	public function statementRowCount($statement) {
119
		return $statement->rowCount ();
120
	}
121
122
	public function inTransaction() {
123
		return $this->dbInstance->inTransaction ();
124
	}
125
126 4
	public function commit() {
127 4
		return $this->dbInstance->commit ();
128
	}
129
130 3
	public function rollBack() {
131 3
		return $this->dbInstance->rollBack ();
132
	}
133
134 8
	public function beginTransaction() {
135 8
		return $this->dbInstance->beginTransaction ();
136
	}
137
138 4
	public function savePoint($level) {
139 4
		$this->dbInstance->exec ( 'SAVEPOINT LEVEL' . $level );
140 4
	}
141
142 2
	public function releasePoint($level) {
143 2
		$this->dbInstance->exec ( 'RELEASE SAVEPOINT LEVEL' . $level );
144 2
	}
145
146 2
	public function rollbackPoint($level) {
147 2
		$this->dbInstance->exec ( 'ROLLBACK TO SAVEPOINT LEVEL' . $level );
148 2
	}
149
150 4
	public function nestable() {
151 4
		return isset ( self::$savepointsDrivers [$this->dbType] );
152
	}
153
154 4
	public function ping() {
155 4
		return ($this->dbInstance != null) && (1 === \intval ( $this->queryColumn ( 'SELECT 1', 0 ) ));
156
	}
157
158 1
	public function getPrimaryKeys($tableName) {
159 1
		$fieldkeys = array ();
160 1
		$recordset = $this->dbInstance->query ( "SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'" );
161 1
		$keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
162 1
		foreach ( $keys as $key ) {
163 1
			$fieldkeys [] = $key ['Column_name'];
164
		}
165 1
		return $fieldkeys;
166
	}
167
168 1
	public function getForeignKeys($tableName, $pkName, $dbName = null) {
169 1
		$recordset = $this->dbInstance->query ( "SELECT *
170
												FROM
171
												 information_schema.KEY_COLUMN_USAGE
172
												WHERE
173 1
												 REFERENCED_TABLE_NAME = '" . $tableName . "'
174 1
												 AND REFERENCED_COLUMN_NAME = '" . $pkName . "'
175 1
												 AND TABLE_SCHEMA = '" . $dbName . "';" );
176 1
		return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
177
	}
178
179 1
	public function getFieldsInfos($tableName) {
180 1
		$fieldsInfos = array ();
181 1
		$recordset = $this->dbInstance->query ( "SHOW COLUMNS FROM `{$tableName}`" );
182 1
		$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
183 1
		foreach ( $fields as $field ) {
184 1
			$fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ];
185
		}
186 1
		return $fieldsInfos;
187
	}
188
189 60
	public function _optPrepareAndExecute($sql, array $values = null) {
190 60
		$statement = $this->_getStatement ( $sql );
191 60
		$result = false;
192 60
		if ($statement->execute ( $values )) {
193 60
			$result = $statement->fetchAll ( \PDO::FETCH_ASSOC );
194
		}
195 60
		$statement->closeCursor ();
196 60
		return $result;
197
	}
198
}