Passed
Push — main ( 317226...e95517 )
by Thierry
07:32
created

Database   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
dl 0
loc 228
rs 9.92
c 1
b 0
f 0
wmc 31

17 Methods

Rating   Name   Duplication   Size   Complexity  
A truncateTables() 0 3 1
A __construct() 0 5 1
B dropAndCreate() 0 24 10
A executeSavedQuery() 0 4 1
A sequences() 0 3 1
A schemas() 0 3 1
A dropViews() 0 3 1
A copyTables() 0 3 1
A moveTables() 0 3 1
A routine() 0 3 1
A createView() 0 9 3
A dropView() 0 11 2
A userTypes() 0 3 1
A updateView() 0 18 3
A events() 0 3 1
A routineId() 0 3 1
A routines() 0 3 1
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Db;
4
5
use Exception;
6
use Lagdo\DbAdmin\Driver\DriverInterface;
7
use Lagdo\DbAdmin\Driver\UtilInterface;
8
use Lagdo\DbAdmin\Driver\TranslatorInterface;
9
10
use function trim;
11
use function strtoupper;
12
use function uniqid;
13
14
abstract class Database implements DatabaseInterface
15
{
16
    /**
17
     * @var DriverInterface
18
     */
19
    protected $driver;
20
21
    /**
22
     * @var UtilInterface
23
     */
24
    protected $util;
25
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    protected $trans;
30
31
    /**
32
     * The constructor
33
     *
34
     * @param DriverInterface $driver
35
     * @param UtilInterface $util
36
     * @param TranslatorInterface $trans
37
     */
38
    public function __construct(DriverInterface $driver, UtilInterface $util, TranslatorInterface $trans)
39
    {
40
        $this->driver = $driver;
41
        $this->util = $util;
42
        $this->trans = $trans;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function dropViews(array $views)
49
    {
50
        return false;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function moveTables(array $tables, array $views, string $target)
57
    {
58
        return false;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function copyTables(array $tables, array $views, string $target)
65
    {
66
        return false;
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function truncateTables(array $tables)
73
    {
74
        return false;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function createView(array $values)
81
    {
82
        // From view.inc.php
83
        $name = trim($values['name']);
84
        $type = $values['materialized'] ? ' MATERIALIZED VIEW ' : ' VIEW ';
85
86
        $sql = ($this->driver->jush() === 'mssql' ? 'ALTER' : 'CREATE OR REPLACE') .
87
            $type . $this->driver->table($name) . " AS\n" . $values['select'];
88
        return $this->driver->executeQuery($sql);
89
    }
90
    /**
91
     * Execute remembered queries
92
     *
93
     * @param bool $failed
94
     *
95
     * @return bool
96
     * @throws Exception
97
     */
98
    private function executeSavedQuery(bool $failed): bool
99
    {
100
        list($queries/*, $time*/) = $this->driver->queries();
101
        return $this->driver->executeQuery($queries, false, $failed/*, $time*/);
102
    }
103
104
    /**
105
     * Drop old object and create a new one
106
     *
107
     * @param string $drop Drop old object query
108
     * @param string $create Create new object query
109
     * @param string $dropCreated Drop new object query
110
     * @param string $test Create test object query
111
     * @param string $dropTest Drop test object query
112
     * @param string $oldName
113
     * @param string $newName
114
     *
115
     * @return string
116
     * @throws Exception
117
     */
118
    private function dropAndCreate(string $drop, string $create, string $dropCreated,
119
        string $test, string $dropTest, string $oldName, string $newName): string
120
    {
121
        if ($oldName == '' && $newName == '') {
122
            $this->driver->executeQuery($drop);
123
            return 'dropped';
124
        }
125
        if ($oldName == '') {
126
            $this->driver->executeQuery($create);
127
            return 'created';
128
        }
129
        if ($oldName != $newName) {
130
            $created = $this->driver->execute($create);
131
            $dropped = $this->driver->execute($drop);
132
            // $this->executeSavedQuery(!($created && $this->driver->execute($drop)));
133
            if (!$dropped && $created) {
134
                $this->driver->execute($dropCreated);
135
            }
136
            return 'altered';
137
        }
138
        $this->executeSavedQuery(!($this->driver->execute($test) &&
139
            $this->driver->execute($dropTest) &&
140
            $this->driver->execute($drop) && $this->driver->execute($create)));
141
        return 'altered';
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    public function updateView(string $view, array $values): string
148
    {
149
        // From view.inc.php
150
        $origType = 'VIEW';
151
        if ($this->driver->jush() === 'pgsql') {
152
            $status = $this->driver->tableStatus($view);
153
            $origType = strtoupper($status->engine);
154
        }
155
156
        $name = trim($values['name']);
157
        $type = $values['materialized'] ? 'MATERIALIZED VIEW' : 'VIEW';
158
        $tempName = $name . '_adminer_' . uniqid();
159
160
        return $this->dropAndCreate("DROP $origType " . $this->driver->table($view),
161
            "CREATE $type " . $this->driver->table($name) . " AS\n" . $values['select'],
162
            "DROP $type " . $this->driver->table($name),
163
            "CREATE $type " . $this->driver->table($tempName) . " AS\n" . $values['select'],
164
            "DROP $type " . $this->driver->table($tempName), $view, $name);
165
    }
166
167
    /**
168
     * Drop a view
169
     *
170
     * @param string $view The view name
171
     *
172
     * @return bool
173
     * @throws Exception
174
     */
175
    public function dropView(string $view): bool
176
    {
177
        // From view.inc.php
178
        $origType = 'VIEW';
179
        if ($this->driver->jush() == 'pgsql') {
180
            $status = $this->driver->tableStatus($view);
181
            $origType = strtoupper($status->engine);
182
        }
183
184
        $sql = "DROP $origType " . $this->driver->table($view);
185
        return $this->driver->executeQuery($sql);
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191
    public function sequences()
192
    {
193
        return [];
194
    }
195
196
    /**
197
     * @inheritDoc
198
     */
199
    public function userTypes()
200
    {
201
        return [];
202
    }
203
204
    /**
205
     * @inheritDoc
206
     */
207
    public function schemas()
208
    {
209
        return [];
210
    }
211
212
    /**
213
     * @inheritDoc
214
     */
215
    public function events()
216
    {
217
        return [];
218
    }
219
220
    /**
221
     * @inheritDoc
222
     */
223
    public function routine(string $name, string $type)
224
    {
225
        return null;
226
    }
227
228
    /**
229
     * @inheritDoc
230
     */
231
    public function routines()
232
    {
233
        return [];
234
    }
235
236
    /**
237
     * @inheritDoc
238
     */
239
    public function routineId(string $name, array $row)
240
    {
241
        return '';
242
    }
243
}
244