SQLstate.pm

filter: Statementstmt 100% 92 / 92 Branchbran 75.0% 15 / 20 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 26 / 26 Podpod 59.0% 13 / 22 totaltotal 89.3% 151 / 169 CC 14 SCAR 7.4
CC
14
Cov
94%
tokenize
4.0
is_sqlstate_exception
4.0
constantize
3.0
CRAP 2.1
↑ Summary SQLstate/wikipedia.pm »
1 package DBIx::SQLstate;
2  
3  
4  
5 =head1 NAME
6  
7 DBIx::SQLstate - message lookup and tokenization of SQL-State codes
8  
9 =head1 SYNOPSIS
10  
11 use DBI;
12 use DBIx::SQLstate;
13
14 my $dbh = DBI->connect($data_source, $username, $password,
15 {
16 HandleError => sub {
17 my $msg = shift;
18 my $h = shift;
19
20 my $state = $h->state;
21
22 my $message = sprintf("%s - %s",
23 $state, DBIx::SQLstate->token($state)
24 );
25
26 die $message;
27 }
28 }
29
30 );
31  
32 =cut
33  
34  
35  
36 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 8 our $VERSION = 'v0.0.5';
40  
41 8 our $DEFAULT_MESSAGE = 'Unknown SQL-state';
42 8 our $CONST_PREFIX ='SQLSTATE';
43  
44 8 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 8 our @EXPORT = (
47 );
48  
49 8 our @EXPORT_OK = (
50 'is_sqlstate_succes',
51 'is_sqlstate_warning',
52 'is_sqlstate_no_data',
53 'is_sqlstate_exception',
54 'sqlstate_class_codes',
55 'sqlstate_class_const',
56 'sqlstate_class_message',
57 'sqlstate_class_token',
58 'sqlstate_codes',
59 'sqlstate_const',
60 'sqlstate_default_const',
61 'sqlstate_default_message',
62 'sqlstate_default_token',
63 'sqlstate_message',
64 'sqlstate_token',
65 );
66  
67 8 our %EXPORT_TAGS = (
68 message => [
69 'sqlstate_message',
70 'sqlstate_class_message',
71 'sqlstate_default_message',
72 ],
73 token => [
74 'sqlstate_token',
75 'sqlstate_class_token',
76 'sqlstate_default_token',
77 ],
78 const => [
79 'sqlstate_const',
80 'sqlstate_class_const',
81 'sqlstate_default_const',
82 ],
83 predicates => [
84 'is_sqlstate_succes',
85 'is_sqlstate_warning',
86 'is_sqlstate_no_data',
87 'is_sqlstate_exception',
88 ],
89 );
90  
91  
92 # message
93 #
94 # a class method that returns a human readable for a given SQL-state code
95 #
96 # This will fall through from the a subclass message to a class message and at
97 # last the default. The 'message' routines use `undef` if there is no associated
98 # message found.
99 #
100 18 ▶sub message ($) {
Subroutine message: called CC=3 SCAR=11.0
Pod documented
101 18 my $class = shift;
102 18 my $sqlstate = shift;
103
104 18 for (
105 sqlstate_message($sqlstate),
106 sqlstate_class_message($sqlstate),
107 sqlstate_default_message(),
108 21 ▶ ) { return $_ if defined $_ }
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 18 3 21
109 ;
110 }
111  
112 # token
113 #
114 # a class method that will return the tokenized version of the above `message`
115 # method.
116 #
117 10 ▶sub token ($) {
Subroutine token: called CC=1 SCAR=0.0
Pod documented
118 10 my $class = shift;
119 10 my $sqlstate = shift;
120
121 10 my $message = $class->message($sqlstate);
122
123 10 return tokenize($message);
124 }
125  
126 # const
127 #
128 # a class method that will return the constant version of the above `message`
129 # method.
130 #
131 5 ▶sub const ($) {
Subroutine const: called CC=1 SCAR=0.0
Pod undocumented
132 5 my $class = shift;
133 5 my $sqlstate = shift;
134
135 5 my $message = $class->message($sqlstate);
136
137 5 return constantize($message);
138 }
139  
140  
141  
142 8 my %SQLstate = ();
143  
144  
145  
146 # sqlstate_message
147 #
148 # returns the human readable message for a known SQL-state
149 # or
150 # returns undef in all other cases (missing arg or non existent)
151 #
152 1239 ▶sub sqlstate_message ($) {
Subroutine sqlstate_message: called CC=2 SCAR=7.2
Pod documented
153 1239 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 1239 1239
154 1239 return $SQLstate{$_[0]};
155 }
156  
157  
158  
159 # sqlstate_class_message
160 #
161 # returns a human readable message for any known SQL-state
162 # or
163 # returns undef in all other cases
164 #
165 # this is typically used when there is not a known SQL-state message
166 #
167 21 ▶sub sqlstate_class_message ($) {
Subroutine sqlstate_class_message: called CC=2 SCAR=7.2
Pod documented
168 21 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 21 21
169 21 return +{ sqlstate_class_codes() }->{sqlstate_class($_[0])};
170 }
171  
172  
173  
174 # sqlstate_default_message
175 #
176 # returns the default SQL-state message
177 #
178 21 ▶sub sqlstate_default_message () {
Subroutine sqlstate_default_message: called CC=1 SCAR=0.0
Pod documented
179 21 return $DEFAULT_MESSAGE;
180 }
181  
182  
183  
184 # sqlstate_token
185 #
186 # returns a tokenized version of the sqlstate_message (or undef)
187 #
188 1 ▶sub sqlstate_token ($) {
Subroutine sqlstate_token: called CC=1 SCAR=0.0
Pod documented
189 1 return tokenize( sqlstate_message(shift) );
190 }
191  
192  
193  
194 # sqlstate_class_token
195 #
196 # returns the tokenized version of sqlstate_class_message
197 #
198 1 ▶sub sqlstate_class_token ($) {
Subroutine sqlstate_class_token: called CC=1 SCAR=0.0
Pod documented
199 1 return tokenize( sqlstate_class_message(shift) );
200 }
201  
202  
203  
204 # sqlstate_default_token
205 #
206 # returns the tokenized version of sqlstate_default_message
207 #
208 1 ▶sub sqlstate_default_token () {
Subroutine sqlstate_default_token: called CC=1 SCAR=0.0
Pod documented
209 1 return tokenize( sqlstate_default_message() );
210 }
211  
212  
213  
214 # sqlstate_const
215 #
216 # returns the constant version of sqlstate_message
217 #
218 1 ▶sub sqlstate_const ($) {
Subroutine sqlstate_const: called CC=1 SCAR=0.0
Pod undocumented
219 1 return constantize( sqlstate_message(shift) );
220 }
221  
222  
223 # sqlstate_class_const
224 #
225 # returns the constant version of sqlstate_class_message
226 #
227 1 ▶sub sqlstate_class_const ($) {
Subroutine sqlstate_class_const: called CC=1 SCAR=0.0
Pod undocumented
228 1 return constantize( sqlstate_class_message(shift) );
229 }
230  
231  
232  
233 # sqlstate_default_const
234 #
235 # returns the constant version of sqlstate_default_message
236 #
237 1 ▶sub sqlstate_default_const () {
Subroutine sqlstate_default_const: called CC=1 SCAR=0.0
Pod undocumented
238 1 return constantize( sqlstate_default_message() );
239 }
240  
241  
242  
243 # sqlstate_class
244 #
245 # returns the 2-byte code from a given 5-byte SQL-state
246 #
247 1259 ▶sub sqlstate_class ($) {
Subroutine sqlstate_class: called CC=2 SCAR=7.2
Pod documented
248 1259 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 1259 1259
249 1259 return substr($_[0],0,2);
250 }
251  
252  
253  
254 # sqlstate_codes
255 #
256 # returns a list of key=value pairs of 'registered' SQL-states codes
257 #
258 30 ▶sub sqlstate_codes () {
Subroutine sqlstate_codes: called CC=1 SCAR=0.0
Pod undocumented
259 30 return %SQLstate;
260 }
261  
262  
263 # sqlstate_known_codes
264 #
265 # returns the list of key/value pairs of all known SQL-state codes
266 #
267 8 ▶sub sqlstate_known_codes () {
Subroutine sqlstate_known_codes: called CC=1 SCAR=0.0
Pod undocumented
268 8 ▶ use DBIx::SQLstate::wikipedia;
Subroutine BEGIN: called CC=1 SCAR=0.0
269
270 return (
271 8 %DBIx::SQLstate::wikipedia::SQLstate,
272 );
273 }
274  
275  
276  
277 # sqlstate_class_codes
278 #
279 # returns a list of key/value pairs for 'registered' SQL-state classes
280 #
281 # that is, the keys are the 2-byte values of the SQL-states that end in '000'
282 #
283 21 ▶sub sqlstate_class_codes () {
Subroutine sqlstate_class_codes: called CC=1 SCAR=0.0
Pod undocumented
284 my %sqlstate_class_codes = map {
285 1218 sqlstate_class($_) => sqlstate_message($_)
286 21 } grep { /..000/ } keys %{{ sqlstate_codes() }};
287
288 21 return %sqlstate_class_codes;
289 }
290  
291  
292  
293 # tokenize
294 #
295 # turns any given string into a kind of CamelCase string
296 #
297 # removing non alpha-numeric characters, preserving or correcting capitalisation
298 #
299 13 ▶sub tokenize ($) {
Subroutine tokenize: called CC=4 SCAR=13.9
Pod undocumented
300 13 ▶ return if !defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 13 13
301
302 13 my $text = shift;
303
304 # remove rubish first
305 13 $text =~ s/,/ /ig;
306 13 $text =~ s/-/ /ig;
307 13 $text =~ s/_/ /ig;
308 13 $text =~ s/\//_/ig;
309
310 # create special cases
311 13 $text =~ s/sql /sql_/ig;
312 13 $text =~ s/xml /xml_/ig;
313 13 $text =~ s/cli /cli_/ig;
314 13 $text =~ s/fdw /fdw_/ig;
315 13 $text =~ s/null /null_/ig;
316
317
318 13 $text = join qq(_), map { lc } split /_/, $text;
319 13 ▶ $text = join qq(), map { ucfirst(lc($_)) } grep { $_ ne 'a' and $_ ne 'an' and $_ ne 'the' } split /\s+/, $text;
Condition66%4/6
$_ ne 'a' && $_ ne 'an' && $_ ne 'the'
!ll&&!rl&&r
1049
$_ ne "a" and $_ ne "an"
!ll&&!rl&&r
0149
MC/DC33%1/3
$_ ne 'a' && $_ ne 'an' && $_ ne 'the'
$_ ne "a" $_ ne "an" $_ ne 'the'
Truth table50%2/4
A and B && C
A= $_ ne "a"B= $_ ne "an"C= $_ ne 'the'
ABCresult
0XX0
10X0
1100
1111
320
321 # fix special cases
322 13 $text =~ s/sql_/SQL/ig;
323 13 $text =~ s/xml_/XML/ig;
324 13 $text =~ s/cli_/CLI/ig;
325 13 $text =~ s/fdw_/FDW/ig;
326 13 $text =~ s/null_/NULL/ig;
327 13 $text =~ s/xquery/XQuery/ig;
328  
329 13 return $text;
330 }
331  
332  
333  
334 # constantize
335 #
336 # returns a uppercase snake-case version of the string
337 #
338 8 ▶sub constantize ($) {
Subroutine constantize: called CC=3 SCAR=11.0
Pod undocumented
339 8 ▶ return if !defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 8 8
340
341 8 my $text = shift;
342
343 # remove common words
344 8 $text =~ s/\b(?:a|an|the)\b//ig;
345
346 # substitute anything not an alpha-numeric
347 8 $text =~ s/[^\d\w]+/_/ig;
348
349 # trim leading or trailing underscores
350 8 $text =~ s/^_|_$//ig;
351
352 8 $text = uc($text);
353 8 ▶ $text = join '_', $CONST_PREFIX, $text
Branch: 1 branch
BranchTrueFalseTotal
if defined $DBIx::SQLstate::CONST_PREFIX 4 4 8
354 if defined $CONST_PREFIX;
355
356 8 return $text;
357 }
358  
359  
360  
361 5 ▶sub is_sqlstate_succes($) {
Subroutine is_sqlstate_succes: called CC=1 SCAR=0.0
Pod documented
362 5 return '00' eq sqlstate_class($_[0])
363 }
364  
365  
366 5 ▶sub is_sqlstate_warning($) {
Subroutine is_sqlstate_warning: called CC=1 SCAR=0.0
Pod documented
367 5 return '01' eq sqlstate_class($_[0])
368 }
369  
370  
371 5 ▶sub is_sqlstate_no_data($) {
Subroutine is_sqlstate_no_data: called CC=1 SCAR=0.0
Pod documented
372 5 return '02' eq sqlstate_class($_[0])
373 }
374  
375  
376 5 ▶sub is_sqlstate_exception($) {
Subroutine is_sqlstate_exception: called CC=4 SCAR=13.9
Pod documented
377 5 my $sqlstate_class = sqlstate_class($_[0]);
378
379 5 ▶ return !!undef if '00' eq $sqlstate_class;
Branch: 1 branch
BranchTrueFalseTotal
if "00" eq $sqlstate_class 1 4 5
380 4 ▶ return !!undef if '01' eq $sqlstate_class;
Branch: 1 branch
BranchTrueFalseTotal
if "01" eq $sqlstate_class 1 3 4
381 3 ▶ return !!undef if '02' eq $sqlstate_class;
Branch: 1 branch
BranchTrueFalseTotal
if "02" eq $sqlstate_class 1 2 3
382
383 2 return !undef;
384 }
385  
386  
387  
388 8 %SQLstate = sqlstate_known_codes();
389  
390  
391  
392 =head1 DESCRIPTION
393  
394 Database Management Systems, and L<DBI> have their own way of reporting errors.
395 Very often, errors are quit expressive in what happened. Many SQL based systems
396 do also include a SQL-State with each request. This module turns the SQL-State 5
397 byte code into human readable strings.
398  
399 =head1 SQLSTATE Classes and Sub-Classes
400  
401 Programs calling a database which accords to the SQL standard receive an
402 indication about the success or failure of the call. This return code - which is
403 called SQLSTATE - consists of 5 bytes. They are divided into two parts: the
404 first and second bytes contain a class and the following three a subclass. Each
405 class belongs to one of four categories: "S" denotes "Success" (class 00), "W"
406 denotes "Warning" (class 01), "N" denotes "No data" (class 02) and "X" denotes
407 "Exception" (all other classes).
408  
409 =cut
410  
411  
412  
413 =head1 CLASS METHODS
414  
415 The following two class methods have been added for the programmer convenience:
416  
417 =head2 C<message($sqlstate)>
418  
419 Returns a subclass-message or class-message for a given and exisitng SQLstate,
420 or the default C<'Unkown SQL-state'>.
421  
422 my $message = DBIx::SQLstate->message("25006");
423 #
424 # "read-only SQL-transaction"
425  
426 =head2 C<token($sqlstate)>
427  
428 Returns the tokenized (See L<tokenize>) version of the message from above.
429  
430 $sqlstate = "22XXX"; # non existing code
431 $LOG->error(DBIx::SQLstate->token $sqlstate)
432 #
433 # logs an error with "DataException"
434  
435 =cut
436  
437  
438  
439 =head1 EXPORT_OK SUBROUTINES
440  
441 =head2 C<sqlstate_message($sqlstate)>
442  
443 Returns the human readable message defined for the given SQL-State code.
444  
445 my $sqlstate = '25006';
446 say sqlstate_message();
447 #
448 # prints "read-only SQL-transaction"
449  
450  
451  
452 =head2 C<sqlstate_class_message($sqlstate)>
453  
454 Returns the human readable message for the SQL-state class. This might be useful
455 reduce the amount of variations of log-messages. But since not all SQLstate
456 codes might be present in the current table, this will provide a decent fallback
457 message.
458  
459 my $sqlstate = '22X00'; # a madeup code
460 my $m = sqlstate_message($sqlstate) // sqlstate_class_message($sqlstate);
461 say $m;
462 #
463 # prints "data exception"
464  
465  
466  
467 =head2 C<sqlstate_default_message()>
468  
469 Returns a default message. The value can be set with
470 C<our $DBIx::SQLstate::$DEFAULT_MESSAGE>, and defaults to C<'Unkown SQL-state'>.
471  
472  
473  
474 =head2 C<sqlstate_token($sqlstate)>
475  
476 Returns a tokenized string (See L<DBIx::SQLstate::tokenize>).
477  
478 my $sqlstate = '01007';
479 $LOG->warn sqlstate_token($sqlstate);
480 #
481 # logs a warning message with "PrivilegeNotGranted"
482  
483  
484  
485 =head2 C<sqlstate_class_token($sqlstate)>
486  
487 Returns the tokenized string for the above L<sqlstate_class_message>. See
488 L<tokenize>.
489  
490  
491  
492 =head2 C<sqlstate_default_token()>
493  
494 Returns the tokenized version of the default message.
495  
496  
497  
498 =head2 C<sqlstate_class($sqlstate)>
499  
500 Returns the 2-byte SQL-state class code.
501  
502  
503  
504 =head2 C<is_sqlstate_succes($sqlstate)>
505  
506 Returns I<true> is the SQL-state class is C<00>.
507  
508  
509  
510 =head2 C<is_sqlstate_warning($sqlstate)>
511  
512 Returns I<true> is the SQL-state class is C<01>.
513  
514  
515  
516 =head2 C<is_sqlstate_no_data($sqlstate)>
517  
518 Returns I<true> is the SQL-state class is C<02>.
519  
520  
521  
522 =head2 C<is_sqlstate_exception($sqlstate)>
523  
524 Returns I<true> is the SQL-state class is any other than the above mentioned
525 C<00>, C<01>, or C<02>.
526  
527  
528  
529 =head1 Tokenization
530  
531 The tokenized strings can be useful in logging, or for L<Throwable> ( or
532 L<Exception::Class>) object creations etc. These are mostly camel-case. However,
533 for some common abreviations, like 'SQL', 'XML' or 'XQuery' this module tries to
534 correct the charactercase-folding.
535  
536 For now, do not rely on the consitent case-folding, it may change in the future.
537  
538  
539  
540 =head1 AUTHOR
541  
542 Theo van Hoesel <tvanhoesel@perceptyx.com>
543  
544  
545  
546 =head1 COPYRIGHT AND LICENSE
547  
548 'DBIx::SQLstate'
549 is Copyright (C) 2023, Perceptyx Inc
550  
551 This library is free software; you can redistribute it and/or modify it under
552 the terms of the Artistic License 2.0.
553  
554 This package is distributed in the hope that it will be useful, but it is
555 provided "as is" and without any express or implied warranties.
556  
557 For details, see the full text of the license in the file LICENSE.
558  
559  
560 =cut
561  
562  
563  
564 8 1;
565  
566  
567  
568 __END__
↑ Summary SQLstate/wikipedia.pm »