Showing 2 changed files with 66 additions and 13 deletions
+36 -2
lib/DBI/Custom.pm
... ...
@@ -149,11 +149,32 @@ sub run_tranzaction {
149 149
     $self->_auto_commit(1);
150 150
 }
151 151
 
152
+sub prepare {
153
+    my ($self, $sql) = @_;
154
+    eval{$self->connect unless $self->connected};
155
+    croak($@) if $@;
156
+    
157
+    my $sth = eval{$self->dbh->prepare($sql)};
158
+    croak($@) if $@;
159
+    return $sth;
160
+}
161
+
162
+sub do{
163
+    my ($self, $sql, @bind_values) = @_;
164
+    eval{$self->connect unless $self->connected};
165
+    croak($@) if $@;
166
+    
167
+    eval{$self->dbh->do($sql, @bind_values)};
168
+    croak($@) if $@;
169
+}
170
+
152 171
 sub create_query {
153 172
     my ($self, $template) = @_;
154 173
     
155 174
     # Create query from SQL template
156
-    my $query = $self->sql_template->create_query($template);
175
+    my $sql_template = $self->sql_template;
176
+    my $query = eval{$sql_template->create_query($template)};
177
+    croak($@) if $@;
157 178
     
158 179
     # Create Query object;
159 180
     $query = DBI::Custom::Query->new($query);
... ...
@@ -162,7 +183,8 @@ sub create_query {
162 183
     $self->connect unless $self->connected;
163 184
     
164 185
     # Prepare statement handle
165
-    my $sth = $self->dbh->prepare($query->{sql});
186
+    my $sth = eval{$self->dbh->prepare($query->{sql})};
187
+    croak($@) if $@;
166 188
     
167 189
     # Set statement handle
168 190
     $query->sth($sth);
... ...
@@ -344,6 +366,18 @@ Version 0.0101
344 366
 
345 367
 dbi_options is used when you connect database by using connect.
346 368
 
369
+=head2 prepare
370
+
371
+    $sth = $dbi->prepare($sql);
372
+
373
+This method is same as DBI::prepare
374
+
375
+=head2 do
376
+
377
+    $dbi->do($sql, @bind_values);
378
+
379
+This method is same as DBI::do
380
+
347 381
 =head2 sql_template
348 382
 
349 383
     # Set and get SQL::Template object
+30 -11
t/02-sqlite.t
... ...
@@ -20,6 +20,10 @@ sub test {
20 20
 }
21 21
 
22 22
 # Varialbes for test
23
+our $CREATE_TABLE = {
24
+    0 => 'create table table1 (key1 char(255), key2 char(255));'
25
+};
26
+
23 27
 my $dbi;
24 28
 my $sth;
25 29
 my $tmpl;
... ...
@@ -33,14 +37,15 @@ my $rows;
33 37
 my $query;
34 38
 my $select_query;
35 39
 my $insert_query;
40
+my $ret_val;
36 41
 
37 42
 
38 43
 
39 44
 # Prepare table
40 45
 $dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
41 46
 $dbi->connect;
42
-$dbi->dbh->do("create table table1 (key1 char(255), key2 char(255))");
43
-$sth = $dbi->dbh->prepare("insert into table1 (key1, key2) values (?, ?);");
47
+$dbi->do($CREATE_TABLE->{0});
48
+$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
44 49
 $sth->execute(1, 2);
45 50
 $sth->execute(3, 4);
46 51
 
... ...
@@ -104,22 +109,36 @@ __END__
104 109
 
105 110
 test 'Filter';
106 111
 $dbi->reconnect;
107
-$dbi->dbh->do("create table table1 (key1 char(255), key2 char(255));");
112
+$dbi->dbh->do($CREATE_TABLE->{0});
108 113
 
109
-$tmpl = "insert into {insert_values key1 key2};";
110
-$query = $dbi->create_query($tmpl);
111
-$query->bind_filter(sub {
114
+$insert_tmpl  = "insert into table1 {insert_values key1 key2};";
115
+$insert_query = $dbi->create_query($insert_tmpl);
116
+$insert_query->bind_filter(sub {
112 117
     my ($key, $value) = @_;
113
-    if ($key eq 'k1') {
114
-        return "$value-$key-$column";
118
+    if ($key eq 'key1') {
119
+        return $value * 2;
115 120
     }
116 121
     return $value;
117 122
 });
123
+$DB::single = 1;
124
+$ret_val = $dbi->execute($insert_query, {key1 => 1, key2 => 2});
125
+ok($ret_val, "Insert success return value");
126
+
127
+$select_tmpl  = "select k1, k2 from table1";
128
+$select_query = $dbi->create_query($select_query);
129
+$select_query->fetch_filter(sub {
130
+    my ($key, $value);
131
+    if ($key eq 'key2') {
132
+        return $value * 3;
133
+    }
134
+    return $value;
135
+});
136
+$result = $dbi->execute($select_query);
118 137
 
138
+$rows = $result->fetch_all_hash;
139
+is_deeply($rows, {k1 => 2, k2 => 6}, "$test : bind_filter fetch_filter");
119 140
 
120
-$query->fetch_filter(sub {
121
-    my ($key, $value)
122
-});
141
+__END__
123 142
 
124 143
 $dbi->fetch_filter(sub {
125 144
     my ($key, $value, $type, $sth, $i) = @_;