| 1 | package LCC::Backend::DBI; | ||
| 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::ISA = qw(LCC::Backend); | ||
| 9 | $LCC::Backend::DBI::VERSION = '0.02'; | ||
| 10 | |||
| 11 | # Make sure we have the external modules that we always need | ||
| 12 | |||
| 13 | use 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 | # IN: 1 class of object | ||
| 26 | # 2 instantiated LCC object | ||
| 27 | # 3 source specification | ||
| 28 | # 4 method => value pairs to be executed | ||
| 29 | # OUT: 1 instantiated LCC::Backend::xxx object | ||
| 30 | |||
| 31 | sub _new { | ||
| 32 | |||
| 33 | # Obtain the class | ||
| 34 | # Obtain the LCC object | ||
| 35 | # Obtain the source (a ref to a database handle, table name list) | ||
| 36 | |||
| 37 | my $class = shift; | ||
| 38 | my $lcc = shift; | ||
| 39 | my $source = shift; | ||
| 40 | |||
| 41 | # Obtain the database handle | ||
| 42 | # Obtain the driver name | ||
| 43 | # Add error if driver not found | ||
| 44 | |||
| 45 | my ($dbh) = __PACKAGE__->_dbh_table( $source ); | ||
| 46 | my $driver = $dbh->{'Driver'}->{'Name'} || ''; | ||
| 47 | $lcc->_add_error( "Unable to determine which DBI driver to use" ) | ||
| 48 | unless $driver; | ||
| 49 | |||
| 50 | # Create the module name | ||
| 51 | # Make sure that we have the right driver module if not loaded yet | ||
| 52 | # Reset to original class if there is no specific support for this driver | ||
| 53 | # Create the object, blessed with the database driver specific class | ||
| 54 | |||
| 55 | my $module = $class.'::'.$driver; | ||
| 56 | eval "use $module" unless defined( ${$module.'::VERSION'} ); | ||
| 57 | $module = $class unless defined( ${$module.'::VERSION'} ); | ||
| 58 | my $self = $module->SUPER::_new( $lcc,@_ ); | ||
| 59 | |||
| 60 | # Save the source specification | ||
| 61 | # Make sure that the table exists | ||
| 62 | # Return the object | ||
| 63 | |||
| 64 | $self->{'source'} = $source; | ||
| 65 | $self->create; | ||
| 66 | return $self; | ||
| 67 | } #_new | ||
| 68 | |||
| 69 | #------------------------------------------------------------------------ | ||
| 70 | |||
| 71 | # The following methods change the object | ||
| 72 | |||
| 73 | #------------------------------------------------------------------------ | ||
| 74 | |||
| 75 | # IN: 1 instantiated object | ||
| 76 | |||
| 77 | sub create { | ||
| 78 | |||
| 79 | # Obtain the object | ||
| 80 | # Obtain the database handle and the table name | ||
| 81 | # Create of the table, wrapped in eval to catch errors | ||
| 82 | |||
| 83 | my $self = shift; | ||
| 84 | my ($dbh,$table) = $self->_dbh_table; | ||
| 85 | eval {$dbh->do( "CREATE TABLE $table (id CHAR(255), value CHAR(255))" )}; | ||
| 86 | } #create | ||
| 87 | |||
| 88 | #------------------------------------------------------------------------ | ||
| 89 | |||
| 90 | # IN: 1 instantiated object | ||
| 91 | # 2 (optional) flag, specifying whether to force partial update | ||
| 92 | |||
| 93 | sub partial { | ||
| 94 | |||
| 95 | # Obtain the object | ||
| 96 | # Obtain database handle and table name | ||
| 97 | # Perform the selection | ||
| 98 | |||
| 99 | my $self = shift; | ||
| 100 | my ($dbh,$table) = $self->_dbh_table; | ||
| 101 | my $sth = $dbh->prepare( "SELECT id,value FROM $table" ); | ||
| 102 | $sth->execute; | ||
| 103 | |||
| 104 | # Create the reference to the hash to be filled | ||
| 105 | # While there are records to be fetched | ||
| 106 | # Save the value in the hash, special keys start with null byte | ||
| 107 | # Check whether UNS required full action | ||
| 108 | |||
| 109 | my $old = $self->{'old'} = {}; | ||
| 110 | while (my ($key,$list) = $sth->fetchrow_array) { | ||
| 111 | $key =~ s#^\0## ? $self->{$key} = $list : $old->{$key} = $list; | ||
| 112 | } | ||
| 113 | $self->_check_uns_complete unless shift || ''; | ||
| 114 | } #partial | ||
| 115 | |||
| 116 | #------------------------------------------------------------------------ | ||
| 117 | |||
| 118 | # IN: 1 instantiated object | ||
| 119 | |||
| 120 | sub update { | ||
| 121 | |||
| 122 | # Obtain the object | ||
| 123 | # Add error if we haven't got anything to compare with | ||
| 124 | # Return now if there is nothing to do | ||
| 125 | |||
| 126 | my $self = shift; | ||
| 127 | $self->_add_error( "Unclear whether 'complete' or 'partial' update" ) | ||
| 128 | unless exists( $self->{'old'} ); | ||
| 129 | return unless exists $self->{'new'}; | ||
| 130 | |||
| 131 | # Obtain the database handle and table name | ||
| 132 | # Create local copy of reference to old hash | ||
| 133 | # Initialize the statement handle for removing entries | ||
| 134 | |||
| 135 | my ($dbh,$table) = $self->_dbh_table; | ||
| 136 | my $old = $self->{'old'} || {}; | ||
| 137 | my $delete; | ||
| 138 | |||
| 139 | # If we are doing a partial update | ||
| 140 | # Remove the special fields | ||
| 141 | # Set the statement handle for removing | ||
| 142 | # Else (we're starting with a clean slate) | ||
| 143 | # Make sure table is empty | ||
| 144 | |||
| 145 | if (keys %{$old} ) { | ||
| 146 | $dbh->do( "DELETE FROM $table WHERE id LIKE '\0%'" ); | ||
| 147 | $delete = $dbh->prepare( "DELETE FROM $table WHERE id=?" ); | ||
| 148 | } else { | ||
| 149 | $dbh->do( "DELETE FROM $table" ); | ||
| 150 | } | ||
| 151 | |||
| 152 | # Create statement handle for updating | ||
| 153 | # For all of the special keys | ||
| 154 | # Save the key, prefixed by a null byte to mark as special, and its value | ||
| 155 | |||
| 156 | my $insert = $dbh->prepare( "INSERT INTO $table (id,value) VALUES (?,?)"); | ||
| 157 | foreach ($self->_additional_fields) { | ||
| 158 | $insert->execute( "\0$_","$self->{$_}" ); # value in quotes needed! | ||
| 159 | } # because the first value encountered determines quoting | ||
| 160 | |||
| 161 | # Create local copy of reference to new hash | ||
| 162 | # For all of the key => value pairs in the new hash | ||
| 163 | # Remove the entry if we need to remove | ||
| 164 | # Add the entry | ||
| 165 | # Save/Overwrite the new value in the old hash, expand list if necessary | ||
| 166 | # Forget about any changes made | ||
| 167 | |||
| 168 | my $new = $self->{'new'}; | ||
| 169 | while (my ($id,$value) = each( %{$new} )) { | ||
| 170 | $delete->execute( $id ) if $delete; | ||
| 171 | $insert->execute( $id,$value ); | ||
| 172 | $old->{$id} = $value; | ||
| 173 | } | ||
| 174 | delete( $self->{'new'} ); | ||
| 175 | } #update | ||
| 176 | |||
| 177 | #------------------------------------------------------------------------ | ||
| 178 | |||
| 179 | # The following subroutines deal with standard Perl features | ||
| 180 | |||
| 181 | #------------------------------------------------------------------------ | ||
| 182 | |||
| 183 | # Internal subroutines | ||
| 184 | |||
| 185 | #------------------------------------------------------------------------ | ||
| 186 | |||
| 187 | # IN: 1 instantiated object | ||
| 188 | # 2 the source specification (default: of the object itself) | ||
| 189 | # OUT: 1 database handle | ||
| 190 | # 2 table name to be used | ||
| 191 | |||
| 192 | sub _dbh_table { | ||
| 193 | |||
| 194 | # Obtain the object | ||
| 195 | # Obtain the source | ||
| 196 | # Return the database handle and table name | ||
| 197 | |||
| 198 | my $self = shift; | ||
| 199 | my $source = shift || $self->{'source'}; | ||
| 200 | return ref($source) eq 'ARRAY' ? @{$source} : ($source,'LCC') ; | ||
| 201 | } #_dbh_table | ||
| 202 | |||
| 203 | #------------------------------------------------------------------------ | ||
| 204 | |||
| 205 | __END__ |