DBO/Visitor/Insert.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 108 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #------------------------------------------------------------------------------
2 # DBO::Visitor::Insert
3 #
4 # DESCRIPTION
5 #
6 # AUTHOR
7 # Gareth Rees
8 #
9 # COPYRIGHT
10 # Copyright (c) 1999 Canon Research Centre Europe Ltd.
11 #
12 # $Id: Insert.pm,v 1.2 1999/06/29 17:09:31 garethr Exp $
13 #------------------------------------------------------------------------------
14  
15 use strict;
16  
17 # We maintain a cache of prepared SQL statements accessible on a
18 # combination of Table and Handle. The prepared statement can be
19 # retrieved and executed quickly.
20  
21 my %INSERT_STH;
22  
23 package DBO::Visitor::PrepareInsert;
24 use base qw(DBO::Visitor);
25 use Class::Multimethods;
26  
27 multimethod visit_table =>
28 qw(DBO::Visitor::PrepareInsert DBO::Table DBO::Handle::DBI) =>
29 sub {
30 my ($vis, $table, $dbh) = @_;
31 my @sql = ("INSERT INTO", $table->{name}, "(");
32 my $cols = 0;
33 foreach my $col (@{$table->{columns}}){
34 $vis->{sql} = [];
35 visit_column($vis, $col, $dbh);
36 if (@{$vis->{sql}}) {
37 ++ $cols;
38 push @sql, @{$vis->{sql}}, ",";
39 }
40 }
41 $cols or die DBO::Exception
42 (NO_COLUMNS => "No columns found while preparing insert statement");
43 splice @sql, -1, 1, ") VALUES (", ("?",",") x $cols;
44 splice @sql, -1, 1, ")";
45 my $sql = join ' ', @sql;
46 $INSERT_STH{$table,$dbh} = $dbh->prepare($sql) or die DBO::Exception
47 (PREPARE => "Failed to prepare SQL statement %s: %s.", $sql, $dbh->errstr);
48 };
49  
50 multimethod visit_column =>
51 qw(DBO::Visitor::PrepareInsert DBO::Column::Base DBO::Handle::DBI) =>
52 sub {
53 my ($vis, $col, $dbh) = @_;
54 push @{$vis->{sql}}, $col->{name};
55 };
56  
57 package DBO::Visitor::Insert;
58 use base qw(DBO::Visitor);
59 use Class::Multimethods;
60  
61 multimethod visit_table =>
62 qw(DBO::Visitor::Insert DBO::Table DBO::Handle::DBI) =>
63 sub {
64 my ($vis, $table, $dbh) = @_;
65 my $sth = $INSERT_STH{$table,$dbh}
66 || visit_table(DBO::Visitor::PrepareInsert->new, $table, $dbh);
67 $sth->execute(map { visit_column($vis, $_, $dbh) } @{$table->{columns}})
68 or die DBO::Exception
69 (EXECUTE => "Failed to execute insert: %s.", $dbh->errstr);
70 $sth->finish;
71 };
72  
73 multimethod visit_column =>
74 qw(DBO::Visitor::Insert DBO::Column::Base DBO::Handle::DBI) =>
75 sub {
76 my ($vis, $col, $dbh) = @_;
77 $vis->{record}{$col->{name}};
78 };
79  
80  
81 #------------------------------------------------------------------------------
82 # Method instances for mSQL databases
83 #------------------------------------------------------------------------------
84  
85 multimethod visit_table =>
86 qw(DBO::Visitor::Insert DBO::Table DBO::Handle::DBI::mSQL) =>
87 sub {
88 my ($vis, $table, $dbh) = @_;
89 # Discover the new value of the sequence, if any.
90 if ($table->{autoincrement}) {
91 my $sql = "SELECT _seq FROM $table->{name}";
92 my @seq = $dbh->selectrow_array($sql) or die DBO::Exception
93 (SELECTALL => "Failed to execute SQL statement %s: %s.", $sql, $dbh->errstr);
94 $vis->{record}{$table->{autoincrement}} = $seq[0];
95 }
96 # visit_table($vis, $table, superclass($dbh));
97 call_next_method();
98 };
99  
100  
101 #------------------------------------------------------------------------------
102 # Method instances for MySQL databases.
103 #------------------------------------------------------------------------------
104  
105 my %MYSQL_STH;
106  
107 multimethod visit_table =>
108 qw(DBO::Visitor::Insert DBO::Table DBO::Handle::DBI::mysql) =>
109 sub {
110 my ($vis, $table, $dbh) = @_;
111 # visit_table($vis, $table, superclass($dbh));
112 call_next_method();
113 # Find out the new value of the autoincrement field, if any.
114 if ($table->{autoincrement}) {
115 my $sth = $MYSQL_STH{$table,$dbh} || do {
116 my $sql = "SELECT $table->{autoincrement} FROM $table->{name} WHERE $table->{autoincrement} IS NULL";
117 $MYSQL_STH{$table,$dbh} = $dbh->prepare($sql) or die DBO::Exception
118 (PREPARE => "Failed to prepare SQL statement %s: %s.", $sql,
119 $dbh->errstr);
120 };
121 $sth->execute or die DBO::Exception
122 (EXECUTE => "Failed to select autoincrement column: %s.", $dbh->errstr);
123 $vis->{record}{$table->{autoincrement}} = $sth->fetchrow_array;
124 $sth->finish;
125 }
126 };
127  
128 multimethod visit_column =>
129 qw(DBO::Visitor::Insert DBO::Column::AutoIncrement DBO::Handle::DBI::mysql) =>
130 sub {
131 my ($vis, $col, $dbh) = @_;
132 undef; # MySQL turns the NULL value into the next unused integer
133 };
134  
135 1;