blib/lib/DBIx/QuickORM/SchemaBuilder/ORM.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 10 / 10 Conditioncond 46.6% 7 / 15 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 9 / 9 Podpod 80.0% 4 / 5 totaltotal 84.7% 78 / 92 CC 11 SCAR 13.7
CC
11
Cov
89%
extension
6.5
setting
5.1
schema_sql
2.0
CRAP 3.9
« blib/lib/DBIx/QuickORM/Type/DateTime.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/SQLite.pm »
1 package DBIx::QuickORM::SchemaBuilder::ORM;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.000033';
6  
7 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 +settings
11 +extensions
12 +sql
13 3 };
14  
15 =pod
16  
17 =encoding UTF-8
18  
19 =head1 NAME
20  
21 DBIx::QuickORM::SchemaBuilder::ORM - What schema building creates around the tables.
22  
23 =head1 DESCRIPTION
24  
25 Collects what the C<schema> block inside an C<orm> block describes (see
26 L<DBIx::QuickORM::DSL>): session settings, extensions, and SQL to run before
27 or after the tables. Schema building writes them in that order, with the
28 named enums the tables use after the extensions: settings, extensions,
29 enums, SQL, the tables and views, then the SQL given C<after>. Enums are
30 defined with the DSL's C<enum>, outside any block.
31  
32 =head1 SYNOPSIS
33  
34 The C<orm> block's C<schema> block gets the builder:
35  
36 orm {
37 tables ['My::App::Users'];
38  
39 schema {
40 my $builder = shift;
41 $builder->setting(search_path => 'public');
42 $builder->extension('pg_trgm');
43 $builder->schema_sql(
44 postgresql => 'CREATE INDEX users_name_trgm ON users USING gin (name gin_trgm_ops)',
45 after => 1,
46 );
47 };
48 };
49  
50 On PostgreSQL, around the tables, that builds:
51  
52 =for highlighter language=sql
53  
54 SET search_path = 'public';
55 CREATE EXTENSION IF NOT EXISTS "pg_trgm";
56  
57 -- the tables and views
58  
59 CREATE INDEX users_name_trgm ON users USING gin (name gin_trgm_ops);
60  
61 =for highlighter language=perl
62  
63 The DSL's C<setting>, C<extension>, and C<schema_sql> keywords call the
64 methods of the same names.
65  
66 =cut
67  
68 9 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
69 9 my $self = shift;
70 9 $self->{+SETTINGS} = [];
71 9 $self->{+EXTENSIONS} = [];
72 9 $self->{+SQL} = [];
73 }
74  
75 =head1 PUBLIC METHODS
76  
77 Each describing method returns the builder.
78  
79 =over 4
80  
81 =item $builder->setting($name => $value)
82  
83 A session setting, made first on the connection that builds the schema:
84 C<SET> on PostgreSQL, MySQL, MariaDB, and DuckDB, C<PRAGMA> on SQLite. With
85 C<deploy>, that connection is the ORM's, so the setting stays in effect
86 there.
87  
88 =item $builder->extension($name)
89  
90 An extension: C<CREATE EXTENSION IF NOT EXISTS> on PostgreSQL (some come
91 installed), C<INSTALL> and C<LOAD> on DuckDB. MySQL, MariaDB, and SQLite have
92 none, so building croaks there.
93  
94 =item $builder->schema_sql(dialect => $sql, ..., after => $bool)
95  
96 SQL to run before the tables, or after the tables and views with C<after>.
97  
98 $builder->schema_sql(
99 postgresql => 'CREATE INDEX users_name_lower ON users (lower(name))',
100 any_mysql => 'CREATE INDEX users_name_prefix ON users (name(20))',
101 after => 1,
102 ); # none on SQLite and DuckDB
103  
104 The SQL is given as C<< dialect => $sql >> pairs keyed as L<DBIx::QuickORM::Dialect>'s
105 C<dialect_keys> (C<any_mysql> for MySQL and MariaDB, C<any> for every
106 database), the exact dialect first. A dialect with no entry, or an undef
107 one, skips it: such SQL is usually for some databases only.
108  
109 =item \%spec = $builder->spec
110  
111 The description as plain data: C<settings> (C<[$name, $value]> pairs),
112 C<extensions>, and C<sql>
113 (C<< {sql => \%by_dialect, after => $bool} >>), each in the order given.
114  
115 =back
116  
117 =cut
118  
119 5 ▶sub setting {
Subroutine setting: called CC=5 SCAR=16.4
Pod documented
120 5 my $self = shift;
121 5 my ($name, $value) = @_;
122 5 ▶ croak "setting() takes a name and a value" unless @_ == 2 && defined($name) && $name =~ m/^\w+(?:\.\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 and defined $name and $name =~ /^\w+(?:\.\w+)*\z/ 1 4 5
Condition50%3/6
@_ == 2 and defined $name and $name =~ /^\w+(?:\.\w+)*\z/
!ll&&!rl&&r
014
@_ == 2 and defined $name
!ll&&!rl&&r
005
MC/DC33%1/3
@_ == 2 and defined $name and $name =~ /^\w+(?:\.\w+)*\z/
@_ == 2 defined $name $name =~ /^\w+(?:\.\w+)*\z/
Truth table50%2/4
A and B and C
A= @_ == 2B= defined $nameC= $name =~ /^\w+(?:\.\w+)*\z/
ABCresult
0XX0
10X0
1100
1111
123 4 ▶ croak "setting('$name') takes a plain value" if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 1 3 4
124 3 push @{$self->{+SETTINGS}} => [$name, $value];
125 3 return $self;
126 }
127  
128 5 ▶sub extension {
Subroutine extension: called CC=6 SCAR=18.7
Pod documented
129 5 my $self = shift;
130 5 my ($name) = @_;
131 5 ▶ croak "extension() takes one extension name" unless @_ == 1 && defined($name) && !ref($name) && $name =~ m/^[\w-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $name and not ref $name and $name =~ /^[\w-]+\z/ 1 4 5
Condition44%4/9
@_ == 1 and defined $name and not ref $name and $name =~ /^[\w-]+\z/
!ll&&!rl&&r
014
@_ == 1 and defined $name and not ref $name
!ll&&!rl&&r
005
@_ == 1 and defined $name
!ll&&!rl&&r
005
MC/DC25%1/4
@_ == 1 and defined $name and not ref $name and $name =~ /^[\w-]+\z/
@_ == 1 defined $name not ref $name $name =~ /^[\w-]+\z/
Truth table40%2/5
A and B and not(C) and D
A= @_ == 1B= defined $nameC= not ref $nameD= $name =~ /^[\w-]+\z/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
132 4 ▶ push @{$self->{+EXTENSIONS}} => $name unless grep { $_ eq $name } @{$self->{+EXTENSIONS}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @{$$self{"extensions"};} 3 1 4
133 4 return $self;
134 }
135  
136 8 ▶sub schema_sql {
Subroutine schema_sql: called CC=2 SCAR=6.9
Pod documented
137 8 my $self = shift;
138 8 require DBIx::QuickORM::Dialect;
139 8 my ($sql, $opts) = DBIx::QuickORM::Dialect->dialect_pairs('schema_sql()', [@_], options => ['after']);
140 6 ▶ push @{$self->{+SQL}} => {sql => $sql, after => $opts->{after} ? 1 : 0};
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'after'} ? : 3 3 6
141 6 return $self;
142 }
143  
144 3 ▶sub spec {
Subroutine spec: called CC=1 SCAR=0.0
Pod documented
145 3 my $self = shift;
146 return {
147 3 settings => [map { [@$_] } @{$self->{+SETTINGS}}],
148 3 extensions => [@{$self->{+EXTENSIONS}}],
149 3 sql => [map { +{%$_} } @{$self->{+SQL}}],
150 };
151 }
152  
153 3 1;
154  
155 __END__
« blib/lib/DBIx/QuickORM/Type/DateTime.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/SQLite.pm »