Database::createTable()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
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
        $counts = [];
32
        $query = "SELECT count(*) FROM sqlite_master WHERE type IN ('table', 'view')";
33
        foreach ($databases as $database) {
34
            $counts[$database] = 0;
35
            $connection = $this->driver->connect($database);
36
            $statement = $connection->query($query);
37
            if (is_object($statement) && ($row = $statement->fetchRow())) {
38
                $counts[$database] = intval($row[0]);
39
            }
40
        }
41
        return $counts;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function dropViews(array $views)
48
    {
49
        return $this->driver->applyQueries('DROP VIEW', $views);
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function dropTables(array $tables)
56
    {
57
        return $this->driver->applyQueries('DROP TABLE', $tables);
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function moveTables(array $tables, array $views, string $target)
64
    {
65
        return false;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function truncateTables(array $tables)
72
    {
73
        return $this->driver->applyQueries('DELETE FROM', $tables);
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function createTable(TableEntity $tableAttrs)
80
    {
81
        foreach ($tableAttrs->fields as $key => $field) {
82
            $tableAttrs->fields[$key] = '  ' . implode($field);
83
        }
84
        $tableAttrs->fields = array_merge($tableAttrs->fields, array_filter($tableAttrs->foreign));
85
        if (!$this->driver->execute('CREATE TABLE ' . $this->driver->escapeTableName($tableAttrs->name) .
0 ignored issues
show
Bug introduced by
The method escapeTableName() does not exist on Lagdo\DbAdmin\Driver\DriverInterface. ( Ignorable by Annotation )

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

85
        if (!$this->driver->execute('CREATE TABLE ' . $this->driver->/** @scrutinizer ignore-call */ escapeTableName($tableAttrs->name) .

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
            " (\n" . implode(",\n", $tableAttrs->fields) . "\n)")) {
87
            // implicit ROLLBACK to not overwrite $this->driver->error()
88
            return false;
89
        }
90
        $this->setAutoIncrement($tableAttrs->name, $tableAttrs->autoIncrement);
91
        return true;
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function alterTable(string $table, TableEntity $tableAttrs)
98
    {
99
        $clauses = $this->getAlterTableClauses($tableAttrs);
100
        $queries = [];
101
        foreach ($clauses as $clause) {
102
            $queries[] = 'ALTER TABLE ' . $this->driver->escapeTableName($table) . ' ' . $clause;
103
        }
104
        if ($table != $tableAttrs->name) {
105
            $queries[] = 'ALTER TABLE ' . $this->driver->escapeTableName($table) . ' RENAME TO ' .
106
                $this->driver->escapeTableName($tableAttrs->name);
107
        }
108
        if (!$this->executeQueries($queries)) {
109
            return false;
110
        }
111
        $this->setAutoIncrement($tableAttrs->name, $tableAttrs->autoIncrement);
112
        return true;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function alterIndexes(string $table, array $alter, array $drop)
119
    {
120
        $queries = [];
121
        foreach (array_reverse($drop) as $index) {
122
            $queries[] = 'DROP INDEX ' . $this->driver->escapeId($index->name);
123
        }
124
        foreach (array_reverse($alter) as $index) {
125
            // Can't alter primary keys
126
            if ($index->type !== 'PRIMARY') {
127
                $queries[] =  $this->driver->getCreateIndexQuery($table, $index->type,
0 ignored issues
show
Bug introduced by
The method getCreateIndexQuery() does not exist on Lagdo\DbAdmin\Driver\DriverInterface. ( Ignorable by Annotation )

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

127
                /** @scrutinizer ignore-call */ 
128
                $queries[] =  $this->driver->getCreateIndexQuery($table, $index->type,

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
                    $index->name, '(' . implode(', ', $index->columns) . ')');
129
            }
130
        }
131
        return $this->executeQueries($queries);
132
    }
133
}
134