Test Failed
Pull Request — master (#100)
by
unknown
17:04
created

MysqlDriverMetas::getPrimaryKeys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Ubiquity\db\providers\pdo\drivers;
4
5
class MysqlDriverMetas extends AbstractDriverMetaDatas {
6
7
	public function getTablesName(): array {
8
		$query = $this->dbInstance->query ( 'SHOW TABLES' );
9
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
10
	}
11
12
	public function getPrimaryKeys(string $tableName): array {
13
		$fieldkeys = array ();
14
		$recordset = $this->dbInstance->query ( "SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'" );
15
		$keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
16
		foreach ( $keys as $key ) {
17
			$fieldkeys [] = $key ['Column_name'];
18
		}
19
		return $fieldkeys;
20
	}
21
22
	public function getForeignKeys(string $tableName, string $pkName, ?string $dbName = null): array {
23
		$recordset = $this->dbInstance->query ( "SELECT *
24
												FROM
25
												 information_schema.KEY_COLUMN_USAGE
26
												WHERE
27
												 REFERENCED_TABLE_NAME = '" . $tableName . "'
28
												 AND REFERENCED_COLUMN_NAME = '" . $pkName . "'
29
												 AND TABLE_SCHEMA = '" . $dbName . "';" );
30
		return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
31
	}
32
33
	public function getFieldsInfos(string $tableName): array {
34
		$fieldsInfos = array ();
35
		$recordset = $this->dbInstance->query ( "SHOW COLUMNS FROM `{$tableName}`" );
36
		$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
37
		foreach ( $fields as $field ) {
38
			$fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ];
39
		}
40
		return $fieldsInfos;
41
	}
42
}
43
44