';
- echo ''.hsc($row['id']).' | ';
echo ''.hsc($row['name']).' | ';
$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;
}