#!/usr/local/bin/perl

#   A Data::All as database loader example

use strict;
use lib $ENV{'SBLIBPERL'};

use Data::All;
use Data::Dumper;


sub main()
{
    #load_headgroups();
    #load_customers();
    #load_headings();
    #load_keywords();
    #load_newinstalls();
}

sub read_headings()
{
    my $dsn = 'DBI:mysql:database=cats;host=YOURHOST;port=3306';
    my $qry = 'SELECT * FROM categories';
    my %heading_db  =
    (
        path        => [$dsn, 'user', 'pass', $qry],
        ioconf      => ['db', 'r' ]
    );
    
    my $headings = Data::All->new(%heading_db);
    $headings->open();
    return $headings->read(); 
}

sub load_newinstalls()
{
    my $dsn = 'DBI:mysql:database=cats;host=YOURHOST;port=3306';
    my $qry = 'INSERT INTO new_installs (prov,phone_areacode,phone_number,name,title,heading) VALUES(?,?,?,?,?,?)';

    my %file = (path => 'data/NewListings-March9.csv', profile => 'csv');
    my %db  = (path => [$dsn, 'user', 'pass', $qry], ioconf => ['db', 'w' ]);
    
    load(\%file, \%db);
}

sub load_keywords()
{
    my $dsn = 'DBI:mysql:database=cats;host=YOURHOST;port=3306';
    my $qry = 'INSERT INTO keywords (Keyword,Description,Head_Id,HeadGroup_Id,Language,original) VALUES(?,?,?,?,?,?)';

    my %file1 = (path => 'data/heading_master-march152-keywords.csv', profile => 'csv', ioconf => {with_original => 1});
    my %db  = (path => [$dsn, 'user', 'pass', $qry], ioconf => ['db', 'w', '1'], fields => [qw(Keyword Description Head_Id HeadGroup_Id Language), '_ORIGINAL']);
    
    load(\%file1, \%db);
}

sub load_customers()
{
    my $dsn = 'DBI:mysql:database=cats;host=YOURHOST;port=3306';
    my $qry = 'INSERT INTO customers (Name3,IndexBy,Head_Id,CustStatus_Id,LastModDt,LastModBy) VALUES(?,?,?,?,?,?)';

    my %file = (path => 'data/03-08-04_PROD_Customer.txt', profile => 'csv');
    my %db  = (path => [$dsn, 'user', 'pass', $qry], ioconf => ['db', 'w' ]);
    
    load(\%file, \%db);
}


sub load_headgroups()
{
    my $dsn = 'DBI:mysql:database=cats;host=YOURHOST;port=3306';
    my $qry = 'INSERT INTO heading_groups (Headgroup_Id,Head_Id,EffectiveDt,ObsoleteDt,LastModDt,LastModBy) VALUES(?,?,?,?,?,?)';

    my %file = (path => 'data/03-09-04_PROD_HeadGroupHeading.txt', profile => 'csv');
    my %db  = (path => [$dsn, 'user', 'pass', $qry], ioconf => ['db', 'w' ]);
    
    load(\%file, \%db);
}

sub load_headings()
{
    my $dsn = 'DBI:mysql:database=cats;host=YOURHOST;port=3306';
    my $qry = 'INSERT INTO categories (Head_Id,Description,Short_Description,IndexBy,LastModDt,LastModBy) VALUES(?,?,?,?,?,?)';
    
    my %file = (path => 'data/03-09-04_EAST_Heading.txt', profile => 'csv');
    my %db  = (path => [$dsn, 'user', 'pass', $qry], ioconf => ['db', 'w' ]);
    
    load(\%file, \%db);
}

sub load()
{
    my $input  = shift;
    my $output = shift;
    
    #my $cats = collection($input);
    #print Dumper($cats);
    
    my $cat = Data::All->new(%{ $input });    
    $cat->convert($output); 
}


main();
