index 2e838333f0edb6cae2ea347605b2ec6eeb88ded4..97d3edf166b4dca3168da83b5a57752d8f8c7b8f 100644 (file)
use warnings;
use DBI;
use Data::Dumper;
+use GOSA::GosaSupportDaemon;
use threads;
use Time::HiRes qw(usleep);
+
+
my $col_names = {};
sub new {
my $lock = $self->{db_lock};
my $result=(-f $lock);
if($result) {
- #print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n";
+ &main::daemon_log("(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden", 8);
usleep 100;
}
return $result;
sub create_lock : locked {
my $self=shift;
my $funcname=shift;
- #print STDERR "(".((defined $funcname)?$funcname:"").") Erzeuge Lock (PID ".$$.") ".($self->{db_lock})."\n";
+ &main::daemon_log("(".((defined $funcname)?$funcname:"").") Erzeuge Lock (PID ".$$.") ".($self->{db_lock}),8);
my $lock = $self->{db_lock};
while( -f $lock ) {
- #print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n";
+ print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n";
sleep 1;
}
sub remove_lock : locked {
my $self=shift;
my $funcname=shift;
- #print STDERR "(".((defined $funcname)?$funcname:"").") Entferne Lock (PID ".$$.") ".$self->{db_lock}."\n";
+ &main::daemon_log("(".((defined $funcname)?$funcname:"").") Entferne Lock (PID ".$$.") ".$self->{db_lock}, 8);
close($self->{db_lock_handle});
unlink($self->{db_lock});
}
+
sub create_table {
my $self = shift;
my $table_name = shift;
&remove_lock($self,'create_table');
return 0;
}
-
sub add_dbentry {
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 {
$arg->{id} = $id;
}
- # check wether value to primary key is specified
- if ( not exists $arg->{ $primkey } ) {
- return 3;
- }
-
# if timestamp is not provided, add timestamp
if( not exists $arg->{timestamp} ) {
$arg->{timestamp} = &get_time;
my $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) };
&remove_lock($self,'add_dbentry');
if ($res == 0) {
+ # primekey is unique
+
# fetch column names of table
my $col_names = &get_table_columns("",$table);
}
my $sql_statement = "INSERT INTO $table VALUES ('".join("', '", @add_list)."')";
- print STDERR $sql_statement;
+
&create_lock($self,'add_dbentry');
my $db_res = $self->{dbh}->do($sql_statement);
&remove_lock($self,'add_dbentry');
if( $db_res != 1 ) {
return 4;
- } else {
- return 0;
- }
+ }
} else {
- my $update_hash = { table=>$table };
- $update_hash->{where} = [ { $primkey=>[ $arg->{$primkey} ] } ];
- $update_hash->{update} = [ {} ];
+ # entry already exists, so update it
+ my $where_str= " WHERE $primkey='".$arg->{$primkey}."'";
+
+ my @update_l;
while( my ($pram, $val) = each %{$arg} ) {
if( $pram eq 'table' ) { next; }
if( $pram eq 'primkey' ) { next; }
- $update_hash->{update}[0]->{$pram} = [$val];
- }
- my $db_res = &update_dbentry( $self, $update_hash );
- if( $db_res != 1 ) {
- return 5;
- } else {
- return 0;
+ push(@update_l, "$pram='$val'");
}
+ my $update_str= join(", ", @update_l);
+ $update_str= " SET $update_str";
+
+ my $sql_statement= "UPDATE $table $update_str $where_str";
+ my $db_res = &update_dbentry($self, $sql_statement );
}
+
+ return 0;
}
sub update_dbentry {
my $db_answer= &exec_statement($self, $sql);
# fetch column list of db and create a hash with column_name->column_value of the select query
- $sql =~ /FROM ([\S]*?) /g;
+ $sql =~ /FROM ([\S]*?)( |$)/g;
my $table = $1;
my $column_list = &get_table_columns($self, $table);
my $list_len = @{ $column_list } ;
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 = @{$self->{dbh}->selectall_arrayref( "SELECT * FROM $table_name")};
+ my $res= &exec_statement($self, $sql_statement);
&remove_lock($self,'show_table');
+
my @answer;
- foreach my $hit (@res) {
+ foreach my $hit (@{$res}) {
push(@answer, "hit: ".join(', ', @{$hit}));
}
return join("\n", @answer);
}
-sub get_time {
- my ($seconds, $minutes, $hours, $monthday, $month,
- $year, $weekday, $yearday, $sommertime) = localtime(time);
- $hours = $hours < 10 ? $hours = "0".$hours : $hours;
- $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
- $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
- $month+=1;
- $month = $month < 10 ? $month = "0".$month : $month;
- $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
- $year+=1900;
- return "$year$month$monthday$hours$minutes$seconds";
-}
+sub count_dbentries {
+ my ($self, $table)= @_;
+ my $error= 0;
+ 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;
+}
1;