line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Gantry::Utils::SQL; |
2
|
3
|
|
|
3
|
|
1371
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
130
|
|
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
19
|
use Carp qw( confess ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
2503
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
############################################################ |
7
|
|
|
|
|
|
|
# Variables # |
8
|
|
|
|
|
|
|
############################################################ |
9
|
|
|
|
|
|
|
############################################################ |
10
|
|
|
|
|
|
|
# Functions # |
11
|
|
|
|
|
|
|
############################################################ |
12
|
|
|
|
|
|
|
sub new { |
13
|
0
|
|
|
0
|
1
|
|
my ( $class, $opt ) = @_; |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
my $self = {}; |
16
|
0
|
|
|
|
|
|
bless( $self, $class ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# populate self with data from site |
19
|
0
|
|
|
|
|
|
return( $self ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
} # end new |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
#------------------------------------------------- |
24
|
|
|
|
|
|
|
# $sql_helper->sql_bool( $bool_string ) |
25
|
|
|
|
|
|
|
#------------------------------------------------- |
26
|
|
|
|
|
|
|
sub sql_bool { |
27
|
0
|
|
|
0
|
1
|
|
my( $self, $input ) = shift; |
28
|
|
|
|
|
|
|
|
29
|
0
|
0
|
0
|
|
|
|
return( 'FALSE' ) if ( ( ! defined $input ) || ( ! length $input ) ); |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
|
if ( $input =~ /^(t|y|1)$/i ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
return( 'TRUE' ); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
elsif ( $input =~ /^(f|n|0)$/i ) { |
35
|
0
|
|
|
|
|
|
return( 'FALSE' ); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
elsif ( defined $input ) { |
38
|
0
|
|
|
|
|
|
return( 'TRUE' ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else { |
41
|
0
|
|
|
|
|
|
return( 'FALSE' ); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
} # END sql_bool |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
#------------------------------------------------- |
47
|
|
|
|
|
|
|
# $sql_helper->sql_insert( $table, %data ) |
48
|
|
|
|
|
|
|
#------------------------------------------------- |
49
|
|
|
|
|
|
|
sub sql_insert { |
50
|
0
|
|
|
0
|
1
|
|
my ( $self, $table, @vals ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
my ( @fields, @values ); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
while ( @vals ) { |
55
|
0
|
|
|
|
|
|
push ( @fields, shift( @vals ) ); |
56
|
|
|
|
|
|
|
|
57
|
0
|
0
|
|
|
|
|
if ( @vals ) { |
58
|
0
|
|
|
|
|
|
push( @values, shift( @vals ) ); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
0
|
|
|
|
|
|
confess( 'Error: Incorrect number of arguements.' ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
return( "INSERT INTO $table ( ". join( ', ', @fields ). ' ) VALUES ( '. |
66
|
|
|
|
|
|
|
join( ', ', @values ). ' )' ); |
67
|
|
|
|
|
|
|
} # END sql_insert |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#------------------------------------------------- |
70
|
|
|
|
|
|
|
# $sql_helper->sql_num( $number ) |
71
|
|
|
|
|
|
|
#------------------------------------------------- |
72
|
|
|
|
|
|
|
sub sql_num { |
73
|
0
|
|
|
0
|
1
|
|
my ( $self, $number ) = ( shift, shift ); |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
0
|
|
|
|
return( 'NULL' ) if ( ( ! defined $number ) || ! length ( $number) ); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$number =~ s/\\/\\\\/g; |
78
|
0
|
|
|
|
|
|
$number =~ s/\'/\\'/g; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return( "'$number'" ); |
81
|
|
|
|
|
|
|
} # END sql_num |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#------------------------------------------------- |
84
|
|
|
|
|
|
|
# $sql_helper->sql_str( $string ) |
85
|
|
|
|
|
|
|
#------------------------------------------------- |
86
|
|
|
|
|
|
|
sub sql_str { |
87
|
0
|
|
|
0
|
1
|
|
my( $self, $string ) = ( shift, shift ); |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
0
|
|
|
|
return( "''" ) if ( !defined( $string ) || ! length( $string ) ); |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
$string =~ s/\\/\\\\/g; |
92
|
0
|
|
|
|
|
|
$string =~ s/\'/\\'/g; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return( "'$string'" ); |
95
|
|
|
|
|
|
|
} # END sql_str |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
#------------------------------------------------- |
98
|
|
|
|
|
|
|
# $sql_helper->sql_update( $table, $clause, $data ) |
99
|
|
|
|
|
|
|
#------------------------------------------------- |
100
|
|
|
|
|
|
|
sub sql_update { |
101
|
0
|
|
|
0
|
1
|
|
my ( $self, $table, $clause, @vals ) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
$clause = '' if ( ! defined $clause ); |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my @updates; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
while ( @vals ) { |
108
|
0
|
|
|
|
|
|
my $field = shift( @vals ); |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ( @vals ) { |
111
|
0
|
|
|
|
|
|
push( @updates, "$field=". shift( @vals ) ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
else { |
114
|
0
|
|
|
|
|
|
confess( 'Error: Incorrect number of arguements.' ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
return( "UPDATE $table SET ". join( ', ', @updates ). ' '. $clause ); |
119
|
|
|
|
|
|
|
} # END sql_update |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
#------------------------------------------------- |
122
|
|
|
|
|
|
|
# $sql_helper->sql_quote( $string ) |
123
|
|
|
|
|
|
|
#------------------------------------------------- |
124
|
|
|
|
|
|
|
sub sql_quote { |
125
|
0
|
|
|
0
|
1
|
|
my ( $self, $sql ) = ( shift, shift ); |
126
|
|
|
|
|
|
|
|
127
|
0
|
0
|
|
|
|
|
return( '' ) if ( ! defined $sql ); |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
$sql =~ s/\\/\\\\/g; |
130
|
0
|
|
|
|
|
|
$sql =~ s/'/''/g; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
return( $sql ); |
133
|
|
|
|
|
|
|
} # END sql_quote |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# EOF |
136
|
|
|
|
|
|
|
1; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
__END__ |