2bb2e487ded26b676375b9e90004f224250734f9
1 package databases;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5 "get_events",
6 "query_jobdb",
7 "count_jobdb",
8 "delete_jobdb_entry",
9 "clear_jobdb",
10 "update_status_jobdb_entry",
11 "query_packages_list",
12 "count_packages_list",
13 "query_fai_server",
14 "count_fai_server",
15 "query_fai_release",
16 "count_fai_release",
17 );
18 @EXPORT = @events;
20 use strict;
21 use warnings;
22 use GOSA::GosaSupportDaemon;
23 use Data::Dumper;
25 BEGIN {}
27 END {}
29 ### Start ######################################################################
31 #&main::read_configfile($main::cfg_file, %cfg_defaults);
33 sub get_events {
34 return \@events;
35 }
37 sub query_fai_releases{ return &query_db( @_ ); }
38 sub query_fai_server{ return &query_db( @_ ) ; }
39 sub query_packages_list { return &query_db( @_ ) ; }
40 sub query_jobdb { return &query_db( @_ ) ; }
41 sub query_db {
42 my ($msg, $msg_hash, $session_id) = @_ ;
43 my $header = @{$msg_hash->{'header'}}[0];
44 my $target = @{$msg_hash->{'target'}}[0];
45 my $source = @{$msg_hash->{'source'}}[0];
46 my $table;
47 my $db;
48 if( $header =~ /query_jobdb/ ) {
49 $table = $main::job_queue_tn;
50 $db = $main::job_db;
51 } elsif( $header =~ /query_packages_list/ ) {
52 $table = $main::packages_list_tn;
53 $db = $main::packages_list_db;
54 } elsif( $header =~ /query_fai_server/ ) {
55 $table = $main::fai_server_tn;
56 $db = $main::fai_server_db;
57 } elsif( $header =~ /count_fai_release/ ) {
58 $table = $main::fai_release_tn;
59 $db = $main::fai_server_db;
60 }
63 # prepare sql statement and execute query
64 my $select= &get_select_statement($msg, $msg_hash);
65 my $where= &get_where_statement($msg, $msg_hash);
66 my $limit= &get_limit_statement($msg, $msg_hash);
67 my $orderby= &get_orderby_statement($msg, $msg_hash);
68 my $sql_statement= "SELECT $select FROM $table $where $orderby $limit";
69 my $res_hash = $db->select_dbentry($sql_statement);
71 my $out_xml = &db_res2si_msg($res_hash, $header, $target, $source);
72 $out_xml =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
73 my @out_msg_l = ( $out_xml );
74 return @out_msg_l;
75 }
77 sub count_fai_releas{ return &count_db( @_ ); }
78 sub count_fai_server{ return &count_db( @_ ); }
79 sub count_packages_list{ return &count_db( @_ ); }
80 sub count_jobdb{ return &count_db( @_ ); }
81 sub count_db {
82 my ($msg, $msg_hash, $session_id) = @_;
83 my $header = @{$msg_hash->{'header'}}[0];
84 my $target = @{$msg_hash->{'target'}}[0];
85 my $source = @{$msg_hash->{'source'}}[0];
86 my $table;
87 my $db;
89 if( $header =~ /count_jobdb/ ) {
90 $table = $main::job_queue_tn;
91 $db = $main::job_db;
92 } elsif( $header =~ /count_packages_list/ ) {
93 $table = $main::packages_list_tn;
94 $db = $main::packages_list_db;
95 } elsif( $header =~ /count_fai_server/ ) {
96 $table = $main::fai_server_tn;
97 $db = $main::fai_server_db;
98 } elsif( $header =~ /count_fai_release/ ) {
99 $table = $main::fai_release_tn;
100 $db = $main::fai_server_db;
101 }
104 # prepare sql statement and execute query
105 my $res_hash = $db->select_dbentry("SELECT * FROM $table");
107 my $count = keys(%{$res_hash});
108 my $out_xml= "<xml><header>answer</header><source>$target</source><target>$source</target><count>$count</count><session_id>$session_id</session_id></xml>";
109 my @out_msg_l = ( $out_xml );
110 return @out_msg_l;
111 }
113 sub delete_jobdb_entry {
114 my ($msg, $msg_hash, $session_id) = @_ ;
115 my $target = @{$msg_hash->{'target'}}[0];
116 my $source = @{$msg_hash->{'source'}}[0];
118 # prepare query sql statement
119 my $table= $main::job_queue_tn;
120 my $where= &get_where_statement($msg, $msg_hash);
121 my $sql_statement = "DELETE FROM $table $where";
123 # execute db query
124 my $db_res = $main::job_db->del_dbentry($sql_statement);
126 my $res;
127 if( $db_res > 0 ) {
128 $res = 0 ;
129 } else {
130 $res = 1;
131 }
133 # prepare xml answer
134 my $out_xml = "<xml><header>answer</header><source>$target</source><target>$source</target><answer1>$res</answer1><session_id>$session_id</session_id></xml>";
135 my @out_msg_l = ( $out_xml );
136 return @out_msg_l;
138 }
141 sub clear_jobdb {
142 my ($msg, $msg_hash, $session_id) = @_ ;
143 my $target = @{$msg_hash->{'target'}}[0];
144 my $source = @{$msg_hash->{'source'}}[0];
146 my $error= 0;
147 my $out_xml= "<xml><answer1>1</answer1></xml>";
149 my $table= $main::job_queue_tn;
151 my $sql_statement = "DELETE FROM $table";
152 my $db_res = $main::job_db->del_dbentry($sql_statement);
153 if( not $db_res > 0 ) { $error++; };
155 if( $error == 0 ) {
156 $out_xml = "<xml><header>answer</header><source>$target</source><target>$source</target><answer1>0</answer1><session_id>$session_id</session_id></xml>";
157 }
158 my @out_msg_l = ( $out_xml );
159 return @out_msg_l;
160 }
163 sub update_status_jobdb_entry {
164 my ($msg, $msg_hash, $session_id) = @_ ;
165 my $target = @{$msg_hash->{'target'}}[0];
166 my $source = @{$msg_hash->{'source'}}[0];
168 my $error= 0;
169 my $out_xml= "<xml><header>answer</header><source>$target</source><target>$source</target><answer1>1</answer1><session_id>$session_id</session_id></xml>";
171 my @len_hash = keys %{$msg_hash};
172 if( 0 == @len_hash) { $error++; };
174 # prepare query sql statement
175 if( $error == 0) {
176 my $table= $main::job_queue_tn;
177 my $where= &get_where_statement($msg, $msg_hash);
178 my $update= &get_update_statement($msg, $msg_hash);
180 # conditions
181 # no timestamp update if status eq waiting
182 my $sql_statement = "SELECT * FROM $table $where AND status='processing'";
183 my $res_hash = $main::job_db->select_dbentry($sql_statement);
184 if( (0 != keys(%$res_hash)) && ($update =~ /timestamp/i) ) {
185 $error ++;
186 $out_xml = "<answer1>1</answer1><error_string>there is no timestamp update allowed while status is 'processing'</error_string>";
187 }
189 if( $error == 0 ) {
190 my $sql_statement = "UPDATE $table $update $where";
191 # execute db query
192 my $db_res = $main::job_db->update_dbentry($sql_statement);
194 # check success of db update
195 if( not $db_res > 0 ) { $error++; };
196 }
197 }
199 if( $error == 0) {
200 $out_xml = "<answer1>0</answer1>";
201 }
203 my $out_msg = sprintf("<xml><header>answer</header><source>%s</source><target>%s</target>%s<session_id>$session_id</session_id></xml>", $target, $source, $out_xml);
204 my @out_msg_l = ( $out_msg );
205 return @out_msg_l;
206 }
208 1;