index f82a0d9738fdab6b2561b63afd171c3a5646edc6..e03152960c16d113e269f4e4368304dd9cd35ccd 100644 (file)
-package DBsqlite;
+package GOSA::DBsqlite;
use strict;
use warnings;
use DBI;
use Data::Dumper;
+use threads;
+use Time::HiRes qw(usleep);
-
+my $col_names = {};
sub new {
- my $object = shift;
+ my $class = shift;
my $db_name = shift;
- my $obj_ref = {};
- bless($obj_ref,$object);
+ my $lock='/tmp/gosa_si_lock';
+ my $_lock = $db_name;
+ $_lock =~ tr/\//_/;
+ $lock.=$_lock;
+ my $self = {dbh=>undef,db_name=>undef,db_lock=>undef,db_lock_handle=>undef};
my $dbh = DBI->connect("dbi:SQLite:dbname=$db_name");
- $obj_ref->{dbh} = $dbh;
+ $self->{dbh} = $dbh;
+ $self->{db_name} = $db_name;
+ $self->{db_lock} = $lock;
+ bless($self,$class);
+
+ return($self);
+}
+
+sub lock_exists : locked {
+ my $self=shift;
+ my $funcname=shift;
+ my $lock = $self->{db_lock};
+ my $result=(-f $lock);
+ if($result) {
+ #print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n";
+ 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";
- return($obj_ref);
+ my $lock = $self->{db_lock};
+ while( -f $lock ) {
+ #print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n";
+ sleep 1;
+ }
+
+ open($self->{db_lock_handle},'>',$self->{db_lock});
}
+sub remove_lock : locked {
+ my $self=shift;
+ my $funcname=shift;
+ #print STDERR "(".((defined $funcname)?$funcname:"").") Entferne Lock (PID ".$$.") ".$self->{db_lock}."\n";
+ close($self->{db_lock_handle});
+ unlink($self->{db_lock});
+}
sub create_table {
- my $object = shift;
+ my $self = shift;
my $table_name = shift;
my $col_names_ref = shift;
- my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name (".join(', ', @{$col_names_ref}).")";
- $object->{dbh}->do($sql_statement);
+ $col_names->{ $table_name } = $col_names_ref;
+ my $col_names_string = join(', ', @{$col_names_ref});
+ my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( $col_names_string )";
+ &create_lock($self,'create_table');
+ $self->{dbh}->do($sql_statement);
+ &remove_lock($self,'create_table');
return 0;
}
sub add_dbentry {
-
- my $obj = shift;
+ my $self = shift;
my $arg = shift;
# if dbh not specified, return errorflag 1
my $table = $arg->{table};
- if (not defined $table) {
- return 1;
+ if( not defined $table ) {
+ return 1 ;
}
-
- # incrementing running id
- if (not exists $arg->{id}) {
- my $max_id = @{@{$obj->{dbh}->selectall_arrayref("SELECT MAX(id) FROM $table")}[0]}[0];
- if (not defined $max_id) {
- $max_id = 0;
- }
- $arg->{id} = $max_id + 1;
+
+ # specify primary key in table
+ if (not exists $arg->{primkey}) {
+ return 2;
}
-
-
- # fetch column names of table
- my $col_names = $obj->get_table_columns($table);
-
- # assign values to column name variables
- my @add_list;
- foreach my $col_name (@{$col_names}) {
- if (exists $arg->{$col_name}) {
- push(@add_list, $arg->{$col_name});
+ my $primkey = $arg->{primkey};
+
+ # if primkey is id, fetch max id from table and give new job id= max(id)+1
+ if ($primkey eq 'id') {
+ my $id;
+ 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');
+ if( defined $max_id) {
+ $id = $max_id + 1;
} else {
- my $default_val = "none";
- if ($col_name eq "timestamp") {
- $default_val = "19700101000000";
- }
- push(@add_list, $default_val);
+ $id = 1;
}
-
- }
+ $arg->{id} = $id;
+ }
- # check wether id does not exists in table, otherwise return errorflag 2
- my $res = @{$obj->{dbh}->selectall_arrayref( "SELECT * FROM $table WHERE id='$arg->{id}'")};
- if ($res != 0) {
- return 2;
+ # 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 $sql_statement = " INSERT INTO $table VALUES ('".join("', '", @add_list)."') ";
- print " INSERT INTO $table VALUES ('".join("', '", @add_list)."')\n";
- $obj->{dbh}->do($sql_statement);
+ # check wether primkey is unique in table, otherwise return errorflag
+ my $sql_statement = "SELECT * FROM $table WHERE $primkey='$arg->{$primkey}'";
+ &create_lock($self,'add_dbentry');
+ my $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) };
+ &remove_lock($self,'add_dbentry');
+ if ($res == 0) {
+ # fetch column names of table
+ my $col_names = &get_table_columns("",$table);
+
+ # assign values to column name variables
+ my @add_list;
+ foreach my $col_name (@{$col_names}) {
+ # 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)."')";
+ 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;
+ }
- return 0;
+ } else {
+ my $update_hash = { table=>$table };
+ $update_hash->{where} = [ { $primkey=>[ $arg->{$primkey} ] } ];
+ $update_hash->{update} = [ {} ];
+ 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;
+ }
+ }
}
-sub change_dbentry {
- my $obj = shift;
+
+# error-flags
+# 1 no table ($table) defined
+# 2 no restriction parameter ($restric_pram) defined
+# 3 no restriction value ($restric_val) defined
+# 4 column name not known in table
+# 5 no column names to change specified
+sub update_dbentry {
+ my $self = shift;
my $arg = shift;
+
# check completeness of function parameter
# extract table statement from arg hash
my $table = $arg->{table};
} else {
delete $arg->{table};
}
+
# extract where parameter from arg hash
- my $restric_pram = $arg->{where};
- if (not defined $restric_pram) {
- return 2;
- } else {
- delete $arg->{'where'};
- }
- # extrac where value from arg hash
- my $restric_val = $arg->{$restric_pram};
- if (not defined $restric_val) {
- return 3;
- } else {
- delete $arg->{$restric_pram};
- }
-
- # check wether table has all specified columns
- my $columns = {};
- my @res = @{$obj->{dbh}->selectall_arrayref("pragma table_info('$table')")};
- foreach my $column (@res) {
- $columns->{@$column[1]} = "";
- }
- my @pram_list = keys %$arg;
- foreach my $pram (@pram_list) {
- if (not exists $columns->{$pram}) {
- return 4;
+ my $where_statement = "";
+ if( exists $arg->{where} ) {
+ my $where_hash = @{ $arg->{where} }[0];
+ if( 0 < keys %{ $where_hash } ) {
+ my @where_list;
+ while( my ($rest_pram, $rest_val) = each %{ $where_hash } ) {
+ my $statement;
+ if( $rest_pram eq 'timestamp' ) {
+ $statement = "$rest_pram<'@{ $rest_val }[0]'";
+ } else {
+ $statement = "$rest_pram='@{ $rest_val }[0]'";
+ }
+ push( @where_list, $statement );
+ }
+ $where_statement .= "WHERE ".join('AND ', @where_list);
}
}
-
-
- # select all changes
- my @change_list;
- my $sql_part;
- while (my($pram, $val) = each(%{$arg})) {
- push(@change_list, "$pram='$val'");
- }
-
- if (not@change_list) {
- return 5;
+ # extract update parameter from arg hash
+ my $update_hash = @{ $arg->{update} }[0];
+ my $update_statement = "";
+ if( 0 < keys %{ $update_hash } ) {
+ my @update_list;
+ while( my ($rest_pram, $rest_val) = each %{ $update_hash } ) {
+ my $statement = "$rest_pram='@{ $rest_val }[0]'";
+ push( @update_list, $statement );
+ }
+ $update_statement .= join(', ', @update_list);
}
- $obj->{dbh}->do("UPDATE $table SET ".join(', ',@change_list)." WHERE $restric_pram='$restric_val'");
- return 0;
+ my $sql_statement = "UPDATE $table SET $update_statement $where_statement";
+ &create_lock($self,'update_dbentry');
+ my $db_answer = $self->{dbh}->do($sql_statement);
+ &remove_lock($self,'update_dbentry');
+ return $db_answer;
}
sub del_dbentry {
- my $obj = shift;
+ my $self = shift;
my $arg = shift;
+
# check completeness of function parameter
# extract table statement from arg hash
my $table = $arg->{table};
} else {
delete $arg->{table};
}
- # extract where parameter from arg hash
- my $restric_pram = $arg->{where};
- if (not defined $restric_pram) {
- return 2;
- } else {
- delete $arg->{'where'};
+
+ # collect select statements
+ my @del_list;
+ while (my ($pram, $val) = each %{$arg}) {
+ if ( $pram eq 'timestamp' ) {
+ push(@del_list, "$pram < '$val'");
+ } else {
+ push(@del_list, "$pram = '$val'");
+ }
}
- # extrac where value from arg hash
- my $restric_val = $arg->{$restric_pram};
- if (not defined $restric_val) {
- return 3;
+
+ my $where_statement;
+ if( not @del_list ) {
+ $where_statement = "";
} else {
- delete $arg->{$restric_pram};
- }
-
- # check wether entry exists
- my $res = @{$obj->{dbh}->selectall_arrayref( "SELECT * FROM $table WHERE $restric_pram='$restric_val'")};
- if ($res == 0) {
- return 4;
+ $where_statement = "WHERE ".join(' AND ', @del_list);
}
- $obj->{dbh}->do("DELETE FROM $table WHERE $restric_pram='$restric_val'");
-
- return 0;
+ my $sql_statement = "DELETE FROM $table $where_statement";
+ &create_lock($self,'del_dbentry');
+ my $db_res = $self->{dbh}->do($sql_statement);
+ &remove_lock($self,'del_dbentry');
+ return $db_res;
}
sub get_table_columns {
- my $obj = shift;
+ my $self = shift;
my $table = shift;
-
- my @columns;
- my @res = @{$obj->{dbh}->selectall_arrayref("pragma table_info('$table')")};
- foreach my $column (@res) {
- push(@columns, @$column[1]);
+ my @column_names;
+
+ if(exists $col_names->{$table}) {
+ @column_names = @{$col_names->{$table}};
+ } else {
+ &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]);
+ }
}
+ return \@column_names;
- return \@columns;
}
sub select_dbentry {
- my $obj = shift;
+ my $self = shift;
my $arg = shift;
+
# check completeness of function parameter
# extract table statement from arg hash
my $table = $arg->{table};
# collect select statements
my @select_list;
- my $sql_part;
+ my $sql_statement;
while (my ($pram, $val) = each %{$arg}) {
- push(@select_list, "$pram = '$val'");
+ if ( $pram eq 'timestamp' ) {
+ push(@select_list, "$pram < '$val'");
+ } else {
+ push(@select_list, "$pram = '$val'");
+ }
}
-
- my $sql_statement = "SELECT * FROM 'jobs' WHERE ".join(' AND ', @select_list);
- my $answer = $obj->{dbh}->selectall_arrayref($sql_statement);
+
+ if (@select_list == 0) {
+ $sql_statement = "SELECT * FROM '$table'";
+ } else {
+ $sql_statement = "SELECT * FROM '$table' WHERE ".join(' AND ', @select_list);
+ }
+
+ # query db
+ &create_lock($self,'select_dbentry');
+ my $query_answer = $self->{dbh}->selectall_arrayref($sql_statement);
+ &remove_lock($self,'select_dbentry');
+
+ # fetch column list of db and create a hash with column_name->column_value of the select query
+ my $column_list = &get_table_columns($self, $table);
+ my $list_len = @{ $column_list } ;
+ my $answer = {};
+ my $hit_counter = 0;
+
+ foreach my $hit ( @{ $query_answer }) {
+ $hit_counter++;
+ for ( my $i = 0; $i < $list_len; $i++) {
+ $answer->{ $hit_counter }->{ @{ $column_list }[$i] } = @{ $hit }[$i];
+ }
+ }
+
return $answer;
}
sub show_table {
- my $obj = shift;
+ my $self = shift;
my $table_name = shift;
- my @res = @{$obj->{dbh}->selectall_arrayref( "SELECT * FROM $table_name")};
+ &create_lock($self,'show_table');
+ my @res = @{$self->{dbh}->selectall_arrayref( "SELECT * FROM $table_name")};
+ &remove_lock($self,'show_table');
my @answer;
foreach my $hit (@res) {
push(@answer, "hit: ".join(', ', @{$hit}));
sub exec_statement {
- my $obj = shift;
+ my $self = shift;
my $sql_statement = shift;
- my @res = @{$obj->{dbh}->selectall_arrayref($sql_statement)};
+ &create_lock($self,'exec_statement');
+ my @res = @{$self->{dbh}->selectall_arrayref($sql_statement)};
+ &remove_lock($self, 'exec_statement');
return \@res;
}
+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";
+}
+
+
1;