#!/usr/bin/env perl

use strict;
use warnings;
use Data::Compare;
use Data::Dumper;
use OPTiMaDe::Filter::Parser;
use Scalar::Util qw(blessed);

$Data::Dumper::Sortkeys = 1;

my %options = map { $_ => 1 } grep { /^--/ } @ARGV;
@ARGV = grep { !/^--/ } @ARGV;

if( $options{'--allow-LIKE-operator'} ) {
    $OPTiMaDe::Filter::Parser::allow_LIKE_operator = 1;
}

my $use_placeholders = 0;
if( $options{'--use-placeholders'} ) {
    $use_placeholders = 1;
}

@ARGV = ( '-' ) unless @ARGV;

for (@ARGV) {
    my $parser = new OPTiMaDe::Filter::Parser;
    my $tree = $parser->Run( $_ );
    print Dumper $tree;
    print "== Filter ==\n";
    print $tree->to_filter . "\n";
    print "== SQL ==\n";
    if( $use_placeholders ) {
        my( $sql, $values ) = $tree->to_SQL( { placeholder => '?' } );
        print "$sql\n";
        print "=== Values ===\n";
        print Dumper $values;
    } else {
        print $tree->to_SQL . "\n";
    }

    my $filter = $tree->to_filter;
    $parser = new OPTiMaDe::Filter::Parser;
    my $tree_now = $parser->parse_string( $filter );
    Compare( $tree, $tree_now ) || print "Roundtrip NOT passed\n";
}