Newer Older
690 lines | 18.569kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
package DBIx::Custom::SQL::Template;
2
use Object::Simple;
3

            
update document
yuki-kimoto authored on 2009-11-17
4
use strict;
5
use warnings;
packaging one directory
yuki-kimoto authored on 2009-11-16
6
use Carp 'croak';
7

            
8
# Accessor is created by Object::Simple. Please read Object::Simple document
9

            
10
### Class-Object accessors
11

            
12
# Tag start
13
sub tag_start   : ClassObjectAttr {
14
    initialize => {default => '{', clone => 'scalar'}
15
}
16

            
17
# Tag end
18
sub tag_end     : ClassObjectAttr {
19
    initialize => {default => '}', clone => 'scalar'}
20
}
21

            
22
# Tag syntax
23
sub tag_syntax  : ClassObjectAttr {
24
    initialize => {default => <<'EOS', clone => 'scalar'}}
25
[tag]                     [expand]
26
{? name}                  ?
27
{= name}                  name = ?
28
{<> name}                 name <> ?
29

            
30
{< name}                  name < ?
31
{> name}                  name > ?
32
{>= name}                 name >= ?
33
{<= name}                 name <= ?
34

            
35
{like name}               name like ?
36
{in name number}          name in [?, ?, ..]
37

            
38
{insert key1 key2} (key1, key2) values (?, ?)
39
{update key1 key2}    set key1 = ?, key2 = ?
40
EOS
41

            
42
# Tag processors
43
sub tag_processors : ClassObjectAttr {
44
    type => 'hash',
45
    deref => 1,
46
    initialize => {
47
        clone => 'hash', 
48
        default => sub {{
49
            '?'             => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
50
            '='             => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
51
            '<>'            => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
52
            '>'             => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
53
            '<'             => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
54
            '>='            => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
55
            '<='            => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
56
            'like'          => \&DBIx::Custom::SQL::Template::TagProcessor::expand_basic_tag,
57
            'in'            => \&DBIx::Custom::SQL::Template::TagProcessor::expand_in_tag,
58
            'insert'        => \&DBIx::Custom::SQL::Template::TagProcessor::expand_insert_tag,
59
            'update'    => \&DBIx::Custom::SQL::Template::TagProcessor::expand_update_tag
60
        }}
61
    }
62
}
63

            
64
# Add Tag processor
65
sub add_tag_processor {
66
    my $invocant = shift;
67
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
68
    $invocant->tag_processors(%{$invocant->tag_processors}, %{$tag_processors});
69
    return $invocant;
70
}
71

            
72
# Clone
73
sub clone {
74
    my $self = shift;
75
    my $new = $self->new;
76
    
77
    $new->tag_start($self->tag_start);
78
    $new->tag_end($self->tag_end);
79
    $new->tag_syntax($self->tag_syntax);
80
    $new->tag_processors({%{$self->tag_processors || {}}});
81
    
82
    return $new;
83
}
84

            
85

            
86
### Object Methods
87

            
88
# Create Query
89
sub create_query {
90
    my ($self, $template)  = @_;
91
    
92
    # Parse template
93
    my $tree = $self->_parse_template($template);
94
    
95
    # Build query
96
    my $query = $self->_build_query($tree);
97
    
98
    return $query;
99
}
100

            
101
# Parse template
102
sub _parse_template {
103
    my ($self, $template) = @_;
104
    $template ||= '';
105
    
106
    my $tree = [];
107
    
108
    # Tags
109
    my $tag_start = quotemeta $self->tag_start;
110
    my $tag_end   = quotemeta $self->tag_end;
111
    
112
    # Tokenize
113
    my $state = 'text';
114
    
115
    # Save original template
116
    my $original_template = $template;
117
    
118
    # Parse template
119
    while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) {
120
        my $text = $1;
121
        my $tag  = $2;
122
        
123
        # Parse tree
124
        push @$tree, {type => 'text', tag_args => [$text]} if $text;
125
        
126
        if ($tag) {
127
            # Get tag name and arguments
128
            my ($tag_name, @tag_args) = split /\s+/, $tag;
129
            
130
            # Tag processor is exist?
131
            unless ($self->tag_processors->{$tag_name}) {
132
                my $tag_syntax = $self->tag_syntax;
133
                croak("Tag '{$tag}' in SQL template is not exist.\n\n" .
134
                      "<SQL template tag syntax>\n" .
135
                      "$tag_syntax\n" .
136
                      "<Your SQL template>\n" .
137
                      "$original_template\n\n");
138
            }
139
            
140
            # Check tag arguments
141
            foreach my $tag_arg (@tag_args) {
142
                # Cannot cantain placehosder '?'
143
                croak("Tag '{t }' arguments cannot contain '?'")
144
                  if $tag_arg =~ /\?/;
145
            }
146
            
147
            # Add tag to parsing tree
148
            push @$tree, {type => 'tag', tag_name => $tag_name, tag_args => [@tag_args]};
149
        }
150
    }
