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

SqliteDriverMetas::groupConcat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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