Newer Older
627 lines | 14.921kb
update pod
Yuki Kimoto authored on 2011-01-28
1
=encoding utf8
2

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
3
=head1 NAME
4

            
update pod
Yuki Kimoto authored on 2011-01-28
5
DBIx::Custom::Guide - DBIx::Custom Guide
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
6

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
7
=head1 FEATURES
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
8

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
9
L<DBIx::Custom> is the wrapper class of L<DBI> to execute SQL easily.
10
This module have the following features.
update pod
Yuki Kimoto authored on 2011-01-28
11

            
12
=over 4
13

            
update pod
Yuki Kimoto authored on 2011-08-18
14
=item *
update pod
Yuki Kimoto authored on 2011-01-28
15

            
update pod
Yuki Kimoto authored on 2011-08-18
16
Execute C<insert>, C<update>, C<delete>, or C<select> statement easily
update pod
Yuki Kimoto authored on 2011-01-28
17

            
update pod
Yuki Kimoto authored on 2011-08-18
18
=item *
update pod
Yuki Kimoto authored on 2011-01-28
19

            
update pod
Yuki Kimoto authored on 2011-08-18
20
Create C<where> clause flexibly
update pod
Yuki Kimoto authored on 2011-01-28
21

            
update pod
Yuki Kimoto authored on 2011-08-18
22
=item *
23

            
24
Named place holder support
25

            
26
=item *
27

            
28
Model support
29

            
30
=item *
31

            
32
Connection manager support
33

            
34
=item *
35

            
36
Choice your favorite relational database management system,
37
C<MySQL>, C<SQLite>, C<PostgreSQL>, C<Oracle>,
38
C<Microsoft SQL Server>, C<Microsoft Access>, C<DB2> or anything, 
39

            
40
=item *
41

            
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
42
Filtering by data type or column name
update pod
Yuki Kimoto authored on 2011-08-18
43

            
44
=item *
45

            
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
46
Create C<order by> clause flexibly
update pod
Yuki Kimoto authored on 2011-01-28
47

            
48
=back
49

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
50
=head1 GUIDE
update pod
Yuki Kimoto authored on 2011-01-28
51

            
fixed end_filter DEPRECATED ...
Yuki Kimoto authored on 2011-07-01
52
=head2 Connect to database
update pod
Yuki Kimoto authored on 2011-01-28
53

            
54
    use DBIx::Custom;
55
    my $dbi = DBIx::Custom->connect(
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
56
        dsn => "dbi:mysql:database=bookshop",
update pod
Yuki Kimoto authored on 2011-01-28
57
        user => 'ken',
58
        password => '!LFKD%$&',
updatedd pod
Yuki Kimoto authored on 2011-06-12
59
        dbi_option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-01-28
60
    );
