Framework/DB.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Framework/Errorcodes.pm ↑ Summary Framework/Auth.pm »
1 #!/usr/bin/perl -I../lib -w
2  
3 # a database module abstracting the "magic" of what we need for the
4 # general daemon
5 # the static config file is in etc/db.conf
6  
7 package Net::Server::Framework::DB;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use Switch;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Net::Server::Framework::Config;
13 use Data::Dumper;
14 use warnings;
15 use Carp;
16 use vars qw(@EXPORT $VERSION);
17  
18 our ($VERSION) = '1.25';
19  
20 sub dbconnect {
21 my $scope = shift;
22 my $conf = Net::Server::Framework::Config::file2hash("etc/db.conf");
23 my $host;
24 my $dsn;
25 switch ( $conf->{ $scope . "_type" } ) {
26 case /sqlite/ {
27 $dsn = "DBI:SQLite:dbname=" . $conf->{ $scope . "_host" };
28 }
29 case /mysql/ {
30 if ( $conf->{ $scope . "_host" } =~ m{sock$}xm ) {
31 $host = "mysql_socket=" . $conf->{ $scope . "_host" };
32 }
33 else {
34 $host = "host=" . $conf->{ $scope . "_host" };
35 }
36 $dsn = "DBI:mysql:database=" . $conf->{ $scope . "_db" } . q{:} . $host;
37 }
38 case /pgsql/ {
39 if ( $conf->{ $scope . "_host" } =~ m{local$}xm ) {
40 $host = q{};
41 }
42 else {
43 $host = "host=" . $conf->{ $scope . "_host" };
44 }
45 $dsn = "DBI:Pg:dbname=" . $conf->{ $scope . "_db" } . q{;} . $host;
46 }
47 }
48  
49 #$log->debug("HOST: $host");
50 my $dbh = DBI->connect(
51 $dsn,
52 $conf->{ $scope . "_user" } || q{},
53 $conf->{ $scope . "_pass" } || q{},
54 {
55 RaiseError => 0,
56 PrintError => 1,
57 AutoCommit => 1
58 }
59 ) or carp( "Error while connecting " . DBI::errstr );
60  
61 # or die;
62 return $dbh;
63 }
64  
65 sub get {
66 my $request = shift;
67  
68 my $select;
69 switch ( $request->{key} ) {
70  
71 # SELECT statement go here ############
72 case "ID" { $select = qq /SELECT id AS _one, hash, ts, status FROM spool WHERE id=?/; }
73 case "virgin" { $select = qq /SELECT id AS _one, hash, ts, status FROM spool WHERE id=? AND status <> 'virgin'/; }
74 case "host" { $select = qq /SELECT host AS _one, port FROM services WHERE service = ?/; }
75 case "status" { $select = qq /SELECT service AS _one, host, port FROM services WHERE status = ?/; }
76 case "service" { $select = qq /SELECT service AS _one, host, port, lastcheck, startup, status FROM services WHERE service = ?/; }
77 case "auth" { $select = qq /SELECT username AS _one, password FROM users WHERE username = ? AND active=TRUE/; }
78 else { return "not implemented" }
79 #######################################
80 }
81 my $sth = $request->{dbh}->prepare($select);
82 if ( defined $request->{term} ) {
83 $sth->execute( $request->{term} );
84 }
85 else {
86 $sth->execute();
87 }
88 my $result;
89 while ( my $line = $sth->fetchrow_hashref() ) {
90 my ( $ONE, $TWO );
91 $ONE = delete( $line->{_one} );
92 if ( exists $line->{_two} ) { $TWO = delete( $line->{_two} ) }
93 my ( $h1, $h2 );
94 foreach my $key ( keys %{$line} ) {
95 if ($TWO) {
96 foreach my $key ( keys %{$line} ) {
97 if ( defined $request->{array} ) {
98 $h2->{$TWO}->{$key} = $line->{$key};
99 }
100 else {
101 $result->{$ONE}->{$TWO}->{$key} = $line->{$key};
102 }
103 }
104 }
105 else {
106 if ( defined $request->{array} ) {
107 $h1->{$ONE}->{$key} = $line->{$key};
108 }
109 else {
110 $result->{$ONE}->{$key} = $line->{$key};
111 }
112 }
113 }
114 if ( defined $request->{array} ) {
115 if ( defined $h1 ) {
116 push( @{ $result->{$ONE} }, $h1->{$ONE} );
117 }
118 if ( defined $h2 ) {
119 push( @{ $result->{$ONE}->{$TWO} }, $h2->{$TWO} );
120 }
121 }
122 }
123 return $result;
124 }
125  
126 sub put {
127 my $request = shift;
128  
129 my ( $key, $value );
130 foreach my $k ( keys %{ $request->{data} } ) {
131 if ( $request->{data}->{$k} =~ m{^\d+$}xm ) {
132 $key .= $k . q{,};
133 $value .= $request->{data}->{$k} . q{,};
134 }
135 elsif ( $request->{data}->{$k} =~ m{^[A-Z]+\(\)$}xm ) {
136 $key .= $k . q{,};
137 $value .= $request->{data}->{$k} . q{,};
138 }
139 else {
140 $key .= $k . q{,};
141 $value .= q{'} . $request->{data}->{$k} . q{',};
142 }
143 }
144 chop($key);
145 chop($value);
146 my $string = 'INSERT INTO ';
147 $string = 'REPLACE INTO ' if (defined $request->{replace_into});
148 my $insert =
149 $string . $request->{table} . " ($key) VALUES ($value)";
150 print STDERR $insert . "\n";
151 $request->{dbh}->do($insert)
152 or carp(DBI::errstr);
153 #TODO test this line with all DBs!
154 #my $id = $request->{dbh}->last_insert_id(undef,undef,$request->{table},undef);
155 #return ($id ? $id : 0);
156 return ;
157 }
158  
159 sub do {
160 my $request = shift;
161 my $statement;
162  
163 switch ( $request->{key} ) {
164 case "del" { $statement = qq /DELETE FROM spool WHERE ID = ?/; }
165 case "vacuum" { $statement = qq /VACUUM/; }
166 else { return "not implemented" }
167  
168 }
169  
170 my $sth = $request->{dbh}->prepare($statement)
171 or carp(DBI::errstr);
172 if ( defined $request->{term} ) {
173 $sth->execute( $request->{term} );
174 }
175 else {
176 $sth->execute();
177 }
178 return 0;
179 }
180  
181 1;
182  
183 =head1 NAME
184  
185 Net::Server::Framework::DB - DBI wrapper for Net::Server::Framework
186 based daemons
187  
188  
189 =head1 VERSION
190  
191 This documentation refers to C<Net::Server::Framework::Auth> version 1.25.
192  
193  
194 =head1 SYNOPSIS
195  
196 A typical invocation looks like this:
197  
198 my $dbh = Net::Server::Framework::DB::dbconnect($DB);
199 my $res = Net::Server::Framework::DB::get( {
200 dbh => $dbh,
201 key => 'host',
202 term => $service
203 } );
204  
205  
206 =head1 DESCRIPTION
207  
208 This is a lib that is used to collect all SQL statements from one
209 application. Typically you end up with one DB.pm for many daemons that
210 are used in one project. The DB.pm provided with the package covers only
211 the internal functions needed by the platform itself like registry and
212 queue. The syntax is really minimal and that was the purpose. Small easy
213 queries are fast for high load environments.
214  
215 =head1 BASIC METHODS
216  
217 The commands accepted by the lib are:
218  
219 =head2 dbconnect
220  
221 Connect to a database. Supported databases in the moment are SQLite
222 (default) MySQL and PostgreSQL. Adding more databases is straight
223 forward.
224  
225 =head2 get
226  
227 Define your queries here. This is the central place for SQL in a
228 application written in C<Net::Server::Framework>. All queries have an
229 identifier and are limited to a maximum of three dimensions in the
230 output hash.
231  
232 =head2 put
233  
234 This inserts data into the database
235  
236 =head2 do
237  
238 All stuff that is not an insert or an update goes here. Predefined
239 queries as in get.
240  
241 =head1 CONFIGURATION AND ENVIRONMENT
242  
243 The library needs a working etc/db.conf file.
244  
245 =head1 BUGS AND LIMITATIONS
246  
247 There are no known bugs in this module.
248 Please report problems to
249 Lenz Gschwendtner ( <lenz@springtimesoft.com> )
250 Patches are welcome.
251  
252 =head1 AUTHOR
253  
254 Lenz Gschwendtner ( <lenz@springtimesoft.com> )
255  
256  
257  
258 =head1 LICENCE AND COPYRIGHT
259  
260 Copyright (c)
261 2007 Lenz Gschwerndtner ( <lenz@springtimesoft.comn> )
262 All rights reserved.
263  
264 This module is free software; you can redistribute it and/or
265 modify it under the same terms as Perl itself. See L<perlartistic>.
266  
267 This program is distributed in the hope that it will be useful,
268 but WITHOUT ANY WARRANTY; without even the implied warranty of
269 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
« Framework/Errorcodes.pm ↑ Summary Framework/Auth.pm »