File Coverage

blib/lib/SQL/DMLGenerator.pm
Criterion Covered Total %
statement 36 36 100.0
branch 3 6 50.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 50 53 94.3


line stmt bran cond sub pod time code
1             package SQL::DMLGenerator;
2              
3 2     2   9799 use strict;
  2         4  
  2         90  
4 2     2   12 use warnings;
  2         6  
  2         66  
5 2     2   11 use vars qw($VERSION);
  2         5  
  2         101  
6              
7 2     2   11 use Abstract::Meta::Class ':all';
  2         5  
  2         403  
8 2     2   15 use SQL::Entity::Condition;
  2         4  
  2         937  
9              
10             $VERSION = 0.01;
11              
12             =head1 NAME
13              
14             SQL::DMLGenerator - Data Manipulation Language SQL generator.
15              
16             =cut
17              
18             =head1 SYNOPSIS
19              
20             use SQL::DMLGenerator;
21              
22             =head1 DESCRIPTION
23              
24             Represent DML SQL generator.(insert/update/delete)
25              
26             =head1 EXPORT
27              
28             None
29              
30             =head2 METHODS
31              
32             =over
33              
34             =item insert
35              
36             Returns insert sql statements, bind variables as array ref.
37             Takes entity object, filed values as hash ref
38              
39             my ($sql, $bind_variables) = SQL::DMLGenerator->insert($entity, $field_values)
40              
41             =cut
42              
43             sub insert {
44 2     2 1 5 my ($class, $entity, $field_values) = @_;
45 2         13 my @fields = sort keys %$field_values;
46 2         8 my $sql = sprintf "INSERT INTO %s (%s) VALUES (%s)",
47             $entity->name, join(",", @fields), join(",", ("?")x @fields);
48 2         29 ($sql, [map {$field_values->{$_}} @fields]);
  6         23  
49             }
50              
51              
52             =item update
53              
54             Returns update sql statements, bind variables as array ref.
55             Takes entity object, filed values as hash ref, condition - that may be hash ref or condition object
56              
57             my ($sql, $bind_variables) = SQL::DMLGenerator->update($entity, $field_values, $codition)
58              
59             =cut
60              
61             sub update {
62 2     2 1 4 my ($class, $entity, $field_values, $conditions) = @_;
63 2         16 my @fields = sort keys %$field_values;
64 2 50       7 return () unless @fields;
65 2 50       15 my $condition = ref($conditions) eq 'SQL::Entity::Condition'
66             ? $conditions
67             : SQL::Entity::Condition->struct_to_condition($entity->normalise_field_names(%$conditions));
68 2         6 my $bind_variables = [];
69 2         8 my $where_clause = $condition->as_string({}, $bind_variables);
70 6         36 my $sql = sprintf "UPDATE %s SET %s WHERE %s",
71             $entity->name,
72 2         11 join (", ", map { $_ . ' = ?' } @fields),
73             $where_clause;
74 2         5 ($sql, [(map {$field_values->{$_}} @fields), @$bind_variables]);
  6         28  
75             }
76              
77              
78             =item delete
79              
80             Returns delete sql statements, bind variables as array ref.
81             Takes entity object, filed values as hash ref, condition - that may be hash ref or condition object
82              
83             my ($sql, $bind_variables) = SQL::DMLGenerator->delete($entity, $codition);
84              
85             =cut
86              
87             sub delete {
88 2     2 1 9 my ($class, $entity, @args) = @_;
89 2 50       366 my $condition = @args == 1
90             ? $args[0]
91             : SQL::Entity::Condition->struct_to_condition($entity->normalise_field_names(@args));
92 2         5 my $bind_variables = [];
93 2         374 my $where_clause = $condition->as_string({}, $bind_variables);
94 2         25 my $sql = sprintf "DELETE FROM %s WHERE %s",
95             $entity->name,
96             $where_clause;
97 2         50 ($sql, $bind_variables);
98             }
99              
100              
101             1;
102              
103             __END__