Passed
Push — main ( 37b0c3...6cf3a1 )
by Thierry
07:04
created

Database::alterTable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 4
nop 2
dl 0
loc 14
rs 9.9332
c 4
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Sqlite\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
6
use Lagdo\DbAdmin\Driver\Db\Database as AbstractDatabase;
7
8
use function is_object;
9
use function intval;
10
use function implode;
11
use function array_reverse;
12
13
class Database extends AbstractDatabase
14
{
15
    use DatabaseTrait;
16
17
    /**
18
     * @inheritDoc
19
     */
20
    public function tables()
21
    {
22
        return $this->driver->keyValues('SELECT name, type FROM sqlite_master ' .
23
            "WHERE type IN ('table', 'view') ORDER BY (name = 'sqlite_sequence'), name");
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function countTables(array $databases)
30
    {
31
        $connection = $this->driver->createConnection(); // New connection
32
        $counts = [];
33
        $query = "SELECT count(*) FROM sqlite_master WHERE type IN ('table', 'view')";
34
        foreach ($databases as $database) {
35
            $counts[$database] = 0;
36
            $connection->open($database);
37
            $statement = $connection->query($query);
38
            if (is_object($statement) && ($row = $statement->fetchRow())) {
39
                $counts[$database] = intval($row[0]);
40
            }
41
        }
42
        return $counts;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function dropViews(array $views)
49
    {
50
        return $this->driver->applyQueries('DROP VIEW', $views);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function dropTables(array $tables)
57
    {
58
        return $this->driver->applyQueries('DROP TABLE', $tables);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function moveTables(array $tables, array $views, string $target)
65
    {
66
        return false;
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function truncateTables(array $tables)
73
    {
74
        return $this->driver->applyQueries('DELETE FROM', $tables);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function createTable(TableEntity $tableAttrs)
81
    {
82
        foreach ($tableAttrs->fields as $key => $field) {
83
            $tableAttrs->fields[$key] = '  ' . implode($field);
84
        }
85
        $tableAttrs->fields = array_merge($tableAttrs->fields, array_filter($tableAttrs->foreign));
86
        if (!$this->driver->execute('CREATE TABLE ' . $this->driver->table($tableAttrs->name) .
87
            " (\n" . implode(",\n", $tableAttrs->fields) . "\n)")) {
88
            // implicit ROLLBACK to not overwrite $this->driver->error()
89
            return false;
90
        }
91
        $this->setAutoIncrement($tableAttrs->name, $tableAttrs->autoIncrement);
92
        return true;
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function alterTable(string $table, TableEntity $tableAttrs)
99
    {
100
        $clauses = $this->getAlterTableClauses($tableAttrs);
101
        $queries = [];
102
        foreach ($clauses as $clause) {
103
            $queries[] = 'ALTER TABLE ' . $this->driver->table($table) . ' ' . $clause;
104
        }
105
        if ($table != $tableAttrs->name) {
106
            $queries[] = 'ALTER TABLE ' . $this->driver->table($table) . ' RENAME TO ' .
107
                $this->driver->table($tableAttrs->name);
108
        }
109
        $this->executeQueries($queries);
110
        $this->setAutoIncrement($tableAttrs->name, $tableAttrs->autoIncrement);
111
        return true;
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function alterIndexes(string $table, array $alter, array $drop)
118
    {
119
        $queries = [];
120
        foreach (array_reverse($drop) as $index) {
121
            $queries[] = 'DROP INDEX ' . $this->driver->escapeId($index->name);
122
        }
123
        foreach (array_reverse($alter) as $index) {
124
            // Can't alter primary keys
125
            if ($index->type !== 'PRIMARY') {
126
                $queries[] =  $this->driver->sqlForCreateIndex($table, $index->type,
127
                    $index->name, '(' . implode(', ', $index->columns) . ')');
128
            }
129
        }
130
        // TODO: Wrap queries into a transaction
131
        foreach ($queries as $query) {
132
            $this->driver->execute($query);
133
        }
134
        return true;
135
    }
136
}
137