61

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
62
You can connect to database by C<connect> method.
63
C<dsn> is data source name, C<user> is user name, C<password> is password.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
64

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
65
C<dbi_option> is L<DBI> option.
66
By default, the following option is set.
fixed end_filter DEPRECATED ...
Yuki Kimoto authored on 2011-07-01
67
Exeption is thrown when fatal error occur and commit mode is auto commit.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
68

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
69
    {
70
        RaiseError  =>  1
71
        PrintError  =>  0
72
        AutoCommit  =>  1
73
    }
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
74

            
fixed end_filter DEPRECATED ...
Yuki Kimoto authored on 2011-07-01
75
=head2 Execute query
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
76

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
77
=head3 Insert Statement : C<insert>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
78

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
79
If you want to execute insert statement, use C<insert> method.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
80

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
81
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
82

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
83
First argument is insert row data, C<table>  is table name.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
84

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
85
=head3 Update Statement : C<update>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
86

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
87
If you want to execute update stateimuse, use C<update> method.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
88

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
89
    $dbi->update(
90
        {title => 'Perl', author => 'Ken'},
91
        table  => 'book', 
92
        where  => {id => 5}
93
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
94

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
95
First argument is update row data, C<table> is table name, C<where> is condition.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
96

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
97
Note that you can't execute C<update> method without C<where>.
98
If you want to update all rows, use update_all.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
99

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
100
    $dbi->update_all({title => 'Perl', author => 'Ken'}, table  => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
101

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
102
=head3 Delete Statement : C<delete>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
103

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
104
If you want to execute delete statement, use C<delete> method.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
105

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
106
    $dbi->delete(table  => 'book', where  => {author => 'Ken'});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
107

            
update pod
Yuki Kimoto authored on 2011-01-31
108
C<table> is table name, C<where> is condition.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
109

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
110
Note that you can't execute C<delete> method without C<where>.
111
If you want to delete all rows, use C<delete_all> method.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
112

            
update pod
Yuki Kimoto authored on 2011-01-28
113
    $dbi->delete_all(table  => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
114

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
115
=head3 Select Statement : C<select>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
116

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
117
If you want to execute select statement, use C<select> method.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
118

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
119
    my $result = $dbi->select(table => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
120

            
update pod
Yuki Kimoto authored on 2011-01-31
121
Return value is L<DBIx::Custom::Result> object.
updatedd pod
Yuki Kimoto authored on 2011-06-12
122
You can fetch rows by C<fetch> method.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
123

            
124
    while (my $row = $result->fetch) {
125
        my $title  = $row->[0];
126
        my $author = $row->[1];
127
    }
128

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
129
See also L<Fetch row/"Fetch row"> about L<DBIx::Custom::Result>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
130

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
131
You can specify column names by C<column> option
132
and condition by C<where> option.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
133

            
134
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
135
        table  => 'book',
update pod
Yuki Kimoto authored on 2011-01-28
136
        column => ['author',  'title'],
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
137
        where  => {author => 'Ken'}
138
    );
139

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
140
You can specify join clause by C<join> option.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
141

            
142
    my $result = $dbi->select(
cleanup
Yuki Kimoto authored on 2011-03-10
143
        table  => 'book',
updatedd pod
Yuki Kimoto authored on 2011-06-12
144
        column => ['company.name as company_name']
cleanup
Yuki Kimoto authored on 2011-03-10
145
        where  => {'book.name' => 'Perl'},
146
        join   => ['left outer join company on book.company_id = company.id]
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
147
    );
148

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
149
Note that join clause is joined only when C<where> or C<column> option contains table name,
150
such as book.name.
cleanup
Yuki Kimoto authored on 2011-03-21
151

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
152
You can append statement to the end of whole statement by C<append> option.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
153

            
154
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
155
        table  => 'book',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
156
        where  => {author => 'Ken'},
update pod
Yuki Kimoto authored on 2011-01-28
157
        append => 'for update',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
158
    );
159

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
160
=head3 C<execute>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
161

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
162
If you want to execute SQL, use C<execute> method.
update pod
Yuki Kimoto authored on 2011-01-28
163

            
164
    $dbi->execute("select * from book;");
165

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
166
You can specify named placeholder.
update pod
Yuki Kimoto authored on 2011-01-28
167

            
168
    $dbi->execute(
updated pod
Yuki Kimoto authored on 2011-06-07
169
        "select * from book title = :title and author = :author;"
updatedd pod
Yuki Kimoto authored on 2011-06-12
170
        {title => 'Perl', author => 'Ken'}
update pod
Yuki Kimoto authored on 2011-01-28
171
    );
172

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
173
:title and :author is named placeholder, which is replaced to placeholers.
update pod
Yuki Kimoto authored on 2011-01-28
174

            
175
    select * from book title = ? and author = ?;
176

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
177
=head3 C<dbh>
update pod
Yuki Kimoto authored on 2011-01-28
178

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
179
    my $dbh = $dbi->dbh;
update pod
Yuki Kimoto authored on 2011-01-28
180

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
181
Get get database handle object of L<DBI>.
update pod
Yuki Kimoto authored on 2011-01-28
182

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
183
=head3 C<DBI> methods
update pod
Yuki Kimoto authored on 2011-01-28
184

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
185
    $dbi->do(...);
186
    $dbi->begin_work;
update pod
Yuki Kimoto authored on 2011-01-28
187

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
188
You can call all methods of L<DBI> from L<DBIx::Custom> object.
update pod
Yuki Kimoto authored on 2011-01-28
189

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
190
=head2 Fetch Rows
191

            
192
C<select> method return value is L<DBIx::Custom::Result> object.
193
You can fetch a row or rows by various methods.
194

            
195
=head3 Fetch a row (array) : C<fetch>
update pod
Yuki Kimoto authored on 2011-02-04
196

            
197
    my $row = $result->fetch;
198

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
199
C<fetch> method fetch a row and put it into array reference.
200
You can continue to fetch 
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
201

            
202
    while (my $row = $result->fetch) {
update pod
Yuki Kimoto authored on 2011-01-28
203
        my $title  = $row->[0];
204
        my $author = $row->[1];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
205
    }
206

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
207
=head3 Fetch only first row (array) : C<fetch_first>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
208

            
209
    my $row = $result->fetch_first;
210

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
211
C<fetch_first> fetch a only first row and finish statment handle,
212
and put it into array refrence.
update pod
Yuki Kimoto authored on 2011-01-28
213

            
update pod
Yuki Kimoto authored on 2011-02-11
214
=head3 Fetch all rows (array) : C<fetch_all>
update pod
Yuki Kimoto authored on 2011-01-28
215

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
216
    my $rows = $result->fetch_all;
217

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
218
C<fetch_all> fetch all rows and put them into array of array reference.
update pod
Yuki Kimoto authored on 2011-01-28
219

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
220
=head3 Fetch a row (hash) : C<fetch_hash>
update pod
Yuki Kimoto authored on 2011-01-28
221

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
222
    my $row = $result->fetch_hash;
update pod
Yuki Kimoto authored on 2011-01-28
223

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
224
C<fetch_hash> fetch a row and put it into hash reference.
225
You can fetch a row while row exists.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
226

            
227
    while (my $row = $result->fetch_hash) {
228
        my $title  = $row->{title};
229
        my $author = $row->{author};
230
    }
231

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
232
=head3 Fetch only a first row (hash) : C<fetch_hash_first>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
233

            
234
    my $row = $result->fetch_hash_first;
update pod
Yuki Kimoto authored on 2011-01-28
235

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
236
C<fetch_hash_first> fetch only a first row and finish statement handle,
237
and put them into hash refrence.
update pod
Yuki Kimoto authored on 2011-01-28
238

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
239
C<one> is C<fetch_hash_first> synonym to save word typing.
update pod
Yuki Kimoto authored on 2011-01-28
240

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
241
    my $row = $result->one;
update pod
Yuki Kimoto authored on 2011-01-28
242

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
243
=head3 Fetch all rows (hash) : C<fetch_hash_all>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
244

            
245
    my $rows = $result->fetch_hash_all;
246

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
247
C<fetch_hash_all> fetch all rows and put them into array of hash reference.
update pod
Yuki Kimoto authored on 2011-01-28
248

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
249
=head3 Statement Handle : C<sth>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
250

            
251
    my $sth = $result->sth;
252

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
253
If you want to get statment handle, use <sth> method.
update pod
Yuki Kimoto authored on 2011-01-28
254

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
255
=head2 Named placeholder
update pod
Yuki Kimoto authored on 2011-01-28
256

            
updated pod
Yuki Kimoto authored on 2011-06-07
257
=head3 Basic of Parameter
update pod
Yuki Kimoto authored on 2011-01-28
258

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
259
You can embedd named placeholder into SQL.
update pod
Yuki Kimoto authored on 2011-01-28
260

            
updated pod
Yuki Kimoto authored on 2011-06-07
261
    select * from book where title = :title and author like :author;
update pod
Yuki Kimoto authored on 2011-01-28
262

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
263
:title and :author is named placeholder
update pod
Yuki Kimoto authored on 2011-01-28
264

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
265
Named placeholder is replaced by place holder.
update pod
Yuki Kimoto authored on 2011-01-28
266

            
267
    select * from book where title = ? and author like ?;
268

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
269
use C<execute> to execute SQL.
update pod
Yuki Kimoto authored on 2011-01-28
270

            
updated pod
Yuki Kimoto authored on 2011-06-07
271
    my $sql = "select * from book where title = :title and author like :author;"
updatedd pod
Yuki Kimoto authored on 2011-06-12
272
    $dbi->execute($sql, {title => 'Perl', author => '%Ken%'});
update pod
Yuki Kimoto authored on 2011-01-28
273

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
274
You can specify C<filter> at C<execute>.
update pod
Yuki Kimoto authored on 2011-01-28
275

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
276
    $dbi->execute($sql, {title => 'Perl', author => '%Ken%'}
update pod
Yuki Kimoto authored on 2011-01-28
277
                  filter => {title => 'to_something');
278

            
update pod
Yuki Kimoto authored on 2011-02-07
279
=head3 Manipulate same name's columns
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
280

            
update pod
Yuki Kimoto authored on 2011-02-07
281
It is ok if there are same name's columns.
282
Let's think two date comparison.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
283

            
updated pod
Yuki Kimoto authored on 2011-06-07
284
    my $sql = "select * from table where date > :date and date < :date;";
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
285

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
286
In this case, You specify parameter values as array reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
287

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
288
    my $dbi->execute($sql, {date => ['2010-10-01', '2012-02-10']});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
289

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
290
=head2 Create where clause
update pod
Yuki Kimoto authored on 2011-01-28
291

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
292
=head3 Dinamically create where clause : where
update pod
Yuki Kimoto authored on 2011-01-28
293

            
update pod
Yuki Kimoto authored on 2011-02-11
294
You want to search multiple conditions in many times.
295
Let's think the following three cases.
update pod
Yuki Kimoto authored on 2011-01-28
296

            
update pod
Yuki Kimoto authored on 2011-02-11
297
Case1: Search only C<title>
update pod
Yuki Kimoto authored on 2011-01-28
298

            
updated pod
Yuki Kimoto authored on 2011-06-07
299
    where title = :title
update pod
Yuki Kimoto authored on 2011-01-28
300

            
update pod
Yuki Kimoto authored on 2011-02-11
301
Case2: Search only C<author>
update pod
Yuki Kimoto authored on 2011-01-28
302

            
updated pod
Yuki Kimoto authored on 2011-06-07
303
    where author = :author
update pod
Yuki Kimoto authored on 2011-01-28
304

            
update pod
Yuki Kimoto authored on 2011-02-11
305
Case3: Search C<title> and C<author>
update pod
Yuki Kimoto authored on 2011-01-28
306

            
updated pod
Yuki Kimoto authored on 2011-06-07
307
    where title = :title and author = :author
update pod
Yuki Kimoto authored on 2011-01-28
308

            
update pod
Yuki Kimoto authored on 2011-02-11
309
L<DBIx::Custom> support dinamic where clause creating.
updatedd pod
Yuki Kimoto authored on 2011-06-12
310
At first, create L<DBIx::Custom::Where> object by C<where>.
update pod
Yuki Kimoto authored on 2011-01-28
311

            
312
    my $where = $dbi->where;
313

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
314
Set clause by C<clause>
update pod
Yuki Kimoto authored on 2011-01-28
315

            
316
    $where->clause(
updated pod
Yuki Kimoto authored on 2011-06-07
317
        ['and', 'title = :title, 'author = :author']
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
318
    );
319

            
update pod
Yuki Kimoto authored on 2011-02-11
320
C<clause> is the following format.
update pod
Yuki Kimoto authored on 2011-01-28
321

            
updated pod
Yuki Kimoto authored on 2011-06-07
322
    ['or' or 'and', PART1, PART1, PART1]
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
323

            
update pod
Yuki Kimoto authored on 2011-02-11
324
First argument is 'or' or 'and'.
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
325
Later than first argument are part which contains named placeholder.
update pod
Yuki Kimoto authored on 2011-01-28
326

            
update pod
Yuki Kimoto authored on 2011-02-11
327
You can write more complex format.
update pod
Yuki Kimoto authored on 2011-01-28
328

            
329
    ['and', 
updated pod
Yuki Kimoto authored on 2011-06-07
330
      'title = :title', 
331
      ['or', 'author = :author', 'date like :date']
update pod
Yuki Kimoto authored on 2011-01-28
332
    ]
333

            
updated pod
Yuki Kimoto authored on 2011-06-07
334
This mean "title = :title and ( author = :author or date like :date )".
update pod
Yuki Kimoto authored on 2011-02-11
335

            
336
After setting C<clause>, set C<param>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
337
    
update pod
Yuki Kimoto authored on 2011-02-11
338
    $where->param({title => 'Perl'});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
339

            
update pod
Yuki Kimoto authored on 2011-02-11
340
In this example, parameter contains only title.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
341

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
342
If you execute C<string_to>, you can get where clause
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
343
which contain only named placeholder.
update pod
Yuki Kimoto authored on 2011-01-28
344

            
345
    my $where_clause = $where->to_string;
346

            
update pod
Yuki Kimoto authored on 2011-02-11
347
Parameter name is only title, the following where clause is created.
update pod
Yuki Kimoto authored on 2011-01-28
348

            
updated pod
Yuki Kimoto authored on 2011-06-07
349
    where title = :title
update pod
Yuki Kimoto authored on 2011-01-28
350

            
update pod
Yuki Kimoto authored on 2011-02-11
351
You can also create where clause by stringification.
update pod
Yuki Kimoto authored on 2011-01-28
352

            
353
    my $where_clause = "$where";
354

            
update pod
Yuki Kimoto authored on 2011-02-11
355
This is useful to embbed it into SQL. 
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
356

            
update pod
Yuki Kimoto authored on 2011-02-11
357
=head3 In case where clause contains same name columns
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
358

            
updated pod
Yuki Kimoto authored on 2011-06-07
359
Even if same name parameters exists, you can create where clause.
update pod
Yuki Kimoto authored on 2011-02-11
360
Let's think that there are starting date and ending date.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
361

            
update pod
Yuki Kimoto authored on 2011-01-28
362
    my $param = {start_date => '2010-11-15', end_date => '2011-11-21'};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
363

            
update pod
Yuki Kimoto authored on 2011-02-11
364
In this case, you set parameter value as array reference.
update pod
Yuki Kimoto authored on 2011-01-28
365

            
366
    my $p = {date => ['2010-11-15', '2011-11-21']};
367

            
updated pod
Yuki Kimoto authored on 2011-06-07
368
You can embbed these values into same name parameters.
update pod
Yuki Kimoto authored on 2011-01-28
369

            
370
    $where->clause(
updated pod
Yuki Kimoto authored on 2011-06-07
371
        ['and', 'date > :date', 'date < :date']
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
372
    );
update pod
Yuki Kimoto authored on 2011-01-28
373
    $where->param($p);
374

            
update pod
Yuki Kimoto authored on 2011-02-11
375
If starting date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
376

            
377
    my $p = {date => [$dbi->not_exists, '2011-11-21']};
378

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
379
You can get DBIx::Custom::NotExists object by C<not_exists>
update pod
Yuki Kimoto authored on 2011-02-11
380
This mean correnspondinf value isn't exists.
update pod
Yuki Kimoto authored on 2011-01-28
381

            
update pod
Yuki Kimoto authored on 2011-02-11
382
If ending date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
383

            
384
    my $p = {date => ['2010-11-15']};
385

            
update pod
Yuki Kimoto authored on 2011-02-11
386
If both date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
387

            
388
    my $p = {date => []};
389

            
update pod
Yuki Kimoto authored on 2011-02-11
390
This logic is a little difficut. See the following ones.
update pod
Yuki Kimoto authored on 2011-01-28
391

            
392
    my @date;
393
    push @date, exists $param->{start_date} ? $param->{start_date}
394
                                            : $dbi->not_exists;
395
    push @date, $param->{end_date} if exists $param->{end_date};
396
    my $p = {date => \@date};
397

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
398
=head3 With C<select>
update pod
Yuki Kimoto authored on 2011-01-28
399

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
400
You can pass L<DBIx::Custom::Where> object to C<where> of C<select>.
update pod
Yuki Kimoto authored on 2011-01-28
401
    
402
    my $where = $dbi->where;
updated pod
Yuki Kimoto authored on 2011-06-07
403
    $where->clause(['and', 'title = :title', 'author = :author']);
update pod
Yuki Kimoto authored on 2011-02-11
404
    $where->param({title => 'Perl'});
update pod
Yuki Kimoto authored on 2011-01-28
405
    my $result = $dbi->select(table => 'book', where => $where);
406

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
407
You can also pass it to C<where> of C<update>AC<delete>
update pod
Yuki Kimoto authored on 2011-01-28
408

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
409
=head3 With C<execute>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
410

            
update pod
Yuki Kimoto authored on 2011-02-11
411
L<DBIx::Custom::Where> object is embedded into SQL.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
412

            
update pod
Yuki Kimoto authored on 2011-01-28
413
    my $where = $dbi->where;
updated pod
Yuki Kimoto authored on 2011-06-07
414
    $where->clause(['and', 'title = :title', 'author = :author']);
update pod
Yuki Kimoto authored on 2011-02-11
415
    $where->param({title => 'Perl'});
update pod
Yuki Kimoto authored on 2011-01-28
416

            
update pod
Yuki Kimoto authored on 2011-02-11
417
    my $sql = <<"EOS";
updated pod
Yuki Kimoto authored on 2011-06-07
418
    select * from book;
update pod
Yuki Kimoto authored on 2011-01-28
419
    $where
420
    EOS
421

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
422
    $dbi->execute($sql, $param, table => 'book');
423

            
424
=head2 Filtering
425

            
426
=head3 Register filter : C<register_filter>
427

            
428
If you want to register filter, use C<register_filter>.
429

            
430
    $dbi->register_filter(
431
        # Time::Piece object to DATE format
432
        tp_to_date => sub {
433
            my $date = shift;
434
            return $tp->strftime('%Y-%m-%d');
435
        },
436
        
437
        # DATE to Time::Piece object
438
        date_to_tp => sub {
439
            my $date = shift;
440
            return Time::Piece->strptime($date, '%Y-%m-%d');
441
        },
442
    );
443

            
444
=head3 Filter before sending data into database : C<filter> option
445

            
446
If you filter sending data, use C<filter> option.
447

            
448
    $dbi->execute(
449
        'insert into book (date) values (:date)',
450
        {date => $tp},
451
        filter => {date => 'tp_to_date'}
452
    );
453

            
454
You can use C<filter> option in C<insert>, C<update>, C<delete>, C<select> method.
455

            
456
    $dbi->insert(
457
        {date => $tp},
458
        table => 'book',
459
        filter => {date => 'tp_to_date'}
460
    );
461

            
462
=head3 Filter after fetching data from database.
463

            
464
If you filter fetch data, use L<DBIx::Custom::Result>'s C<filter> method.
465

            
466
    my $result = $dbi->select(column => 'date', table => 'book');
467
    $result->filter(date => 'date_to_tp');
468
    my $row = $result->one;
update pod
Yuki Kimoto authored on 2011-01-28
469

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
470
=head2 7. Model
update pod
Yuki Kimoto authored on 2011-01-28
471

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
472
=head3 Model
update pod
Yuki Kimoto authored on 2011-01-28
473

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
474
you can define model extending L<DBIx::Custom::Model>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
475
to improve source code view.
update pod
Yuki Kimoto authored on 2011-01-28
476

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
477
At first, you create basic model class extending <DBIx::Custom::Model>.
updatedd pod
Yuki Kimoto authored on 2011-06-12
478
Each L<DBIx::Custom> class inherit L<Object::Simple>.
479
so you can inherit the following way.
update pod
Yuki Kimoto authored on 2011-01-28
480

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
481
    package MyModel;
updatedd pod
Yuki Kimoto authored on 2011-06-12
482
    use DBIx::Custom::Model -base;
update pod
Yuki Kimoto authored on 2011-01-28
483

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
484
Next, you create each model classes.
update pod
Yuki Kimoto authored on 2011-01-28
485

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
486
MyModel::book
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
487

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
488
    package MyModel::book;
updatedd pod
Yuki Kimoto authored on 2011-06-12
489
    use MyModel -base;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
490
    
491
    sub insert { ... }
492
    sub list { ... }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
493

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
494
MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
495

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
496
    package MyModel::company;
updatedd pod
Yuki Kimoto authored on 2011-06-12
497
    use MyModel -base;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
498
    
499
    sub insert { ... }
500
    sub list { ... }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
501

            
502
The follwoing modules location is needed.
503

            
504
    MyModel.pm
505
    MyModel / book.pm
506
            / company.pm
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
507

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
508
You can include these models by C<include_model>
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
509

            
510
    $dbi->include_model('MyModel');
511

            
512
First argument is name space of model.
513

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
514
You can use model like this.
515

            
516
    my $result = $dbi->model('book')->list;
517

            
518
In mode, You can use such as methods,
updatedd pod
Yuki Kimoto authored on 2011-06-12
519
C<insert>, C<update>, C<update_all>,
520
C<delete>, C<delete_all>, C<select>
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
521
without C<table> option.
522

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
523
    $dbi->model('book')->insert($param);
update pod
Yuki Kimoto authored on 2011-01-28
524

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
525
Model is L<DBIx::Custom::Model>.
update pod
Yuki Kimoto authored on 2011-01-28
526

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
527
If you need table nameAyou can get it by C<table>.
update pod
Yuki Kimoto authored on 2011-01-28
528

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
529
    my $table = $model->table;
update pod
Yuki Kimoto authored on 2011-01-28
530

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
531
You can get L<DBIx::Custom>.
update pod
Yuki Kimoto authored on 2011-01-28
532

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
533
    my $dbi = $model->dbi;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
534

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
535
You can also call all methods of L<DBIx::Custom> and L<DBI>. 
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
536

            
update pod
Yuki Kimoto authored on 2011-02-11
537
    # DBIx::Custom method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
538
    $model->execute($sql);
update pod
Yuki Kimoto authored on 2011-02-11
539
    
540
    # DBI method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
541
    $model->begin_work;
542
    $model->commit;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
543

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
544
If you want to get all models, you can get them by keys of C<models>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
545

            
546
    my @models = keys %{$self->models};
547

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
548
You can set primary key to model.
549

            
550
   $model->primary_key(['id', 'number_id']);
551

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
552
Primary key is used by C<insert>, C<update>, C<delete>,
553
and C<select> methods.
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
554

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
555
You can set column names
556

            
557
    $model->columns(['id', 'number_id']);
558

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
559
Column names is automarically set by C<setup_model>.
560
This method is needed to be call after C<include_model>.
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
561

            
562
    $dbi->setup_model;
563

            
cleanup
Yuki Kimoto authored on 2011-03-10
564
You can set C<join>
cleanup
Yuki Kimoto authored on 2011-02-22
565

            
cleanup
Yuki Kimoto authored on 2011-03-10
566
    $model->join(['left outer join company on book.company_id = company.id']);
cleanup
Yuki Kimoto authored on 2011-02-22
567

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
568
C<join> is used by C<select> method.
cleanup
Yuki Kimoto authored on 2011-02-22
569

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
570
=head2 Create column clause automatically : mycolumn, column
cleanup
Yuki Kimoto authored on 2011-03-21
571

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
572
To create column clause automatically, use C<mycolumn>.
cleanup
Yuki Kimoto authored on 2011-03-21
573
Valude of C<table> and C<columns> is used.
574

            
cleanup
Yuki Kimoto authored on 2011-06-13
575
    my $mycolumns = $model->mycolumn;
cleanup
Yuki Kimoto authored on 2011-03-21
576

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
577
If C<table> is 'book'AC<column> is ['id', 'name'],
cleanup
Yuki Kimoto authored on 2011-03-21
578
the following clause is created.
579

            
580
    book.id as id, book.name as name
581

            
582
These column name is for removing column name ambiguities.
583

            
584
You can create column clause from columns of other table.
585

            
cleanup
Yuki Kimoto authored on 2011-06-13
586
    my $columns = $model->column('company');
cleanup
Yuki Kimoto authored on 2011-03-21
587

            
cleanup
Yuki Kimoto authored on 2011-06-13
588
If C<table> is "company", C<column> return ['id', 'name'],
cleanup
Yuki Kimoto authored on 2011-03-21
589
the following clause is created.
590

            
cleanup
Yuki Kimoto authored on 2011-06-13
591
    company.id as "company.id", company.name as "company.name"
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
592

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
593
=head2 Model Examples
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
594

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
595
Model examples
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
596

            
update pod
Yuki Kimoto authored on 2011-01-28
597
    package MyDBI;
updatedd pod
Yuki Kimoto authored on 2011-06-12
598
    use DBIx::Custom -base;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
599
    
update pod
Yuki Kimoto authored on 2011-01-28
600
    sub connect {
601
        my $self = shift->SUPER::connect(@_);
602
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
603
        $self->include_model(
604
            MyModel => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
605
                'book',
606
                'company'
607
            ]
update pod
Yuki Kimoto authored on 2011-01-28
608
        );
609
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
610
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
611
    package MyModel::book;
updatedd pod
Yuki Kimoto authored on 2011-06-12
612
    use DBIx::Custom::Model -base;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
613
    
updatedd pod
Yuki Kimoto authored on 2011-06-12
614
    has primary_key => sub { ['id'] };
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
615
    
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
616
    sub insert { ... }
617
    sub list { ... }
618
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
619
    package MyModel::company;
updatedd pod
Yuki Kimoto authored on 2011-06-12
620
    use DBIx::Custom::Model -base;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
621

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
622
    has primary_key => sub { ['id'] };
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
623
    
624
    sub insert { ... }
625
    sub list { ... }
update pod
Yuki Kimoto authored on 2011-01-28
626

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
627
=cut