DBI Query Plugin<-- Contributions to this plugin are appreciated. Please update the plugin page at http://twiki.org/cgi-bin/view/Plugins/DBIQueryPlugin Page contents
OverviewThis plugin is intended to provide TWiki with ability to make complex database requests using DBI Perl module.
Syntax RulesSyntax:
%DBI_QUERY{"db_identifier" ...}% SELECT ... .header head .body %column% %DBI_SUBQUERY{"name"}% .footer footer %DBI_QUERY% %DBI_DO{"db_identifier" ...}% # Some Perl code. %DBI_DO% %DBI_DO{"db_identifier" topic="SomeTopic" script="some_script"}% %DBI_CALL{"subquery"}% %DBI_CODE{...}% # Some Perl Code %DBI_CODE%
DBI_QUERY
Each query consist of two parts: a query statement (
Read below on how this plugin works in order to get more detailed explanation of the meaning of each syntax element. Parameters:
A small note on DBI_DO
As a matter of fact,
Parameters:
DBI_CALL
Parameters:
Moreover, named parameters are transfered to a subquery as if they are columns of a database record. Consider the following example:
%DBI_CALL{"example" uid="12"}% %DBI_QUERY{"db_identifier" subquery="example"}% SELECT name FROM Users WHERE id = %uid% .header .... %DBI_QUERY%
DBI_CODE
Parameters:
How it works
DBI_QUERYThis plugin has been written with the idea in mind that table is not the only way to represent database content. Therefore some more flexibility is required in order to format a query result. Yet, what could provide more control over the output than templates keeping it all as simple as possible? With this view in mind we come to the following procedure:
Variable Expansion
The first step of expansion is done by changing every
SELECT Name, PersonalID, SomeOtherInfo FROM PersonData
would provide us with variables
There are some special cases like
The second step is subquery processing. Quoting of Values
Values fetched from database are quoted using Subqueries
Subqueries are processed in same manner as common queries. The only thing which makes them slightly different in behaviour is the fact that they can use column values (variables) from the parent queries. It is also possible to have a chain of subqueries: For instance, in the following code:
%DBI_QUERY{...}% SELECT col1, col2 FROM someTable WHERE col3 = %parent_query_col1% .body ... %DBI_QUERY%
we choose only the rows which are somehow related to a row in a parent query. Of course, relatively similar approach would be to use nested
![]() .body uses subquery column names. Take care of this! Best of all avoid this situation by using SQL aliasing:
Parent:
SELECT col1 as parent_col1 .... Subquery:
SELECT col1 as subquery_col1 ...
DBI_DO
First of all it shall be stated that
Besides,
So, let's define
Implementation
As it was stated in syntax section,
%DBI_CODE{"test"}% if ($varParams{test}) { $rc = "This is test."; } else { $rc = "This is for real."; } %DBI_CODE% would output table like this:
It would look much better with SourceHighlightPlugin
%DBI_CODE{"test"}% %CODE{"perl"}% if ($varParams{test}) { $rc = "This is test."; } else { $rc = "This is for real."; } %ENDCODE% %DBI_CODE%
Since the
There is one special variable
Useful functionsThe following plugin functions could be useful while creating a script:
Database connection configuration
This plugin relies on the TWiki:Plugins.DatabaseContrib
Below is an example of the configuration of two database connections,
connection1 => { usermap => { TWikiAdminGroup => { user => 'dbuser1', password => 'dbpassword1', }, SpecialGroup => { user => 'specialdb', password => 'specialpass', }, }, user => 'guest', password => 'guestpass', driver => 'mysql', database => 'some_db', codepage => 'koi8r', host => 'your.server.name', }, test => { usermap => { TWikiAdminGroup => { user => 'dbuser2', password => 'dbpassword2', }, SomeUser => { user => 'someuser', password => 'somepassword', }, }, allow_do => { default => [qw(TWikiAdminGroup)], 'Sandbox.SomeUserSandbox' => [qw(TWikiAdminGroup SpecialGroup)], }, #user => 'nobody', #password => 'never', driver => 'mysql', database => 'test', # host => 'localhost', } Access Control
This plugin relies on the TWiki:Plugins.DatabaseContrib
Additional access protection is implemented for
In the example above, for database
Drawback and problemsWorking with a database isn't a simple task, in common. With this plugin I was trying to make it both as simple as possible and flexible same time. Balancing between these two extremes led to some compromises and side effects.
The biggest compromise was usage of Perl inlines for
and several others of the kind was arising, the more final structure was looking like a new language. So, why developing a new one if Perl is here? But then again, as it was mentioned before, this way is not secure-enough and an administrator must take serious considerations before allowing usage of The other issue is about plugin execution order. As one can see from MessageBoard example, attached to this topic, usage of other plugins could significally improve control over DBIQueryPlugin output. However, it is not guaranteed that another plugin would not be called in first place causing unpredictable results like unwanted changes in a Perl script.
Considering this issue the decision was made that DBIQueryPlugin must act as a preprocessor. For those who understand, it does all the job in
%CALC{"$SET(var,1)"}% %DBI_QUERY{"..."}% SELECT ... WHERE field = %CALC{"$GET(var)"}% %DBI_QUERY%
One will not get what would be expected because at the time
The last issue was the cause to implement classic plugin handling when it is requested during the inclusion procedure. Possible side effects of this hack are not studied yet and may create some headache.
Plugin Settings
Plugin settings are stored as preferences variables. To reference
a plugin setting write
Plugin Installation InstructionsNote: You do not need to install anything on the browser to use this plugin. The following instructions are for the administrator who installs the plugin on the TWiki server.
Plugin Info
Related Topics: DatabaseContrib, TWikiPreferences, TWikiPlugins |
DBI Query Plugin<-- Contributions to this plugin are appreciated. Please update the plugin page at http://twiki.org/cgi-bin/view/Plugins/DBIQueryPlugin Page contents
OverviewThis plugin is intended to provide TWiki with ability to make complex database requests using DBI Perl module.
Syntax RulesSyntax:
%DBI_QUERY{"db_identifier" ...}% SELECT ... .header head .body %column% %DBI_SUBQUERY{"name"}% .footer footer %DBI_QUERY% %DBI_DO{"db_identifier" ...}% # Some Perl code. %DBI_DO% %DBI_DO{"db_identifier" topic="SomeTopic" script="some_script"}% %DBI_CALL{"subquery"}% %DBI_CODE{...}% # Some Perl Code %DBI_CODE%
DBI_QUERY
Each query consist of two parts: a query statement (
Read below on how this plugin works in order to get more detailed explanation of the meaning of each syntax element. Parameters:
A small note on DBI_DO
As a matter of fact,
Parameters:
DBI_CALL
Parameters:
Moreover, named parameters are transfered to a subquery as if they are columns of a database record. Consider the following example:
%DBI_CALL{"example" uid="12"}% %DBI_QUERY{"db_identifier" subquery="example"}% SELECT name FROM Users WHERE id = %uid% .header .... %DBI_QUERY%
DBI_CODE
Parameters:
How it works
DBI_QUERYThis plugin has been written with the idea in mind that table is not the only way to represent database content. Therefore some more flexibility is required in order to format a query result. Yet, what could provide more control over the output than templates keeping it all as simple as possible? With this view in mind we come to the following procedure:
Variable Expansion
The first step of expansion is done by changing every
SELECT Name, PersonalID, SomeOtherInfo FROM PersonData
would provide us with variables
There are some special cases like
The second step is subquery processing. Quoting of Values
Values fetched from database are quoted using Subqueries
Subqueries are processed in same manner as common queries. The only thing which makes them slightly different in behaviour is the fact that they can use column values (variables) from the parent queries. It is also possible to have a chain of subqueries: For instance, in the following code:
%DBI_QUERY{...}% SELECT col1, col2 FROM someTable WHERE col3 = %parent_query_col1% .body ... %DBI_QUERY%
we choose only the rows which are somehow related to a row in a parent query. Of course, relatively similar approach would be to use nested
![]() .body uses subquery column names. Take care of this! Best of all avoid this situation by using SQL aliasing:
Parent:
SELECT col1 as parent_col1 .... Subquery:
SELECT col1 as subquery_col1 ...
DBI_DO
First of all it shall be stated that
Besides,
So, let's define
Implementation
As it was stated in syntax section,
%DBI_CODE{"test"}% if ($varParams{test}) { $rc = "This is test."; } else { $rc = "This is for real."; } %DBI_CODE% would output table like this:
It would look much better with SourceHighlightPlugin
%DBI_CODE{"test"}% %CODE{"perl"}% if ($varParams{test}) { $rc = "This is test."; } else { $rc = "This is for real."; } %ENDCODE% %DBI_CODE%
Since the
There is one special variable
Useful functionsThe following plugin functions could be useful while creating a script:
Database connection configuration
This plugin relies on the TWiki:Plugins.DatabaseContrib
Below is an example of the configuration of two database connections,
connection1 => { usermap => { TWikiAdminGroup => { user => 'dbuser1', password => 'dbpassword1', }, SpecialGroup => { user => 'specialdb', password => 'specialpass', }, }, user => 'guest', password => 'guestpass', driver => 'mysql', database => 'some_db', codepage => 'koi8r', host => 'your.server.name', }, test => { usermap => { TWikiAdminGroup => { user => 'dbuser2', password => 'dbpassword2', }, SomeUser => { user => 'someuser', password => 'somepassword', }, }, allow_do => { default => [qw(TWikiAdminGroup)], 'Sandbox.SomeUserSandbox' => [qw(TWikiAdminGroup SpecialGroup)], }, #user => 'nobody', #password => 'never', driver => 'mysql', database => 'test', # host => 'localhost', } Access Control
This plugin relies on the TWiki:Plugins.DatabaseContrib
Additional access protection is implemented for
In the example above, for database
Drawback and problemsWorking with a database isn't a simple task, in common. With this plugin I was trying to make it both as simple as possible and flexible same time. Balancing between these two extremes led to some compromises and side effects.
The biggest compromise was usage of Perl inlines for
and several others of the kind was arising, the more final structure was looking like a new language. So, why developing a new one if Perl is here? But then again, as it was mentioned before, this way is not secure-enough and an administrator must take serious considerations before allowing usage of The other issue is about plugin execution order. As one can see from MessageBoard example, attached to this topic, usage of other plugins could significally improve control over DBIQueryPlugin output. However, it is not guaranteed that another plugin would not be called in first place causing unpredictable results like unwanted changes in a Perl script.
Considering this issue the decision was made that DBIQueryPlugin must act as a preprocessor. For those who understand, it does all the job in
%CALC{"$SET(var,1)"}% %DBI_QUERY{"..."}% SELECT ... WHERE field = %CALC{"$GET(var)"}% %DBI_QUERY%
One will not get what would be expected because at the time
The last issue was the cause to implement classic plugin handling when it is requested during the inclusion procedure. Possible side effects of this hack are not studied yet and may create some headache.
Plugin Settings
Plugin settings are stored as preferences variables. To reference
a plugin setting write
Plugin Installation InstructionsNote: You do not need to install anything on the browser to use this plugin. The following instructions are for the administrator who installs the plugin on the TWiki server.
Plugin Info
Related Topics: DatabaseContrib, TWikiPreferences, TWikiPlugins |