From: janw Date: Tue, 22 Apr 2008 07:04:37 +0000 (+0000) Subject: Recreated automatic id assignment. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ae41f0568613762e704890290305f3272a957816;p=gosa.git Recreated automatic id assignment. git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@10597 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/modules/DBsqlite.pm b/gosa-si/modules/DBsqlite.pm index 420a56949..e845ef6d0 100644 --- a/gosa-si/modules/DBsqlite.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -84,12 +84,36 @@ sub add_dbentry { my $prim_statement=""; if( 0 != @$primkeys ) { # more than one primkey exist in list my @prim_list; - foreach my $primkey (@$primkeys) { - if( not exists $arg->{$primkey} ) { - return (3, "primkey '$primkey' has no value for add_dbentry"); - } - push(@prim_list, "$primkey='".$arg->{$primkey}."'"); - } + 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; + eval { + $max_id = @{ @{ $self->{dbh}->selectall_arrayref($sql_statement) }[0] }[0]; + }; + if($@) { + $self->{dbh}->do("ANALYZE"); + eval { + $max_id = @{ @{ $self->{dbh}->selectall_arrayref($sql_statement) }[0] }[0]; + }; + if($@) { + &main::daemon_log("ERROR: $sql_statement failed with $@", 1); + } + } + 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); # check wether primkey is unique in table, otherwise return errorflag