use v5.12;
use warnings;
use Data::Dump qw/pp dd/;

BEGIN {
    do "./overload4.pl";
    *hmp = \&HMP::hmp;
}

use DBI;

# warn pp
my @driver_names = DBI->available_drivers;

my %drivers      = DBI->installed_drivers;

# warn pp  \@driver_names;


 # warn pp
 #  my @data_sources = DBI->data_sources('SQLite');

my $driver = 'mysql';
my $db     = 'perl_test';
my $host   = '127.0.0.1';
my $port   = '3306';


my $dsn = "dbi:$driver:database=$db;host=$host;port=$port";
my $user = 'perl_test';
my $pwd = 'perl_test';


if(1) {                                 # SQLite
    $dsn ="dbi:SQLite:dbname=:memory:";
    $user = "";
    $pwd = "";
}


my $dbh = DBI->connect($dsn, $user, $pwd,
                       { RaiseError => 1, AutoCommit => 0 });

# warn pp $dbh;


# ====== run tests
my $first = 'rolf';
my $last  = 'lanx';
#{ key => 'LanX' };


if(0) {
    warn pp 'classic(): ', classic($first);
    warn pp 'placeholder(): ', placeholder($first);
}

warn pp 'wanted(): ', wanted($first,$last);


sub classic {
    my ( $firstname ) = @_ ;

    my $sth = $dbh->prepare(<<"__SQL__");
SELECT *
    FROM users
    WHERE firstname = '$firstname'
;
__SQL__

    my $rv = $sth->execute();

    my $hash_ref = $sth->fetchall_hashref( 'id' );

    return $hash_ref;
}


sub placeholder {
    my ( $firstname ) = @_ ;

    my $sth = $dbh->prepare(<<"__SQL__");
SELECT *
    FROM users
    WHERE firstname = ?
;
__SQL__

    my $rv = $sth->execute($firstname);

    my $hash_ref = $sth->fetchall_hashref( 'id' );

    return $hash_ref;
}


sub wanted {
    my ( $first, $last ) = @_ ;

    my $hmp = hmp {
        return  <<"__SQL__";
SELECT *
    FROM users
    WHERE First = $first
      AND Last  = $last
;
__SQL__


    };


    #warn pp '$hmp: ', $hmp;
    #return $hmp;
    

    exit;

    my $sth = $dbh->prepare(  );

    my $rv = $sth->execute();

    my $hash_ref = $sth->fetchall_hashref( 'id' );

    return $hash_ref;
}


$dbh->disconnect();


