Красимир Беров > DBIx-Simple-Class-1.008 > DBIx::Simple::Class::Schema

Download:
DBIx-Simple-Class-1.008.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.006   Source  

NAME ^

DBIx::Simple::Class::Schema - Create and use classes representing tables from a database

SYNOPSIS ^

  #Somewhere in a utility script or startup() of your application.
  DBIx::Simple::Class::Schema->dbix(DBIx::Simple->connect(...));
  my $perl_code = DBIx::Simple::Class::Schema->load_schema(
    namespace =>'My::Model',
    table => '%',              #all tables from the current database
    type  => "'TABLE','VIEW'", # make classes for tables and views
  );

  #Now eval() to use your classes.
  eval $perl_code || Carp::croak($@);


  #Or load and save it for more customisations and later usage.
  DBIx::Simple::Class::Schema->load_schema(
    namespace =>'My::Model',
    table => '%',              #all tables from the current database
    type  => "'TABLE','VIEW'", # make classes for tables and views
  );
  DBIx::Simple::Class::Schema->dump_schema_at(
    lib_root => "$ENV{PERL_LOCAL_LIB_ROOT}/lib"
    overwrite =>1 #overwrite existing files
  ) || Carp::croak 'Something went wrong! See above...';

DESCRIPTION ^

DBIx::Simple::Class::Schema automates the creation of classes from database tables. You can use it when you want to prototype quickly your application. It is also very convenient as an initial generator and dumper of your classes representing your database tables.

METHODS ^

load_schema

Class method.

  Params:
    namespace - String. The class name for your base class,
      default: 'DSCS::'.(join '', map { ucfirst lc } split /_/, $database)
    table - SQL string for a LIKE clause,
      default: '%'
    type - SQL String for an IN clause.
      default: "'TABLE','VIEW'"

Extracts tables' information from the current connection and generates Perl classes representing those tables or/and views. If called in list context returns an array with perl code for each package. The first package is the base class. The base class is generated only the argument table is '%' or empty. If called in scalar context returns all the generated code as a string.

The generated classes are saved internally and are available for use by "dump_schema_at". This makes it very convenient for quickly prototyping applications by just modifying tables in your database.

  my $perl_code = DBIx::Simple::Class::Schema->load_schema();
  #concatenaded code as one string
  eval $perl_code || Carp::croak($@);
  #...
  my $user = Dbname::User->find(2345);
  
  #or My::Schema, My::Schema::Table1, My::Schema::Table2,...
  my @perl_code = DBIx::Simple::Class::Schema->load_schema();
  
  #or just prepare code before dumping it to disk.
  DBIx::Simple::Class::Schema->load_schema();

dump_schema_at

Class method.

  Params:
    lib_root: String - Where classes will be dumped.
      default: $INC[0]
    overwrite: boolean -1/0 Should it overwrite existing classes with the same name?
      default: 0

Uses the generated code by "load_schema" and saves each class on the disk. Does several checks:

For every check above issues a warning so you, the developer, can decide what to do. Returns true on success.

SUPPORTED DATABASE DRIVERS ^

DBIx::Simple::Class::Schema strives to be DBD agnostic and uses only functionality specified by DBI. This means that if a driver implements the methods specifyed in DBI it is supported. However currently only tests for DBD::SQLite and DBD::mysql are written. Feel free to contribute with tests for your prefered driver. The following methods are used to retreive information form the database:

SUPPORTED SQL TYPES ^

Currently some minimal "CHECKS" in DBIx::Simple::Class are automatically generated for TYPE_NAMEs matching /INT/i,/FLOAT|DOUBLE|DECIMAL/i, /CHAR|TEXT|CLOB/i. You are supposed to write your own business-specific checks.

SEE ALSO ^

DBIx::Simple::Class, DBIx::Simple, DBIx::Class::Schema::Loader, Mojolicious::Plugin::DSC

LICENSE AND COPYRIGHT ^

Copyright 2012-2013 Красимир Беров (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://www.opensource.org/licenses/artistic-license-2.0 for more information.

syntax highlighting: