=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 C, C, C, or C If you want to execute select statement, use C method return value is L object. You can fetch a row or rows by various methods. =head3 Fetch a row (array) : C my $row = $result->fetch; C method fetch a row and put it into array reference. You can continue to fetch while (my $row = $result->fetch) { my $title = $row->[0]; my $author = $row->[1]; } =head3 Fetch only first row (array) : C my $row = $result->fetch_first; C fetch a only first row and finish statment handle, and put it into array refrence. =head3 Fetch all rows (array) : C my $rows = $result->fetch_all; C fetch all rows and put them into array of array reference. =head3 Fetch a row (hash) : C my $row = $result->fetch_hash; C fetch a row and put it into hash reference. You can fetch a row while row exists. while (my $row = $result->fetch_hash) { my $title = $row->{title}; my $author = $row->{author}; } =head3 Fetch only a first row (hash) : C my $row = $result->fetch_hash_first; C fetch only a first row and finish statement handle, and put them into hash refrence. C is C synonym to save word typing. my $row = $result->one; =head3 Fetch all rows (hash) : C my $rows = $result->fetch_hash_all; C fetch all rows and put them into array of hash reference. =head3 Statement Handle : C my $sth = $result->sth; If you want to get statment handle, use method. =head2 Named placeholder =head3 Basic of Parameter You can embedd named placeholder into SQL. select * from book where title = :title and author like :author; :title and :author is named placeholder Named placeholder is replaced by place holder. select * from book where title = ? and author like ?; use C to execute SQL. my $sql = "select * from book where title = :title and author like :author;" $dbi->execute($sql, {title => 'Perl', author => '%Ken%'}); You can specify C at C. $dbi->execute($sql, {title => 'Perl', author => '%Ken%'} filter => {title => 'to_something'); =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 named placeholder. 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 named placeholder. 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. 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 Create column clause automatically : mycolumn, column To create column clause automatically, use C<mycolumn>. Valude of C<table> and C<columns> is used. my $mycolumns = $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 $columns = $model->column('company'); If C<table> is "company", C<column> 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