Newer Older
116 lines | 2.629kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
package DBIx::Custom::Query;
2
use Object::Simple -base;
3

            
4
use Carp 'croak';
5
use DBIx::Custom::Util '_subname';
6

            
7
has 'sth',
8
    sql => '',
9
    columns => sub { [] };
10

            
11
# DEPRECATED!
12
has 'default_filter';
13
sub filters {
14
    warn "DBIx::Custom::Query filters attribute method is DEPRECATED!";
15
    my $self = shift;
16
    if (@_) {
17
        $self->{filters} = $_[0];
18
        return $self;
19
    }
20
    return $self->{filters};
21
}
22

            
23
# DEPRECATED!
24
sub tables {
25
    warn "DBIx::Custom::Query tables attribute method is DEPRECATED!";
26
    my $self = shift;
27
    if (@_) {
28
        $self->{tables} = $_[0];
29
        return $self;
30
    }
31
    return $self->{tables} ||= [];
32
}
33

            
34
#DEPRECATED!
35
sub filter {
36
    Carp::carp "DBIx::Custom::Query filter method is DEPRECATED!";
37
    my $self = shift;
38
    if (@_) {
39
        my $filter = {};
40
        if (ref $_[0] eq 'HASH') {
41
            $filter = $_[0];
42
        }
43
        else {
44
            my $ef = @_ > 1 ? [@_] : $_[0];
45
            for (my $i = 0; $i < @$ef; $i += 2) {
46
                my $column = $ef->[$i];
47
                my $f = $ef->[$i + 1];
48
                if (ref $column eq 'ARRAY') {
49
                    foreach my $c (@$column) {
50
                        $filter->{$c} = $f;
51
                    }
52
                }
53
                else {
54
                    $filter->{$column} = $f;
55
                }
56
            }
57
        }
58
        foreach my $column (keys %$filter) {
59
            my $fname = $filter->{$column};
60
            if  (exists $filter->{$column}
61
              && defined $fname
62
              && ref $fname ne 'CODE') 
63
            {
64
                my $filters = $self->{filters} || {};
65
                croak qq{Filter "$fname" is not registered" } . _subname
66
                  unless exists $filters->{$fname};
67
                $filter->{$column} = $filters->{$fname};
68
            }
69
        }
70
        $self->{filter} = {%{$self->{filter} || {}}, %$filter};
71
        return $self;
72
    }
73
    return $self->{filter} ||= {};
74
}
75

            
76
1;
77

            
78
=head1 NAME
79

            
80
DBIx::Custom::Query - Query
81

            
82
=head1 SYNOPSIS
83
    
84
    my $query = DBIx::Custom::Query->new;
85
    my $sth = $query->sth;
86
    my $sql = $query->sql;
87
    my $columns = $query->columns;
88
    
89
=head1 ATTRIBUTES
90

            
91
=head2 C<columns>
92

            
93
    my $columns = $query->columns;
94
    $query      = $query->columns(['auhtor', 'title']);
95

            
96
Column names.
97

            
98
=head2 C<sql>
99

            
100
    my $sql = $query->sql;
101
    $query  = $query->sql('select * from books where author = ?;');
102

            
103
SQL statement.
104

            
105
=head2 C<sth>
106

            
107
    my $sth = $query->sth;
108
    $query  = $query->sth($sth);
109

            
110
Statement handle of L<DBI>
111

            
112
=head1 METHODS
113

            
114
L<DBIx::Custom::Query> inherits all methods from L<Object::Simple>.
115

            
116
=cut