Query::lastAutoIncrementId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
7
8
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
9
10
use Lagdo\DbAdmin\Driver\Db\Query as AbstractQuery;
11
12
use function count;
13
use function array_keys;
14
use function implode;
15
use function strlen;
16
use function preg_match;
17
use function preg_replace;
18
19
class Query extends AbstractQuery
20
{
21
    /**
22
     * @inheritDoc
23
     */
24
    protected function limitToOne(string $table, string $query, string $where)
25
    {
26
        return $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

26
        return $this->driver->/** @scrutinizer ignore-call */ 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...
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function insert(string $table, array $values)
33
    {
34
        if (!empty($values)) {
35
            return parent::insert($table, $values);
36
        }
37
        $result = $this->driver->execute('INSERT INTO ' . $this->driver->escapeTableName($table) . ' () VALUES ()');
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

37
        $result = $this->driver->execute('INSERT INTO ' . $this->driver->/** @scrutinizer ignore-call */ escapeTableName($table) . ' () VALUES ()');

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...
38
        return $result !== false;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function insertOrUpdate(string $table, array $rows, array $primary)
45
    {
46
        $columns = array_keys(reset($rows));
47
        $prefix = 'INSERT INTO ' . $this->driver->escapeTableName($table) . ' (' . implode(', ', $columns) . ') VALUES ';
48
        $values = [];
49
        foreach ($columns as $key) {
50
            $values[$key] = "$key = VALUES($key)";
51
        }
52
        $suffix = ' ON DUPLICATE KEY UPDATE ' . implode(', ', $values);
53
        $values = [];
54
        $length = 0;
55
        foreach ($rows as $set) {
56
            $value = '(' . implode(', ', $set) . ')';
57
            if (!empty($values) && (strlen($prefix) + $length + strlen($value) + strlen($suffix) > 1e6)) {
58
                // 1e6 - default max_allowed_packet
59
                if (!$this->driver->execute($prefix . implode(",\n", $values) . $suffix)) {
60
                    return false;
61
                }
62
                $values = [];
63
                $length = 0;
64
            }
65
            $values[] = $value;
66
            $length += strlen($value) + 2; // 2 - strlen(",\n")
67
        }
68
        $result = $this->driver->execute($prefix . implode(",\n", $values) . $suffix);
69
        return $result !== false;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function slowQuery(string $query, int $timeout)
76
    {
77
        // $this->connection->timeout = $timeout;
78
        if ($this->driver->minVersion('5.7.8', '10.1.2')) {
79
            if (preg_match('~MariaDB~', $this->driver->serverInfo())) {
80
                return "SET STATEMENT max_statement_time=$timeout FOR $query";
81
            } elseif (preg_match('~^(SELECT\b)(.+)~is', $query, $match)) {
82
                return "$match[1] /*+ MAX_EXECUTION_TIME(" . ($timeout * 1000) . ") */ $match[2]";
83
            }
84
        }
85
        return null;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function convertSearch(string $idf, array $val, TableFieldEntity $field)
92
    {
93
        return (preg_match('~char|text|enum|set~', $field->type) &&
94
            !preg_match('~^utf8~', $field->collation) && preg_match('~[\x80-\xFF]~', $val['val']) ?
95
            "CONVERT($idf USING " . $this->driver->charset() . ')' : $idf
96
        );
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function user()
103
    {
104
        return $this->driver->result('SELECT USER()');
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function view(string $name)
111
    {
112
        return [
113
            'name' => $name,
114
            'type' => 'VIEW',
115
            'materialized' => false,
116
            'select' => preg_replace('~^(?:[^`]|`[^`]*`)*\s+AS\s+~isU', '',
117
                $this->driver->result('SHOW CREATE VIEW ' . $this->driver->escapeTableName($name), 1)),
118
        ];
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function lastAutoIncrementId()
125
    {
126
        return $this->driver->result('SELECT LAST_INSERT_ID()'); // mysql_insert_id() truncates bigint
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function explain(ConnectionInterface $connection, string $query)
133
    {
134
        return $connection->query('EXPLAIN ' . ($this->driver->minVersion(5.1) &&
135
            !$this->driver->minVersion(5.7) ? 'PARTITIONS ' : '') . $query);
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function countRows(TableEntity $tableStatus, array $where)
142
    {
143
        return (!empty($where) || $tableStatus->engine != 'InnoDB' ? null : count($tableStatus->rows));
144
    }
145
}
146