NAME

Mojolicious::Plugin::DSC - use DBIx::Simple::Class in your application.

SYNOPSIS

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

DESCRIPTION

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.

CONFIGURATION

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:

load_classes

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.

  #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']
  });

DEBUG

Boolean. When the current "mode" in Mojolicious is development this value is 1.

  $app->plugin('DSC', {
    DEBUG => 1,
    namespace => My::Schema::Class,
    load_classes => ['Groups', 'Users']
  });

dbh_attributes

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 }
  });

dsn

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

driver

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

database

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',
  });

host

String. defaults to localhost.

port

String. Not added to the connection string if not provided.

namespace

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',
  });

user

String. Username used to connect to the database.

password

String. Password used to connect to the database.

onconnect_do

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 } );
        }
    ],
  });

postpone_connect

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".

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.

METHODS

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.

config

This plugin own configuration. Returns a HASHref.

  #debug
  $app->log->debug($app->dumper($plugin->config));

SEE ALSO

DBIx::Simple::Class, Mojolicious, Mojolicious::Guides, http://mojolicio.us.

LICENSE AND COPYRIGHT

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.