From 7bdee5800e9c10136500b69218591a91796b36f5 Mon Sep 17 00:00:00 2001 From: rettenbe Date: Thu, 17 Apr 2008 10:56:54 +0000 Subject: [PATCH] complete new function add_dbentry at DBsqlite.pm git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@10534 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/gosa-si-server | 6 ++-- gosa-si/modules/DBsqlite.pm | 61 +++++++++++---------------------- gosa-si/modules/GosaPackages.pm | 3 +- gosa-si/tests/client.php | 8 +++-- 4 files changed, 29 insertions(+), 49 deletions(-) diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index 4ce75e957..b70ec8430 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -115,12 +115,12 @@ our $prg= basename($0); our $job_db; our $job_queue_tn = 'jobs'; my $job_queue_file_name; -my @job_queue_col_names = ("id INTEGER", - "timestamp", +my @job_queue_col_names = ("id INTEGER PRIMARY KEY", + "timestamp DEFAULT 'none'", "status DEFAULT 'none'", "result DEFAULT 'none'", "progress DEFAULT 'none'", - "headertag DEFAULT 'none'", + "headertag DEFAULT 'none'", "targettag DEFAULT 'none'", "xmlmessage DEFAULT 'none'", "macaddress DEFAULT 'none'", diff --git a/gosa-si/modules/DBsqlite.pm b/gosa-si/modules/DBsqlite.pm index a629320a1..2b260dac1 100644 --- a/gosa-si/modules/DBsqlite.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -22,7 +22,7 @@ sub new { 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"); + my $dbh = DBI->connect("dbi:SQLite:dbname=$db_name", "", "", {RaiseError => 1, AutoCommit => 1}); $self->{dbh} = $dbh; $self->{db_name} = $db_name; $self->{db_lock} = $lock; @@ -31,20 +31,14 @@ sub new { return($self); } + sub create_table { 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); - } + my $col_names_string = join(", ", @{$col_names_ref}); + my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ($col_names_string )"; - $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; } @@ -53,6 +47,7 @@ sub create_table { sub add_dbentry { my $self = shift; my $arg = shift; + my $res = 0; # default value # if dbh not specified, return errorflag 1 my $table = $arg->{table}; @@ -60,46 +55,31 @@ sub add_dbentry { return 1 ; } - # 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"); + # if timestamp is not provided, add timestamp + if( not exists $arg->{timestamp} ) { + $arg->{timestamp} = &get_time; } + + # check primkey and run insert or update my $primkeys = $arg->{'primkey'}; my $prim_statement=""; if( 0 != @$primkeys ) { # more than one primkey exist in list 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); - } - - # if timestamp is not provided, add timestamp - if( not exists $arg->{timestamp} ) { - $arg->{timestamp} = &get_time; - } - # 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) }; + # check wether primkey is unique in table, otherwise return errorflag + my $sql_statement = "SELECT * FROM $table $prim_statement"; + $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) }; + } - # primekey is unique + # primekey is unique or no primkey specified -> run insert if ($res == 0) { # fetch column names of table my $col_names = &get_table_columns($self, $table); @@ -110,18 +90,18 @@ sub add_dbentry { 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}); - } + push(@col_list, "'".$col_name."'"); + push(@val_list, "'".$arg->{$col_name}."'"); + } } - my $sql_statement = "INSERT INTO $table (".join(", ", @col_list).") VALUES ('".join("', '", @val_list)."')"; + my $sql_statement = "INSERT INTO $table (".join(", ", @col_list).") VALUES (".join(", ", @val_list).")"; my $db_res = $self->{dbh}->do($sql_statement); if( $db_res != 1 ) { return (4, $sql_statement); } - # entry already exists, so update it + # entry already exists -> run update } else { my @update_l; while( my ($pram, $val) = each %{$arg} ) { @@ -215,7 +195,6 @@ sub show_table { my $sql_statement= "SELECT * FROM $table_name ORDER BY timestamp"; my $res= &exec_statement($self, $sql_statement); - my @answer; foreach my $hit (@{$res}) { push(@answer, "hit: ".join(', ', @{$hit})); diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index 1141a1d59..f03074b18 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -375,7 +375,7 @@ sub process_job_msg { if( $error == 0 ) { # add job to job queue my $func_dic = {table=>$main::job_queue_tn, - primkey=>['id'], + primkey=>[], timestamp=>$timestamp, status=>'waiting', result=>'none', @@ -386,7 +386,6 @@ sub process_job_msg { macaddress=>$macaddress, plainname=>$plain_name, }; - my $res = $main::job_db->add_dbentry($func_dic); if (not $res == 0) { &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1); diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index 7609df6a9..f21fa09d1 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -17,7 +17,7 @@ if($sock->connected()){ # jobdb add #$data = "
gosa_network_completition
GOSAGOSAws-muc-2
"; -#$data = "
job_sayHello
10.89.1.155:2008300:01:6c:9d:b9:fa00:1B:77:04:8A:6C 20130102133900
"; +$data = "
job_sayHello
10.89.1.155:2008300:01:6c:9d:b9:fa00:1B:77:04:8A:6C 20130102133908
"; #$data = "
job_ping
GOSA 00:01:6c:9d:b9:fa 00:01:6c:9d:b9:fa19700101000000
"; @@ -70,9 +70,11 @@ if($sock->connected()){ # get_client_for_login_usr #$data = "
gosa_get_client_for_login_usr
GOSA GOSA harald
"; -# recreate_fai_server_db +################## +# recreate fai dbs #$data = "
gosa_recreate_fai_server_db
GOSA GOSA
"; -$data = "
gosa_recreate_fai_release_db
GOSA GOSA
"; +#$data = "
gosa_recreate_fai_release_db
GOSA GOSA
"; +#$data = "
gosa_recreate_packages_list_db
GOSA GOSA
"; ########### # messaging -- 2.30.2