151
    
152
    # Add text to parsing tree 
153
    push @$tree, {type => 'text', tag_args => [$template]} if $template;
154
    
155
    return $tree;
156
}
157

            
158
# Build SQL from parsing tree
159
sub _build_query {
160
    my ($self, $tree) = @_;
161
    
162
    # SQL
163
    my $sql = '';
164
    
165
    # All parameter key infomation
166
    my $all_key_infos = [];
167
    
168
    # Build SQL 
169
    foreach my $node (@$tree) {
170
        
171
        # Get type, tag name, and arguments
172
        my $type     = $node->{type};
173
        my $tag_name = $node->{tag_name};
174
        my $tag_args = $node->{tag_args};
175
        
176
        # Text
177
        if ($type eq 'text') {
178
            # Join text
179
            $sql .= $tag_args->[0];
180
        }
181
        
182
        # Tag
183
        elsif ($type eq 'tag') {
184
            
185
            # Get tag processor
186
            my $tag_processor = $self->tag_processors->{$tag_name};
187
            
188
            # Tag processor is code ref?
189
            croak("Tag processor '$tag_name' must be code reference")
190
              unless ref $tag_processor eq 'CODE';
191
            
192
            # Expand tag using tag processor
193
            my ($expand, $key_infos)
194
              = $tag_processor->($tag_name, $tag_args);
195
            
196
            # Check tag processor return value
197
            croak("Tag processor '$tag_name' must return (\$expand, \$key_infos)")
198
              if !defined $expand || ref $key_infos ne 'ARRAY';
199
            
200
            # Check placeholder count
201
            croak("Placeholder count in SQL created by tag processor '$tag_name' " .
202
                  "must be same as key informations count")
203
              unless $self->_placeholder_count($expand) eq @$key_infos;
204
            
205
            # Add key information
206
            push @$all_key_infos, @$key_infos;
207
            
208
            # Join expand tag to SQL
209
            $sql .= $expand;
210
        }
211
    }
212
    
213
    # Add semicolon
214
    $sql .= ';' unless $sql =~ /;$/;
215
    
216
    # Query
217
    my $query = {sql => $sql, key_infos => $all_key_infos};
218
    
219
    return $query;
220
}
221

            
222
# Get placeholder count
223
sub _placeholder_count {
224
    my ($self, $expand) = @_;
225
    $expand ||= '';
226
    
227
    my $count = 0;
228
    my $pos   = -1;
229
    while (($pos = index($expand, '?', $pos + 1)) != -1) {
230
        $count++;
231
    }
232
    return $count;
233
}
234

            
235
Object::Simple->build_class;
236

            
237

            
238
package DBIx::Custom::SQL::Template::TagProcessor;
239
use strict;
240
use warnings;
241
use Carp 'croak';
242

            
243
# Expand tag '?', '=', '<>', '>', '<', '>=', '<=', 'like'
244
sub expand_basic_tag {
245
    my ($tag_name, $tag_args) = @_;
246
    my $original_key = $tag_args->[0];
247
    
248
    # Key is not exist
249
    croak("You must be pass key as argument to tag '{$tag_name }'")
250
      if !$original_key;
251
    
252
    # Expanded tag
253
    my $expand = $tag_name eq '?'
254
               ? '?'
255
               : "$original_key $tag_name ?";
256
    
257
    # Get table and clumn name
258
    my ($table, $column) = get_table_and_column($original_key);
259
    
260
    # Parameter key infomation
261
    my $key_info = {};
262
    
263
    # Original key
264
    $key_info->{original_key} = $original_key;
265
    
266
    # Table
267
    $key_info->{table}  = $table;
268
    
269
    # Column name
270
    $key_info->{column} = $column;
271
    
272
    # Access keys
273
    my $access_keys = [];
274
    push @$access_keys, [$original_key];
275
    push @$access_keys, [$table, $column] if $table && $column;
276
    $key_info->{access_keys} = $access_keys;
277
    
278
    # Add parameter key information
279
    my $key_infos = [];
280
    push @$key_infos, $key_info;
281
    
282
    return ($expand, $key_infos);
283
}
284

            
285
# Expand tag 'in'
286
sub expand_in_tag {
287
    my ($tag_name, $tag_args) = @_;
288
    my ($original_key, $placeholder_count) = @$tag_args;
289
    
290
    # Key must be specified
291
    croak("You must be pass key as first argument of tag '{$tag_name }'\n" . 
292
          "Usage: {$tag_name \$key \$placeholder_count}")
293
      unless $original_key;
294
      
295
    
296
    # Place holder count must be specified
297
    croak("You must be pass placeholder count as second argument of tag '{$tag_name }'\n" . 
298
          "Usage: {$tag_name \$key \$placeholder_count}")
299
      if !$placeholder_count || $placeholder_count =~ /\D/;
300

            
301
    # Expand tag
302
    my $expand = "$original_key $tag_name (";
303
    for (my $i = 0; $i < $placeholder_count; $i++) {
304
        $expand .= '?, ';
305
    }
306
    
307
    $expand =~ s/, $//;
308
    $expand .= ')';
309
    
310
    # Get table and clumn name
311
    my ($table, $column) = get_table_and_column($original_key);
312
    
313
    # Create parameter key infomations
314
    my $key_infos = [];
315
    for (my $i = 0; $i < $placeholder_count; $i++) {
316
        # Parameter key infomation
317
        my $key_info = {};
318
        
319
        # Original key
320
        $key_info->{original_key} = $original_key;
321
        
322
        # Table
323
        $key_info->{table}   = $table;
324
        
325
        # Column name
326
        $key_info->{column}  = $column;
327
        
328
        # Access keys
329
        my $access_keys = [];
330
        push @$access_keys, [$original_key, [$i]];
331
        push @$access_keys, [$table, $column, [$i]] if $table && $column;
332
        $key_info->{access_keys} = $access_keys;
333
        
334
        # Add parameter key infos
335
        push @$key_infos, $key_info;
336
    }
337
    
338
    return ($expand, $key_infos);
339
}
340

            
341
# Get table and column
342
sub get_table_and_column {
343
    my $key = shift;
344
    $key ||= '';
345
    
346
    return ('', $key) unless $key =~ /\./;
347
    
348
    my ($table, $column) = split /\./, $key;
349
    
350
    return ($table, $column);
351
}
352

            
353
# Expand tag 'insert'
354
sub expand_insert_tag {
355
    my ($tag_name, $tag_args) = @_;
356
    my $original_keys = $tag_args;
357
    
358
    # Insert key (k1, k2, k3, ..)
359
    my $insert_keys = '(';
360
    
361
    # placeholder (?, ?, ?, ..)
362
    my $place_holders = '(';
363
    
364
    foreach my $original_key (@$original_keys) {
365
        # Get table and column
366
        my ($table, $column) = get_table_and_column($original_key);
367
        
368
        # Join insert column
369
        $insert_keys   .= "$column, ";
370
        
371
        # Join place holder
372
        $place_holders .= "?, ";
373
    }
374
    
375
    # Delete last ', '
376
    $insert_keys =~ s/, $//;
377
    
378
    # Close 
379
    $insert_keys .= ')';
380
    $place_holders =~ s/, $//;
381
    $place_holders .= ')';
382
    
383
    # Expand tag
384
    my $expand = "$insert_keys values $place_holders";
385
    
386
    # Create parameter key infomations
387
    my $key_infos = [];
388
    foreach my $original_key (@$original_keys) {
389
        # Get table and clumn name
390
        my ($table, $column) = get_table_and_column($original_key);
391
        
392
        # Parameter key infomation
393
        my $key_info = {};
394
        
395
        # Original key
396
        $key_info->{original_key} = $original_key;
397
        
398
        # Table
399
        $key_info->{table}   = $table;
400
        
401
        # Column name
402
        $key_info->{column}  = $column;
403
        
404
        # Access keys
405
        my $access_keys = [];
406
        push @$access_keys, ['#insert', $original_key];
407
        push @$access_keys, ['#insert', $table, $column] if $table && $column;
408
        push @$access_keys, [$original_key];
409
        push @$access_keys, [$table, $column] if $table && $column;
410
        $key_info->{access_keys} = $access_keys;
411
        
412
        # Add parameter key infos
413
        push @$key_infos, $key_info;
414
    }
415
    
416
    return ($expand, $key_infos);
417
}
418

            
419
# Expand tag 'update'
420
sub expand_update_tag {
421
    my ($tag_name, $tag_args) = @_;
422
    my $original_keys = $tag_args;
423
    
424
    # Expanded tag
425
    my $expand = 'set ';
426
    
427
    # 
428
    foreach my $original_key (@$original_keys) {
429
        # Get table and clumn name
430
        my ($table, $column) = get_table_and_column($original_key);
431

            
432
        # Join key and placeholder
433
        $expand .= "$column = ?, ";
434
    }
435
    
436
    # Delete last ', '
437
    $expand =~ s/, $//;
438
    
439
    # Create parameter key infomations
440
    my $key_infos = [];
441
    foreach my $original_key (@$original_keys) {
442
        # Get table and clumn name
443
        my ($table, $column) = get_table_and_column($original_key);
444
        
445
        # Parameter key infomation
446
        my $key_info = {};
447
        
448
        # Original key
449
        $key_info->{original_key} = $original_key;
450
        
451
        # Table
452
        $key_info->{table}  = $table;
453
        
454
        # Column name
455
        $key_info->{column} = $column;
456
        
457
        # Access keys
458
        my $access_keys = [];
459
        push @$access_keys, ['#update', $original_key];
460
        push @$access_keys, ['#update', $table, $column] if $table && $column;
461
        push @$access_keys, [$original_key];
462
        push @$access_keys, [$table, $column] if $table && $column;
463
        $key_info->{access_keys} = $access_keys;
464
        
465
        # Add parameter key infos
466
        push @$key_infos, $key_info;
467
    }
468
    
469
    return ($expand, $key_infos);
470
}
471

            
472
1;
473

            
474
=head1 NAME
475

            
update document
yuki-kimoto authored on 2009-11-17
476
DBIx::Custom::SQL::Template - DBIx::Custom SQL Template
packaging one directory
yuki-kimoto authored on 2009-11-16
477

            
478
=head1 SYNOPSIS
479
    
