Test Failed
Pull Request — master (#105)
by Jean-Christophe
11:44
created

SqliteDriverMetas   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 22
c 3
b 0
f 0
dl 0
loc 43
ccs 0
cts 8
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryKeys() 0 10 3
A getFieldsInfos() 0 8 2
A getRowNum() 0 7 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
10
 * @version 1.0.0
11
 *
12
 */
13
class SqliteDriverMetas extends AbstractDriverMetaDatas {
14
15
16
	public function getForeignKeys($tableName, $pkName, $dbName = null): array {
17
18
		// SQL lite may return error if key is not found when manual model editing can cause it by mistype of model field name.
19
		$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."';");
20
		return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
21
	}
22
23
	public function getTablesName(): array {
24
		$query = $this->dbInstance->query ( 'SELECT name FROM sqlite_master WHERE type=\'table\';'  );
25
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
26
	}
27
28
	public function getPrimaryKeys($tableName): array {
29
		$fieldkeys = array ();
30
		$recordset = $this->dbInstance->query ( "PRAGMA TABLE_INFO(".$table_name.");" );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table_name does not exist. Did you maybe mean $tableName?
Loading history...
31
		$keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
32
		foreach ( $keys as $key ) {
33
            if ($key['pk']==1) {
34
                $fieldkeys[] = $key['name'];
35
            }			
36
		}
37
		return $fieldkeys;
38
	}
39
40
	public function getFieldsInfos($tableName): array {
41
		$fieldsInfos = array ();
42
		$recordset = $this->dbInstance->query (  "PRAGMA TABLE_INFO(".$table_name.");" );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table_name does not exist. Did you maybe mean $tableName?
Loading history...
43
		$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
44
		foreach ( $fields as $field ) {
45
			$fieldsInfos [$field ['name']] = [ "Type" => $field ['type'],"Nullable" => $field ["notnull"] ];
46
		}
47
		return $fieldsInfos;
48
	}
49
	public function getRowNum(string $tableName, string $pkName, string $condition): int
50
	{
51
		$query = $this->dbInstance->query ( "SELECT num FROM (SELECT *,row_number() OVER (ORDER BY {$pkName}) AS num FROM \"{$tableName}\") x where " . $condition );
52
		if ($query) {
53
			return $query->fetchColumn ( 0 );
54
		}
55
		return 0;
56
	}
57
}
58
59