NAME

SQL::Simple - A very simple SQL-building library

VERSION

Version 0.01

SYNOPSIS

my ($sql,$bind) = sql_select( 'users', [ 'name', 'status' ], { status => [ 'Deleted', 'Inactive' ] );

my ($sql,$bind) = sql_insert( 'users', { name => 'Dave', status => 'Active' } );

my ($sql,$bind) = sql_update( 'users', { status => 'Inactive' }, { password => undef } );

my ($sql,$bind) = sql_delete( 'users', { status => 'Inactive' } );

DOCUMENTATION

A very simple SQL-building library. It's not for all your SQL needs, only the very simple ones.

It doesn't handle JOINs. It doesn't handle GROUP BY. It doens't handle subselects. It's only for simple SQL.

In my test suites, I have a lot of ad hoc SQL queries, and it drives me nuts to have so much SQL code lying around. SQL::Simple is for generating SQL code for simple cases.

I'd far rather have:

my ($sql,$binds) = sql_insert( 'users', { name => 'Dave', status => 'Active' } );

than hand-coding:

my $sql   = 'INSERT INTO users (name,status) VALUES (:name,:status)';
my $binds = { ':name' => 'Dave', ':status' => 'Active' };

or even the positional:

my $sql   = 'INSERT INTO users (name,status) VALUES (?,?)';
my $binds = [ 'Dave', 'Active' ];

You don't want to use SQL::Simple where speed is essential.

EXPORT

All subs can be exported, but none are by default.

SUBROUTINES/METHODS

sql_select( $table, \@columns, \%where [, \%other ] )

Creates simple SELECTs and binds.

Calling:

my ($sql,$binds) = sql_select(
    'users',
    [qw( userid name )],
    { status => 'X' ],
    { order_by => 'name' },
);

returns:

$sql   = 'SELECT userid,name FROM users WHERE status=? ORDER BY name';
$binds = [ 'X' ];

sql_insert( $table, \%values )

Creates simple INSERTs and binds.

Calling:

my ($sql,$binds) = sql_insert(
    'users',
    {
        serialno   => '12345',
        name       => 'Dave',
        rank       => 'Sergeant',
        height     => undef,
        date_added => \'SYSDATE()',
    }
);

returns:

$sql   = 'INSERT INTO users (date_added,height,name,rank,serialno) VALUES (SYSDATE(),NULL,?,?,?)';
$binds = [ 'Dave', 'Sergeant', 12345 ]

sql_update( $table, \%values, \%where )

Creates simple UPDATE calls and binds.

Calling:

my ($sql,$binds) = sql_update(
    'users',
    {
        status     => 'X',
        lockdate   => undef,
    },
    {
        orderdate => \'SYSDATE()',
    },
);

returns:

$sql   = 'UPDATE users SET lockdate=NULL, status=? WHERE orderdate=SYSDATE()'
$binds = [ 'X' ]

sql_delete( $table, \%where )

Creates simple DELETE calls and binds.

Calling:

my ($sql,$binds) = sql_delete(
    'users',
    {
        serialno   => 12345,
        height     => undef,
        date_added => \'SYSDATE()',
        status     => [qw( X Y Z )],
    },
);

returns:

$sql   = 'DELETE FROM users WHERE date_added = SYSDATE() AND height IS NULL AND serialno = ? AND status IN (?,?,?)'
$binds = [ 12345, 'X', 'Y', 'Z' ]

AUTHOR

Andy Lester, <andy at petdance.com>

BUGS

Please report any bugs or feature requests to bug-sql-simple at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=SQL-Simple. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc SQL::Simple

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2019 Andy Lester.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0