480
    my $sql_tmpl = DBIx::Custom::SQL::Template->new;
481
    
482
    my $tmpl   = "select from table {= k1} && {<> k2} || {like k3}";
483
    my $param = {k1 => 1, k2 => 2, k3 => 3};
484
    
485
    my $query = $sql_template->create_query($tmpl);
486
    
487
    
488
    # Using query from DBIx::Custom
489
    use DBIx::Custom;
490
    my $dbi = DBI->new(
491
       data_source => $data_source,
492
       user        => $user,
493
       password    => $password, 
494
       dbi_options => {PrintError => 0, RaiseError => 1}
495
    );
496
    
497
    $query = $dbi->create_query($tmpl); # This is SQL::Template create_query
498
    $dbi->query($query, $param);
499

            
500
=head1 CLASS-OBJECT ACCESSORS
501

            
502
Class-Object accessor is used from both object and class
503

            
504
    $class->$accessor # call from class
505
    $self->$accessor  # call form object
506

            
507
=head2 tag_processors
508

            
509
    # Set and get
510
    $self           = $sql_tmpl->tag_processors($tag_processors);
511
    $tag_processors = $sql_tmpl->tag_processors;
512
    
513
    # Sample
514
    $sql_tmpl->tag_processors(
515
        '?' => \&expand_question,
516
        '=' => \&expand_equel
517
    );
