Skip to content

Commit 6dc5ccf

Browse files
committed
Merge branch 'sqlite-devel'
* sqlite-devel: use GROUP_CONCAT_DISTINCT adjust method call for getting PDO access Make all tests run with the new SQLiteDB class Use new \dokuwiki\plugin\sqlite\SQLiteDB class for DB access
2 parents 87f800c + acc82d6 commit 6dc5ccf

22 files changed

+119
-238
lines changed

_test/AccessTableDataDBTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,20 @@ public function test_saveData()
190190
$result = $schemaData->saveData($testdata);
191191

192192
// assert
193-
$res = $this->sqlite->query(
193+
$actual_saved_single = $this->sqlite->queryRecord(
194194
"SELECT pid, col1, col2 FROM data_testtable WHERE pid = ? ORDER BY rev DESC LIMIT 1",
195195
['testpage']
196196
);
197-
$actual_saved_single = $this->sqlite->res2row($res);
198197
$expected_saved_single = [
199198
'pid' => 'testpage',
200199
'col1' => 'value1_saved',
201200
'col2' => 'value2.1_saved' # copy of the multi-value's first value
202201
];
203202

204-
$res = $this->sqlite->query(
203+
$actual_saved_multi = $this->sqlite->queryAll(
205204
"SELECT colref, row, value FROM multi_testtable WHERE pid = ? ORDER BY rev DESC LIMIT 3",
206205
['testpage']
207206
);
208-
$actual_saved_multi = $this->sqlite->res2arr($res);
209207
$expected_saved_multi = [
210208
[
211209
'colref' => '2',

_test/AccessTableDataReplacementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public function setUp(): void
2626
$schemafoo['new']['new1']['class'] = 'Page';
2727
$schemafoo['new']['new1']['isenabled'] = '1';
2828
$schemafoo['new']['new1']['config'] = null;
29-
$schemafoo['new']['new1']['sort'] = null;
29+
$schemafoo['new']['new1']['sort'] = 10;
3030

3131
$schemabar['new']['new2']['label'] = 'data';
3232
$schemabar['new']['new2']['ismulti'] = 0;
3333
$schemabar['new']['new2']['class'] = 'Text';
3434
$schemabar['new']['new2']['isenabled'] = '1';
3535
$schemabar['new']['new2']['config'] = null;
36-
$schemabar['new']['new2']['sort'] = null;
36+
$schemabar['new']['new2']['sort'] = 20;
3737

3838
$builder_foo = new meta\SchemaBuilder('foo', $schemafoo);
3939
$builder_foo->build();

_test/AccessTableDataSQLTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function buildGetDataSQL_testdata()
5151
"SELECT DATA.pid AS PID,
5252
DATA.col1 AS out1,
5353
DATA.col2 AS out2,
54-
GROUP_CONCAT(M3.value,'" . Search::CONCAT_SEPARATOR . "') AS out3
54+
GROUP_CONCAT_DISTINCT(M3.value,'" . Search::CONCAT_SEPARATOR . "') AS out3
5555
FROM data_testtable AS DATA
5656
LEFT OUTER JOIN multi_testtable AS M3
5757
ON DATA.pid = M3.pid
@@ -70,8 +70,8 @@ public static function buildGetDataSQL_testdata()
7070
'multis' => ['dokuwiki\\plugin\\struct\\types\\Text', 'dokuwiki\\plugin\\struct\\types\\Text']
7171
],
7272
"SELECT DATA.pid AS PID,
73-
GROUP_CONCAT(M1.value,'" . Search::CONCAT_SEPARATOR . "') AS out1,
74-
GROUP_CONCAT(M2.value,'" . Search::CONCAT_SEPARATOR . "') AS out2
73+
GROUP_CONCAT_DISTINCT(M1.value,'" . Search::CONCAT_SEPARATOR . "') AS out1,
74+
GROUP_CONCAT_DISTINCT(M2.value,'" . Search::CONCAT_SEPARATOR . "') AS out2
7575
FROM data_testtable AS DATA
7676
LEFT OUTER JOIN multi_testtable AS M2
7777
ON DATA.pid = M2.pid

_test/HelperDBTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ public function test_json()
2727
$helper = plugin_load('helper', 'struct_db');
2828
$sqlite = $helper->getDB();
2929

30-
$res = $sqlite->query("SELECT STRUCT_JSON('foo', 'bar') ");
31-
$result = $sqlite->res2single($res);
32-
$sqlite->res_close($res);
30+
$result = $sqlite->queryValue("SELECT STRUCT_JSON('foo', 'bar') ");
3331
$expect = '["foo","bar"]';
3432
$this->assertEquals($expect, $result);
3533

36-
$res = $sqlite->query("SELECT STRUCT_JSON(id, tbl) AS col FROM schemas");
37-
$result = $sqlite->res2arr($res);
38-
$sqlite->res_close($res);
34+
$result = $sqlite->queryAll("SELECT STRUCT_JSON(id, tbl) AS col FROM schemas");
3935

4036
$expect = [
4137
['col' => '[1,"schema1"]'],

_test/SchemaBuilderTest.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ public function test_build_new()
5555
$result = $builder->build();
5656

5757
/** @noinspection SqlResolve */
58-
$res = $this->sqlite->query("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", 'data_' . $testname);
59-
$tableSQL = $this->sqlite->res2single($res);
60-
$this->sqlite->res_close($res);
58+
$tableSQL = $this->sqlite->queryValue("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", ['data_' . $testname]);
6159
$expected_tableSQL = "CREATE TABLE data_testtable (
6260
pid TEXT DEFAULT '',
6361
rid INTEGER,
@@ -67,9 +65,7 @@ public function test_build_new()
6765
PRIMARY KEY(pid, rid, rev)
6866
)";
6967

70-
$res = $this->sqlite->query("SELECT * FROM types");
71-
$actual_types = $this->sqlite->res2arr($res);
72-
$this->sqlite->res_close($res);
68+
$actual_types = $this->sqlite->queryAll("SELECT * FROM types");
7369
$expected_types = [
7470
[
7571
'id' => "1",
@@ -87,9 +83,7 @@ public function test_build_new()
8783
]
8884
];
8985

90-
$res = $this->sqlite->query("SELECT * FROM schema_cols");
91-
$actual_cols = $this->sqlite->res2arr($res);
92-
$this->sqlite->res_close($res);
86+
$actual_cols = $this->sqlite->queryAll("SELECT * FROM schema_cols");
9387
$expected_cols = [
9488
[
9589
'sid' => "1",
@@ -107,9 +101,7 @@ public function test_build_new()
107101
]
108102
];
109103

110-
$res = $this->sqlite->query("SELECT * FROM schemas");
111-
$actual_schema = $this->sqlite->res2row($res);
112-
$this->sqlite->res_close($res);
104+
$actual_schema = $this->sqlite->queryRecord("SELECT * FROM schemas");
113105

114106
$this->assertSame(1, $result);
115107
$this->assertEquals($expected_tableSQL, $tableSQL);
@@ -156,9 +148,7 @@ public function test_build_update()
156148
$builder = new SchemaBuilder($testname, $updatedata);
157149
$result = $builder->build();
158150

159-
$res = $this->sqlite->query("SELECT * FROM types");
160-
$actual_types = $this->sqlite->res2arr($res);
161-
$this->sqlite->res_close($res);
151+
$actual_types = $this->sqlite->queryAll("SELECT * FROM types");
162152
$expected_types = [
163153
[
164154
'id' => "1",

_test/mock/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ class Search extends meta\Search
2020
public function isNotPublisher()
2121
{
2222
$this->dbHelper = new helper_plugin_struct_db;
23-
$this->sqlite->create_function('IS_PUBLISHER', [$this->dbHelper, 'IS_PUBLISHER'], -1);
23+
$this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this->dbHelper, 'IS_PUBLISHER'], -1);
2424
}
2525
}

_test/types/PageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Testing the Page Type
1212
*
13-
* @group plugin_struct
13+
* @group plugin_structp
1414
* @group plugins
1515
*/
1616
class PageTest extends StructTest

action/cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function handleCacheAggregation(Doku_Event $event, $param)
7979
// cache depends on last database save
8080
$sqlite = $db->getDB(false);
8181
if ($sqlite) {
82-
$cache->depends['files'][] = $sqlite->getAdapter()->getDbFile();
82+
$cache->depends['files'][] = $sqlite->getDbFile();
8383
}
8484

8585
// dynamic renders should never overwrite the default page cache

action/migration.php

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public function register(Doku_Event_Handler $controller)
3030
*/
3131
public function handleMigrations(Doku_Event $event, $param)
3232
{
33-
if ($event->data['sqlite']->getAdapter()->getDbname() !== 'struct') {
33+
if ($event->data['adapter']->getDbname() !== 'struct') {
3434
return;
3535
}
3636
$to = $event->data['to'];
3737

3838
if (is_callable(array($this, "migration$to"))) {
3939
$event->preventDefault();
40-
$event->result = call_user_func(array($this, "migration$to"), $event->data['sqlite']);
40+
$event->result = call_user_func(array($this, "migration$to"), $event->data['adapter']);
4141
}
4242
}
4343

@@ -46,16 +46,14 @@ public function handleMigrations(Doku_Event $event, $param)
4646
*
4747
* Add a latest column to all existing multi tables
4848
*
49-
* @param helper_plugin_sqlite $sqlite
49+
* @param \dokuwiki\plugin\sqlite\SQLiteDB $sqlite
5050
* @return bool
5151
*/
52-
protected function migration12(helper_plugin_sqlite $sqlite)
52+
protected function migration12($sqlite)
5353
{
5454
/** @noinspection SqlResolve */
5555
$sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
56-
$res = $sqlite->query($sql);
57-
$tables = $sqlite->res2arr($res);
58-
$sqlite->res_close($res);
56+
$tables = $sqlite->queryAll($sql);
5957

6058
foreach ($tables as $row) {
6159
$sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
@@ -70,27 +68,23 @@ protected function migration12(helper_plugin_sqlite $sqlite)
7068
*
7169
* Unifies previous page and lookup schema types
7270
*
73-
* @param helper_plugin_sqlite $sqlite
71+
* @param \dokuwiki\plugin\sqlite\SQLiteDB $sqlite
7472
* @return bool
7573
*/
76-
protected function migration16(helper_plugin_sqlite $sqlite)
74+
protected function migration16($sqlite)
7775
{
7876
// get tables and their SQL definitions
7977
$sql = "SELECT sql, name FROM sqlite_master
8078
WHERE type = 'table'
8179
AND (name LIKE 'data_%' OR name LIKE 'multi_%')";
82-
$res = $sqlite->query($sql);
83-
$tables = $sqlite->res2arr($res);
84-
$sqlite->res_close($res);
80+
$tables = $sqlite->queryAll($sql);
8581

8682
// get latest versions of schemas with islookup property
8783
$sql = "SELECT MAX(id) AS id, tbl, islookup FROM schemas
8884
GROUP BY tbl
8985
";
90-
$res = $sqlite->query($sql);
91-
$schemas = $sqlite->res2arr($res);
86+
$schemas = $sqlite->queryAll($sql);
9287

93-
$sqlite->query('BEGIN TRANSACTION');
9488
$ok = true;
9589

9690
// Step 1: move original data to temporary tables and create new ones with modified schemas
@@ -153,8 +147,7 @@ protected function migration16(helper_plugin_sqlite $sqlite)
153147

154148
// introduce composite ids in lookup columns
155149
$s = $this->getLookupColsSql($sid);
156-
$res = $sqlite->query($s);
157-
$cols = $sqlite->res2arr($res);
150+
$cols = $sqlite->queryAll($s);
158151

159152
if ($cols) {
160153
foreach ($cols as $col) {
@@ -208,8 +201,7 @@ protected function migration16(helper_plugin_sqlite $sqlite)
208201
$sql = "SELECT sql FROM sqlite_master
209202
WHERE type = 'table'
210203
AND name = 'schemas'";
211-
$res = $sqlite->query($sql);
212-
$t = $sqlite->res2arr($res);
204+
$t = $sqlite->queryAll($sql);
213205
$sql = $t[0]['sql'];
214206
$sql = str_replace('islookup INTEGER,', '', $sql);
215207

@@ -224,12 +216,7 @@ protected function migration16(helper_plugin_sqlite $sqlite)
224216
$s = 'INSERT INTO schemas SELECT id, tbl, ts, user, comment, config FROM temp_schemas';
225217
$ok = $ok && $sqlite->query($s);
226218

227-
if (!$ok) {
228-
$sqlite->query('ROLLBACK TRANSACTION');
229-
return false;
230-
}
231-
$sqlite->query('COMMIT TRANSACTION');
232-
return true;
219+
return $ok;
233220
}
234221

235222
/**
@@ -239,27 +226,24 @@ protected function migration16(helper_plugin_sqlite $sqlite)
239226
* All lookups were presumed to reference lookup data, not pages, so the migrated value
240227
* was always ["", <previous-pid-aka-new-rid>]. For page references it is ["<previous-pid>", 0]
241228
*
242-
* @param helper_plugin_sqlite $sqlite
229+
* @param \dokuwiki\plugin\sqlite\SQLiteDB $sqlite
243230
* @return bool
244231
*/
245-
protected function migration17(helper_plugin_sqlite $sqlite)
232+
protected function migration17($sqlite)
246233
{
247234
$sql = "SELECT MAX(id) AS id, tbl FROM schemas
248235
GROUP BY tbl
249236
";
250-
$res = $sqlite->query($sql);
251-
$schemas = $sqlite->res2arr($res);
237+
$schemas = $sqlite->queryAll($sql);
252238

253-
$sqlite->query('BEGIN TRANSACTION');
254239
$ok = true;
255240

256241
foreach ($schemas as $schema) {
257242
// find lookup columns
258243
$name = $schema['tbl'];
259244
$sid = $schema['id'];
260245
$s = $this->getLookupColsSql($sid);
261-
$res = $sqlite->query($s);
262-
$cols = $sqlite->res2arr($res);
246+
$cols = $sqlite->queryAll($s);
263247

264248
if ($cols) {
265249
$colnames = array_map(function ($c) {
@@ -268,8 +252,7 @@ protected function migration17(helper_plugin_sqlite $sqlite)
268252

269253
// data_ tables
270254
$s = 'SELECT pid, rid, rev, ' . implode(', ', $colnames) . " FROM data_$name";
271-
$res = $sqlite->query($s);
272-
$allValues = $sqlite->res2arr($res);
255+
$allValues = $sqlite->queryAll($s);
273256

274257
if (!empty($allValues)) {
275258
foreach ($allValues as $row) {
@@ -289,8 +272,7 @@ protected function migration17(helper_plugin_sqlite $sqlite)
289272

290273
// multi_ tables
291274
$s = "SELECT colref, pid, rid, rev, row, value FROM multi_$name";
292-
$res = $sqlite->query($s);
293-
$allValues = $sqlite->res2arr($res);
275+
$allValues = $sqlite->queryAll($s);
294276

295277
if (!empty($allValues)) {
296278
foreach ($allValues as $row) {
@@ -312,64 +294,45 @@ protected function migration17(helper_plugin_sqlite $sqlite)
312294
}
313295
}
314296

315-
if (!$ok) {
316-
$sqlite->query('ROLLBACK TRANSACTION');
317-
return false;
318-
}
319-
$sqlite->query('COMMIT TRANSACTION');
320-
return true;
297+
return $ok;
321298
}
322299

323300
/**
324301
* Removes a temp table left over by migration 16
325302
*
326-
* @param helper_plugin_sqlite $sqlite
303+
* @param \dokuwiki\plugin\sqlite\SQLiteDB $sqlite
327304
* @return bool
328305
*/
329-
protected function migration18(helper_plugin_sqlite $sqlite)
306+
protected function migration18($sqlite)
330307
{
331308
$ok = true;
332-
$sqlite->query('BEGIN TRANSACTION');
333309

334310
$sql = 'DROP TABLE IF EXISTS temp_schemas';
335311
$ok = $ok && $sqlite->query($sql);
336312

337-
if (!$ok) {
338-
$sqlite->query('ROLLBACK TRANSACTION');
339-
return false;
340-
}
341-
$sqlite->query('COMMIT TRANSACTION');
342-
return true;
313+
return $ok;
343314
}
344315
/**
345316
* Executes Migration 19
346317
*
347318
* Add "published" column to all existing tables
348319
*
349-
* @param helper_plugin_sqlite $sqlite
320+
* @param \dokuwiki\plugin\sqlite\SQLiteDB $sqlite
350321
* @return bool
351322
*/
352-
protected function migration19(helper_plugin_sqlite $sqlite)
323+
protected function migration19($sqlite)
353324
{
354325
$ok = true;
355-
$sqlite->query('BEGIN TRANSACTION');
356326

357327
/** @noinspection SqlResolve */
358328
$sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND (name LIKE 'data_%' OR name LIKE 'multi_%')";
359-
$res = $sqlite->query($sql);
360-
$tables = $sqlite->res2arr($res);
361-
$sqlite->res_close($res);
329+
$tables = $sqlite->queryAll($sql);
362330

363331
foreach ($tables as $row) {
364332
$sql = 'ALTER TABLE ? ADD COLUMN published INT DEFAULT NULL';
365333
$ok = $ok && $sqlite->query($sql, $row['name']);
366334
}
367-
if (!$ok) {
368-
$sqlite->query('ROLLBACK TRANSACTION');
369-
return false;
370-
}
371-
$sqlite->query('COMMIT TRANSACTION');
372-
return true;
335+
return $ok;
373336
}
374337

375338

0 commit comments

Comments
 (0)