index 97d3edf166b4dca3168da83b5a57752d8f8c7b8f..bce1e620cebb432b6325b374cf7dd7c6461d2415 100644 (file)
my $class = shift;
my $db_name = shift;
- my $lock='/tmp/gosa_si_lock';
- my $_lock = $db_name;
- $_lock =~ tr/\//_/;
- $lock.=$_lock;
+ my $lock = $db_name.".lock";
+ # delete existing lock - instance should be running only once
+ if(stat($lock)) {
+ unlink($lock);
+ }
my $self = {dbh=>undef,db_name=>undef,db_lock=>undef,db_lock_handle=>undef};
my $dbh = DBI->connect("dbi:SQLite:dbname=$db_name");
$self->{dbh} = $dbh;
my $lock = $self->{db_lock};
my $result=(-f $lock);
if($result) {
- &main::daemon_log("(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden", 8);
+ #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock found", 8);
usleep 100;
}
return $result;
sub create_lock : locked {
my $self=shift;
my $funcname=shift;
- &main::daemon_log("(".((defined $funcname)?$funcname:"").") Erzeuge Lock (PID ".$$.") ".($self->{db_lock}),8);
+ #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Creating Lock (PID ".$$.") ".($self->{db_lock}),8);
my $lock = $self->{db_lock};
while( -f $lock ) {
- print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n";
+ #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock found",8);
sleep 1;
}
sub remove_lock : locked {
my $self=shift;
my $funcname=shift;
- &main::daemon_log("(".((defined $funcname)?$funcname:"").") Entferne Lock (PID ".$$.") ".$self->{db_lock}, 8);
+ #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Removing Lock (PID ".$$.") ".$self->{db_lock}, 8);
close($self->{db_lock_handle});
unlink($self->{db_lock});
}
my $self = shift;
my $table_name = shift;
my $col_names_ref = shift;
+ my @col_names;
+ foreach my $col_name (@$col_names_ref) {
+ my @t = split(" ", $col_name);
+ $col_name = $t[0];
+ push(@col_names, $col_name);
+ }
+
$col_names->{ $table_name } = $col_names_ref;
- my $col_names_string = join(', ', @{$col_names_ref});
+ my $col_names_string = join(', ', @col_names);
my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( $col_names_string )";
&create_lock($self,'create_table');
$self->{dbh}->do($sql_statement);
my $sql_statement = "SELECT MAX(CAST(id AS INTEGER)) FROM $table";
&create_lock($self,'add_dbentry');
my $max_id = @{ @{ $self->{dbh}->selectall_arrayref($sql_statement) }[0] }[0];
- &remove_lock($self,'add_dbentry');
+ &remove_lock($self,'add_dbentry');
if( defined $max_id) {
$id = $max_id + 1;
} else {
# primekey is unique
# fetch column names of table
- my $col_names = &get_table_columns("",$table);
+ my $col_names = &get_table_columns($self, $table);
# assign values to column name variables
my @add_list;
foreach my $col_name (@{$col_names}) {
- # use function parameter for column values
+ # use function parameter for column values
+
if (exists $arg->{$col_name}) {
push(@add_list, $arg->{$col_name});
}
}
my $sql_statement = "INSERT INTO $table VALUES ('".join("', '", @add_list)."')";
-
&create_lock($self,'add_dbentry');
my $db_res = $self->{dbh}->do($sql_statement);
&remove_lock($self,'add_dbentry');
&create_lock($self,'get_table_columns');
my @res = @{$self->{dbh}->selectall_arrayref("pragma table_info('$table')")};
&remove_lock($self,'get_table_columns');
+
foreach my $column (@res) {
push(@column_names, @$column[1]);
}
sub show_table {
my $self = shift;
my $table_name = shift;
- #&create_lock($self,'show_table');
- #my @res = @{$self->{dbh}->selectall_arrayref( "SELECT * FROM $table_name ORDER BY timestamp")};
- #&remove_lock($self,'show_table');
my $sql_statement= "SELECT * FROM $table_name ORDER BY timestamp";
- &create_lock($self,'show_table');
my $res= &exec_statement($self, $sql_statement);
- &remove_lock($self,'show_table');
my @answer;
foreach my $hit (@{$res}) {
my $answer= -1;
my $sql_statement= "SELECT * FROM $table";
- &create_lock($self,'count_dbentries');
my $db_answer= &select_dbentry($self, $sql_statement);
- &remove_lock($self, 'count_dbentries');
my $count = keys(%{$db_answer});
return $count;