Yuki Kimoto cleanup
d71f127 13 years ago
2 contributor
111 lines | 2.373kb
  1. package DBIx::Custom::Query;
  2. use Object::Simple -base;
  3.  
  4. use Carp 'croak';
  5. use DBIx::Custom::Util '_subname';
  6.  
  7. has [qw/sth filters/],
  8. sql => '',
  9. tables => sub { [] },
  10. columns => sub { [] };
  11.  
  12. sub filter {
  13. my $self = shift;
  14. if (@_) {
  15. my $filter = {};
  16. if (ref $_[0] eq 'HASH') {
  17. $filter = $_[0];
  18. }
  19. else {
  20. my $ef = @_ > 1 ? [@_] : $_[0];
  21. for (my $i = 0; $i < @$ef; $i += 2) {
  22. my $column = $ef->[$i];
  23. my $f = $ef->[$i + 1];
  24. if (ref $column eq 'ARRAY') {
  25. foreach my $c (@$column) {
  26. $filter->{$c} = $f;
  27. }
  28. }
  29. else {
  30. $filter->{$column} = $f;
  31. }
  32. }
  33. }
  34. foreach my $column (keys %$filter) {
  35. my $fname = $filter->{$column};
  36.  
  37. if (exists $filter->{$column}
  38. && defined $fname
  39. && ref $fname ne 'CODE')
  40. {
  41. croak qq{Filter "$fname" is not registered" } . _subname
  42. unless exists $self->filters->{$fname};
  43. $filter->{$column} = $self->filters->{$fname};
  44. }
  45. }
  46. $self->{filter} = {%{$self->filter}, %$filter};
  47. return $self;
  48. }
  49. return $self->{filter} ||= {};
  50. }
  51.  
  52. # DEPRECATED!
  53. has 'default_filter';
  54.  
  55. 1;
  56.  
  57. =head1 NAME
  58.  
  59. DBIx::Custom::Query - Query
  60.  
  61. =head1 SYNOPSIS
  62. my $query = DBIx::Custom::Query->new;
  63. =head1 ATTRIBUTES
  64.  
  65. =head2 C<columns>
  66.  
  67. my $columns = $query->columns;
  68. $query = $query->columns(['auhtor', 'title']);
  69.  
  70. Column names.
  71.  
  72. =head2 C<filter>
  73.  
  74. my $filter = $query->filter;
  75. $query = $query->filter(author => 'to_something',
  76. title => 'to_something');
  77.  
  78. $query = $query->filter([qw/author title/] => 'to_something');
  79.  
  80. Filters when parameter binding is executed.
  81.  
  82. =head2 C<sql>
  83.  
  84. my $sql = $query->sql;
  85. $query = $query->sql('select * from books where author = ?;');
  86.  
  87. SQL statement.
  88.  
  89. =head2 C<sth>
  90.  
  91. my $sth = $query->sth;
  92. $query = $query->sth($sth);
  93.  
  94. Statement handle of L<DBI>
  95.  
  96. =head1 METHODS
  97.  
  98. L<DBIx::Custom::Query> inherits all methods from L<Object::Simple>.
  99.  
  100. =cut