SQL::DMLGenerator - Data Manipulation Language SQL generator.


SQL-Entity documentation Contained in the SQL-Entity distribution.

Index


Code Index:

NAME

Top

SQL::DMLGenerator - Data Manipulation Language SQL generator.

SYNOPSIS

Top

    use SQL::DMLGenerator;

DESCRIPTION

Top

Represent DML SQL generator.(insert/update/delete)

EXPORT

Top

None

METHODS

insert

Returns insert sql statements, bind variables as array ref. Takes entity object, filed values as hash ref

    my ($sql, $bind_variables) = SQL::DMLGenerator->insert($entity, $field_values)

update

Returns update sql statements, bind variables as array ref. Takes entity object, filed values as hash ref, condition - that may be hash ref or condition object

    my ($sql, $bind_variables) = SQL::DMLGenerator->update($entity, $field_values, $codition)

delete

Returns delete sql statements, bind variables as array ref. Takes entity object, filed values as hash ref, condition - that may be hash ref or condition object

    my ($sql, $bind_variables) = SQL::DMLGenerator->delete($entity, $codition);

SEE ALSO

Top

SQL::Entity

COPYRIGHT AND LICENSE

Top

AUTHOR

Top

Adrian Witas, adrian@webapp.strefa.pl


SQL-Entity documentation Contained in the SQL-Entity distribution.
package SQL::DMLGenerator;

use strict;
use warnings;
use vars qw($VERSION);

use Abstract::Meta::Class ':all';
use SQL::Entity::Condition;

$VERSION = 0.01;

sub insert {
    my ($class, $entity, $field_values) = @_;
    my @fields = sort keys %$field_values;
    my $sql = sprintf "INSERT INTO %s (%s) VALUES (%s)",
        $entity->name, join(",", @fields), join(",", ("?")x @fields);
    ($sql, [map {$field_values->{$_}} @fields]);
}


sub update {
    my ($class, $entity, $field_values, $conditions) = @_;
    my @fields = sort keys %$field_values;
    return () unless @fields;
    my $condition = ref($conditions) eq 'SQL::Entity::Condition'
        ? $conditions
        : SQL::Entity::Condition->struct_to_condition($entity->normalise_field_names(%$conditions));
    my $bind_variables = [];
    my $where_clause = $condition->as_string({}, $bind_variables);
    my $sql = sprintf "UPDATE %s SET %s WHERE %s",
        $entity->name,
        join (", ", map { $_ . ' = ?' } @fields),
        $where_clause;
    ($sql, [(map {$field_values->{$_}} @fields), @$bind_variables]);
}


sub delete {
    my ($class, $entity, @args) = @_;
    my $condition = @args == 1 
        ? $args[0]
        : SQL::Entity::Condition->struct_to_condition($entity->normalise_field_names(@args));
    my $bind_variables = [];
    my $where_clause = $condition->as_string({}, $bind_variables);    
    my $sql = sprintf "DELETE FROM %s WHERE %s",
        $entity->name,
        $where_clause;
    ($sql, $bind_variables);
}


1;

__END__

1;