DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
457 lines | 10.723kb
first commit
yuki-kimoto authored on 2009-10-13
1
package DBI::Custom;
2
use Object::Simple;
add test
yuki-kimoto authored on 2009-10-16
3

            
4
our $VERSION = '0.0101';
5

            
6
use Carp 'croak';
add some method
yuki-kimoto authored on 2009-10-14
7
use DBI;
first commit
yuki-kimoto authored on 2009-10-13
8

            
cleanup
yuki-kimoto authored on 2009-10-15
9
# Model
10
sub model : ClassAttr { auto_build => \&_inherit_model }
first commit
yuki-kimoto authored on 2009-10-13
11

            
cleanup
yuki-kimoto authored on 2009-10-15
12
# Inherit super class model
13
sub _inherit_model {
add test
yuki-kimoto authored on 2009-10-16
14
    my $class = shift;
cleanup
yuki-kimoto authored on 2009-10-15
15
    my $super = do {
16
        no strict 'refs';
17
        ${"${class}::ISA"}[0];
18
    };
19
    my $model = eval{$super->can('model')}
20
                         ? $super->model->clone
21
                         : $class->Object::Simple::new;
cleanup
yuki-kimoto authored on 2009-10-14
22
    
23
    $class->model($model);
first commit
yuki-kimoto authored on 2009-10-13
24
}
25

            
cleanup
yuki-kimoto authored on 2009-10-15
26
# New
27
sub new {
28
    my $self = shift->Object::Simple::new(@_);
29
    my $class = ref $self;
30
    return bless {%{$class->model->clone}, %{$self}}, $class;
first commit
yuki-kimoto authored on 2009-10-13
31
}
32

            
cleanup
yuki-kimoto authored on 2009-10-15
33
# Initialize modle
34
sub initialize_model {
35
    my ($class, $callback) = @_;
first commit
yuki-kimoto authored on 2009-10-13
36
    
cleanup
yuki-kimoto authored on 2009-10-15
37
    # Callback to initialize model
38
    $callback->($class->model);
first commit
yuki-kimoto authored on 2009-10-13
39
}
40

            
cleanup
yuki-kimoto authored on 2009-10-15
41
# Clone
42
sub clone {
cleanup
yuki-kimoto authored on 2009-10-14
43
    my $self = shift;
cleanup
yuki-kimoto authored on 2009-10-15
44
    my $new = $self->Object::Simple::new;
add test
yuki-kimoto authored on 2009-10-16
45
    $new->connect_info(%{$self->connect_info || {}});
cleanup
yuki-kimoto authored on 2009-10-15
46
    $new->filters(%{$self->filters || {}});
add test
yuki-kimoto authored on 2009-10-16
47
    $new->bind_filter($self->bind_filter);
48
    $new->fetch_filter($self->fetch_filter);
add various things
yuki-kimoto authored on 2009-10-17
49
    $new->result_class($self->result_class);
cleanup
yuki-kimoto authored on 2009-10-14
50
}
51

            
cleanup
yuki-kimoto authored on 2009-10-15
52
# Attribute
53
sub connect_info       : Attr { type => 'hash',  auto_build => sub { shift->connect_info({}) } }
cleanup
yuki-kimoto authored on 2009-10-15
54

            
add test
yuki-kimoto authored on 2009-10-16
55
sub bind_filter : Attr {}
56
sub fetch_filter : Attr {}
cleanup
yuki-kimoto authored on 2009-10-15
57

            
add test
yuki-kimoto authored on 2009-10-16
58
sub filters : Attr { type => 'hash', deref => 1, auto_build => sub { shift->filters({}) } }
cleanup
yuki-kimoto authored on 2009-10-15
59
sub add_filter { shift->filters(@_) }
add various things
yuki-kimoto authored on 2009-10-17
60
sub result_class : Attr { default => 'DBI::Custom::Result' }
cleanup
yuki-kimoto authored on 2009-10-15
61

            
cleanup
yuki-kimoto authored on 2009-10-15
62
sub dbh          : Attr { auto_build => sub { shift->connect } }
add test
yuki-kimoto authored on 2009-10-17
63
sub sql_template : Attr { auto_build => sub { shift->sql_template(DBI::Custom::SQLTemplate->new) } }
add test
yuki-kimoto authored on 2009-10-16
64

            
add various things
yuki-kimoto authored on 2009-10-17
65

            
add test
yuki-kimoto authored on 2009-10-16
66
our %VALID_CONNECT_INFO = map {$_ => 1} qw/data_source user password options/;
cleanup
yuki-kimoto authored on 2009-10-14
67

            
add various things
yuki-kimoto authored on 2009-10-17
68
# Connect
add some method
yuki-kimoto authored on 2009-10-14
69
sub connect {
70
    my $self = shift;
71
    my $connect_info = $self->connect_info;
72
    
add test
yuki-kimoto authored on 2009-10-16
73
    foreach my $key (keys %{$self->connect_info}) {
add test
yuki-kimoto authored on 2009-10-17
74
        croak("connect_info '$key' is invald")
75
          unless $VALID_CONNECT_INFO{$key};
add test
yuki-kimoto authored on 2009-10-16
76
    }
77
    
add some method
yuki-kimoto authored on 2009-10-14
78
    my $dbh = DBI->connect(
add test
yuki-kimoto authored on 2009-10-16
79
        $connect_info->{data_source},
add some method
yuki-kimoto authored on 2009-10-14
80
        $connect_info->{user},
81
        $connect_info->{password},
82
        {
83
            RaiseError => 1,
84
            PrintError => 0,
85
            AutoCommit => 1,
86
            %{$connect_info->{options} || {} }
87
        }
88
    );
89
    
add various things
yuki-kimoto authored on 2009-10-17
90
    $self->auto_commit($self->dbh->{AutoCommit});
add some method
yuki-kimoto authored on 2009-10-14
91
    $self->dbh($dbh);
92
}
first commit
yuki-kimoto authored on 2009-10-13
93

            
add various things
yuki-kimoto authored on 2009-10-17
94
# Is connected?
95
sub connected {
96
    my $self = shift;
97
    return exists $sefl->{dbh};
98
}
99

            
100
# Disconnect
101
sub disconnect {
102
    my $self = shift;
103
    if ($self->conneced) {
104
        $self->dbh->disconnect;
105
        delete $self->{dbh};
106
    }
107
}
108

            
109
# Reconnect
110
sub reconnect {
111
    my $self = shift;
112
    $sefl->disconnect if $self->connected;
113
    $self->connect;
114
}
115

            
116

            
add test
yuki-kimoto authored on 2009-10-17
117
sub create_sql {
118
    my $self = shift;
119
    
120
    my ($sql, @bind) = $self->sql_template->create_sql(@_);
121
    
122
    return ($sql, @bind);
123
}
124

            
add some method
yuki-kimoto authored on 2009-10-14
125
sub query {
try varioud way
yuki-kimoto authored on 2009-10-17
126
    my ($self, $template, $values, $filter)  = @_;
127
    
128
    $filter ||= $self->bind_filter;
129
    
add various things
yuki-kimoto authored on 2009-10-17
130
    my ($sql, @bind) = $self->create_sql($template, $values, $filter);
131
    my (
132
    my $sth = $self->dbh->prepare($sql);
133
    $sth->execute(@bind);
134
    
135
    # Select
136
    if ($sth->{NUM_OF_FIELDS}) {
137
        my $result_class = $self->result_class;
138
        my $result = $result_class->new({sth => $sth});
139
        return $result;
140
    }
141
    return;
add test
yuki-kimoto authored on 2009-10-17
142
}
143

            
144
sub query_raw_sql {
145
    my ($self, $sql, @bind) = @_;
add various things
yuki-kimoto authored on 2009-10-17
146
    my $sth = $self->dbh->prepare($sql);
147
    $sth->execute(@bind);
148
    return $sth;
add test
yuki-kimoto authored on 2009-10-17
149
}
150

            
add various things
yuki-kimoto authored on 2009-10-17
151
sub auto_commit : Attr {
152

            
add test
yuki-kimoto authored on 2009-10-17
153
Object::Simple->build_class;
154

            
add various things
yuki-kimoto authored on 2009-10-17
155
package DBI::Custom::Result;
156
use Object::Simple;
157

            
158
sub sth : Attr {};
159

            
160
sub fetchrow_arrayref {
161
    my $self = shift;
162
    $self->sth;
163
    
164
    
165
}
166

            
167

            
168
*fetch = \&fetchrow_arrayref;
169

            
170
sub err    { shift->sth->err }
171
sub errstr { shift->sth->errstr }
172
sub finish { shift->sth->finish }
173
sub rows   { shift->sth->rows }
174
sub state  { shift->sth->state }
175

            
176
Object::Simple->build_class;
177

            
178

            
add test
yuki-kimoto authored on 2009-10-17
179
package DBI::Custom::SQLTemplate;
180
use Object::Simple;
try various way
yuki-kimoto authored on 2009-10-17
181
use Carp 'croak';
add test
yuki-kimoto authored on 2009-10-17
182

            
try varioud way
yuki-kimoto authored on 2009-10-17
183
### Attributes;
try various way
yuki-kimoto authored on 2009-10-17
184
sub tag_start   : Attr { default => '{' }
185
sub tag_end     : Attr { default => '}' }
186
sub template    : Attr {};
187
sub tree        : Attr { auto_build => sub { shift->tree([]) } }
188
sub bind_filter : Attr {}
189
sub values      : Attr {}
190
sub upper_case  : Attr {default => 0}
try varioud way
yuki-kimoto authored on 2009-10-17
191

            
add test
yuki-kimoto authored on 2009-10-17
192
sub create_sql {
try varioud way
yuki-kimoto authored on 2009-10-17
193
    my ($self, $template, $values, $filter)  = @_;
194
    
try various way
yuki-kimoto authored on 2009-10-17
195
    $filter ||= $self->bind_filter;
196
    
try varioud way
yuki-kimoto authored on 2009-10-17
197
    $self->parse($template);
198
    
try various way
yuki-kimoto authored on 2009-10-17
199
    my ($sql, @bind) = $self->build_sql({bind_filter => $filter, values => $values});
try varioud way
yuki-kimoto authored on 2009-10-17
200
    
201
    return ($sql, @bind);
202
}
203

            
204
our $TAG_SYNTAX = <<'EOS';
205
[tag]            [expand]
206
{= name}         name = ?
try various way
yuki-kimoto authored on 2009-10-17
207
{<> name}        name <> ?
try varioud way
yuki-kimoto authored on 2009-10-17
208

            
209
{< name}         name < ?
210
{> name}         name > ?
211
{>= name}        name >= ?
212
{<= name}        name <= ?
213

            
214
{like name}      name like ?
215
{in name}        name in [?, ?, ..]
216

            
217
{insert_values}  (key1, key2, key3) values (?, ?, ?)
218
{update_values}  set key1 = ?, key2 = ?, key3 = ?
219
EOS
220

            
try various way
yuki-kimoto authored on 2009-10-17
221
our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_set/;
try varioud way
yuki-kimoto authored on 2009-10-17
222
sub parse {
223
    my ($self, $template) = @_;
224
    $self->template($template);
225
    
226
    # Clean start;
227
    delete $self->{tree};
228
    
229
    # Tags
230
    my $tag_start = quotemeta $self->tag_start;
231
    my $tag_end   = quotemeta $self->tag_end;
first commit
yuki-kimoto authored on 2009-10-13
232
    
try varioud way
yuki-kimoto authored on 2009-10-17
233
    # Tokenize
234
    my $state = 'text';
235
    
236
    # Save original template
237
    my $original_template = $template;
238
    
239
    # Text
240
    while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) {
try various way
yuki-kimoto authored on 2009-10-17
241
        my $text = $1;
try varioud way
yuki-kimoto authored on 2009-10-17
242
        my $tag  = $2;
243
        
try various way
yuki-kimoto authored on 2009-10-17
244
        push @{$self->tree}, {type => 'text', args => [$text]} if $text;
try varioud way
yuki-kimoto authored on 2009-10-17
245
        
246
        if ($tag) {
247
            
try various way
yuki-kimoto authored on 2009-10-17
248
            my ($tag_name, @args) = split /\s+/, $tag;
try varioud way
yuki-kimoto authored on 2009-10-17
249
            
try various way
yuki-kimoto authored on 2009-10-17
250
            $tag ||= '';
251
            croak("Tag '$tag' in SQL template is invalid.\n\n" .
252
                  "SQL template tag syntax\n$TAG_SYNTAX\n\n" .
253
                  "Your SQL template is \n$original_template\n\n")
254
              unless $VALID_TAG_NAMES{$tag_name};
try varioud way
yuki-kimoto authored on 2009-10-17
255
            
try various way
yuki-kimoto authored on 2009-10-17
256
            push @{$self->tree}, {type => 'tag', tag_name => $tag_name, args => [@args]};
try varioud way
yuki-kimoto authored on 2009-10-17
257
        }
258
    }
259
    
try various way
yuki-kimoto authored on 2009-10-17
260
    push @{$self->tree}, {type => 'text', args => [$template]} if $template;
first commit
yuki-kimoto authored on 2009-10-13
261
}
262

            
try various way
yuki-kimoto authored on 2009-10-17
263
our %EXPAND_PLACE_HOLDER = map {$_ => 1} qw/= <> < > >= <= like/;
264
sub build_sql {
265
    my ($self, $args) = @_;
266
    
267
    my $tree        = $args->{tree} || $self->tree;
268
    my $bind_filter = $args->{bind_filter} || $self->bind_filter;
269
    my $values      = exists $args->{values} ? $args->{values} : $self->values;
270
    
271
    my @bind_values;
272
    my $sql = '';
273
    foreach my $node (@$tree) {
274
        my $type     = $node->{type};
275
        my $tag_name = $node->{tag_name};
276
        my $args     = $node->{args};
277
        
278
        if ($type eq 'text') {
279
            # Join text
280
            $sql .= $args->[0];
281
        }
282
        elsif ($type eq 'tag') {
283
            if ($EXPAND_PLACE_HOLDER{$tag_name}) {
284
                my $key = $args->[0];
285
                
286
                # Filter Value
287
                if ($bind_filter) {
try various way
yuki-kimoto authored on 2009-10-17
288
                    push @bind_values, scalar $bind_filter->($key, $values->{$key});
try various way
yuki-kimoto authored on 2009-10-17
289
                }
290
                else {
291
                    push @bind_values, $values->{$key};
292
                }
293
                $tag_name = uc $tag_name if $self->upper_case;
294
                my $place_holder = "$key $tag_name ?";
295
                $sql .= $place_holder;
296
            }
try various way
yuki-kimoto authored on 2009-10-17
297
            elsif ($tag_name eq 'insert_values') {
298
                my $statement_keys          = '(';
299
                my $statement_place_holders = '(';
300
                
301
                $values = $values->{insert_values};
302
                
303
                foreach my $key (sort keys %$values) {
304
                    if ($bind_filter) {
305
                        push @bind_values, scalar $bind_filter->($key, $values->{$key});
306
                    }
307
                    else {
308
                        push @bind_values, $values->{$key};
309
                    }
310
                    
311
                    $statement_keys          .= "$key, ";
312
                    $statement_place_holders .= "?, ";
313
                }
314
                
315
                $statement_keys =~ s/, $//;
316
                $statement_keys .= ')';
317
                
318
                $statement_place_holders =~ s/, $//;
319
                $statement_place_holders .= ')';
320
                
321
                $sql .= "$statement_keys values $statement_place_holders";
322
            }
323
            elsif ($tag_name eq 'update_set') {
324
                my $statement          = 'set ';
325
                
326
                $values = $values->{update_set};
327
                
328
                foreach my $key (sort keys %$values) {
329
                    if ($bind_filter) {
330
                        push @bind_values, scalar $bind_filter->($key, $values->{$key});
331
                    }
332
                    else {
333
                        push @bind_values, $values->{$key};
334
                    }
335
                    
336
                    $statement          .= "$key = ?, ";
337
                }
338
                
339
                $statement =~ s/, $//;
340
                
341
                $sql .= $statement;
342
            }
try various way
yuki-kimoto authored on 2009-10-17
343
        }
344
    }
345
    $sql .= ';' unless $sql =~ /;$/;
346
    return ($sql, @bind_values);
347
}
try varioud way
yuki-kimoto authored on 2009-10-17
348

            
349

            
first commit
yuki-kimoto authored on 2009-10-13
350
Object::Simple->build_class;
351

            
add various things
yuki-kimoto authored on 2009-10-17
352
package DBI::Custom;
353
1;
354

            
first commit
yuki-kimoto authored on 2009-10-13
355
=head1 NAME
356

            
add test
yuki-kimoto authored on 2009-10-17
357
DBI::Custom - Customizable simple DBI
first commit
yuki-kimoto authored on 2009-10-13
358

            
359
=head1 VERSION
360

            
add test
yuki-kimoto authored on 2009-10-16
361
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
362

            
363
=cut
364

            
365
=head1 SYNOPSIS
366

            
add test
yuki-kimoto authored on 2009-10-16
367
  my $dbi = DBI::Custom->new;
first commit
yuki-kimoto authored on 2009-10-13
368

            
add test
yuki-kimoto authored on 2009-10-16
369
=head1 METHODS
first commit
yuki-kimoto authored on 2009-10-13
370

            
add test
yuki-kimoto authored on 2009-10-16
371
=head2 add_filter
first commit
yuki-kimoto authored on 2009-10-13
372

            
add test
yuki-kimoto authored on 2009-10-16
373
=head2 bind_filter
first commit
yuki-kimoto authored on 2009-10-13
374

            
add test
yuki-kimoto authored on 2009-10-16
375
=head2 clone
first commit
yuki-kimoto authored on 2009-10-13
376

            
add test
yuki-kimoto authored on 2009-10-16
377
=head2 connect
first commit
yuki-kimoto authored on 2009-10-13
378

            
add test
yuki-kimoto authored on 2009-10-16
379
=head2 connect_info
first commit
yuki-kimoto authored on 2009-10-13
380

            
add test
yuki-kimoto authored on 2009-10-16
381
=head2 dbh
first commit
yuki-kimoto authored on 2009-10-13
382

            
add test
yuki-kimoto authored on 2009-10-16
383
=head2 fetch_filter
first commit
yuki-kimoto authored on 2009-10-13
384

            
add test
yuki-kimoto authored on 2009-10-16
385
=head2 filters
first commit
yuki-kimoto authored on 2009-10-13
386

            
add test
yuki-kimoto authored on 2009-10-16
387
=head2 initialize_model
first commit
yuki-kimoto authored on 2009-10-13
388

            
add test
yuki-kimoto authored on 2009-10-16
389
=head2 model
first commit
yuki-kimoto authored on 2009-10-13
390

            
add test
yuki-kimoto authored on 2009-10-16
391
=head2 new
392

            
393
=head2 query
first commit
yuki-kimoto authored on 2009-10-13
394

            
add test
yuki-kimoto authored on 2009-10-17
395
=head2 create_sql
396

            
397
=head2 query_raw_sql
398

            
399
=head2 sql_template
400

            
first commit
yuki-kimoto authored on 2009-10-13
401
=head1 AUTHOR
402

            
403
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
404

            
405
=head1 BUGS
406

            
407
Please report any bugs or feature requests to C<bug-dbi-custom at rt.cpan.org>, or through
408
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBI-Custom>.  I will be notified, and then you'll
409
automatically be notified of progress on your bug as I make changes.
410

            
411

            
412

            
413

            
414
=head1 SUPPORT
415

            
416
You can find documentation for this module with the perldoc command.
417

            
418
    perldoc DBI::Custom
419

            
420

            
421
You can also look for information at:
422

            
423
=over 4
424

            
425
=item * RT: CPAN's request tracker
426

            
427
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBI-Custom>
428

            
429
=item * AnnoCPAN: Annotated CPAN documentation
430

            
431
L<http://annocpan.org/dist/DBI-Custom>
432

            
433
=item * CPAN Ratings
434

            
435
L<http://cpanratings.perl.org/d/DBI-Custom>
436

            
437
=item * Search CPAN
438

            
439
L<http://search.cpan.org/dist/DBI-Custom/>
440

            
441
=back
442

            
443

            
444
=head1 ACKNOWLEDGEMENTS
445

            
446

            
447
=head1 COPYRIGHT & LICENSE
448

            
449
Copyright 2009 Yuki Kimoto, all rights reserved.
450

            
451
This program is free software; you can redistribute it and/or modify it
452
under the same terms as Perl itself.
453

            
454

            
455
=cut
456

            
457
1; # End of DBI::Custom