Passed
Push — main ( af1024...b746de )
by Thierry
02:04
created

Query::setAffectedRows()   A

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
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
use Lagdo\DbAdmin\Driver\Entity\TableSelectEntity;
7
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
8
9
use Lagdo\DbAdmin\Driver\DriverInterface;
10
use Lagdo\DbAdmin\Driver\UtilInterface;
11
use Lagdo\DbAdmin\Driver\TranslatorInterface;
12
13
use function implode;
14
use function array_keys;
15
use function intval;
16
use function microtime;
17
use function is_object;
18
use function preg_replace;
19
20
abstract class Query implements QueryInterface
21
{
22
    /**
23
     * @var DriverInterface
24
     */
25
    protected $driver;
26
27
    /**
28
     * @var UtilInterface
29
     */
30
    protected $util;
31
32
    /**
33
     * @var TranslatorInterface
34
     */
35
    protected $trans;
36
37
    /**
38
     * @var ConnectionInterface
39
     */
40
    protected $connection;
41
42
    /**
43
     * The constructor
44
     *
45
     * @param DriverInterface $driver
46
     * @param UtilInterface $util
47
     * @param TranslatorInterface $trans
48
     * @param ConnectionInterface $connection
49
     */
50
    public function __construct(DriverInterface $driver, UtilInterface $util,
51
        TranslatorInterface $trans, ConnectionInterface $connection)
52
    {
53
        $this->driver = $driver;
54
        $this->util = $util;
55
        $this->trans = $trans;
56
        $this->connection = $connection;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function schema()
63
    {
64
        return "";
65
    }
66
67
    /**
68
     * Formulate SQL modification query with limit 1
69
     *
70
     * @param string $table
71
     * @param string $query Everything after UPDATE or DELETE
72
     * @param string $where
73
     * @param string $separator
74
     *
75
     * @return string
76
     */
77
    abstract protected function limitToOne(string $table, string $query, string $where, string $separator = "\n");
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function select(string $table, array $select, array $where,
83
        array $group, array $order = [], int $limit = 1, int $page = 0)
84
    {
85
        $entity = new TableSelectEntity($table, $select, $where, $group, $order, $limit, $page);
86
        $query = $this->driver->buildSelectQuery($entity);
87
        // $this->start = intval(microtime(true));
88
        return $this->connection->query($query);
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function insert(string $table, array $values)
95
    {
96
        $table = $this->driver->table($table);
97
        if (!empty($values)) {
98
            $result = $this->execute("INSERT INTO $table DEFAULT VALUES");
0 ignored issues
show
Bug introduced by
The method execute() does not exist on Lagdo\DbAdmin\Driver\Db\Query. ( Ignorable by Annotation )

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

98
            /** @scrutinizer ignore-call */ 
99
            $result = $this->execute("INSERT INTO $table DEFAULT 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...
99
            return $result !== false;
100
        }
101
        $result = $this->execute("INSERT INTO $table (" .
102
            implode(", ", array_keys($values)) . ") VALUES (" . implode(", ", $values) . ")");
103
        return $result !== false;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function update(string $table, array $values, string $queryWhere, int $limit = 0, string $separator = "\n")
110
    {
111
        $assignments = [];
112
        foreach ($values as $name => $value) {
113
            $assignments[] = "$name = $value";
114
        }
115
        $query = $this->driver->table($table) . " SET$separator" . implode(",$separator", $assignments);
116
        if (!$limit) {
117
            $result = $this->execute('UPDATE ' . $query . $queryWhere);
118
            return $result !== false;
119
        }
120
        $result = $this->execute('UPDATE' . $this->limitToOne($table, $query, $queryWhere, $separator));
121
        return $result !== false;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function delete(string $table, string $queryWhere, int $limit = 0)
128
    {
129
        $query = 'FROM ' . $this->driver->table($table);
130
        if (!$limit) {
131
            $result = $this->execute("DELETE $query $queryWhere");
132
            return $result !== false;
133
        }
134
        $result = $this->execute('DELETE' . $this->limitToOne($table, $query, $queryWhere));
135
        return $result !== false;
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function explain(ConnectionInterface $connection, string $query)
142
    {
143
        return false;
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function slowQuery(string $query, int $timeout)
150
    {
151
        return null;
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157
    public function countRows(TableEntity $tableStatus, array $where)
158
    {
159
        return null;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function convertSearch(string $idf, array $val, TableFieldEntity $field)
166
    {
167
        return $idf;
168
    }
169
170
    /**
171
     * @inheritDoc
172
     */
173
    public function view(string $name)
174
    {
175
        return [];
176
    }
177
}
178