518

            
519
You can use add_tag_processor to add tag processor
520

            
521
=head2 tag_start
522

            
523
    # Set and get
524
    $self      = $sql_tmpl->tag_start($tag_start);
525
    $tag_start = $sql_tmpl->tag_start;
526
    
527
    # Sample
528
    $sql_tmpl->tag_start('{');
529

            
530
Default is '{'
531

            
532
=head2 tag_end
533

            
534
    # Set and get
535
    $self    = $sql_tmpl->tag_start($tag_end);
536
    $tag_end = $sql_tmpl->tag_start;
537
    
538
    # Sample
539
    $sql_tmpl->tag_start('}');
540

            
541
Default is '}'
542
    
543
=head2 tag_syntax
544
    
545
    # Set and get
546
    $self       = $sql_tmpl->tag_syntax($tag_syntax);
547
    $tag_syntax = $sql_tmpl->tag_syntax;
548
    
549
    # Sample
550
    $sql_tmpl->tag_syntax(
551
        "[Tag]            [Expand]\n" .
552
        "{? name}         ?\n" .
553
        "{= name}         name = ?\n" .
554
        "{<> name}        name <> ?\n"
555
    );
556

            
557
=head1 METHODS
558

            
559
=head2 create_query
560
    
561
    # Create SQL form SQL template
