Passed
Push — master ( 3ae514...7292d2 )
by Jean-Christophe
11:38
created

SqliteDriverMetas   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 10
eloc 23
c 4
b 0
f 1
dl 0
loc 46
ccs 0
cts 37
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryKeys() 0 10 3
A groupConcat() 0 2 1
A getFieldsInfos() 0 8 2
A getRowNum() 0 6 2
A getForeignKeys() 0 5 1
A getTablesName() 0 3 1
1
<?php
2
3
namespace Ubiquity\db\providers\pdo\drivers;
4
5
/**
6
 * Ubiquity\db\providers\pdo\drivers$SqliteDriverMetas
7
 * This class is part of Ubiquity
8
 *
9
 * @author Ulaş SAYGIN
10
 * @version 1.0.0
11
 *
12
 */
13
class SqliteDriverMetas extends AbstractDriverMetaDatas {
14
15
	public function getForeignKeys($tableName, $pkName, $dbName = null): array {
16
17
		// SQL lite may return error if key is not found when manual model editing can cause it by mistype of model field name.
18
		$recordset = $this->dbInstance->query ( "SELECT \"TABLE\" as TABLE_NAME , \"to\" as COLUMN_NAME , \"from\" as REFERENCED_TABLE_SCHEMA FROM pragma_foreign_key_list('" . $tableName . "') WHERE \"to\"='" . $pkName . "';" );
19
		return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
20
	}
21
22
	public function getTablesName(): array {
23
		$query = $this->dbInstance->query ( 'SELECT name FROM sqlite_master WHERE type=\'table\';' );
24
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
25
	}
26
27
	public function getPrimaryKeys($tableName): array {
28
		$fieldkeys = array ();
29
		$recordset = $this->dbInstance->query ( "PRAGMA TABLE_INFO(" . $tableName . ");" );
30
		$keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
31
		foreach ( $keys as $key ) {
32
			if ($key ['pk'] == 1) {
33
				$fieldkeys [] = $key ['name'];
34
			}
35
		}
36
		return $fieldkeys;
37
	}
38
39
	public function getFieldsInfos($tableName): array {
40
		$fieldsInfos = array ();
41
		$recordset = $this->dbInstance->query ( "PRAGMA TABLE_INFO(" . $tableName . ");" );
42
		$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
43
		foreach ( $fields as $field ) {
44
			$fieldsInfos [$field ['name']] = [ "Type" => $field ['type'],"Nullable" => $field ["notnull"] ];
45
		}
46
		return $fieldsInfos;
47
	}
48
49
	public function getRowNum(string $tableName, string $pkName, string $condition): int {
50
		$query = $this->dbInstance->query ( "SELECT num FROM (SELECT *,row_number() OVER (ORDER BY {$pkName}) AS num FROM \"{$tableName}\") x where " . $condition );
51
		if ($query) {
52
			return $query->fetchColumn ( 0 );
53
		}
54
		return 0;
55
	}
56
57
	public function groupConcat(string $fields, string $separator): string {
58
		return "GROUP_CONCAT({$fields},'{$separator}')";
59
	}
60
}
61
62