#!/usr/bin/env perl

use strict;
use warnings;
use Pod::Usage;
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat);

use Data::Generator::FromDDL;
use Data::Generator::FromDDL::Util;

sub parse_num_option {
    # $num_option_str is like 'users:20,bugs:30,100'
    my $num_option_str = shift;
    my @nums = split ',', $num_option_str;

    my $parsed = {
        all => undef,
        tables => {},
    };
    for (@nums) {
        my ($table, $n) = split ':', $_;
        if ($n) {
            $parsed->{tables}{$table} = $n;
        } else {
            # $table contains number
            $parsed->{all} = $table;
        }
    }
    return $parsed;
}

sub read_ddl {
    my $ddl_files = shift;
    local $/;
    if (@$ddl_files) {
        # read from multiple files
        my $ddl_str = '';
        for my $ddl_file (@$ddl_files) {
            open my $fh, '<', $ddl_file
                or die("Can't open $ddl_file to read\n");
            $ddl_str .= <$fh>;
        }
        return $ddl_str;
    } else {
        return <STDIN>;
    }
}

sub main {
    my $help;
    my $n;
    my $parser = 'mysql';
    my $include = '';
    my $exclude = '';
    my $out;
    my $format = 'sql';
    my $pretty;
    GetOptions(
        "help|h" => \$help,
        "num|n=s" => \$n,
        "parser|p=s" => \$parser,
        "include|i=s" => \$include,
        "exclude|e=s" => \$exclude,
        "out|o=s" => \$out,
        "format|f=s" => \$format,
        "pretty" => \$pretty,
    ) or pod2usage(2);

    pod2usage({
        -exitval => 0,
        -verbose => 99,
        -noperldoc => 1,
        -sections => 'SYNOPSIS|OPTIONS',
    }) if $help;
    pod2usage({
        -message => "Can't specify both of --include and --exclude options",
        -exitval => 1,
        -verbose => 99,
        -noperldoc => 1,
        -sections => 'SYNOPSIS|OPTIONS',
    }) if $include && $exclude;

    my $ddl = read_ddl(\@ARGV);

    my $out_fh;
    if ($out) {
        open $out_fh, '>', $out
            or die("Can't open $out to write\n");
    } else {
        $out_fh = *STDOUT;
    }

    my @include = split ',', $include;
    my @exclude = split ',', $exclude;
    my $num = parse_num_option($n);

    my $generator = Data::Generator::FromDDL->new({
        ddl => $ddl,
        parser => $parser,
        include => \@include,
        exclude => \@exclude,
    });
    $generator->generate($num, $out_fh, $format, $pretty);
}

&main();

__END__

=encoding utf-8

=head1 NAME

datagen_from_ddl - dummy data generator from DDL statements

=head1 SYNOPSIS

    $ datagen_from_ddl [options] your_ddl.sql
    $ datagen_from_ddl --num=users:10,100 --include=users,blogs --format=sql --pretty your_ddl.sql

=head1 OPTIONS

=over 4

=item B<-n|--num>

Number of records generated.

Example:

    --num=20 (20 records for all tables)
    --num=users:10,100 (10 records for users and 100 records for other tables)

=item B<-p|--parser (default: MySQL)>

Parser for DDL. Choices are MySQL, SQLite, Oracle, or PostgreSQL.

=item B<-i|--include>

Only tables which are specified this option are processed.

=item B<-e|--exclude>

Tables which are specified this option are ignored(--include and --exclude options are exclusively used).

=item B<-o|--out>

Output file.

=item B<-f|--format (default: SQL)>

Output format. Choices are SQL, JSON, or YAML.

=item B<--pretty>

Print output prettily.

=back

=cut

