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

SqliteDriverMetas::getRowNum()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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