package SQL::Composer; use strict; use warnings; our $VERSION = '0.19'; use base 'Exporter'; our @EXPORT_OK = qw(sql_select sql_insert sql_delete sql_update sql_upsert); our %EXPORT_TAGS = (funcs => [qw(sql_select sql_insert sql_delete sql_update sql_upsert)]); require Carp; use SQL::Composer::Select; use SQL::Composer::Insert; use SQL::Composer::Delete; use SQL::Composer::Update; use SQL::Composer::Upsert; $Carp::Internal{(__PACKAGE__)}++; $Carp::Internal{"SQL::Composer::$_"}++ for qw/ Select Insert Delete Update Upsert Expression Join Quoter /; sub build { my $class = shift; my ($name) = shift; my $class_name = 'SQL::Composer::' . ucfirst($name); return $class_name->new(@_); } sub sql_select { build(__PACKAGE__, 'select', @_) } sub sql_insert { build(__PACKAGE__, 'insert', @_) } sub sql_update { build(__PACKAGE__, 'update', @_) } sub sql_delete { build(__PACKAGE__, 'delete', @_) } sub sql_upsert { build(__PACKAGE__, 'upsert', @_) } 1; __END__ =pod =head1 NAME SQL::Composer - sql builder =head1 SYNOPSIS use DBI; my $select = SQL::Composer->build('select', from => 'book_description', columns => ['description'], join => [ { source => 'book', columns => ['title'], on => ['book_description.book_id' => {-col => 'book.id'}], join => [ { source => 'author', columns => ['name'], on => ['book.author_id' => {-col => 'author.id'}] } ] } ] ); my $sql = $select->to_sql; my @bind = $select->to_bind; my $dbh = DBI->connect(...); my $sth = dbh->prepare($sql); $sth->execute(@bind); my $rows = $sth->fetchall_arrayref; my $objects = $select->from_rows($rows); # $objects = [ # description => 'Nice Book', # book => { # title => 'My Book', # author => { # name => 'Author' # } # } # ] =head1 DESCRIPTION L is a SQL builder and rows parser in one module. It allows deep joins and automatic convertion from arrayref to a hashref, keeping the nested join structure if needed. This module itself is just a factory for the common SQL statements: C