index e6ec1048c289149ddeb576211a72b3b491c8b3a6..985963da4a01e994e99d87dd0a858d8eef26fd9a 100644 (file)
use strict;
use warnings;
+
use Carp;
use DBI;
use GOSA::GosaSupportDaemon;
use Time::HiRes qw(usleep);
+use Data::Dumper;
use Fcntl qw/:DEFAULT :flock/; # import LOCK_* constants
our $col_names = {};
my @ret = $sth->fetchall_arrayref();
$sth->finish();
if(length(@ret)==1 && $ret[0][0][0] eq 'ok') {
- &main::daemon_log("0 DEBUG: Database disk image '".$self->{db_name}."' is ok.", 7);
+ &main::daemon_log("0 DEBUG: Database disk image '".$self->{db_name}."' is ok.", 74);
} else {
&main::daemon_log("0 ERROR: Database disk image '".$self->{db_name}."' is malformed, creating new database!", 1);
$self->{dbh}->disconnect() or &main::daemon_log("0 ERROR: Could not disconnect from database '".$self->{db_name}."'!", 1);
goto get_lock;
} else {
seek($self->{db_lock_handle}, 0, 2);
- &main::daemon_log("0 DEBUG: Acquired lock for database ".$self->{db_name}, 8);
+ &main::daemon_log("0 DEBUG: Acquired lock for database ".$self->{db_name}, 74);
$self->connect();
}
return;
&main::daemon_log("0 BIG ERROR: Lockfile for database ".$self->{db_name}."got closed within critical section!", 1);
}
flock($self->{db_lock_handle}, LOCK_UN);
- &main::daemon_log("0 DEBUG: Released lock for database ".$self->{db_name}, 8);
+ &main::daemon_log("0 DEBUG: Released lock for database ".$self->{db_name}, 74);
$self->disconnect();
return;
}
foreach my $col_name (@$col_names_ref) {
push(@col_names, $col_name);
}
-
$col_names->{ $table_name } = \@col_names;
my $col_names_string = join(", ", @col_names);
+
+ # Not activated yet
+ # Check schema
+ if($self->check_schema($table_name, $col_names_ref)) {
+ $self->exec_statement("DROP TABLE $table_name");
+ &main::daemon_log("WARNING: Schema of table $table_name has changed! Table will be recreated!", 3);
+ }
+
my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( $col_names_string )";
my $res = $self->exec_statement($sql_statement);
}
+sub check_schema {
+ my $self = shift;
+ my $table_name = shift;
+ my $col_names_ref = shift; # ['id INTEGER PRIMARY KEY', 'timestamp VARCHAR(14) DEFAULT \'none\'', ... ]
+ my $col_names_length = @$col_names_ref;
+
+ my $sql = "PRAGMA table_info($table_name)";
+ my $res = $self->exec_statement($sql); # [ ['0', 'id', 'INTEGER', '0', undef, '1' ], ['1', 'timestamp', 'VARCHAR(14)', '0', '\'none\'', '0'], ... ]
+ my $db_table_length = @$res;
+
+ # Tabel does not exists, so no differences
+ if ($db_table_length == 0)
+ {
+ return 0;
+ }
+
+
+
+ # The number of columns is diffrent
+ if ($col_names_length != $db_table_length)
+ {
+ return 1;
+ }
+
+ # The column name and column type to not match
+ for (my $i=0; $i < $db_table_length; $i++)
+ {
+ my @col_names_list = split(" ", @$col_names_ref[$i]);
+ if (($col_names_list[0] ne @{@$res[$i]}[1]) || ($col_names_list[1] ne @{@$res[$i]}[2]))
+ {
+ return 1;
+ }
+ }
+
+
+ return 0;
+}
+
+
+
sub add_dbentry {
my $self = shift;
my $arg = shift;
my $sth = $self->{dbh}->prepare($sql_statement);
$db_res = $sth->execute();
$sth->finish();
- &main::daemon_log("0 DEBUG: Execution of statement '$sql_statement' succeeded!", 9);
+ &main::daemon_log("0 DEBUG: Execution of statement '$sql_statement' succeeded!", 74);
$success = 1;
};
if($@) {
my $sth = $self->{dbh}->prepare($sql_statement);
$db_res = $sth->execute();
$sth->finish();
- &main::daemon_log("0 DEBUG: Execution of statement '$sql_statement' succeeded!", 9);
+ &main::daemon_log("0 DEBUG: Execution of statement '$sql_statement' succeeded!", 74);
$success = 1;
};
if($@) {
my $sth = $self->{dbh}->prepare($sql_statement);
$db_res = $sth->execute();
$sth->finish();
- &main::daemon_log("0 DEBUG: Execution of statement '$sql_statement' succeeded!", 7);
+ &main::daemon_log("0 DEBUG: Execution of statement '$sql_statement' succeeded!", 74);
$success = 1;
};
if($@) {
@db_answer = @{$sth->fetchall_arrayref()};
$sth->finish();
$success=1;
- &main::daemon_log("0 DEBUG: $sql_statement succeeded.", 9);
+ &main::daemon_log("0 DEBUG: $sql_statement succeeded.", 74);
};
if($@) {
eval {
@db_answer = @{$sth->fetchall_arrayref()};
$sth->finish();
$success=1;
- &main::daemon_log("0 DEBUG: $sql_statement succeeded.", 9);
+ &main::daemon_log("0 DEBUG: $sql_statement succeeded.", 74);
};
if($@) {
eval {
@db_answer = @{$sth->fetchall_arrayref()};
$sth->finish();
DBI->trace(0);
- &main::daemon_log("0 DEBUG: $sql_statement succeeded.", 9);
+ &main::daemon_log("0 DEBUG: $sql_statement succeeded.", 74);
};
if($@) {
DBI->trace(0);