index 06f2d0e0efbeb49f8e2125b1cdc7b6b4ee574215..cf53043a0d5bdb279bc0fd5fc3ee5728a74e59a2 100644 (file)
use DBI;
use Data::Dumper;
use GOSA::GosaSupportDaemon;
-use threads;
use Time::HiRes qw(usleep);
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};
return($self);
}
-sub lock_exists : locked {
- my $self=shift;
- my $funcname=shift;
- my $lock = $self->{db_lock};
- my $result=(-f $lock);
- if($result) {
- #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock found", 8);
- usleep 500;
- }
- return $result;
-}
-
-sub create_lock : locked {
- my $self=shift;
- my $funcname=shift;
- #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Creating Lock (PID ".$$.") ".($self->{db_lock}),8);
-
- my $lock = $self->{db_lock};
- while( -f $lock ) {
- #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock found",8);
- usleep 100;
- }
-
- open($self->{db_lock_handle},'>',$self->{db_lock});
-}
-
-sub remove_lock : locked {
- my $self=shift;
- my $funcname=shift;
- #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Removing 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;
$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' )";
- &create_lock($self,'create_table');
$self->{dbh}->do($sql_statement);
- &remove_lock($self,'create_table');
return 0;
}
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";
- &create_lock($self,'add_dbentry');
my $max_id = @{ @{ $self->{dbh}->selectall_arrayref($sql_statement) }[0] }[0];
- &remove_lock($self,'add_dbentry');
my $id;
if( defined $max_id) {
$id = $max_id + 1;
# check wether primkey is unique in table, otherwise return errorflag
my $sql_statement = "SELECT * FROM $table $prim_statement";
- &create_lock($self,'add_dbentry');
my $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) };
- &remove_lock($self,'add_dbentry');
if ($res == 0) {
# primekey is unique
}
my $sql_statement = "INSERT INTO $table (".join(", ", @col_list).") VALUES ('".join("', '", @val_list)."')";
- &create_lock($self,'add_dbentry');
+ &main::daemon_log("DEBUG: $sql_statement", 7);
my $db_res = $self->{dbh}->do($sql_statement);
- &remove_lock($self,'add_dbentry');
if( $db_res != 1 ) {
return (4, $sql_statement);
}
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]);
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 =~ /FROM ([\S]*?)( |$)/g;
- my $table = $1;
- my $column_list = &get_table_columns($self, $table);
- my $list_len = @{ $column_list } ;
+ $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 {
+ # remove all blanks and split string to list of column names
+ $selected_cols =~ s/ //g;
+ @column_list = split(/,/, $selected_cols);
+ }
+
+ # create answer
my $hit_counter = 0;
- foreach my $hit ( @{ $db_answer }) {
+ 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];
+ $answer->{ $hit_counter }->{ $column_list[$i] } = @{ $hit }[$i];
}
}
-
+
return $answer;
}
my $self = shift;
my $sql_statement = shift;
- &create_lock($self,'exec_statement');
my @db_answer = @{$self->{dbh}->selectall_arrayref($sql_statement)};
- &remove_lock($self, 'exec_statement');
return \@db_answer;
}
my $sql_list = shift;
my @db_answer;
- &create_lock($self,'exec_statement');
foreach my $sql (@$sql_list) {
@db_answer = @{$self->{dbh}->selectall_arrayref($sql)};
}
- &remove_lock($self, 'exec_statement');
return \@db_answer;
}
my $sql_statement_drop = "DROP TABLE IF EXISTS $to";
my $sql_statement_alter = "ALTER TABLE $from RENAME TO $to";
- &create_lock($self,'move_table');
- my $db_res = $self->{dbh}->do($sql_statement_drop);
- $db_res = $self->{dbh}->do($sql_statement_alter);
- &remove_lock($self,'move_table');
return;
}