Replace lazy_build with lazt + builder
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract.pm
CommitLineData
a0185af2 1use MooseX::Declare;
a0185af2 2
3
4class SQL::Abstract {
5
6 use Carp qw/croak/;
7 use Data::Dump qw/pp/;
8
9 use Moose::Util::TypeConstraints;
3e63a4d5 10 use MooseX::Types -declare => [qw/NameSeparator/];
0c371882 11 use MooseX::Types::Moose qw/ArrayRef Str Int HashRef CodeRef/;
4769c837 12 use MooseX::AttributeHelpers;
cbcfedc1 13 use SQL::Abstract::Types qw/NameSeparator AST ArrayAST/;
4769c837 14
c314b35d 15 clean;
a0185af2 16
a0185af2 17 our $VERSION = '2.000000';
18
19 our $AST_VERSION = '1';
20
3e63a4d5 21 # Operator precedence for bracketing
22 our %PRIO = (
23 and => 10,
24 or => 50
25 );
26
0bf8a8c4 27 our %BINOP_MAP = (
3e63a4d5 28 '>' => '>',
29 '<' => '<',
30 '==' => '=',
31 '!=' => '!=',
0bf8a8c4 32 # LIKE is always "field LIKE <value>"
33 '-like' => 'IN',
34 '-not_like' => 'NOT LIKE',
3e63a4d5 35 );
36
0bf8a8c4 37 has where_dispatch_table => (
38 is => 'ro',
eb22ecd3 39 lazy => 1,
40 builder => '_build_where_dispatch_table',
0c371882 41 isa => HashRef[CodeRef],
42 metaclass => 'Collection::ImmutableHash',
43 provides => {
44 get => 'lookup_where_dispatch'
45 }
0bf8a8c4 46 );
47
48 has binop_map => (
49 is => 'ro',
eb22ecd3 50 lazy => 1,
51 builder => '_build_binops',
0bf8a8c4 52 isa => HashRef,
53 metaclass => 'Collection::ImmutableHash',
54 provides => {
55 exists => 'is_valid_binop',
56 get => 'binop_mapping',
57 keys => 'binary_operators'
58 }
59 );
60
eb22ecd3 61 # List of default binary operators (for in where clauses)
62 sub _build_binops { return {%BINOP_MAP} };
0bf8a8c4 63
64 method _build_where_dispatch_table {
0c371882 65 my $binop = $self->can('_binop') or croak "InternalError: $self can't do _binop!";
0bf8a8c4 66 return {
67 map { $_ => $binop } $self->binary_operators
68 }
69 }
70
c314b35d 71 has ast_version => (
72 is => 'ro',
73 isa => Int,
74 required => 1
75 );
76
a0185af2 77 has name_separator => (
78 is => 'rw',
79 isa => NameSeparator,
80 default => sub { ['.'] },
81 coerece => 1,
82 required => 1,
83 );
84
85 has list_separator => (
86 is => 'rw',
87 isa => Str,
88 default => ', ',
89 required => 1,
90 );
91
4769c837 92 has binds => (
93 isa => ArrayRef,
5bf8c024 94 is => 'ro',
0bf8a8c4 95 clearer => '_clear_binds',
96 lazy => 1,
4769c837 97 default => sub { [ ] },
98 metaclass => 'Collection::Array',
99 provides => {
100 push => 'add_bind',
4769c837 101 }
102 );
103
14774be0 104 # TODO: once MXMS supports %args, use that here
105 method create(ClassName $class: Int $ver) {
106 croak "AST version $ver is greater than supported version of $AST_VERSION"
107 if $ver > $AST_VERSION;
108
109 my $name = "${class}::AST::v$ver";
110 Class::MOP::load_class($name);
111
112 return $name->new(ast_version => $ver);
c314b35d 113 }
114
115 # Main entry point
cbcfedc1 116 method generate(ClassName $class: AST $ast) {
c314b35d 117 croak "SQL::Abstract AST version not specified"
118 unless ($ast->[0] eq '-ast_version');
119
120 my (undef, $ver) = splice(@$ast, 0, 2);
121
14774be0 122 # TODO: once MXMS supports %args, use that here
123 my $self = $class->create($ver);
c314b35d 124
125 return ($self->dispatch($ast), $self->binds);
126 }
127
0bf8a8c4 128 method reset() {
129 $self->_clear_binds();
130 }
131
cbcfedc1 132 method dispatch (AST $ast) {
133 # I want multi methods!
134 my $tag;
135 if (is_ArrayAST($ast)) {
136 ($tag = $ast->[0]) =~ s/^-/_/;
137 } else {
138 $tag = "_" . $ast->{-type};
139 }
0bf8a8c4 140
cbcfedc1 141 my $meth = $self->can($tag) || croak "Unknown tag '$tag'";
0bf8a8c4 142 return $meth->($self, $ast);
143 }
4769c837 144
a0185af2 145};