Test Failed
Pull Request — master (#197)
by
unknown
10:13
created

MysqlDriverMetas   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 11
eloc 29
c 0
b 0
f 0
dl 0
loc 58
ccs 30
cts 31
cp 0.9677
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTablesName() 0 3 1
A groupConcat() 0 2 1
A __construct() 0 4 1
A getRowNum() 0 6 2
A getPrimaryKeys() 0 8 2
A getFieldsInfos() 0 8 2
A getForeignKeys() 0 9 1
A setIsolationLevel() 0 2 1
1
<?php
2
3
namespace Ubiquity\db\providers\pdo\drivers;
4
5
use Ubiquity\db\providers\DbOperations;
6
7
/**
8
 * Ubiquity\db\providers\pdo\drivers$MysqlDriverMetas
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.3
13
 *
14
 */
15
class MysqlDriverMetas extends AbstractDriverMetaDatas {
16
17 43
	public function __construct($dbInstance) {
18 43
		parent::__construct($dbInstance);
19 43
		$this->operations[DbOperations::CREATE_TABLE]='CREATE TABLE {name} ({fields}) ENGINE=InnoDB DEFAULT CHARSET=utf8';
20 43
		$this->operations[DbOperations::AUTO_INC]='ALTER TABLE {tableName} MODIFY {fieldInfos} AUTO_INCREMENT, AUTO_INCREMENT={value}';
21
	}
22
	
23 3
	public function getTablesName(): array {
24 3
		$query = $this->dbInstance->query ( 'SHOW TABLES' );
25 3
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
26
	}
27
28 1
	public function getPrimaryKeys(string $tableName): array {
29 1
		$fieldkeys = array ();
30 1
		$recordset = $this->dbInstance->query ( "SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'" );
31 1
		$keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
32 1
		foreach ( $keys as $key ) {
33 1
			$fieldkeys [] = $key ['Column_name'];
34
		}
35 1
		return $fieldkeys;
36
	}
37
38 1
	public function getForeignKeys(string $tableName, string $pkName, ?string $dbName = null): array {
39 1
		$recordset = $this->dbInstance->query ( "SELECT *
40
												FROM
41
												 information_schema.KEY_COLUMN_USAGE
42
												WHERE
43
												 REFERENCED_TABLE_NAME = '" . $tableName . "'
44
												 AND REFERENCED_COLUMN_NAME = '" . $pkName . "'
45
												 AND TABLE_SCHEMA = '" . $dbName . "';" );
46 1
		return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
47
	}
48
49 1
	public function getFieldsInfos(string $tableName): array {
50 1
		$fieldsInfos = array ();
51 1
		$recordset = $this->dbInstance->query ( "SHOW COLUMNS FROM `{$tableName}`" );
52 1
		$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
53 1
		foreach ( $fields as $field ) {
54 1
			$fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ];
55
		}
56 1
		return $fieldsInfos;
57
	}
58
59 5
	public function getRowNum(string $tableName, string $pkName, string $condition): int {
60 5
		$query = $this->dbInstance->query ( "SELECT num FROM (SELECT *, @rownum:=@rownum + 1 AS num FROM `{$tableName}`, (SELECT @rownum:=0) r ORDER BY {$pkName}) d WHERE " . $condition );
61 5
		if ($query) {
62 5
			return $query->fetchColumn ( 0 );
63
		}
64
		return 0;
65
	}
66
67 34
	public function groupConcat(string $fields, string $separator): string {
68 34
		return "GROUP_CONCAT({$fields} SEPARATOR '{$separator}')";
69
	}
70
71
	public function setIsolationLevel($isolationLevel) {
72
		return $this->dbInstance->exec("SET TRANSACTION ISOLATION LEVEL $isolationLevel");
73
	}
74
}
75