LCC/Backend/DBI/mysql.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 41 SCAR 30.0
CC
4
Cov
0%
update
20.0
create
2.0
BEGIN
2.0
CRAP 20.0
1 package LCC::Backend::DBI::mysql;
2  
3 # Make sure we do everything by the book
4 # Set modules to inherit from
5 # Set version information
6  
7 use strict;
8 @LCC::Backend::DBI::mysql::ISA = qw(LCC::Backend::DBI);
9 $LCC::Backend::DBI::mysql::VERSION = '0.02';
10  
11 # Use the internal modules that we always need
12  
13 use LCC::Backend::DBI ();
14  
15 # Return true value for use
16  
17 1;
18  
19 #------------------------------------------------------------------------
20  
21 # The following methods are class methods
22  
23 #------------------------------------------------------------------------
24  
25 # The following methods change the object
26  
27 #------------------------------------------------------------------------
28  
29 # IN: 1 instantiated object
30  
31 sub create {
32  
33 # Obtain the object
34 # Obtain the database handle and the table name
35  
36 my $self = shift;
37 my ($dbh,$table) = $self->_dbh_table;
38  
39 # Return the result of creation of the table
40  
41 return $dbh->do( <<EOD );
42 CREATE TABLE IF NOT EXISTS $table (
43 id VARCHAR(255) NOT NULL,
44 value VARCHAR(255) NOT NULL,
45 UNIQUE id (id)
46 )
47 EOD
48 } #create
49  
50 #------------------------------------------------------------------------
51  
52 # IN: 1 instantiated object
53  
54 sub update {
55  
56 # Obtain the object
57 # Add error if we haven't got anything to compare with
58 # Return now if there is nothing to do
59  
60 my $self = shift;
61 $self->_add_error( "Unclear whether 'complete' or 'partial' update" )
62 unless exists( $self->{'old'} );
63 return unless exists $self->{'new'};
64  
65 # Obtain the database handle and table name
66 # Create local copy of reference to old hash
67 # Make sure table is empty if we did a complete update
68 # Create statement handle for updating
69
70 my ($dbh,$table) = $self->_dbh_table;
71 my $old = $self->{'old'};
72 $dbh->do( "DELETE FROM $table" ) unless each( %{$old} );
73 my $sth = $dbh->prepare( "REPLACE INTO $table (id,value) VALUES (?,?)");
74  
75 # For all of the special keys
76 # Save the key, prefixed by a null byte to mark as special, and its value
77  
78 foreach ($self->_additional_fields) {
79 $sth->execute( "\0$_","$self->{$_}" ); # value in quotes needed!
80 } # because the first value encountered determines quoting
81  
82 # Create local copy of reference to new hash
83 # For all of the key => value pairs in the new hash
84 # Update the database table
85 # Save/Overwrite the new value in the old hash, expand list if necessary
86 # Forget about any changes made
87  
88 my $new = $self->{'new'};
89 while (my ($id,$value) = each( %{$new} )) {
90 $sth->execute( $id,$value );
91 $old->{$id} = $value;
92 }
93 delete( $self->{'new'} );
94 } #update
95  
96 #------------------------------------------------------------------------
97  
98 # The following subroutines deal with standard Perl features
99  
100 #------------------------------------------------------------------------
101  
102 # Internal subroutines
103  
104 #------------------------------------------------------------------------
105  
106 __END__