562
    $query = $sql_tmpl->create_query($tmpl);
563
    
564
    # Sample
565
    $query = $sql_tmpl->create_sql(
566
         "select * from table where {= title} && {like author} || {<= price}")
567
    
568
    # Result
569
    $qeury->{sql} : "select * from table where title = ? && author like ? price <= ?;"
570
    $query->{key_infos} : [['title'], ['author'], ['price']]
571
    
572
    # Sample2 (with table name)
573
    ($sql, @bind_values) = $sql_tmpl->create_sql(
574
            "select * from table where {= table.title} && {like table.author}",
575
            {table => {title => 'Perl', author => '%Taro%'}}
576
        )
577
    
578
    # Result2
579
    $query->{sql} : "select * from table where table.title = ? && table.title like ?;"
580
    $query->{key_infos} :[ [['table.title'],['table', 'title']],
581
                           [['table.author'],['table', 'author']] ]
582

            
583
This method create query using by DBIx::Custom.
584
query is two infomation
585

            
586
    1.sql       : SQL
587
    2.key_infos : Parameter access key information
588

            
589
=head2 add_tag_processor
590

            
591
Add tag processor
592
  
593
    # Add
594
    $self = $sql_tmpl->add_tag_processor($tag_processor);
595
    
596
    # Sample
597
    $sql_tmpl->add_tag_processor(
598
        '?' => sub {
599
            my ($tag_name, $tag_args) = @_;
600
            
601
            my $key1 = $tag_args->[0];
602
            my $key2 = $tag_args->[1];
603
            
604
            my $key_infos = [];
605
            
606
            # Expand tag and create key informations
607
            
608
            # Return expand tags and key informations
609
            return ($expand, $key_infos);
610
        }
611
    );
612

            
613
Tag processor recieve 2 argument
614

            
615
    1. Tag name            (?, =, <>, or etc)
616
    2. Tag arguments       (arg1 and arg2 in {tag_name arg1 arg2})
617

            
618
Tag processor return 2 value
619

            
620
    1. Expanded Tag (For exsample, '{= title}' is expanded to 'title = ?')
621
    2. Key infomations
622
    
623
You must be return expanded tag and key infomations.
624

            
625
Key information is a little complex. so I will explan this in future.
626

            
627
If you want to know more, Please see DBIx::Custom::SQL::Template source code.
628

            
629
=head2 clone
630

            
631
    # Clone DBIx::Custom::SQL::Template object
632
    $clone = $self->clone;
633
    
634
=head1 Available Tags
635
    
636
    # Available Tags
637
    [tag]            [expand]
638
    {? name}         ?
639
    {= name}         name = ?
640
    {<> name}        name <> ?
641
    
642
    {< name}         name < ?
643
    {> name}         name > ?
644
    {>= name}        name >= ?
645
    {<= name}        name <= ?
646
    
647
    {like name}      name like ?
648
    {in name}        name in [?, ?, ..]
649
    
650
    {insert}  (key1, key2, key3) values (?, ?, ?)
651
    {update}     set key1 = ?, key2 = ?, key3 = ?
652
    
653
    # Sample1
654
    $query = $sql_tmpl->create_sql(
655
        "insert into table {insert key1 key2}"
656
    );
657
    # Result1
658
    $sql : "insert into table (key1, key2) values (?, ?)"
659
    
660
    
661
    # Sample2
662
    $query = $sql_tmpl->create_sql(
663
        "update table {update key1 key2} where {= key3}"
664
    );
665
    
666
    # Result2
667
    $query->{sql} : "update table set key1 = ?, key2 = ? where key3 = ?;"
668
    
669
=head1 AUTHOR
670

            
671
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
672

            
673
Github 
674
L<http://github.com/yuki-kimoto>
675
L<http://github.com/yuki-kimoto/DBIx-Custom-SQL-Template>
676

            
677
Please let know me bag if you find
678
Please request me if you want to do something
679

            
680
=head1 COPYRIGHT & LICENSE
681

            
682
Copyright 2009 Yuki Kimoto, all rights reserved.
683

            
684
This program is free software; you can redistribute it and/or modify it
685
under the same terms as Perl itself.
686

            
687

            
688
=cut
689

            
690
1; # End of DBIx::Custom::SQL::Template