index f82a0d9738fdab6b2561b63afd171c3a5646edc6..cf53043a0d5bdb279bc0fd5fc3ee5728a74e59a2 100644 (file)
-package DBsqlite;
+package GOSA::DBsqlite;
use strict;
use warnings;
use DBI;
use Data::Dumper;
+use GOSA::GosaSupportDaemon;
+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 = $db_name.".si.lock";
+ # delete existing lock - instance should be running only once
+ if(stat($lock)) {
+ &main::daemon_log("DEBUG: Removed existing lock $lock.", 7);
+ 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");
- $obj_ref->{dbh} = $dbh;
+ $self->{dbh} = $dbh;
+ $self->{db_name} = $db_name;
+ $self->{db_lock} = $lock;
+ bless($self,$class);
- return($obj_ref);
+ return($self);
}
-
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);
+ 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);
+ my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( '$col_names_string' )";
+ $self->{dbh}->do($sql_statement);
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;
- }
-
-
- # 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});
- } else {
- my $default_val = "none";
- if ($col_name eq "timestamp") {
- $default_val = "19700101000000";
- }
- push(@add_list, $default_val);
- }
-
- }
-
- # 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;
- }
-
- my $sql_statement = " INSERT INTO $table VALUES ('".join("', '", @add_list)."') ";
- print " INSERT INTO $table VALUES ('".join("', '", @add_list)."')\n";
- $obj->{dbh}->do($sql_statement);
-
- return 0;
-
-}
-
-sub change_dbentry {
- my $obj = shift;
- my $arg = shift;
- # check completeness of function parameter
- # extract table statement from arg hash
- my $table = $arg->{table};
- if (not defined $table) {
- return 1;
- } else {
- delete $arg->{table};
+ # specify primary key in table
+ if (not exists $arg->{primkey}) {
+ return (2, "a hash key 'primkey' with at least an empty list as value is necessary for add_dbentry");
}
- # extract where parameter from arg hash
- my $restric_pram = $arg->{where};
- if (not defined $restric_pram) {
- return 2;
- } else {
- delete $arg->{'where'};
+ my $primkeys = $arg->{'primkey'};
+ my $prim_statement="";
+ if( 0 != @$primkeys ) {
+ my @prim_list;
+ foreach my $primkey (@$primkeys) {
+ if($primkey eq 'id') {
+ # if primkey is id, fetch max id from table and give new job id= max(id)+1
+ my $sql_statement = "SELECT MAX(CAST(id AS INTEGER)) FROM $table";
+ my $max_id = @{ @{ $self->{dbh}->selectall_arrayref($sql_statement) }[0] }[0];
+ my $id;
+ if( defined $max_id) {
+ $id = $max_id + 1;
+ } else {
+ $id = 1;
+ }
+ $arg->{id} = $id;
+ }
+ if( not exists $arg->{$primkey} ) {
+ return (3, "primkey '$primkey' has no value for add_dbentry");
+ }
+ push(@prim_list, "$primkey='".$arg->{$primkey}."'");
+ }
+ $prim_statement = "WHERE ".join(" AND ", @prim_list);
}
- # extrac where value from arg hash
- my $restric_val = $arg->{$restric_pram};
- if (not defined $restric_val) {
- return 3;
- } else {
- delete $arg->{$restric_pram};
+
+ # if timestamp is not provided, add timestamp
+ if( not exists $arg->{timestamp} ) {
+ $arg->{timestamp} = &get_time;
}
- # 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;
+ # check wether primkey is unique in table, otherwise return errorflag
+ my $sql_statement = "SELECT * FROM $table $prim_statement";
+ my $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) };
+
+ if ($res == 0) {
+ # primekey is unique
+
+ # fetch column names of table
+ my $col_names = &get_table_columns($self, $table);
+
+ # assign values to column name variables
+ my @col_list;
+ my @val_list;
+ foreach my $col_name (@{$col_names}) {
+ # use function parameter for column values
+ if (exists $arg->{$col_name}) {
+ push(@col_list, $col_name);
+ push(@val_list, $arg->{$col_name});
+ }
+ }
+
+ my $sql_statement = "INSERT INTO $table (".join(", ", @col_list).") VALUES ('".join("', '", @val_list)."')";
+ &main::daemon_log("DEBUG: $sql_statement", 7);
+ my $db_res = $self->{dbh}->do($sql_statement);
+ if( $db_res != 1 ) {
+ return (4, $sql_statement);
+ }
+
+ } else {
+ # entry already exists, so update it
+ my @update_l;
+ while( my ($pram, $val) = each %{$arg} ) {
+ if( $pram eq 'table' ) { next; }
+ if( $pram eq 'primkey' ) { next; }
+ push(@update_l, "$pram='$val'");
}
- }
-
+ my $update_str= join(", ", @update_l);
+ $update_str= " SET $update_str";
- # select all changes
- my @change_list;
- my $sql_part;
+ my $sql_statement= "UPDATE $table $update_str $prim_statement";
+ my $db_res = &update_dbentry($self, $sql_statement );
- while (my($pram, $val) = each(%{$arg})) {
- push(@change_list, "$pram='$val'");
}
- if (not@change_list) {
- return 5;
- }
-
- $obj->{dbh}->do("UPDATE $table SET ".join(', ',@change_list)." WHERE $restric_pram='$restric_val'");
return 0;
-}
+}
+sub update_dbentry {
+ my ($self, $sql)= @_;
+ my $db_answer= &exec_statement($self, $sql);
+ return $db_answer;
+}
-sub del_dbentry {
- my $obj = shift;
- my $arg = shift;
- # check completeness of function parameter
- # extract table statement from arg hash
- my $table = $arg->{table};
- if (not defined $table) {
- return 1;
- } 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 entry exists
- my $res = @{$obj->{dbh}->selectall_arrayref( "SELECT * FROM $table WHERE $restric_pram='$restric_val'")};
- if ($res == 0) {
- return 4;
- }
-
- $obj->{dbh}->do("DELETE FROM $table WHERE $restric_pram='$restric_val'");
-
- return 0;
+sub del_dbentry {
+ my ($self, $sql)= @_;;
+ my $db_res= &exec_statement($self, $sql);
+ return $db_res;
}
sub get_table_columns {
- my $obj = shift;
+ my $self = shift;
my $table = shift;
+ my @column_names;
+
+ if(exists $col_names->{$table}) {
+ @column_names = @{$col_names->{$table}};
+ } else {
+ my @res = @{$self->{dbh}->selectall_arrayref("pragma table_info('$table')")};
- my @columns;
- my @res = @{$obj->{dbh}->selectall_arrayref("pragma table_info('$table')")};
- foreach my $column (@res) {
- push(@columns, @$column[1]);
+ foreach my $column (@res) {
+ push(@column_names, @$column[1]);
+ }
}
+ return \@column_names;
- return \@columns;
}
-sub select_dbentry {
- my $obj = shift;
- my $arg = shift;
- # check completeness of function parameter
- # extract table statement from arg hash
- my $table = $arg->{table};
- if (not defined $table) {
- return 1;
+sub select_dbentry {
+ my ($self, $sql)= @_;
+ my $error= 0;
+ my $answer= {};
+ my $db_answer= &exec_statement($self, $sql);
+ my @column_list;
+
+ # fetch column list of db and create a hash with column_name->column_value of the select query
+ $sql =~ /SELECT ([\S\s]*?) FROM ([\S]*?)( |$)/g;
+ my $selected_cols = $1;
+ my $table = $2;
+
+ # all columns are used for creating answer
+ if ($selected_cols eq '*') {
+ @column_list = @{ &get_table_columns($self, $table) };
+
+ # specific columns are used for creating answer
} else {
- delete $arg->{table};
+ # remove all blanks and split string to list of column names
+ $selected_cols =~ s/ //g;
+ @column_list = split(/,/, $selected_cols);
}
- # collect select statements
- my @select_list;
- my $sql_part;
- while (my ($pram, $val) = each %{$arg}) {
- push(@select_list, "$pram = '$val'");
+ # create answer
+ my $hit_counter = 0;
+ my $list_len = @column_list;
+ foreach my $hit ( @{$db_answer} ){
+ $hit_counter++;
+ for ( my $i = 0; $i < $list_len; $i++) {
+ $answer->{ $hit_counter }->{ $column_list[$i] } = @{ $hit }[$i];
+ }
}
-
- my $sql_statement = "SELECT * FROM 'jobs' WHERE ".join(' AND ', @select_list);
- my $answer = $obj->{dbh}->selectall_arrayref($sql_statement);
+
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")};
+
+ my $sql_statement= "SELECT * FROM $table_name ORDER BY timestamp";
+ my $res= &exec_statement($self, $sql_statement);
+
my @answer;
- foreach my $hit (@res) {
+ foreach my $hit (@{$res}) {
push(@answer, "hit: ".join(', ', @{$hit}));
}
return join("\n", @answer);
sub exec_statement {
- my $obj = shift;
+ my $self = shift;
my $sql_statement = shift;
- my @res = @{$obj->{dbh}->selectall_arrayref($sql_statement)};
- return \@res;
+
+ my @db_answer = @{$self->{dbh}->selectall_arrayref($sql_statement)};
+
+ return \@db_answer;
+}
+
+
+sub exec_statementlist {
+ my $self = shift;
+ my $sql_list = shift;
+ my @db_answer;
+
+ foreach my $sql (@$sql_list) {
+ @db_answer = @{$self->{dbh}->selectall_arrayref($sql)};
+ }
+
+ return \@db_answer;
+}
+
+sub count_dbentries {
+ my ($self, $table)= @_;
+ my $error= 0;
+ my $answer= -1;
+
+ my $sql_statement= "SELECT * FROM $table";
+ my $db_answer= &select_dbentry($self, $sql_statement);
+
+ my $count = keys(%{$db_answer});
+ return $count;
}
+
+
+sub move_table {
+ my ($self, $from, $to) = @_;
+
+ my $sql_statement_drop = "DROP TABLE IF EXISTS $to";
+ my $sql_statement_alter = "ALTER TABLE $from RENAME TO $to";
+
+ return;
+}
+
+
1;