Mojolicious::Plugin::DSC - use DBIx::Simple::Class in your application.
#load # Mojolicious $self->plugin('DSC', $config); # Mojolicious::Lite plugin 'DSC', $config; my $user = My::User->find(1234); #or my $user = My::User->query('SELECT * FROM users WHERE user=?','ivan'); #or if SQL::Abstract is isnstalled my $user = My::User->select(user=>'ivan');
Mojolicious::Plugin::DSC is a Mojolicious plugin that helps you use DBIx::Simple::Class in your application. It also adds an app attribute ($app->dbix) and controller helper ($c->dbix) which is a DBIx::Simple instance.
$app->dbix
$c->dbix
The configuration is pretty flexible:
# in Mojolicious startup() $self->plugin('DSC', { dsn => 'dbi:SQLite:database=:memory:;host=localhost' }); #or $self->plugin('DSC', { driver => 'mysqlPP', database => 'mydbname', host => '127.0.0.1', user => 'myself', password => 'secret', onconnect_do => [ 'SET NAMES UTF8', 'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"' sub{my $dbix = shift; do_something_complicated($dbix)} ], dbh_attributes => {AutoCommit=>0}, namespace => 'My', #will load My::User, My::Content, My::Pages load_classes =>['User', 'Content', 'My::Pages'], #now you can use $app->DBIX instead of $app->dbix dbix_helper => 'DBIX' });
The following parameters can be provided:
An ARRAYREF of classes to be loaded. If not provided, all classes under namespace will be loaded. Classes are expected to be already dumped as files using dsc_dump_schema.pl from an existing database.
dsc_dump_schema.pl
#all classes under My::Schema::Class $app->plugin('DSC', { namespace => My::Schema::Class, }); #only My::Schema::Class::Groups and My::Schema::Class::Users $app->plugin('DSC', { namespace => My::Schema::Class, load_classes => ['Groups', 'Users'] });
Boolean. When the current "mode" in Mojolicious is development this value is 1.
development
$app->plugin('DSC', { DEBUG => 1, namespace => My::Schema::Class, load_classes => ['Groups', 'Users'] });
HASHREF. Attributes passed to "connect" in DBIx::Simple. Default values are:
{ RaiseError => 1, AutoCommit => 1, };
They can be overriden:
$app->plugin('DSC', { namespace => My::Schema::Class, dbh_attributes =>{ AutoCommit => 0, sqlite_unicode => 1 } });
Connection string parsed using "parse_dsn" in DBI and passed to "connect" in DBIx::Simple.
From this string we guess the "driver", "database", host, port and the namespace which ends up as camelised form of the "database" name.
If "dsn" is not passed most of the configuration values above must be provided so a valid connection string can be constructed. If "dsn" is provided it will be preferred over the above parameters (excluding namespace) because the developer should know better how exactly to connect to the database.
$app->plugin('DSC', { namespace => My::Schema::Class, dbh_attributes => {sqlite_unicode => 1}, dsn => 'dbi:SQLite:database=myfile.sqlite' });
String. One of "mysql","SQLite","Pg" etc... This string is prepended with "dbi:". No default value.
$app->plugin('DSC', { driver => 'mysql', dbh_attributes => {sqlite_unicode => 1}, dsn => 'dbi:SQLite:database=myfile.sqlite' });
String - the database name. No default value.
$app->plugin('DSC', { database => app->home->rel_file('etc/ado.sqlite'), dbh_attributes => {sqlite_unicode => 1}, driver => 'SQLite', namespace => 'Ado::Model', });
String. defaults to localhost.
localhost
String. Not added to the connection string if not provided.
The class name of your schema class. If not provided the value will be guessed from the database or dsn. It is recommended to provide your schema class name.
$app->plugin('DSC', { database => app->home->rel_file('etc/ado.sqlite'), dbh_attributes => {sqlite_unicode => 1}, driver => 'SQLite', namespace => 'My::Model', });
String. Username used to connect to the database.
String. Password used to connect to the database.
ARRAYREF of SQL statements and callbacks which will be executed right after establiching the connection.
$app->plugin('DSC', { database => app->home->rel_file('etc/ado.sqlite'), dbh_attributes => {sqlite_unicode => 1}, driver => 'SQLite', namespace => 'Ado::Model', onconnect_do => [ 'PRAGMA encoding = "UTF-8"', 'PRAGMA foreign_keys = ON', 'PRAGMA temp_store = 2', #MEMORY 'VACUUM', sub{ shift->dbh->sqlite_create_function( 'now', 0, sub { return time } ); } ], });
Boolean. If set, establishing the connection to the database will be postponed for the first call of $app->dbix or the method name you provided for the "dbix_helper".
String. The name of the helper method that can be created to invoke/use directly the DBIx::Simple instance on your controller or application. Defaults to dbix.
dbix
Mojolicious::Plugin::DSC inherits all methods from Mojolicious::Plugin and implements the following new ones.
register
$plugin->register(Mojolicious->new);
Register plugin in Mojolicious application.
This plugin own configuration. Returns a HASHref.
#debug $app->log->debug($app->dumper($plugin->config));
DBIx::Simple::Class, Mojolicious, Mojolicious::Guides, http://mojolicio.us.
Copyright 2012 Красимир Беров (Krasimir Berov).
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
See http://dev.perl.org/licenses/ for more information.
To install Mojolicious::Plugin::DSC, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::DSC
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::DSC
For more information on module installation, please visit the detailed CPAN module installation guide.