store saved queries in separate database

This commit is contained in:
Szymon Olewniczak
2021-08-27 09:46:04 +02:00
parent 4ea71f86fa
commit 09a0e52b96
5 changed files with 140 additions and 34 deletions

View File

@ -207,7 +207,9 @@ class admin_plugin_sqlite extends DokuWiki_Admin_Plugin {
echo '<li>' . $form->toHTML() . '</li>';
echo '</ul>';
if(!$DBI->init($_REQUEST['db'], '')) return;
/** @var $helper helper_plugin_sqlite */
$sqlite_db = plugin_load('helper', 'sqlite');
$sqlite_db->init('sqlite', DOKU_PLUGIN . 'sqlite/db/');
if($INPUT->str('action') == 'save') {
$ok = true;
@ -219,20 +221,17 @@ class admin_plugin_sqlite extends DokuWiki_Admin_Plugin {
echo '<div class="error">give the query name</div>';
$ok = false;
}
if($ok) {
// Create queries table if not exists
$res = $DBI->query("CREATE TABLE IF NOT EXISTS meta_queries
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
sql TEXT NOT NULL);");
$DBI->storeEntry('meta_queries', array(
$sqlite_db->storeEntry('queries', array(
'db' => $_REQUEST['db'],
'name' => $INPUT->str('name'),
'sql' => $INPUT->str('sql')
));
echo '<div class="success">query saved</div>';
}
} elseif($INPUT->str('action') == 'delete') {
$DBI->query("DELETE FROM meta_queries WHERE id=?;", $INPUT->int('id'));
$sqlite_db->query("DELETE FROM queries WHERE id=?;", $INPUT->int('id'));
echo '<div class="success">query deleted</div>';
}
@ -251,24 +250,19 @@ class admin_plugin_sqlite extends DokuWiki_Admin_Plugin {
$form->printForm();
// List saved queries
$meta_queries_table_name = 'meta_queries';
$res = $DBI->query("SELECT name FROM sqlite_master WHERE type='table' AND name=?;", $meta_queries_table_name);
$cnt = $DBI->res2count($res);
if($cnt == 1) {
print '<h3>Saved queries</h3>';
$res = $DBI->query("SELECT id, name, sql FROM $meta_queries_table_name");
$result = $DBI->res2arr($res);
$res = $sqlite_db->query("SELECT id, name, sql FROM queries WHERE db=?", $_REQUEST['db']);
$result = $sqlite_db->res2arr($res);
if(count($result) > 0) {
echo '<h3>Saved queries</h3>';
echo '<div>';
echo '<table class="inline">';
echo '<tr>';
echo '<th>id</th>';
echo '<th>name</th>';
echo '<th>sql</th>';
echo '<th></th>';
echo '</tr>';
foreach($result as $row) {
echo '<tr>';
echo '<td>'.hsc($row['id']).'</td>';
echo '<td>'.hsc($row['name']).'</td>';
$link = wl($ID, array( 'do'=> 'admin',
'page'=> 'sqlite',
@ -292,9 +286,10 @@ class admin_plugin_sqlite extends DokuWiki_Admin_Plugin {
echo '</div>';
}
$result = $DBI->res2arr($res);
if($_REQUEST['sql']) {
if(!$DBI->init($_REQUEST['db'], '')) return;
print '<h3>Query results</h3>';
$sql = $DBI->SQLstring2array($_REQUEST['sql']);
foreach($sql as $s) {

1
db/latest.version Normal file
View File

@ -0,0 +1 @@
1

16
db/update0001.sql Normal file
View File

@ -0,0 +1,16 @@
-- Defines the queries that can be used in wiki
CREATE TABLE queries (
id INTEGER PRIMARY KEY,
db TEXT NOT NULL,
name TEXT NOT NULL,
sql TEXT NOT NULL
);
-- Defines the syntax parsers used query columns
CREATE TABLE parsers (
id INTEGER PRIMARY KEY,
query_id INTEGER NOT NULL,
column TEXT NOT NULL,
parser TEXT NOT NULL,
FOREIGN KEY(query_id) REFERENCES queries(id)
);

93
helper/db.php Normal file
View File

@ -0,0 +1,93 @@
<?php
/**
* DokuWiki Plugin struct (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
*/
use dokuwiki\plugin\struct\meta\StructException;
class helper_plugin_sqlite_db extends DokuWiki_Plugin
{
/** @var helper_plugin_sqlite */
protected $sqlite;
/**
* helper_plugin_struct_db constructor.
*/
public function __construct()
{
$this->init();
}
/**
* Initialize the database
*
* @throws Exception
*/
protected function init()
{
/** @var helper_plugin_sqlite $sqlite */
$this->sqlite = plugin_load('helper', 'sqlite');
if (!$this->sqlite) {
if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
return;
}
if ($this->sqlite->getAdapter() === null) {
if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
$this->sqlite = null;
return;
}
if ($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
$this->sqlite = null;
return;
}
$this->sqlite->getAdapter()->setUseNativeAlter(true);
// initialize the database connection
if (!$this->sqlite->init('sqlite', DOKU_PLUGIN . 'sqlite/db/')) {
if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.');
$this->sqlite = null;
return;
}
}
/**
* @param bool $throw throw an Exception when sqlite not available?
* @return helper_plugin_sqlite|null
*/
public function getDB($throw = true)
{
global $conf;
$len = strlen($conf['metadir']);
if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) {
$this->init();
}
if (!$this->sqlite && $throw) {
throw new StructException('no sqlite');
}
return $this->sqlite;
}
/**
* Completely remove the database and reinitialize it
*
* You do not want to call this except for testing!
*/
public function resetDB()
{
if (!$this->sqlite) return;
$file = $this->sqlite->getAdapter()->getDbFile();
if (!$file) return;
unlink($file);
clearstatcache(true, $file);
$this->init();
}
}
// vim:ts=4:sw=4:et:

View File

@ -64,8 +64,11 @@ class syntax_plugin_sqlite_query extends DokuWiki_Syntax_Plugin
foreach($xml[0]->attributes() as $a => $b) {
$attributes[$a] = (string) $b;
}
$tag_value = (string) $xml[0];
list($db, $name) = explode('.', $tag_value);
$data = ['name' => (string) $xml[0], 'attributes' => $attributes];
$data = ['db' => $db, 'name' => $name, 'attributes' => $attributes];
return $data;
}
@ -88,29 +91,27 @@ class syntax_plugin_sqlite_query extends DokuWiki_Syntax_Plugin
/** @var $DBI helper_plugin_sqlite */
$DBI = plugin_load('helper', 'sqlite');
$dbname = $data['attributes']['db'];
$path = $conf['metadir'].'/'.$dbname . '.sqlite3'; // FIXME: only sqlite3
if(!file_exists($path)) {
echo '<div class="error">unknown database: '.$dbname.'</div>';
/** @var $helper helper_plugin_sqlite */
$sqlite_db = plugin_load('helper', 'sqlite');
$sqlite_db->init('sqlite', DOKU_PLUGIN . 'sqlite/db/');
$db = $data['db'];
$query_name = $data['name'];
if(!$DBI->init($db, '')) {
echo '<div class="error">unknown database: '.$db.'</div>';
return false;
}
$meta_queries_table_name = 'meta_queries';
$DBI->init($dbname, '');
$res = $DBI->query("SELECT sql FROM $meta_queries_table_name WHERE name=?", $data['name']);
if($res === false) {
return false;
}
$sql = $DBI->res2single($res);
$res = $sqlite_db->query("SELECT sql FROM queries WHERE db=? AND name=?", $db, $query_name);
$sql = $sqlite_db->res2single($res);
if (empty($sql)) {
echo '<div class="error">unknown query: '.$data['name'].'</div>';
echo '<div class="error">unknown query: '.$query_name.'</div>';
return false;
}
$res = $DBI->query($sql);
if($res === false) {
if(!$res) {
echo '<div class="error">cannot execute query: '.$sql.'</div>';
return false;
}