SQLstate.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 127 CC 5 SCAR 12.6
CC
5
Cov
0%
sqlstate_message
6.0
sqlstate_class
6.0
sqlstate_class_message
6.0
CRAP 3.5
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 use strict;
37 use warnings;
38  
39 our $VERSION = 'v0.0.1';
40  
41 our $DEFAULT_MESSAGE = 'Unkown SQL-state';
42  
43 use Exporter qw/import/;
44  
45 our @EXPORT = (
46 );
47  
48 our @EXPORT_OK = (
49 'sqlstate_codes',
50 'sqlstate_message',
51 'sqlstate_token',
52 'sqlstate_class_codes',
53 'sqlstate_class_message',
54 'sqlstate_class_token',
55 'sqlstate_default_message',
56 'sqlstate_default_token',
57 'sqlstate_class_codes',
58 );
59  
60 our %EXPORT_TAGS = (
61 message => [
62 'sqlstate_message',
63 'sqlstate_class_message',
64 'sqlstate_default_message',
65 ],
66 token => [
67 'sqlstate_token',
68 'sqlstate_class_token',
69 'sqlstate_default_token',
70 ],
71 );
72  
73  
74  
75 my %SQLstate = ();
76  
77  
78  
79 sub sqlstate_message ($) {
80 return unless defined $_[0];
81 return $SQLstate{$_[0]};
82 }
83  
84 sub sqlstate_token ($) {
85 return tokenize( sqlstate_message(shift) );
86 }
87  
88 sub sqlstate_class ($) {
89 return unless defined $_[0];
90 return substr($_[0],0,2);
91 }
92  
93 sub sqlstate_class_message ($) {
94 return unless defined $_[0];
95 return +{ sqlstate_class_codes() }->{sqlstate_class($_[0])};
96 }
97  
98 sub sqlstate_class_token ($) {
99 return tokenize( sqlstate_class_message(shift) );
100 }
101  
102 sub sqlstate_default_message () {
103 return $DEFAULT_MESSAGE;
104 }
105  
106 sub sqlstate_default_token () {
107 return tokenize( sqlstate_default_message );
108 }
109  
110 sub sqlstate_codes () {
111 return %SQLstate;
112 }
113  
114 sub sqlstate_known_codes () {
115 use DBIx::SQLstate::wikipedia;
116
117 return (
118 %DBIx::SQLstate::wikipedia::SQLstate,
119 );
120 }
121  
122 sub sqlstate_class_codes () {
123 my %sqlstate_class_codes = map {
124 sqlstate_class($_) => sqlstate_message($_)
125 } grep { /..000/ } keys %{{ sqlstate_codes() }};
126
127 return %sqlstate_class_codes;
128 }
129  
130  
131  
132 sub tokenize ($) {
133 return if !defined $_[0];
134
135 my $text = shift;
136  
137 # remove rubish first
138 $text =~ s/,/ /ig;
139 $text =~ s/-/ /ig;
140 $text =~ s/_/ /ig;
141 $text =~ s/\//_/ig;
142
143 # create special cases
144 $text =~ s/sql /sql_/ig;
145 $text =~ s/xml /xml_/ig;
146 $text =~ s/cli /cli_/ig;
147 $text =~ s/fdw /fdw_/ig;
148 $text =~ s/null /null_/ig;
149
150
151 $text = join qq(_), map { lc } split /_/, $text;
152 $text = join qq(), map { ucfirst(lc($_)) } grep { $_ ne 'a' and $_ ne 'an' and $_ ne 'the' } split /\s+/, $text;
153
154 # fix special cases
155 $text =~ s/sql_/SQL/ig;
156 $text =~ s/xml_/XML/ig;
157 $text =~ s/cli_/CLI/ig;
158 $text =~ s/fdw_/FDW/ig;
159 $text =~ s/null_/NULL/ig;
160 $text =~ s/xquery/XQuery/ig;
161  
162 return $text;
163 }
164  
165  
166  
167 %SQLstate = sqlstate_known_codes();
168  
169  
170  
171 =head1 DESCRIPTION
172  
173 Database Management Systems, and L<DBI> have their own way of reporting errors.
174 Very often, errors are quit expressive in what happened. Many SQL based systems
175 do also include a SQL-State with each request. This module turns the SQL-State 5
176 byte code into human readable strings.
177  
178 =head1 SQLSTATE Classes and Sub-Classes
179  
180 Programs calling a database which accords to the SQL standard receive an
181 indication about the success or failure of the call. This return code - which is
182 called SQLSTATE - consists of 5 bytes. They are divided into two parts: the
183 first and second bytes contain a class and the following three a subclass. Each
184 class belongs to one of four categories: "S" denotes "Success" (class 00), "W"
185 denotes "Warning" (class 01), "N" denotes "No data" (class 02) and "X" denotes
186 "Exception" (all other classes).
187  
188 =cut
189  
190  
191  
192 =head1 CLASS METHODS
193  
194 The following two class methods have been added for the programmer convenience:
195  
196 =head2 C<message($sqlstate)>
197  
198 Returns a subclass-message or class-message for a given and exisitng SQLstate,
199 or the default C<'Unkown SQL-state'>.
200  
201 my $message = DBIx::SQLstate->message("25006");
202 #
203 # "read-only SQL-transaction"
204  
205 =head2 C<token($sqlstate)>
206  
207 Returns the tokenized (See L<tokenize>) version of the message from above.
208  
209 $sqlstate = "22XXX"; # non existing code
210 $LOG->error(DBIx::SQLstate->token $sqlstate)
211 #
212 # logs an error with "DataException"
213  
214 =cut
215  
216  
217  
218 =head1 EXPORT_OK SUBROUTINES
219  
220 =head2 C<sqlstate_message($sqlstate)>
221  
222 Returns the human readable message defined for the given SQL-State code.
223  
224 my $sqlstate = '25006';
225 say sqlstate_message();
226 #
227 # prints "read-only SQL-transaction"
228  
229 =head2 C<sqlstate_token($sqlstate)>
230  
231 Returns a tokenized string (See L<DBIx::SQLstate::tokenize>).
232  
233 my $sqlstate = '01007';
234 $LOG->warn sqlstate_token($sqlstate);
235 #
236 # logs a warning message with "PrivilegeNotGranted"
237  
238 =head2 C<sqlstate_class($sqlstate)>
239  
240 Returns the 2-byte SQL-state class code.
241  
242 =head2 C<sqlstate_class_message($sqlstate)>
243  
244 Returns the human readable message for the SQL-state class. This might be useful
245 reduce the amount of variations of log-messages. But since not all SQLstate
246 codes might be present in the current table, this will provide a decent fallback
247 message.
248  
249 my $sqlstate = '22X00'; # a madeup code
250 my $m = sqlstate_message($sqlstate) // sqlstate_class_message($sqlstate);
251 say $m;
252 #
253 # prints "data exception"
254  
255 =head2 C<sqlstate_class_token($sqlstate)>
256  
257 Returns the tokenized string for the above L<sqlstate_class_message>. See
258 L<tokenize>.
259  
260 =head2 C<sqlstate_default_message()>
261  
262 Returns a default message. The value can be set with
263 C<our $DBIx::SQLstate::$DEFAULT_MESSAGE>, and defaults to C<'Unkown SQL-state'>.
264  
265 =head2 C<sqlstate_default_token()>
266  
267 Returns the tokenized version of the default message.
268  
269 =head1 Tokenization
270  
271 The tokenized strings can be useful in logging, or for L<Throwable> ( or
272 L<Exception::Class>) object creations etc. These are mostly camel-case. However,
273 for some common abreviations, like 'SQL', 'XML' or 'XQuery' this module tries to
274 correct the charactercase-folding.
275  
276 For now, do not rely on the consitent case-folding, it may change in the future.
277  
278 =cut
279  
280  
281  
282 sub message ($) {
283 my $class = shift;
284 my $sqlstate = shift;
285
286 return
287 sqlstate_message($sqlstate)
288 //
289 sqlstate_class_message($sqlstate)
290 //
291 sqlstate_default_message()
292 ;
293 }
294  
295 sub token ($) {
296 my $class = shift;
297 my $sqlstate = shift;
298
299 my $message =
300 sqlstate_message($sqlstate)
301 //
302 sqlstate_class_message($sqlstate)
303 //
304 sqlstate_default_message()
305 ;
306
307 return tokenize($message);
308 }
309  
310  
311  
312 1;
313  
314  
315  
316 =head1 AUTHOR
317  
318 Theo van Hoesel <tvanhoesel@perceptyx.com>
319  
320  
321  
322 =head1 COPYRIGHT AND LICENSE
323  
324 'DBIx::SQLstate'
325 is Copyright (C) 2023, Perceptyx Inc
326  
327 This library is free software; you can redistribute it and/or modify it under
328 the terms of the Artistic License 2.0.
329  
330 This package is distributed in the hope that it will be useful, but it is
331 provided "as is" and without any express or implied warranties.
332  
333 For details, see the full text of the license in the file LICENSE.
334  
335  
336 =cut
337  
338  
339  
340  
341 __END__