Allow connection to different database backends, for now mysql and dblib

This commit is contained in:
Thomas Hooge 2018-12-12 12:14:54 +01:00
parent b852dec610
commit cea39b66c8
7 changed files with 16 additions and 7 deletions

View File

@ -2,11 +2,10 @@
A dokuwiki plugin for processing query to MySQL databases and display results as a table.
This is a improved version which cann connect to different hosts
and databases. The default host and database is set in the plugin
configuration.
Different hosts and databases can be set inside the tag:
and databases. The defaults are set in the plugin configuration.
Different types, hosts and databases can be set inside the tag:
```
<sql host=myhost db=mydb>
<sql type=mysql host=myhost db=mydb>
SELECT foo FROM bar ORDER BY baz
<sql>
```

View File

@ -7,6 +7,7 @@
//$conf['fixme'] = 'FIXME';
$conf['type'] = 'mysql';
$conf['Host'] = 'localhost';
$conf['DB'] = '';
$conf['user'] = '';

View File

@ -5,7 +5,7 @@
* @author George Pirogov <i1557@yandex.ru>
*/
$meta['type'] = array('multichoice', '_choices' => array('mysql', 'dblib'));
$meta['Host'] = array('string');
$meta['DB'] = array('string');
$meta['user'] = array('string');

View File

@ -7,6 +7,7 @@
// keys need to match the config setting name
$lang['type'] = 'DSN-Prefix (Datenbanktyp)';
$lang['Host'] = 'Standard MySQL-Serveradresse (DNS or IP)';
$lang['DB'] = 'Standard MySQL-Datenbankname';
$lang['user'] = 'Datenbankbenutzername';

View File

@ -8,6 +8,7 @@
// keys need to match the config setting name
// $lang['fixme'] = 'FIXME';
$lang['type'] = 'DSN prefix (database type)';
$lang['Host'] = 'Default MySQL server address (DNS or IP)';
$lang['DB'] = 'MySQL database name';
$lang['user'] = 'Database username';

View File

@ -8,6 +8,7 @@
// keys need to match the config setting name
// $lang['fixme'] = 'FIXME';
$lang['type'] = 'Префикс DSN (тип базы данных)';
$lang['Host'] = 'Адрес MySQL сервера по умолчанию (DNS или IP)';
$lang['DB'] = 'Имя базы данных MySQL по умолчанию';
$lang['user'] = 'Логин';

View File

@ -23,7 +23,7 @@ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin {
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('<sql\b(?:\s+(?:host|db)=[\w\-\.$]+?)*\s*>(?:.*?</sql>)', $mode, 'plugin_sqlquery');
$this->Lexer->addSpecialPattern('<sql\b(?:\s+(?:host|db|type)=[\w\-\.$]+?)*\s*>(?:.*?</sql>)', $mode, 'plugin_sqlquery');
}
/**
@ -39,6 +39,12 @@ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin {
public function handle($match, $state, $pos, Doku_Handler $handler) {
$data = array('state' => $state);
if ($state == DOKU_LEXER_SPECIAL) {
# get type (DSN prefix)
if (preg_match('/<sql\b.*type=(mysql|dblib)/', $match, $result)) {
$data['type'] = $result[1];
} else {
$data['type'] = $this->getConf('type');
}
# get host
if (preg_match('/<sql\b.*host=([\w\-\.$]+)/', $match, $result)) {
$data['host'] = $result[1];
@ -79,7 +85,7 @@ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin {
$password = $this->getConf('password');
// connect to database
$dsn = "mysql:host={$data['host']};dbname={$data[db]}";
$dsn = "{$data['type']}:host={$data['host']};dbname={$data[db]}";
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {