=encoding utf8 =head1 NAME DBIx::Custom::Guide - DBIx::Custom Guide =head1 FEATURES L is the wrapper class of L to execute SQL easily. This module have the following features. =over 4 =item * Execute INSERT, UPDATE, DELETE, SELECT statement easily =item * You can specify bind values by hash reference =item * Filtering by data type. and you can set filter to any column =item * Creating where clause and order by clause flexibly =item * Support model =back =head1 GUIDE =head2 Connect to database use DBIx::Custom; my $dbi = DBIx::Custom->connect( dsn => "dbi:mysql:database=bookshop", user => 'ken', password => '!LFKD%$&', dbi_option => {mysql_enable_utf8 => 1} ); You can connect to database by C method. C is data source name, C is user name, C is password. C is L option. By default, the following option is set. Exeption is thrown when fatal error occur and commit mode is auto commit. { RaiseError => 1 PrintError => 0 AutoCommit => 1 } =head2 Execute query =head3 Insert Statement : C If you want to execute insert statement, use C method. $dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book'); First argument is insert row data, C is table name. =head3 Update Statement : C If you want to execute update stateimuse, use C method. $dbi->update( {title => 'Perl', author => 'Ken'}, table => 'book', where => {id => 5} ); First argument is update row data, C
is table name, C is condition. Note that you can't execute C method without C. If you want to update all rows, use update_all. $dbi->update_all({title => 'Perl', author => 'Ken'}, table => 'book'); =head3 Delete Statement : C If you want to execute delete statement, use C method. $dbi->delete(table => 'book', where => {author => 'Ken'}); C
is table name, C is condition. Note that you can't execute C method without C. If you want to delete all rows, use C method. $dbi->delete_all(table => 'book'); =head3 Select Statement : C method. my $result = $dbi->select(table => 'book'); Return value is L object. You can fetch rows by C method. while (my $row = $result->fetch) { my $title = $row->[0]; my $author = $row->[1]; } See also L about L. You can specify column names by C option and condition by C option. my $result = $dbi->select( table => 'book', column => ['author', 'title'], where => {author => 'Ken'} ); You can specify join clause by C option. my $result = $dbi->select( table => 'book', column => ['company.name as company_name'] where => {'book.name' => 'Perl'}, join => ['left outer join company on book.company_id = company.id] ); Note that join clause is joined only when C or C option contains table name, such as book.name. You can append statement to the end of whole statement by C option. my $result = $dbi->select( table => 'book', where => {author => 'Ken'}, append => 'for update', ); =head3 C If you want to execute SQL, use C method. $dbi->execute("select * from book;"); You can specify named placeholder. $dbi->execute( "select * from book title = :title and author = :author;" {title => 'Perl', author => 'Ken'} ); :title and :author is named placeholder, which is replaced to placeholers. select * from book title = ? and author = ?; =head3 C my $dbh = $dbi->dbh; Get get database handle object of L. =head3 C methods $dbi->do(...); $dbi->begin_work; You can call all methods of L from L object. =head2 Fetch Rows C You can pass L object to C of C method. $dbi->insert( {date => $tp}, table => 'book', filter => {date => 'tp_to_date'} ); =head3 Filter after fetching data from database. If you filter fetch data, use L's C method. my $result = $dbi->select(column => 'date', table => 'book'); $result->filter(date => 'date_to_tp'); my $row = $result->one; =head2 7. Model =head3 Model you can define model extending L to improve source code view. At first, you create basic model class extending . Each L class inherit L. so you can inherit the following way. package MyModel; use DBIx::Custom::Model -base; Next, you create each model classes. MyModel::book package MyModel::book; use MyModel -base; sub insert { ... } sub list { ... } MyModel::company package MyModel::company; use MyModel -base; sub insert { ... } sub list { ... } The follwoing modules location is needed. MyModel.pm MyModel / book.pm / company.pm You can include these models by C $dbi->include_model('MyModel'); First argument is name space of model. You can use model like this. my $result = $dbi->model('book')->list; In mode, You can use such as methods, C, C, C, C, C, C
option. $dbi->model('book')->insert($param); Model is L. If you need table nameAyou can get it by C
. my $table = $model->table; You can get L. my $dbi = $model->dbi; You can also call all methods of L and L. # DBIx::Custom method $model->execute($sql); # DBI method $model->begin_work; $model->commit; If you want to get all models, you can get them by keys of C. my @models = keys %{$self->models}; You can set primary key to model. $model->primary_key(['id', 'number_id']); Primary key is used by C, C, C, and C method. =head2 Create column clause automatically : mycolumn, column To create column clause automatically, use C. Valude of C
and C is used. my $mycolumns = $model->mycolumn; If C
is 'book'AC is ['id', 'name'], the following clause is created. book.id as id, book.name as name These column name is for removing column name ambiguities. You can create column clause from columns of other table. my $columns = $model->column('company'); If C
is "company", C return ['id', 'name'], the following clause is created. company.id as "company.id", company.name as "company.name" =head2 Model Examples Model examples package MyDBI; use DBIx::Custom -base; sub connect { my $self = shift->SUPER::connect(@_); $self->include_model( MyModel => [ 'book', 'company' ] ); } package MyModel::book; use DBIx::Custom::Model -base; has primary_key => sub { ['id'] }; sub insert { ... } sub list { ... } package MyModel::company; use DBIx::Custom::Model -base; has primary_key => sub { ['id'] }; sub insert { ... } sub list { ... } =cut