IO/GenLib.pm

filter: Statementstmt 59.2% 32 / 54 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 4 / 9 Podpod 100% 5 / 5 totaltotal 52.5% 41 / 78 CC 7 SCAR 21.0
CC
7
Cov
50%
normalize_date
20.0
normalize_email
6.0
isreal
6.0
CRAP 8.1
« IO/Table.pm ↑ Summary IO/Mask.pm »
1 # GenLib.pm
2 #
3 # $Id: GenLib.pm,v 1.4 2003/07/16 23:22:04 rsandberg Exp $
4 #
5  
6 package DBIx::IO::GenLib;
7  
8 BEGIN
9 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 @ISA = qw(Exporter);
13  
14 1 @EXPORT = qw(
15 normalize_email
16 normalize_date
17 local_normal_sysdate
18 isreal
19 inint
20 $LONG_READ_LENGTH
21 $UNKNOWN_DATE_FORMAT
22 $DATETIME_TYPE
23 $NUMERIC_TYPE
24 $CHAR_TYPE
25 $ROWID_TYPE
26 $LONG_TYPE
27 $LOB_TYPE
28 $BLOB_TYPE
29 $CLOB_TYPE
30 $DATE_TYPE
31 $TIME_TYPE
32 $YEAR_TYPE
33 $EMPTY_STRING
34 );
35  
36 1 %EXPORT_TAGS =
37 (
38 actions =>
39 [qw(
40 $UPDATE_ACTION
41 $READ_ACTION
42 $INSERT_ACTION
43 $DELETE_ACTION
44 )],
45 );
46  
47 1 Exporter::export_ok_tags qw(
48 actions
49 );
50 }
51  
52 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # CONSTANTS
56  
57 # Action constants for DBIx::IO::Restrict
58 1 *DBIx::IO::GenLib::UPDATE_ACTION = \"U";
59 1 *DBIx::IO::GenLib::READ_ACTION = \"S";
60 1 *DBIx::IO::GenLib::INSERT_ACTION = \"I";
61 1 *DBIx::IO::GenLib::DELETE_ACTION = \"D";
62  
63 # Date formats
64 1 *DBIx::IO::GenLib::UNKNOWN_DATE_FORMAT = \'UNKNOWN';
65  
66 # Set the maximum memory used to retrieve LONG or LOB datatypes from the db
67 1 *DBIx::IO::GenLib::LONG_READ_LENGTH = \1000000;
68  
69 # Data type identifiers
70 1 *DBIx::IO::GenLib::DATETIME_TYPE = \'DATETIME';
71 1 *DBIx::IO::GenLib::CHAR_TYPE = \'CHAR';
72 1 *DBIx::IO::GenLib::NUMERIC_TYPE = \'NUMERIC';
73 1 *DBIx::IO::GenLib::ROWID_TYPE = \'ROWID';
74 1 *DBIx::IO::GenLib::LOB_TYPE = \'LOB';
75 1 *DBIx::IO::GenLib::CLOB_TYPE = \'CLOB';
76 1 *DBIx::IO::GenLib::BLOB_TYPE = \'BLOB';
77 1 *DBIx::IO::GenLib::LONG_TYPE = \'LONG';
78  
79 1 *DBIx::IO::GenLib::DATE_TYPE = \'DATE';
80 1 *DBIx::IO::GenLib::TIME_TYPE = \'TIME';
81 1 *DBIx::IO::GenLib::YEAR_TYPE = \'YEAR';
82  
83 # Special empty string to distinguish it from NULL values of ''
84 1 *DBIx::IO::GenLib::EMPTY_STRING = \"\0\0\0\0";
85  
86 =head1 NAME
87  
88 DBIx::IO::GenLib - General helper functions and constants for database apps.
89  
90 =head1 SYNOPSIS
91  
92
93 use DBIx::IO::GenLib;
94 use DBIx::IO::GenLib (); # Don't import default symbols
95 use DBIx::IO::GenLib qw(:tag symbol...) # Import selected symbols
96  
97  
98 =head2 Functions
99  
100 $normal_email = normalize_email($email_address);
101  
102 @normal_dates = normalize_date(@dates_in_any_format);
103 $normal_date = normalize_date($date_in_any_format);
104  
105 $normal_sysdate = local_normal_sysdate();
106  
107 $bool = isreal($scalar);
108  
109 $bool = isint($scalar);
110  
111 =head2 Constants
112  
113 $UPDATE_ACTION
114 $READ_ACTION
115 $INSERT_ACTION
116 $DELETE_ACTION
117  
118 $LONG_READ_LENGTH
119  
120 $UNKNOWN_DATE_FORMAT
121  
122 $DATETIME_TYPE
123 $NUMERIC_TYPE
124 $CHAR_TYPE
125 $ROWID_TYPE
126 $LONG_TYPE
127 $LOB_TYPE
128 $BLOB_TYPE
129 $CLOB_TYPE
130 $DATE_TYPE
131 $TIME_TYPE
132 $YEAR_TYPE
133  
134 $EMPTY_STRING
135  
136 =head1 DESCRIPTION
137  
138 This package contains miscellaneous functions for help in dealing with DBI and related DBIx::IO packages.
139 See $NORMAL_DATETIME_FORMAT for a discussion of the canonical date format, functions are also provided
140 to convert dates to this format.
141  
142 =head1 DETAILS
143  
144 =over 4
145  
146 =head2 Functions
147  
148 =item C<normalize_email>
149  
150 $normal_email = normalize_email($email_address)
151  
152 $email_address will be normalized using the following method.
153 1. Stripped of leading whitespace.
154 2. If whitespace still exists, $email_address will be truncated
155 at that point.
156 3. Stripped of NUL characters "\0"
157 4. Converted to lower-case
158 5. Bounding < and > are removed
159 6. Any non-ascii characters at the end of the address are removed
160  
161 This is useful in doing comparisons.
162 No attempt is made to validate the address.
163  
164 =cut
165 sub normalize_email
166 0 ▶{
Subroutine normalize_email: not called CC=2 SCAR=17.9
Pod documented
167 0 my $email = shift;
168 0 $email =~ s/^\s+//;
169 0 $email =~ s/\s.*$//;
170 0 $email =~ s/\000+//g;
171 0 $email = lc($email);
172 0 ▶ if ($email =~ /^[\]\[}{)(><'"].*[\]\[}{)(><'"]$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ /^[\]\[}{)(><'"].*[\]\[}{)(><'"]$/) 0 0 0
173 {
174 0 $email = substr($email,1,length($email)-2);
175 }
176 0 $email =~ s/[^a-z]+$//;
177 0 return ($email);
178 # $email =~ s/^\s+|\s+$//;
179 # $email =~ s/\s+//g unless $email =~ /\@.*\@/;
180 # $email =~ s/\000+//g;
181 # $clean_vals{EMAIL} = $1 if
182 # ($email) =~ /([^\,\s\@:\?\#\*\"\'\)\(\!\&\;\[\]\=\\\/]+[\@][^\,\s\@:\?\#\*\"\'\)\(\!\&\;\[\]\=\\\/]+)/;
183 # return undef;
184 }
185  
186 =pod
187  
188 =item C<normalize_date>
189  
190 @normal_dates = normalize_date(@dates_in_any_format)
191 $normal_date = normalize_date($date_in_any_format)
192  
193 This function normalizes dates in almost any imaginable
194 format (with the help of Date::Manip). Dates are returned
195 in the normalized format (described elsewhere in this document).
196 If the format of the input date isn't recognized (not likely)
197 the corresponding output date is returned as undef.
198  
199 CAUTION: The corresponding Date::Manip::UnixDate call is slow, if performance
200 is a concern then prepare the date formats ahead of time and don't use
201 this function.
202 Because of Date::Manip's size, it will only be loaded via require
203 if this particular function is used.
204  
205 =cut
206 sub normalize_date
207 0 ▶{
Subroutine normalize_date: not called CC=4 SCAR=30.0
Pod documented
208 0 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
209 0 require Date::Manip;
210 0 my @ret;
211 my $date;
212 0 foreach $date (@_)
213 {
214 0 push @ret, Date::Manip::UnixDate($date,'%q');
215 }
216 0 ▶ return $ret[0] if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
217 0 return @ret;
218 }
219  
220 =pod
221  
222 =item C<local_normal_sysdate>
223  
224 $normal_sysdate = local_normal_sysdate()
225  
226 Return the current date and time
227 in the normalized format (described elsewhere in this document)
228 for use with easy date comparisons.
229  
230 =cut
231 sub local_normal_sysdate
232 0 ▶{
Subroutine local_normal_sysdate: not called CC=1 SCAR=6.9
Pod documented
233 0 my $sysdate = POSIX::strftime('%Y%m%d%H%M%S',localtime()); # Today's date in YYYYMMDDHH24MISS format
234 0 return $sysdate;
235 }
236  
237 =pod
238  
239 =item C<isreal>
240  
241 $bool = isreal($scalar);
242  
243 Return true if $scalar is a real number.
244  
245 =cut
246 sub isreal
247 0 ▶{
Subroutine isreal: not called CC=2 SCAR=17.9
Pod documented
248 0 my ($val) = @_;
249 0 ▶ return ($val =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ? 1 : 0); # from the camel book
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ? : 0 0 0
250 }
251  
252 =pod
253  
254 =item C<isint>
255  
256 $bool = isint($scalar);
257  
258 Return true if $scalar is an integer (signed or unsigned).
259  
260 =cut
261 sub isint
262 0 ▶{
Subroutine isint: not called CC=2 SCAR=17.9
Pod documented
263 0 my ($val) = @_;
264 0 ▶ return ($val =~ /^[+-]?\d+$/ ? 1 : 0); # from the camel book
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /^[+-]?\d+$/ ? : 0 0 0
265 }
266  
267 =pod
268  
269 =head2 Constants
270  
271 Set the maximum memory used to retrieve LONG or LOB datatypes.
272  
273 $LONG_READ_LENGTH = 1000000
274  
275 A string recognized by DBIx::IO::qualify() to convert the date format
276  
277 $UNKNOWN_DATE_FORMAT = 'UNKNOWN'
278  
279 A special string to be distinguished from the special NULL value of ''
280  
281 $EMPTY_STRING = "\0\0\0\0"
282  
283 =item B<Action Constants>
284  
285 These are the allowed values for any function requiring an $action in DBIx::IO::Restrict and related modules:
286  
287 $UPDATE_ACTION = "U"
288 $READ_ACTION = "R"
289 $INSERT_ACTION = "I"
290 $DELETE_ACTION = "D"
291  
292 =item B<Data Types>
293  
294 Data types are represented by the following constants.
295 These are useful in IO::qualify().
296 ##at these lists are incomplete
297  
298 $DATETIME_TYPE
299 $NUMERIC_TYPE
300 $CHAR_TYPE
301 $LONG_TYPE
302 $LOB_TYPE
303  
304 Oracle only:
305 $BLOB_TYPE
306 $CLOB_TYPE
307 $ROWID_TYPE
308  
309 NOTE: LOB types can be inserted/updated but not selected through DBD::Oracle (Version 1.19).
310 If you need to retrieve such columns through DBI, I suggest converting the data type to LONG by rebuilding the table.
311  
312  
313 MySQL Only:
314 $DATE_TYPE
315 $TIME_TYPE
316 $YEAR_TYPE
317  
318 =back
319  
320 =head2 Driver Specific
321  
322 The following are driver specific constants and can be loaded as
323  
324 use DBIx::IO::XXXLib (...);
325 e.g.
326 use DBIx::IO::OracleLib ();
327  
328 The format string, which gives the canonical date format used throughout this and related db packages (DBIx::IO)
329 allows for date comparisons via numerical operators. This is also useful so that all date I/O is normalized in one format.
330  
331 Oracle:
332 $NORMAL_DATETIME_FORMAT = 'YYYYMMDDHH24MISS'
333  
334 MySQL:
335 $NORMAL_DATETIME_FORMAT = '%Y%m%d%H%i%S'
336 $NORMAL_DATE_FORMAT = '%Y%m%d'
337 $NORMAL_TIME_FORMAT = '%H%i%S'
338  
339  
340 Oracle pseudo columns. This is recognized as a column name that always has a datatype of $ROWID_TYPE:
341 $ROWID_COL_NAME = 'ROWID'
342  
343  
344 =head1 SYMBOL IMPORTING
345  
346 =head2 Default
347  
348 These symbols are exported by default by this package:
349  
350 normalize_email
351 normalize_date
352 local_normal_sysdate
353 isreal
354 inint
355 $LONG_READ_LENGTH
356 $UNKNOWN_DATE_FORMAT
357 $EMPTY_STRING
358  
359 $DATETIME_TYPE
360 $NUMERIC_TYPE
361 $CHAR_TYPE
362 $ROWID_TYPE
363 $LONG_TYPE
364 $LOB_TYPE
365 $BLOB_TYPE
366 $CLOB_TYPE
367 $DATE_TYPE
368 $TIME_TYPE
369 $YEAR_TYPE
370  
371 All symbols from driver-specific modules are exported by default.
372  
373 =head2 Tags
374  
375 These tags can be used to import the corresponding symbols:
376  
377 =over 4
378  
379 =item C<actions>
380  
381 $UPDATE_ACTION
382 $READ_ACTION
383 $INSERT_ACTION
384 $DELETE_ACTION
385  
386 =back
387  
388 =cut
389  
390 1 1;
391  
« IO/Table.pm ↑ Summary IO/Mask.pm »