GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
DBIx::Skinny::Schema::Loader(3) User Contributed Perl Documentation DBIx::Skinny::Schema::Loader(3)

DBIx::Skinny::Schema::Loader - Schema loader for DBIx::Skinny

Run-time schema loading:

  package Your::DB::Schema;
  use base qw/DBIx::Skinny::Schema::Loader/;

  __PACKAGE__->load_schema;

  1;

Preloaded schema:

Given a the following source code as publish_schema.pl:

  use DBIx::Skinny::Schema::Loader qw/make_schema_at/;
  print make_schema_at(
    'Your::DB::Schema',
    {
      # options here
    },
    [ 'dbi:SQLite:test.db', '', '' ]
  );

you can execute

    $ perl publish_schema.pl > Your/DB/Schema.pm

to create a static schema class.

DBIx::Skinny::Schema::Loader is schema loader for DBIx::Skinny. It can dynamically load schema at run-time or statically publish them.

It supports MySQL and SQLite, and PostgreSQL.

Probably no need for public use.

Instead, invoke concrete db driver class named "DBIx::Skinny::Schema::Loader::DBI::XXXX".

Dynamically load the schema

  package Your::DB::Schema;
  use base qw/DBIx::Skinny::Schema::Loader/;

  __PACKAGE__->load_schema;

  1;

"load_schema" refers to "connect info" in your Skinny class. When your schema class is named "Your::DB::Schema", Loader considers "Your::DB" as a Skinny class.

"load_schema" executes "install_table" for all tables, automatically setting primary key and columns.

Also the sections "how loader find primary keys" and "additional settings for load_schema".

Return schema file content as a string. This function is exportable.

  use DBIx::Skinny::Schema::Loader qw/make_schema_at/;
  print make_schema_at(
      'Your::DB::Schema',
      {
        # options here
      },
      [ 'dbi:SQLite:test.db', '', '' ]
  );

$schema_class is schema class name that you want publish.

$options are described in the "options of make_schema_at" section.

$connect_info is ArrayRef or HashRef. If it is an arrayref, it contains dsn, username, password to connect to the database. If it is an hashref, it contains same parameters as DBIx::Skinny->new(\%opts).

surely primary key defined at DB, use it as PK.

in case of primary key is not defined at DB, Loader find PK following logic. 1. if table has only one column, use it 2. if table has column 'id', use it

Here is how to use additional settings:

  package Your::DB::Schema;
  use base qw/DBIx::Skinny::Schema::Loader/;

  use DBIx::Skinny::Schema;  # import schema functions

  install_utf8_columns qw/title content/;

  install_table books => schema {
    trigger pre_insert => sub {
      my ($class, $args) = @_;
      $args->{ created_at } ||= DateTime->now;
    };
  };

  __PACKAGE__->load_schema;

  1;

'use DBIx::Skinny::Schema' works to import schema functions. you can write instead of it, 'BEGIN { DBIx::Skinny::Schema->import }' because 'require DBIx::Skinny::Schema' was done by Schema::Loader.

You might be concerned that calling install_table without pk and columns doesn't work. However, DBIx::Skinny allows "install_table" to be called twice or more.

insert your custom template before install_table block.

  my $tmpl = << '...';
  # custom template
  install_utf8_columns qw/title content/;
  ...

  install_table books => schema {
    trigger pre_insert => sub {
      my ($class, $args) = @_;
      $args->{ created_at } ||= DateTime->now;
    }
  }

  print make_schema_at(
      'Your::DB::Schema',
      {
          before_template => $tmpl,
      },
      [ 'dbi:SQLite:test.db', '', '' ]
  );

then you get content inserted your template before install_table block.

after_template works like before_template mostly. after_template inserts template after install_table block.

  print make_schema_at(
      'Your::DB::Schema',
      {
          before_template => $before,
          after_template  => $after,
      },
      [ 'dbi:SQLite:test.db', '', '' ]
  );

there are more detailed example in $before_template section.

you can use both before_template and after_template all together.

DEPRECATED. this option is provided for backward compatibility.

you can use before_template instead of this.

use your custom template for install_table.

  my $table_template = << '...';
  install_table [% table %] => schema {
      pk qw/[% pk %]/;
      columns qw/[% columns %]/;
      trigger pre_insert => $created_at;
  };

  ...

  print make_schema_at(
      'Your::DB::Schema',
      {
          table_template => $table_template,
      },
      [ 'dbi:SQLite:test.db', '', '' ]
  );

your schema's install_table block will be

  install_table books => schema {
      pk 'id';
      columns qw/id author_id name/;
      tritter pre_insert => $created_at;
  };

"make_schema_at" replaces some following variables. [% table %] ... table name [% pk %] ... primary keys joined by a space [% columns %] ... columns joined by a space

you can exclude tables that matching any rules declared in ignore_rules from the schema.

  ignore_rules => [ qr/rs$/, qr/^no/ ],

if you write Your::DB class without setup sentence,

  package MyApp::DB;
  use DBIx::Skinny;
  1;

you should not call "load_schema" in your class file.

  package MyApp::DB::Schema;
  use base qw/DBIx::Skinny::Schema::Loader/;
  1;

call load_schema with dsn manually in your app.

  my $db = MyApp::DB->new;
  my $connect_info = {
      dsn      => $dsn,
      username => $user,
      password => $password,
  };
  $db->connect($connect_info);
  $db->schema->load_schema($connect_info);

Ryo Miyake <ryo.studiom {at} gmail.com>

DBIx::Skinny, DBIx::Class::Schema::Loader

Ryo Miyake "<ryo.studiom __at__ gmail.com>"

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2012-11-18 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.