054d5eab0e22883f77f1eeef6fcbce0ee64b32c6
1 package databases;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5 "get_events",
6 "query_jobdb",
7 "query_packages_list",
8 "count_jobdb",
9 "count_packages_list",
10 "delete_jobdb_entry",
11 "clear_jobdb",
12 "update_status_jobdb_entry",
13 );
14 @EXPORT = @events;
16 use strict;
17 use warnings;
18 use GOSA::GosaSupportDaemon;
19 use Data::Dumper;
20 use utf8;
22 BEGIN {}
24 END {}
26 ### Start ######################################################################
28 #&main::read_configfile($main::cfg_file, %cfg_defaults);
30 sub get_events {
31 return \@events;
32 }
35 sub query_packages_list { return &query_db( @_ ) ; }
36 sub query_jobdb { return &query_db( @_ ) ; }
37 sub query_db {
38 my ($msg, $msg_hash, $session_id) = @_ ;
39 my $header = @{$msg_hash->{'header'}}[0];
40 my $target = @{$msg_hash->{'target'}}[0];
41 my $source = @{$msg_hash->{'source'}}[0];
42 my $table;
43 my $db;
44 if( $header =~ /query_jobdb/ ) {
45 $table = $main::job_queue_tn;
46 $db = $main::job_db;
47 } elsif( $header =~ /query_packages_list/ ) {
48 $table = $main::packages_list_tn;
49 $db = $main::packages_list_db;
50 }
52 # prepare sql statement and execute query
53 my $select= &get_select_statement($msg, $msg_hash);
54 my $where= &get_where_statement($msg, $msg_hash);
55 my $limit= &get_limit_statement($msg, $msg_hash);
56 my $orderby= &get_orderby_statement($msg, $msg_hash);
57 my $sql_statement= "SELECT $select FROM $table $where $orderby $limit";
58 my $res_hash = $db->select_dbentry($sql_statement);
60 my $out_xml = &db_res2si_msg($res_hash, $header, $target, $source);
61 my @out_msg_l = ( $out_xml );
62 return @out_msg_l;
63 }
66 sub count_packages_list{ return &count_db( @_ ); }
67 sub count_jobdb{ return &count_db( @_ ); }
68 sub count_db {
69 my ($msg, $msg_hash, $session_id) = @_;
70 my $header = @{$msg_hash->{'header'}}[0];
71 my $target = @{$msg_hash->{'target'}}[0];
72 my $source = @{$msg_hash->{'source'}}[0];
73 my $table;
74 my $db;
75 if( $header =~ /count_jobdb/ ) {
76 $table = $main::job_queue_tn;
77 $db = $main::job_db;
78 } elsif( $header =~ /count_packages_list/ ) {
79 $table = $main::packages_list_tn;
80 $db = $main::packages_list_db;
81 }
83 # prepare sql statement and execute query
84 my $res_hash = $db->select_dbentry("SELECT * FROM $table");
86 my $count = keys(%{$res_hash});
87 my $out_xml= "<xml><header>answer</header><source>$target</source><target>$source</target><count>$count</count></xml>";
88 my @out_msg_l = ( $out_xml );
89 return @out_msg_l;
90 }
92 sub delete_jobdb_entry {
93 my ($msg, $msg_hash, $session_id) = @_ ;
94 my $target = @{$msg_hash->{'target'}}[0];
95 my $source = @{$msg_hash->{'source'}}[0];
97 # prepare query sql statement
98 my $table= $main::job_queue_tn;
99 my $where= &get_where_statement($msg, $msg_hash);
100 my $sql_statement = "DELETE FROM $table $where";
102 # execute db query
103 my $db_res = $main::job_db->del_dbentry($sql_statement);
105 my $res;
106 if( $db_res > 0 ) {
107 $res = 0 ;
108 } else {
109 $res = 1;
110 }
112 # prepare xml answer
113 my $out_xml = "<xml><header>answer</header><source>$target</source><target>$source</target><answer1>$res</answer1></xml>";
114 my @out_msg_l = ( $out_xml );
115 return @out_msg_l;
117 }
120 sub clear_jobdb {
121 my ($msg, $msg_hash, $session_id) = @_ ;
122 my $target = @{$msg_hash->{'target'}}[0];
123 my $source = @{$msg_hash->{'source'}}[0];
125 my $error= 0;
126 my $out_xml= "<xml><answer1>1</answer1></xml>";
128 my $table= $main::job_queue_tn;
130 my $sql_statement = "DELETE FROM $table";
131 my $db_res = $main::job_db->del_dbentry($sql_statement);
132 if( not $db_res > 0 ) { $error++; };
134 if( $error == 0 ) {
135 $out_xml = "<xml><header>answer</header><source>$target</source><target>$source</target><answer1>0</answer1></xml>";
136 }
137 my @out_msg_l = ( $out_xml );
138 return @out_msg_l;
139 }
142 sub update_status_jobdb_entry {
143 my ($msg, $msg_hash, $session_id) = @_ ;
144 my $target = @{$msg_hash->{'target'}}[0];
145 my $source = @{$msg_hash->{'source'}}[0];
147 my $error= 0;
148 my $out_xml= "<xml><header>answer</header><source>$target</source><target>$source</target><answer1>1</answer1></xml>";
150 my @len_hash = keys %{$msg_hash};
151 if( 0 == @len_hash) { $error++; };
153 # prepare query sql statement
154 if( $error == 0) {
155 my $table= $main::job_queue_tn;
156 my $where= &get_where_statement($msg, $msg_hash);
157 my $update= &get_update_statement($msg, $msg_hash);
159 # conditions
160 # no timestamp update if status eq waiting
161 my $res_hash = $main::job_db->select_dbentry("SELECT * FROM $table $where AND status='processing' ");
162 if( (0 != keys(%$res_hash)) && ($update =~ /timestamp/i) ) {
163 $error ++;
164 $out_xml = "<answer1>1</answer1><error_string>there is no timestamp update allowed while status is 'processing'</error_string>";
165 }
167 if( $error == 0 ) {
168 my $sql_statement = "UPDATE $table $update $where";
169 # execute db query
170 my $db_res = $main::job_db->update_dbentry($sql_statement);
172 # check success of db update
173 if( not $db_res > 0 ) { $error++; };
175 }
176 }
178 if( $error == 0) {
179 $out_xml = "<answer1>0</answer1>";
180 }
182 my $out_msg = sprintf("<xml><header>answer</header><source>%s</source><target>%s</target>%s</xml>", $target, $source, $out_xml);
183 my @out_msg_l = ( $out_msg );
184 return @out_msg_l;
185 }
187 1;