line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#=============================== ldconfig.pm ================================= |
2
|
|
|
|
|
|
|
# Filename: ldconfig.pm |
3
|
|
|
|
|
|
|
# Description: Load configuration variables from database. |
4
|
|
|
|
|
|
|
# Original Author: Dale M. Amon |
5
|
|
|
|
|
|
|
# Revised by: $Author: amon $ |
6
|
|
|
|
|
|
|
# Date: $Date: 2008-08-28 23:14:03 $ |
7
|
|
|
|
|
|
|
# Version: $Revision: 1.8 $ |
8
|
|
|
|
|
|
|
# License: LGPL 2.1, Perl Artistic or BSD |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# Independance guarantee: No function placed in here calls any other *local* |
11
|
|
|
|
|
|
|
# modules and thus will be unaffected by changes in abstraction layer API's. |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
#============================================================================= |
14
|
1
|
|
|
1
|
|
7759
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
71
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
package DMA::ldconfig; |
17
|
1
|
|
|
1
|
|
6
|
use Exporter (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
18
|
1
|
|
|
1
|
|
6
|
use vars qw{@ISA},qw{@EXPORT}; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
88
|
|
19
|
|
|
|
|
|
|
@ISA = qw (Exporter); |
20
|
|
|
|
|
|
|
@EXPORT = qw(load_local_configuration |
21
|
|
|
|
|
|
|
); |
22
|
1
|
|
|
1
|
|
3223
|
use DBI; |
|
1
|
|
|
|
|
43101
|
|
|
1
|
|
|
|
|
507
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#============================================================================= |
25
|
|
|
|
|
|
|
# Exported Routines |
26
|
|
|
|
|
|
|
#============================================================================= |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub load_local_configuration { |
29
|
0
|
|
|
0
|
1
|
|
my ($db,$usr,$pass) = @_; |
30
|
0
|
0
|
|
|
|
|
defined $db || (return 0); |
31
|
0
|
0
|
|
|
|
|
defined $usr || (return 0); |
32
|
0
|
0
|
|
|
|
|
defined $pass || (return 0); |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my ($dbh,$sth,@row,$expr); |
35
|
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
|
($dbh = DBI->connect ("DBI:mysql:${db}", $usr, $pass)) || (return 0); |
37
|
0
|
0
|
|
|
|
|
($sth = $dbh->prepare ("SELECT * FROM configuration")) || (return 0); |
38
|
0
|
0
|
|
|
|
|
($sth->execute()) || (return 0); |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
while (@row = $sth->fetchrow_array) { |
41
|
0
|
|
|
|
|
|
$expr = '$::CFG_' . $row[0] . ' = "' . $row[1] . '";'; |
42
|
0
|
|
|
|
|
|
eval $expr; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
$sth->finish; |
46
|
0
|
|
|
|
|
|
$dbh->disconnect; |
47
|
0
|
|
|
|
|
|
return 1; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#============================================================================== |
51
|
|
|
|
|
|
|
# Pod Documentation |
52
|
|
|
|
|
|
|
#============================================================================== |
53
|
|
|
|
|
|
|
# You may extract and format the documentation section with the 'perldoc' cmd. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 NAME |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
DMA::ldconfig - Load configuration variables from database. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 SYNOPSIS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
use DMA::ldconfig |
62
|
|
|
|
|
|
|
$t = load_local_configuration ($db, $usr, $pass); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 Inheritance |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
None. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 Description |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Most programs have a need for configuration and this lets you keep all of |
71
|
|
|
|
|
|
|
your configuration data in a MySQL database. To use it there must be a |
72
|
|
|
|
|
|
|
configuration Table defined this way: |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
CREATE TABLE configuration ( |
75
|
|
|
|
|
|
|
Name VARCHAR(100) NOT NULL, |
76
|
|
|
|
|
|
|
Value VARCHAR(100) NOT NULL, |
77
|
|
|
|
|
|
|
PRIMARY KEY (Name)); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
If you are setting this up for the first time, you might do something like |
80
|
|
|
|
|
|
|
the following. First make sure the user of this database has a password set. |
81
|
|
|
|
|
|
|
You can set it: |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
mysqladmin -u cfguser --password= password foobaz |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
or change it: |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
mysqladmin -u cfguser --password=foobaz password newbar |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Then create the database and load your definition file |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
mysqladmin -u cfguser --password=newbar create mydatabase |
92
|
|
|
|
|
|
|
mysql -u cfguser --password=newbar < base.mysql |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Where 'base.mysql' (or whatever) contains the lines needed to set up the |
95
|
|
|
|
|
|
|
database: |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
use mydatabase; |
98
|
|
|
|
|
|
|
CREATE TABLE configuration ( |
99
|
|
|
|
|
|
|
Name VARCHAR(100) NOT NULL, |
100
|
|
|
|
|
|
|
Value VARCHAR(100) NOT NULL, |
101
|
|
|
|
|
|
|
PRIMARY KEY (Name)); |
102
|
|
|
|
|
|
|
INSERT INTO configuration VALUES ("MYVAR", "True"); |
103
|
|
|
|
|
|
|
INSERT INTO configuration VALUES ("MYNUMVAR", "5"); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Note that DMA::ldconfig imports its single function into your name space. |
106
|
|
|
|
|
|
|
You should run it in the MAIN namespace so all of your configuration variable |
107
|
|
|
|
|
|
|
are imported into that namespace for easy global usage. To avoid the |
108
|
|
|
|
|
|
|
possibility of namespace collision you should not use a prefix of '$::CFG_' |
109
|
|
|
|
|
|
|
on any of your variables. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
After running load_local_configuration ("mydbname", "cfguser", "newbar") with |
112
|
|
|
|
|
|
|
the above example, your main namespace would contain the scalar variable Names |
113
|
|
|
|
|
|
|
$::CFG_MYVAR and $::CFG_MYNUMVAR initialized to the Values shown in the table |
114
|
|
|
|
|
|
|
example. By re-running this routine you can dynamically update your default |
115
|
|
|
|
|
|
|
values, a feature which is both useful and potentially perilous. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
If it is not obvious, to use this class you must have a MySQL database server |
118
|
|
|
|
|
|
|
running and accepting localhost connections. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Since the class is based on the Perl DBI class, it would be very easy to |
121
|
|
|
|
|
|
|
change to a different database. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Note that MySQL requires a license for commercial use. The licenses are quite |
124
|
|
|
|
|
|
|
inexpensive. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 Examples |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
use DMA::ldconfig |
129
|
|
|
|
|
|
|
$t = load_local_configuration ("mydbname", "cfguser", "newbar"); |
130
|
|
|
|
|
|
|
if ($::CFG_MYVAR eq "True") {print "Yep, we got it!"); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 Variables |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
None. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 Functions |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=over 4 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item B<$t = load_local_configuration ($db, $usr, $pass)> |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Load the contents of a configuration table taken from local MySQL database |
143
|
|
|
|
|
|
|
$db. Connect to it as user $usr with password $pass. For every Name and |
144
|
|
|
|
|
|
|
Value pair in the table's records, generate $::CFG_thisname = $thisvalue. |
145
|
|
|
|
|
|
|
Return true if it this succeeds. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=back 4 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 Private Functions |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
None. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 KNOWN BUGS |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
See TODO. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 SEE ALSO |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
DBI |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 AUTHOR |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Dale Amon |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
#============================================================================= |
168
|
|
|
|
|
|
|
# CVS HISTORY |
169
|
|
|
|
|
|
|
#============================================================================= |
170
|
|
|
|
|
|
|
# $Log: ldconfig.pm,v $ |
171
|
|
|
|
|
|
|
# Revision 1.8 2008-08-28 23:14:03 amon |
172
|
|
|
|
|
|
|
# perldoc section regularization. |
173
|
|
|
|
|
|
|
# |
174
|
|
|
|
|
|
|
# Revision 1.7 2008-08-15 21:47:52 amon |
175
|
|
|
|
|
|
|
# Misc documentation and format changes. |
176
|
|
|
|
|
|
|
# |
177
|
|
|
|
|
|
|
# Revision 1.6 2008-04-18 14:07:54 amon |
178
|
|
|
|
|
|
|
# Minor documentation format changes |
179
|
|
|
|
|
|
|
# |
180
|
|
|
|
|
|
|
# Revision 1.5 2008-04-11 22:25:23 amon |
181
|
|
|
|
|
|
|
# Add blank line after cut. |
182
|
|
|
|
|
|
|
# |
183
|
|
|
|
|
|
|
# Revision 1.4 2008-04-11 18:56:35 amon |
184
|
|
|
|
|
|
|
# Fixed quoting problem with formfeeds. |
185
|
|
|
|
|
|
|
# |
186
|
|
|
|
|
|
|
# Revision 1.3 2008-04-11 18:39:15 amon |
187
|
|
|
|
|
|
|
# Implimented new standard for headers and trailers. |
188
|
|
|
|
|
|
|
# |
189
|
|
|
|
|
|
|
# Revision 1.2 2008-04-10 15:01:08 amon |
190
|
|
|
|
|
|
|
# Added license to headers, removed claim that the documentation section still |
191
|
|
|
|
|
|
|
# relates to the old doc file. |
192
|
|
|
|
|
|
|
# |
193
|
|
|
|
|
|
|
# 20041105 Dale Amon |
194
|
|
|
|
|
|
|
# Generalized, added error checking |
195
|
|
|
|
|
|
|
# and documentation. |
196
|
|
|
|
|
|
|
# |
197
|
|
|
|
|
|
|
# 20000120 Dale Amon |
198
|
|
|
|
|
|
|
# Created. |
199
|
|
|
|
|
|
|
1; |