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

multidb
Thomas Hooge 6 years ago
parent b852dec610
commit cea39b66c8
  1. 7
      README.md
  2. 1
      conf/default.php
  3. 2
      conf/metadata.php
  4. 1
      lang/de/settings.php
  5. 1
      lang/en/settings.php
  6. 1
      lang/ru/settings.php
  7. 10
      syntax.php

@ -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>
```

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

@ -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');

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

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

@ -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'] = 'Логин';

@ -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) {