Skip to content

Commit cdd1649

Browse files
committed
New SQLite function to decode struct JSON
1 parent 6335bc4 commit cdd1649

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

helper/db.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ protected function init()
2929
// register our JSON function with variable parameters
3030
$this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1);
3131

32+
// register our JSON decode function with variable parameters
33+
$this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_LOOKUP', [$this, 'STRUCT_LOOKUP'], -1);
34+
3235
// this function is meant to be overwritten by plugins
3336
$this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1);
3437
}
@@ -83,6 +86,25 @@ public function STRUCT_JSON(...$args) // phpcs:ignore PSR1.Methods.CamelCapsMeth
8386
return json_encode($args, JSON_THROW_ON_ERROR);
8487
}
8588

89+
/**
90+
* Decodes a struct JSON structure and returns the requested value
91+
*
92+
* @param ...$args
93+
* @return mixed|null
94+
*/
95+
public function STRUCT_LOOKUP(...$args) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
96+
{
97+
$json = $args[0];
98+
$field = $args[1];
99+
100+
try {
101+
$vals = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
102+
} catch (\JsonException $exception) {
103+
return null;
104+
}
105+
return $vals[$field];
106+
}
107+
86108
/**
87109
* This dummy implementation can be overwritten by a plugin
88110
*

types/Lookup.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ public function select(QueryBuilder $QB, $tablealias, $colname, $alias)
245245
$tablealias,
246246
$schema,
247247
$rightalias,
248-
"$tablealias.$colname = STRUCT_JSON($rightalias.pid, CAST($rightalias.rid AS DECIMAL)) " .
248+
"STRUCT_LOOKUP($tablealias.$colname, 0) = $rightalias.pid " .
249+
"AND STRUCT_LOOKUP($tablealias.$colname, 1) = $rightalias.rid " .
249250
"AND $rightalias.latest = 1"
250251
);
251252
$column->getType()->select($QB, $rightalias, $field, $alias);

0 commit comments

Comments
 (0)