diff --git a/admin.php b/admin.php index 466e2c0..5619e7a 100644 --- a/admin.php +++ b/admin.php @@ -207,7 +207,9 @@ class admin_plugin_sqlite extends DokuWiki_Admin_Plugin { echo '
  • ' . $form->toHTML() . '
  • '; echo ''; - 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 '
    give the query name
    '; $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 '
    query saved
    '; } } 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 '
    query deleted
    '; } @@ -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 '

    Saved queries

    '; - $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 '

    Saved queries

    '; echo '
    '; echo ''; echo ''; - echo ''; echo ''; echo ''; echo ''; echo ''; foreach($result as $row) { echo ''; - echo ''; echo ''; $link = wl($ID, array( 'do'=> 'admin', 'page'=> 'sqlite', @@ -292,9 +286,10 @@ class admin_plugin_sqlite extends DokuWiki_Admin_Plugin { echo ''; } - $result = $DBI->res2arr($res); - if($_REQUEST['sql']) { + + if(!$DBI->init($_REQUEST['db'], '')) return; + print '

    Query results

    '; $sql = $DBI->SQLstring2array($_REQUEST['sql']); foreach($sql as $s) { diff --git a/db/latest.version b/db/latest.version new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/db/latest.version @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/db/update0001.sql b/db/update0001.sql new file mode 100644 index 0000000..de9e3bf --- /dev/null +++ b/db/update0001.sql @@ -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) +); diff --git a/helper/db.php b/helper/db.php new file mode 100644 index 0000000..80b27ea --- /dev/null +++ b/helper/db.php @@ -0,0 +1,93 @@ + + */ + +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: diff --git a/syntax/query.php b/syntax/query.php index 8ba1d88..8a5bd8f 100644 --- a/syntax/query.php +++ b/syntax/query.php @@ -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 '
    unknown database: '.$dbname.'
    '; + /** @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 '
    unknown database: '.$db.'
    '; 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 '
    unknown query: '.$data['name'].'
    '; + echo '
    unknown query: '.$query_name.'
    '; return false; } $res = $DBI->query($sql); - if($res === false) { + if(!$res) { echo '
    cannot execute query: '.$sql.'
    '; return false; }
    idnamesql
    '.hsc($row['id']).''.hsc($row['name']).'