Query   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 9 1
A limitToOne() 0 7 3
A lastAutoIncrementId() 0 3 1
A insertOrUpdate() 0 9 2
A user() 0 3 1
A explain() 0 3 1
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
    protected function limitToOne(string $table, string $query, string $where, string $separator = "\n")
15
    {
16
        return preg_match('~^INTO~', $query) ||
17
            $this->driver->result("SELECT sqlite_compileoption_used('ENABLE_UPDATE_DELETE_LIMIT')") ?
18
            $this->driver->getLimitClause($query, $where, 1, 0) :
0 ignored issues
show
Bug introduced by
The method getLimitClause() 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

18
            $this->driver->/** @scrutinizer ignore-call */ 
19
                           getLimitClause($query, $where, 1, 0) :

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...
19
            //! use primary key in tables with WITHOUT rowid
20
            " $query WHERE rowid = (SELECT rowid FROM " . $this->driver->escapeTableName($table) . $where . ' LIMIT 1)';
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

20
            " $query WHERE rowid = (SELECT rowid FROM " . $this->driver->/** @scrutinizer ignore-call */ escapeTableName($table) . $where . ' LIMIT 1)';

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...
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public function insertOrUpdate(string $table, array $rows, array $primary)
27
    {
28
        $values = [];
29
        foreach ($rows as $set) {
30
            $values[] = "(" . implode(", ", $set) . ")";
31
        }
32
        $result = $this->driver->execute("REPLACE INTO " . $this->driver->escapeTableName($table) .
33
            " (" . implode(", ", array_keys(reset($rows))) . ") VALUES\n" . implode(",\n", $values));
34
        return $result !== false;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function user()
41
    {
42
        return get_current_user(); // should return effective user
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function view(string $name)
49
    {
50
        return [
51
            'name' => $name,
52
            'type' => 'VIEW',
53
            'materialized' => false,
54
            'select' => preg_replace('~^(?:[^`"[]+|`[^`]*`|"[^"]*")* AS\s+~iU', '',
55
                $this->driver->result("SELECT sql FROM sqlite_master WHERE name = " .
56
                $this->driver->quote($name)))
57
        ]; //! identifiers may be inside []
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function lastAutoIncrementId()
64
    {
65
        return $this->driver->result("SELECT LAST_INSERT_ROWID()");
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function explain(ConnectionInterface $connection, string $query)
72
    {
73
        return $connection->query("EXPLAIN QUERY PLAN $query");
74
    }
75
}
76