=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 flexibly =imte * 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. Fatal error throw exeption 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 parameters. $dbi->execute( "select * from book title = :title and author = :author;" {title => 'Perl', author => 'Ken'} ); :title and :author is parameters, which is replaced to placeholers. select * from book title = ? and author = ?; See also L about parameter. =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
option $dbi->execute($sql, table => ['author', 'book']); =head3 Manipulate same name's columns It is ok if there are same name's columns. Let's think two date comparison. my $sql = "select * from table where date > :date and date < :date;"; In this case, You specify parameter values as array reference. my $dbi->execute($sql, {date => ['2010-10-01', '2012-02-10']}); =head2 Create where clause =head3 Dinamically create where clause : where You want to search multiple conditions in many times. Let's think the following three cases. Case1: Search only C where title = :title Case2: Search only C<author> where author = :author Case3: Search C<title> and C<author> where title = :title and author = :author L<DBIx::Custom> support dinamic where clause creating. At first, create L<DBIx::Custom::Where> object by C<where>. my $where = $dbi->where; Set clause by C<clause> $where->clause( ['and', 'title = :title, 'author = :author'] ); C<clause> is the following format. ['or' or 'and', PART1, PART1, PART1] First argument is 'or' or 'and'. Later than first argument are part which contains parameter. You can write more complex format. ['and', 'title = :title', ['or', 'author = :author', 'date like :date'] ] This mean "title = :title and ( author = :author or date like :date )". After setting C<clause>, set C<param>. $where->param({title => 'Perl'}); In this example, parameter contains only title. If you execute C<string_to>, you can get where clause which contain only parameter name. my $where_clause = $where->to_string; Parameter name is only title, the following where clause is created. where title = :title You can also create where clause by stringification. my $where_clause = "$where"; This is useful to embbed it into SQL. =head3 In case where clause contains same name columns Even if same name parameters exists, you can create where clause. Let's think that there are starting date and ending date. my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; In this case, you set parameter value as array reference. my $p = {date => ['2010-11-15', '2011-11-21']}; You can embbed these values into same name parameters. $where->clause( ['and', 'date > :date', 'date < :date'] ); $where->param($p); If starting date isn't exists, create the following parameter. my $p = {date => [$dbi->not_exists, '2011-11-21']}; You can get DBIx::Custom::NotExists object by C<not_exists> This mean correnspondinf value isn't exists. If ending date isn't exists, create the following parameter. my $p = {date => ['2010-11-15']}; If both date isn't exists, create the following parameter. my $p = {date => []}; This logic is a little difficut. See the following ones. my @date; push @date, exists $param->{start_date} ? $param->{start_date} : $dbi->not_exists; push @date, $param->{end_date} if exists $param->{end_date}; my $p = {date => \@date}; =head3 With C<select> You can pass L<DBIx::Custom::Where> object to C<where> of C<select>. my $where = $dbi->where; $where->clause(['and', 'title = :title', 'author = :author']); $where->param({title => 'Perl'}); my $result = $dbi->select(table => 'book', where => $where); You can also pass it to C<where> of C<update>AC<delete> =head3 With C<execute> L<DBIx::Custom::Where> object is embedded into SQL. my $where = $dbi->where; $where->clause(['and', 'title = :title', 'author = :author']); $where->param({title => 'Perl'}); my $sql = <<"EOS"; select * from book; $where EOS $dbi->execute($sql, $param, table => 'book'); =head2 Filtering =head3 Register filter : C<register_filter> If you want to register filter, use C<register_filter>. $dbi->register_filter( # Time::Piece object to DATE format tp_to_date => sub { my $date = shift; return $tp->strftime('%Y-%m-%d'); }, # DATE to Time::Piece object date_to_tp => sub { my $date = shift; return Time::Piece->strptime($date, '%Y-%m-%d'); }, ); =head3 Filter before sending data into database : C<filter> option If you filter sending data, use C<filter> option. $dbi->execute( 'insert into book (date) values (:date)', {date => $tp}, filter => {date => 'tp_to_date'} ); You can use C<filter> option in C<insert>, C<update>, C<delete>, C<select> 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<DBIx::Custom::Result>'s C<filter> 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<DBIx::Custom::Model> to improve source code view. At first, you create basic model class extending <DBIx::Custom::Model>. Each L<DBIx::Custom> class inherit L<Object::Simple>. 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<include_model> $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<insert>, C<update>, C<update_all>, C<delete>, C<delete_all>, C<select> without C<table> option. $dbi->model('book')->insert($param); Model is L<DBIx::Custom::Model>. If you need table nameAyou can get it by C<table>. my $table = $model->table; You can get L<DBIx::Custom>. my $dbi = $model->dbi; You can also call all methods of L<DBIx::Custom> and L<DBI>. # 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<models>. my @models = keys %{$self->models}; You can set primary key to model. $model->primary_key(['id', 'number_id']); Primary key is used by C<insert>, C<update>, C<delete>, and C<select> methods. by C<filter> you can define filters applied by C<apply_filter> $model->filter({ title => {out => ..., in => ..., end => ...}, author => {out => ..., in => ..., end => ...} }); This filters is applied when C<include_model> is called. You can set column names $model->columns(['id', 'number_id']); Column names is automarically set by C<setup_model>. This method is needed to be call after C<include_model>. $dbi->setup_model; You can set C<join> $model->join(['left outer join company on book.company_id = company.id']); C<join> is used by C<select> method. =head2 Class name, Model name, Table name Class name, model name, and table name is a little different. Generally Class name is model name, and table name is model name. CLASS MODEL TABLE book (CLASS) -> book (MODEL) -> book You can change model name. package MyModel::book; __PACAKGE__->attr(name => 'book_model'); CLASS MODEL TABLE book book_model (MODEL) -> book_model Model name is the name used by L<model> of L<DBIx::Custom>. $dbi->model('book_model'); You can change table name. package MyModel::book; __PACAKGE__->attr(table => 'book_table'); CLASS MODEL TABLE book (CLASS) -> book book_table Table name is the table really accessed. $dbi->model('book')->insert(...); # access to "book_table" =head2 Create column clause automatically : mycolumn, column To create column clause automatically, use C<mycolumn>. Valude of C<table> and C<columns> is used. my $column_clause = $model->mycolumn; If C<table> is 'book'AC<column> 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 $column_clause = $model->column('company'); If C<table> is 'company'AC<column> is ['id', 'name'], the following clause is created. company.id as company__id, company.name as company__name =head2 Create column clause automatically : column_clause To create column clause automatically, use C<column_clause>. Valude of C<table> and C<columns> is used. my $column_clause = $model->column_clause; If C<table> is 'book'AC<column> 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. If you remove some columns, use C<remove> option. my $column_clause = $model->column_clause(remove => ['id']); If you add some column, use C<add> option. my $column_clause = $model->column_clause(add => ['company.id as company__id']); =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