Add IN generator
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract.pm
index e835c6e..5026129 100644 (file)
@@ -55,21 +55,25 @@ class SQL::Abstract {
 
   has binds => (
     isa => ArrayRef,
+    is => 'ro',
     default => sub { [ ] },
     metaclass => 'Collection::Array',
     provides => {
       push => 'add_bind',
-      get => 'binds'
+      clear => '_clear_binds',
     }
   );
 
   method generate (Object|ClassName $self: ArrayRef $ast) {
-    $self = $self->new unless blessed($self);
+    my $class_meth = !blessed($self);
+    $self = $self->new if $class_meth;
 
     local $_ = $ast->[0];
     s/^-/_/g or croak "Unknown type tag '$_'";
     my $meth = $self->can($_) || \&_generic_func;
-    return $meth->($self, $ast);
+    return $class_meth
+         ? ($meth->($self, $ast), $self->binds)
+         : $meth->($self, $ast);
   }
 
   method _select(ArrayRef $ast) {
@@ -179,6 +183,15 @@ class SQL::Abstract {
     );
   }
 
+  method _in($ast) {
+    my (undef, $field, @values) = @$ast;
+
+    return $self->generate($field) .
+           " IN (" .
+           join(", ", map { $self->generate($_) } @values ) .
+           ")";
+  }
+
   method _generic_func(ArrayRef $ast) {
   }