Passed
Push — main ( 3c706c...efa972 )
by Thierry
07:51 queued 05:53
created

Query::begin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Sqlite\Db;
4
5
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
6
7
use Lagdo\DbAdmin\Driver\Db\Query as AbstractQuery;
8
9
class Query extends AbstractQuery
10
{
11
    /**
12
     * @inheritDoc
13
     */
14
    public function insertOrUpdate(string $table, array $rows, array $primary)
15
    {
16
        $values = [];
17
        foreach ($rows as $set) {
18
            $values[] = "(" . implode(", ", $set) . ")";
19
        }
20
        $result = $this->driver->execute("REPLACE INTO " . $this->driver->table($table) .
21
            " (" . implode(", ", array_keys(reset($rows))) . ") VALUES\n" . implode(",\n", $values));
22
        return $result == true;
23
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28
    public function user()
29
    {
30
        return get_current_user(); // should return effective user
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function view(string $name)
37
    {
38
        return [
39
            'name' => $name,
40
            'type' => 'VIEW',
41
            'materialized' => false,
42
            'select' => preg_replace('~^(?:[^`"[]+|`[^`]*`|"[^"]*")* AS\s+~iU', '',
43
                $this->connection->result("SELECT sql FROM sqlite_master WHERE name = " .
44
                $this->driver->quote($name)))
45
        ]; //! identifiers may be inside []
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function lastAutoIncrementId()
52
    {
53
        return $this->connection->result("SELECT LAST_INSERT_ROWID()");
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function explain(ConnectionInterface $connection, string $query)
60
    {
61
        return $connection->query("EXPLAIN QUERY PLAN $query");
62
    }
63
}
64