Passed
Pull Request — master (#70)
by Christian
02:13 queued 50s
created

MysqlAdapter::getColumns()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
namespace Maphper\DataSource;
3
class MysqlAdapter implements DatabaseAdapter {
4
	private $pdo;
5
	private $stmtCache;
6
    private $generalEditor;
7
8
	public function __construct(\PDO $pdo) {
9
		$this->pdo = $pdo;
10
		//Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries
11
		$this->pdo->query('SET sql_mode = STRICT_ALL_TABLES');
12
        $this->stmtCache = new StmtCache($pdo);
13
        $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]);
14
	}
15
16
	public function quote($str) {
17
		return $this->generalEditor->quote($str);
18
	}
19
20
	public function query(\Maphper\Lib\Query $query) {
21
		$stmt = $this->stmtCache->getCachedStmt($query->getSql());
22
		$args = $query->getArgs();
23
        $stmt->execute($args);
24
25
		return $stmt;
26
	}
27
28
    private function alterColumns($table, array $primaryKey, $data) {
29
        foreach ($data as $key => $value) {
30
			if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue;
31
32
			$type = $this->generalEditor->getType($value);
33
			$this->tryAlteringColumn($table, $key, $type);
34
		}
35
    }
36
37
    private function tryAlteringColumn($table, $key, $type) {
38
        try {
39
            if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table');
40
        }
41
        catch (\Exception $e) {
42
            $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
43
        }
44
    }
45
46
	public function alterDatabase($table, array $primaryKey, $data) {
47
		$this->generalEditor->createTable($table, $primaryKey, $data);
48
        $this->alterColumns($table, $primaryKey, $data);
49
	}
50
51
	public function getColumns($table) {
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
	public function getColumns(/** @scrutinizer ignore-unused */ $table) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
		return [];
53
54
		// TODO: test this from https://stackoverflow.com/a/13891451
55
		$result = $this->pdo->query('SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ' . $table . ';')->fetchAll(\PDO::FETCH_ASSOC);
0 ignored issues
show
Unused Code introduced by
$result = $this->pdo->qu...chAll(PDO::FETCH_ASSOC) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
56
		$return = [];
57
		foreach ($result as $row) {
58
			$return[] = $row['COLUMN_NAME'];
59
		}
60
		return $return;
61
	}
62
63
	public function lastInsertId() {
64
		return $this->pdo->lastInsertId();
65
	}
66
67
	public function addIndex($table, array $fields) {
68
		//Sort the fields so that the index is never created twice (col1, col2) then (col2, col1)
69
		sort($fields);
70
		$fields = array_map('strtolower', $fields);
71
		$fields = array_map('trim', $fields);
72
		$keyName = $this->quote(implode('_', $fields));
73
74
		$results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"');
75
		if ($results && count($results->fetchAll()) == 0)  $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
76
	}
77
78
	public function optimiseColumns($table) {
79
		//TODO
80
		return;
81
	}
82
}
83