blib/lib/WWW/Suffit/Model.pm

filter: Statementstmt 83.0% 49 / 59 Branchbran 23.8% 10 / 42 Conditioncond 36.0% 9 / 25 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 68.7% 11 / 16 Podpod 100% 9 / 9 totaltotal 53.6% 88 / 164 CC 33 SCAR 40.4
CC
33
Cov
54%
init
106.1
is_mysql
6.0
is_mariadb
6.0
CRAP 56.9
↑ Summary eg/MyModel.pm »
1 package WWW::Suffit::Model;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 WWW::Suffit::Model - This library provides methods for access to WWW::Suffit models
11  
12 =head1 SYNOPSIS
13  
14 use WWW::Suffit::Model;
15  
16 my $model = WWW::Suffit::Model->new(
17 ds => "sqlite:///tmp/test.db?sqlite_unicode=1"
18 );
19  
20 $model = $model->connect_cached->init;
21  
22 =head1 DESCRIPTION
23  
24 A lightweight and extensible model layer that streamlines database interaction within the WWW::Suffit framework, providing a clean foundation for building consistent, reusable data models
25  
26 =head1 METHODS
27  
28 This class inherits all methods from L<Acrux::DBI> and implements the following new ones
29  
30 =head2 init
31  
32 $model = $model->connect->init($package, $schema, $prefix);
33 $model = $model->connect->init(__PACKAGE__, 'public', 'schema');
34 $model = $model->connect_cached->init;
35  
36 This method initializes the database schema prior to using the connection.
37  
38 =over 4
39  
40 =item B<$package>
41  
42 Specifies the package that holds the C<__DATA__> section containing the C<DDL> blocks
43  
44 Default: your package name of the class inheriting from this one, C<__PACKAGE__>
45  
46 =item B<$schema>
47  
48 Specifies schema name. Default: C<'public'>
49  
50 =item B<$prefix>
51  
52 Specifies preffix of the DDL section name. Default: C<'schema'>
53  
54 For eg., if you specify "test" as the prefix, you will need to declare
55 your C<DDL> blocks in your class as follows:
56  
57 __DATA__
58  
59 @@ test_sqlite
60  
61 ... your DDL for sqlite here ...
62  
63 @@ test_mysql
64  
65 ... your DDL for mysql here ...
66  
67 @@ test_postgresql
68  
69 ... your DDL for postgresql here ...
70  
71 =back
72  
73 =head2 is_mariadb
74  
75 print $model->is_mariadb ? "Is MariaDB" : "Is NOT MariaDB";
76  
77 Returns true if type of current database is MariaDB
78  
79 =head2 is_mysql
80  
81 print $model->is_mysql ? "Is MySQL" : "Is NOT MySQL";
82  
83 Returns true if type of current database is MySQL
84  
85 =head2 is_oracle
86  
87 print $model->is_oracle ? "Is Oracle" : "Is NOT Oracle";
88  
89 Returns true if type of current database is Oracle
90  
91 =head2 is_postgresql
92  
93 print $model->is_postgresql ? "Is PostgreSQL" : "Is NOT PostgreSQL";
94  
95 Returns true if type of current database is PostgreSQL
96  
97 =head2 is_sqlite
98  
99 print $model->is_sqlite ? "Is SQLite" : "Is NOT SQLite";
100  
101 Returns true if type of current database is SQLite
102  
103 =head2 is_initialized
104  
105 print $model->is_initialized ? "Initialized" : "Is NOT initialized";
106  
107 This method returns the initialization status of the model.
108 It returns true if the model is initialized, and false otherwise.
109  
110 =head2 initiator
111  
112 print $model->initiator; # MyModel
113  
114 This method returns the name of the class that called the init method
115  
116 =head2 schema
117  
118 print $model->schema; # public
119  
120 This method returns the schema name
121  
122 =head1 EXAMPLE
123  
124 Add the following block describing the schemas to the end of your class in the "C<__DATA__>" section:
125  
126 @@ schema_sqlite
127  
128 -- # main
129 CREATE TABLE IF NOT EXISTS "test" (
130 "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
131 "comment" TEXT DEFAULT NULL -- Comment
132 );
133  
134 @@ schema_mysql
135  
136 -- # main
137 CREATE DATABASE IF NOT EXISTS `test-db` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
138 USE `test-db`;
139 CREATE TABLE IF NOT EXISTS `test` (
140 `id` INT NOT NULL AUTO_INCREMENT,
141 `comment` LONGTEXT DEFAULT NULL, -- Comment
142 PRIMARY KEY (`id`)
143 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
144  
145 @@ schema_postgresql
146  
147 -- # main
148 CREATE TABLE IF NOT EXISTS "test" (
149 "id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
150 "comment" TEXT DEFAULT NULL -- Comment
151 );
152  
153 See also F<eg/MyModel.pm> for an example of how to use this class correctly
154  
155 =head1 HISTORY
156  
157 See C<Changes> file
158  
159 =head1 TO DO
160  
161 See C<TODO> file
162  
163 =head1 SEE ALSO
164  
165 L<Acrux::DBI>, L<WWW::Suffit>, F<eg/MyModel.pm>
166  
167 =head1 AUTHOR
168  
169 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
170  
171 =head1 COPYRIGHT
172  
173 Copyright (C) 1998-2026 D&D Corporation
174  
175 =head1 LICENSE
176  
177 This program is distributed under the terms of the Artistic License Version 2.0
178  
179 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
180  
181 =cut
182  
183 2 our $VERSION = '1.02';
184  
185 2 ▶use parent 'Acrux::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 2 ▶use Acrux::Util qw/touch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 2 ▶use Acrux::RefUtil qw/isnt_void/;
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
191 2 SCHEMA_NAME => 'public',
192 SCHEMA_SECTION_PREFIX => 'schema',
193 SCHEMA_SECTION_FORMAT => '%s_%s',
194 DRIVERS => {
195 sqlite => 'sqlite',
196 file => 'sqlite',
197 mysql => 'mysql',
198 mariadb => 'mariadb',
199 maria => 'mariadb',
200 pg => 'postgresql',
201 pgsql => 'postgresql',
202 postgres => 'postgresql',
203 postgresql => 'postgresql',
204 oracle => 'oracle',
205 ora => 'oracle',
206 sponge => 'sponge', # For tests only
207 },
208 DEFAULT_MODEL_URI => 'sponge://',
209 DEFAULT_MODEL_DSN => 'DBI:Sponge:',
210 DEFAULT_MODEL_ATTR => {
211 RaiseError => 0,
212 PrintError => 0,
213 PrintWarn => 0,
214 },
215 2 };
216  
217 1 ▶sub is_sqlite { DRIVERS->{(shift->driver)} eq 'sqlite' ? 1 : 0 }
Subroutine is_sqlite: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{(shift())->driver} eq 'sqlite' ? : 1 0 1
218 0 ▶sub is_mysql { DRIVERS->{(shift->driver)} eq 'mysql' ? 1 : 0 }
Subroutine is_mysql: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{(shift())->driver} eq 'mysql' ? : 0 0 0
219 0 ▶sub is_mariadb { DRIVERS->{(shift->driver)} eq 'mariadb' ? 1 : 0 }
Subroutine is_mariadb: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{(shift())->driver} eq 'mariadb' ? : 0 0 0
220 0 ▶sub is_postgresql { DRIVERS->{(shift->driver)} eq 'postgresql' ? 1 : 0 }
Subroutine is_postgresql: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{(shift())->driver} eq 'postgresql' ? : 0 0 0
221 0 ▶sub is_oracle { DRIVERS->{(shift->driver)} eq 'oracle' ? 1 : 0 }
Subroutine is_oracle: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{(shift())->driver} eq 'oracle' ? : 0 0 0
222  
223 # Initialize schema
224 1 ▶sub init {
Subroutine init: called CC=25 SCAR=46.6
Pod documented
225 1 my $self = shift; # shift->connect_cached;
226 1 ▶ my $package = shift // ref($self); # __PACKAGE__
Condition50%1/2
shift() // ref $self
l!l
01
MC/DC0%0/1
shift() // ref $self
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
227 1 ▶ my $schema = shift // SCHEMA_NAME;
Condition50%1/2
shift() // "public"
l!l
01
MC/DC0%0/1
shift() // "public"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
228 1 ▶ my $prefix = shift // SCHEMA_SECTION_PREFIX;
Condition50%1/2
shift() // "schema"
l!l
01
MC/DC0%0/1
shift() // "schema"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
229 1 ▶ my $dbh = $self->dbh or return $self; # Skip if no connect established
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->dbh or return $self 1 0 1
230 1 ▶ my $is_inited = $self->{'_is_initialized'} || 0; # Not initialized
Condition50%1/2
$self->{'_is_initialized'} || 0
l!l
01
MC/DC0%0/1
$self->{'_is_initialized'} || 0
$self->{'_is_initialized'}
Truth table50%1/2
A
A= $self->{'_is_initialized'}
Aresult
11
00
231 1 ▶ return $self if $is_inited; # Already initialized
Branch: 1 branch
BranchTrueFalseTotal
if $is_inited 0 1 1
232  
233 # Check SQLite
234 1 ▶ if ($self->is_sqlite) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->is_sqlite) { } 1 0 1
elsif ($self->is_mariadb or $self->is_mysql) { } 0 0 0
elsif ($self->is_postgresql) { } 0 0 0
235 1 my $file = $dbh->sqlite_db_filename();
236 1 ▶ unless ($file && (-e $file) && !(-z $file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and -e $file and not -z $file) 1 0 1
Condition33%2/6
$file and -e $file and not -z $file
!ll&&!rl&&r
010
$file and -e $file
!ll&&!rl&&r
001
MC/DC0%0/3
$file and -e $file and not -z $file
$file -e $file not -z $file
Truth table25%1/4
A and B and not(C)
A= $fileB= -e $fileC= not -z $file
ABCresult
0XX0
10X0
1100
1111
237 1 touch($file);
238 }
239  
240 # Get table info
241 1 ▶ if (my $sth = $dbh->table_info(undef, undef, undef, 'TABLE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sth = $dbh->table_info(undef, undef, undef, "TABLE")) 1 0 1
242 1 ▶ $is_inited = isnt_void($sth->fetchall_arrayref) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
isnt_void($sth->fetchall_arrayref) ? : 0 1 1
243 }
244 }
245  
246 # Check MariaDB
247 ▶ elsif ($self->is_mariadb || $self->is_mysql) {
Condition0%0/3
$self->is_mariadb or $self->is_mysql
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->is_mariadb or $self->is_mysql
$self->is_mariadb $self->is_mysql
Truth table0%0/3
A or B
A= $self->is_mariadbB= $self->is_mysql
ABresult
1X1
011
000
248 # Get table info
249 0 ▶ if (my $sth = $dbh->table_info('', $schema, '', 'TABLE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sth = $dbh->table_info("", $schema, "", "TABLE")) 0 0 0
250 0 ▶ $is_inited = isnt_void($sth->fetchall_arrayref) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
isnt_void($sth->fetchall_arrayref) ? : 0 0 0
251 }
252 }
253  
254 # Check PostgreSQL
255 elsif ($self->is_postgresql) {
256 # Get table info
257 0 ▶ if (my $sth = $dbh->table_info('', $schema, undef, 'TABLE')) { # schema = 'public'
Branch: 1 branch
BranchTrueFalseTotal
if (my $sth = $dbh->table_info("", $schema, undef, "TABLE")) 0 0 0
258 0 ▶ $is_inited = isnt_void($sth->fetchall_arrayref) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
isnt_void($sth->fetchall_arrayref) ? : 0 0 0
259 }
260 }
261  
262 # Skip initialize otherwise
263 else {
264 0 return $self;
265 }
266  
267 # Get dump instance
268 1 ▶ my $name = sprintf(SCHEMA_SECTION_FORMAT, $prefix, DRIVERS->{($self->driver)} || 'unknown');
Condition50%1/2
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{$self->driver} || 'unknown'
l!l
10
MC/DC0%0/1
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{$self->driver} || 'unknown'
{file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{$self->driver}
Truth table50%1/2
A
A= {file => 'sqlite', maria => 'mariadb', mariadb => 'mariadb', mysql => 'mysql', ora => 'oracle', oracle => 'oracle', pg => 'postgresql', pgsql => 'postgresql', postgres => 'postgresql', postgresql => 'postgresql', sponge => 'sponge', sqlite => 'sqlite'}->{$self->driver}
Aresult
11
00
269 1 my $dump = $self->dump(name => $name)->from_data($package);
270  
271 # Import initial schema if is not inited
272 1 ▶ unless ($is_inited) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_inited) 1 0 1
273 1 $dump->poke(); # main section (default)
274 1 ▶ return $self if $self->error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 1 1
275 }
276  
277 # Check connect
278 1 ▶ return $self->error(sprintf("Can't init database \"%s\". Ping failed: %s",
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 1 1
Condition0%0/2
$self->errstr || 'unknown error'
l!l
00
MC/DC0%0/1
$self->errstr || 'unknown error'
$self->errstr
Truth table0%0/2
A
A= $self->errstr
Aresult
11
00
279 $self->dsn, $self->errstr() || "unknown error")) unless $self->ping;
280  
281 # Ok
282 1 $self->{'_is_initialized'} = 1;
283 1 $self->{'_initiator'} = $package;
284 1 $self->{'_schema'} = $schema;
285 1 return $self;
286 }
287  
288 0 ▶sub is_initialized { shift->{'_is_initialized'} || 0 }
Subroutine is_initialized: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"_is_initialized"} or 0 0 0 0
289 1 ▶sub initiator { shift->{'_initiator'} // '' }
Subroutine initiator: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"_initiator"} // ""
l!l
10
MC/DC0%0/1
+(shift())->{"_initiator"} // ""
+(shift())->{"_initiator"}
Truth table50%1/2
A
A= +(shift())->{"_initiator"}
Aresult
11
00
290 1 ▶sub schema { shift->{'_schema'} // '' }
Subroutine schema: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"_schema"} // ""
l!l
10
MC/DC0%0/1
+(shift())->{"_schema"} // ""
+(shift())->{"_schema"}
Truth table50%1/2
A
A= +(shift())->{"_schema"}
Aresult
11
00
291  
292 2 1;
293  
294 __END__
↑ Summary eg/MyModel.pm »