X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=gosa-si%2Fmodules%2FDBsqlite.pm;h=5bee915c414a74b42152acc92d4beaf4bd8dd92b;hb=5a6026e100abdffd3bebc1b66cc21d88845fa848;hp=76014acdea135a8718846c73ddac3ddf2b7e1cfc;hpb=1b442ce10091bc59e11f55e7e1c8bcd6987a2bb9;p=gosa.git diff --git a/gosa-si/modules/DBsqlite.pm b/gosa-si/modules/DBsqlite.pm index 76014acde..5bee915c4 100644 --- a/gosa-si/modules/DBsqlite.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -52,40 +52,52 @@ sub add_dbentry { if ( ( defined $primkey ) && ( not $arg->{ $primkey } ) ) { return 2; } + + # 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 3 - if ( defined $primkey ) { - my $res = @{ $obj->{dbh}->selectall_arrayref( "SELECT * FROM $table WHERE $primkey='$arg->{$primkey}'") }; - if ($res != 0) { - return 3; - } - } - - # 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}) { + my $res = @{ $obj->{dbh}->selectall_arrayref( "SELECT * FROM $table WHERE $primkey='$arg->{$primkey}'") }; + if ($res == 0) { + # 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}) { # use function parameter for column values - if (exists $arg->{$col_name}) { - push(@add_list, $arg->{$col_name}); + if (exists $arg->{$col_name}) { + push(@add_list, $arg->{$col_name}); + } + } + + my $sql_statement = " INSERT INTO $table VALUES ('".join("', '", @add_list)."')"; + my $db_res = $obj->{dbh}->do($sql_statement); + if( $db_res != 1 ) { + return 1; + } else { + return 0; } - # use default values for column values -# } else { -# my $default_val = "none"; -# if ($col_name eq "timestamp") { -# $default_val = "19700101000000"; -# } -# push(@add_list, $default_val); -# } - } - - 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; + } 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( $obj, $update_hash ); + if( $db_res != 1 ) { + return 1; + } else { + return 0; + } + + } } @@ -107,49 +119,41 @@ sub update_dbentry { } 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"; + my $db_answer = $obj->{dbh}->do($sql_statement); + return $db_answer; } @@ -165,30 +169,28 @@ sub del_dbentry { } 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'"); + my $sql_statement = "DELETE FROM $table $where_statement"; + my $db_res = $obj->{dbh}->do($sql_statement); - return 0; + return $db_res; } @@ -227,8 +229,8 @@ sub select_dbentry { } else { push(@select_list, "$pram = '$val'"); } - } + if (@select_list == 0) { $sql_statement = "SELECT ROWID, * FROM '$table'"; } else { @@ -275,4 +277,18 @@ sub 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;