Geoffrey/Converter/SQLite.pm

filter: Statementstmt 100% 138 / 138 Branchbran 88.4% 23 / 26 Conditioncond 66.6% 16 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 40 / 40 Podpod 100% 17 / 17 totaltotal 89.6% 234 / 261 CC 25 SCAR 10.4
CC
25
Cov
94%
colums_information
9.0
index_information
4.0
foreign_key
3.0
CRAP 2.8
« Geoffrey/Action/Constraint/Default.pm ↑ Summary Geoffrey/Role/Action.pm »
1 package Geoffrey::Converter::SQLite;
2  
3 19 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 $Geoffrey::Converter::SQLite::VERSION = '0.000206';
10  
11 19 ▶use parent 'Geoffrey::Role::Converter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 19 Readonly::Scalar my $I_CONST_LENGTH_VALUE => 2;
14 19 Readonly::Scalar my $I_CONST_NOT_NULL_VALUE => 3;
15 19 Readonly::Scalar my $I_CONST_PRIMARY_KEY_VALUE => 4;
16 19 Readonly::Scalar my $I_CONST_DEFAULT_VALUE => 5;
17  
18 {
19  
20 package Geoffrey::Converter::SQLite::Constraints;
21  
22 19 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
25 4 my $class = shift;
26 4 return bless $class->SUPER::new(
27 not_null => q~NOT NULL~,
28 unique => q~UNIQUE~,
29 primary_key => q~PRIMARY KEY~,
30 foreign_key => q~FOREIGN KEY~,
31 check => q~CHECK~,
32 ),
33 $class;
34 }
35 }
36 {
37  
38 19 package Geoffrey::Converter::SQLite::View;
39  
40 19 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 ▶ sub add { return 'CREATE VIEW {0} AS {1}'; }
Subroutine add: called CC=1 SCAR=0.0
43  
44 3 ▶ sub drop { return 'DROP VIEW {0}'; }
Subroutine drop: called CC=1 SCAR=0.0
45  
46 4 ▶ sub list {
Subroutine list: called CC=1 SCAR=0.0
47 4 my ( $self, $schema ) = @_;
48 4 require Geoffrey::Utils;
49 return
50 4 q~SELECT * FROM ~
51 . Geoffrey::Utils::add_schema($schema)
52 . q~sqlite_master WHERE type='view'~;
53 }
54 }
55 {
56  
57 19 package Geoffrey::Converter::SQLite::ForeignKey;
58 19 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 19 ▶ sub add { return 'FOREIGN KEY ({0}) REFERENCES {1}({2})' }
Subroutine add: called CC=1 SCAR=0.0
60 }
61 {
62 19 package Geoffrey::Converter::SQLite::PrimaryKey;
63 19 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶ sub add { return 'CONSTRAINT {0} PRIMARY KEY ( {1} )'; }
Subroutine add: called CC=1 SCAR=0.0
65 }
66 {
67  
68 19 package Geoffrey::Converter::SQLite::UniqueIndex;
69 19 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶ sub append { return 'CREATE UNIQUE INDEX IF NOT EXISTS {0} ON {1} ( {2} )'; }
Subroutine append: called CC=1 SCAR=0.0
71 2 ▶ sub add { return 'CONSTRAINT {0} UNIQUE ( {1} )'; }
Subroutine add: called CC=1 SCAR=0.0
72 1 ▶ sub drop { return 'DROP INDEX IF EXISTS {1}'; }
Subroutine drop: called CC=1 SCAR=0.0
73 }
74 {
75  
76 19 package Geoffrey::Converter::SQLite::Trigger;
77 19 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 17 ▶ sub add {
Subroutine add: called CC=2 SCAR=6.9
80 17 my ( $self, $options ) = @_;
81 17 my $s_sql_standard = <<'EOF';
82 CREATE TRIGGER {0} UPDATE OF {1} ON {2}
83 BEGIN
84 {4}
85 END
86 EOF
87 17 my $s_sql_view = <<'EOF';
88 CREATE TRIGGER {0} INSTEAD OF UPDATE OF {1} ON {2}
89 BEGIN
90 {4}
91 END
92 EOF
93 17 ▶ return $options->{for_view} ? $s_sql_view : $s_sql_standard;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'for_view'} ? : 1 16 17
94 }
95  
96 6 ▶ sub drop { return 'DROP TRIGGER IF EXISTS {1}'; }
Subroutine drop: called CC=1 SCAR=0.0
97 }
98  
99 19 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
100 24 my $class = shift;
101 24 my $self = $class->SUPER::new(@_);
102 24 $self->{min_version} = '3.7';
103 24 return bless $self, $class;
104 }
105  
106 49 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
107 return {
108 49 current_timestamp => 'CURRENT_TIMESTAMP',
109 autoincrement => 'AUTOINCREMENT',
110 };
111 }
112  
113 82 ▶sub types {
Subroutine types: called CC=1 SCAR=0.0
Pod documented
114 return {
115 82 blob => 'BLOB',
116 integer => 'INTEGER',
117 numeric => 'NUMERIC',
118 real => 'REAL',
119 text => 'TEXT',
120 bool => 'BOOL',
121 double => 'DOUBLE',
122 float => 'FLOAT',
123 char => 'CHAR',
124 varchar => 'VARCHAR',
125 timestamp => 'DATETIME',
126 };
127 }
128  
129 3 ▶sub select_get_table {
Subroutine select_get_table: called CC=1 SCAR=0.0
Pod documented
130 return
131 3 q~SELECT t.name AS table_name FROM sqlite_master t WHERE type='table' AND t.name = ?~;
132 }
133  
134 30 ▶sub convert_defaults {
Subroutine convert_defaults: called CC=1 SCAR=0.0
Pod documented
135 30 my ( $self, $params ) = @_;
136 30 return $params->{default};
137 }
138 1 ▶sub can_create_empty_table { return 0 }
Subroutine can_create_empty_table: called CC=1 SCAR=0.0
Pod documented
139  
140 2 ▶sub colums_information {
Subroutine colums_information: called CC=9 SCAR=22.0
Pod documented
141 2 my ( $self, $ar_raw_data ) = @_;
142 2 ▶ return [] if scalar @{$ar_raw_data} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$ar_raw_data;} == 0 0 2 2
143 2 my $table_row = shift @{$ar_raw_data};
144 2 $table_row->{sql} =~ s/^.*(CREATE|create) (.*)\(/$2/g;
145 2 my $columns = [];
146 2 for ( split m/,/, $table_row->{sql} ) {
147 22 s/^TABLE\s+\S+\s+\((.*)/$1/g;
148 22 s/^\s*(.*)\s*$/$1/g;
149 22 my $rx_not_null = 'NOT NULL';
150 22 my $rx_primary_key = 'PRIMARY KEY';
151 22 my $rx_default = 'AUTOINCREMENT|DEFAULT';
152 22 my $rx_column_values = qr/($rx_not_null)*\s($rx_primary_key)*.*($rx_default \w{1,})*/;
153 22 my @column = m/^(\w+)\s([[:upper:]]+)(\(\d*\))*\s$rx_column_values$/;
154 22 ▶ next if scalar @column == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @column == 0 5 17 22
155 17 ▶ $column[$I_CONST_LENGTH_VALUE] =~ s/([\(\)])//g if $column[$I_CONST_LENGTH_VALUE];
Branch: 1 branch
BranchTrueFalseTotal
if $column[$I_CONST_LENGTH_VALUE] 9 8 17
156 17 ▶ push @{$columns},
Branch: 4 branches
BranchTrueFalseTotal
$column[$I_CONST_LENGTH_VALUE] ? : 9 8 17
$column[$I_CONST_NOT_NULL_VALUE] ? : 10 7 17
$column[$I_CONST_PRIMARY_KEY_VALUE] ? : 2 15 17
$column[$I_CONST_DEFAULT_VALUE] ? : 0 17 17
157 {
158 name => $column[0],
159 type => $column[1],
160 (
161 $column[$I_CONST_LENGTH_VALUE] ? ( length => $column[$I_CONST_LENGTH_VALUE] )
162 : ()
163 ),
164 (
165 $column[$I_CONST_NOT_NULL_VALUE]
166 ? ( not_null => $column[$I_CONST_NOT_NULL_VALUE] )
167 : ()
168 ),
169 (
170 $column[$I_CONST_PRIMARY_KEY_VALUE]
171 ? ( primary_key => $column[$I_CONST_PRIMARY_KEY_VALUE] )
172 : ()
173 ),
174 (
175 $column[$I_CONST_DEFAULT_VALUE]
176 ? ( default => $column[$I_CONST_DEFAULT_VALUE] )
177 : ()
178 ),
179 };
180 }
181 2 return $columns;
182 }
183  
184 3 ▶sub index_information {
Subroutine index_information: called CC=4 SCAR=13.9
Pod documented
185 3 my ( $self, $ar_raw_data ) = @_;
186 3 my @mapped = ();
187 3 for ( @{$ar_raw_data} ) {
188 6 ▶ next if !$_->{sql};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"sql"} 3 3 6
189 3 my ($s_columns) = $_->{sql} =~ m/\((.*)\)$/;
190 3 my @columns = split m/,/, $s_columns;
191 3 s/^\s+|\s+$//g for @columns;
192 push @mapped,
193 {
194 name => $_->{name},
195 table => $_->{tbl_name},
196 3 columns => \@columns
197 };
198 }
199 3 return \@mapped;
200 }
201  
202 3 ▶sub view_information {
Subroutine view_information: called CC=2 SCAR=7.0
Pod documented
203 3 my ( $self, $ar_raw_data ) = @_;
204 3 ▶ return [] unless $ar_raw_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $ar_raw_data 0 3 3
205 3 return [ map { { name => $_->{name}, sql => $_->{sql} } } @{$ar_raw_data} ];
206 }
207  
208 79 ▶sub constraints {
Subroutine constraints: called CC=2 SCAR=7.0
Pod documented
209 79 my ($self) = @_;
210 79 ▶ $self->{constraints} //= Geoffrey::Converter::SQLite::Constraints->new;
Condition66%2/3
$self->{"constraints"} //= "Geoffrey::Converter::SQLite::Constraints"->new
l!l&&r!l&&!r
7540
MC/DC0%0/2
$self->{"constraints"} //= "Geoffrey::Converter::SQLite::Constraints"->new
$self->{"constraints"} "Geoffrey::Converter::SQLite::Constraints"->new
Truth table66%2/3
A //= B
A= $self->{"constraints"}B= "Geoffrey::Converter::SQLite::Constraints"->new
ABresult
1X1
011
000
211 79 return $self->{constraints};
212 }
213  
214 20 ▶sub index {
Subroutine index: called CC=3 SCAR=11.0
Pod documented
215 20 my ( $self, $new_value ) = @_;
216 20 require Geoffrey::Converter::SQLite::Index;
217 20 ▶ $self->{index} = $new_value if defined $new_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_value 1 19 20
218 20 ▶ $self->{index} //= Geoffrey::Converter::SQLite::Index->new;
Condition66%2/3
$self->{"index"} //= "Geoffrey::Converter::SQLite::Index"->new
l!l&&r!l&&!r
1460
MC/DC0%0/2
$self->{"index"} //= "Geoffrey::Converter::SQLite::Index"->new
$self->{"index"} "Geoffrey::Converter::SQLite::Index"->new
Truth table66%2/3
A //= B
A= $self->{"index"}B= "Geoffrey::Converter::SQLite::Index"->new
ABresult
1X1
011
000
219 20 return $self->{index};
220 }
221  
222 28 ▶sub table {
Subroutine table: called CC=2 SCAR=7.0
Pod documented
223 28 my ($self) = @_;
224 28 require Geoffrey::Converter::SQLite::Tables;
225 28 ▶ $self->{table} //= Geoffrey::Converter::SQLite::Tables->new;
Condition66%2/3
$self->{"table"} //= "Geoffrey::Converter::SQLite::Tables"->new
l!l&&r!l&&!r
2350
MC/DC0%0/2
$self->{"table"} //= "Geoffrey::Converter::SQLite::Tables"->new
$self->{"table"} "Geoffrey::Converter::SQLite::Tables"->new
Truth table66%2/3
A //= B
A= $self->{"table"}B= "Geoffrey::Converter::SQLite::Tables"->new
ABresult
1X1
011
000
226 28 return $self->{table};
227 }
228  
229 9 ▶sub view {
Subroutine view: called CC=2 SCAR=7.0
Pod documented
230 9 my ($self) = @_;
231 9 ▶ $self->{view} //= Geoffrey::Converter::SQLite::View->new;
Condition66%2/3
$self->{"view"} //= "Geoffrey::Converter::SQLite::View"->new
l!l&&r!l&&!r
450
MC/DC0%0/2
$self->{"view"} //= "Geoffrey::Converter::SQLite::View"->new
$self->{"view"} "Geoffrey::Converter::SQLite::View"->new
Truth table66%2/3
A //= B
A= $self->{"view"}B= "Geoffrey::Converter::SQLite::View"->new
ABresult
1X1
011
000
232 9 return $self->{view};
233 }
234  
235 80 ▶sub foreign_key {
Subroutine foreign_key: called CC=3 SCAR=11.0
Pod documented
236 80 my ( $self, $new_value ) = @_;
237 80 ▶ $self->{foreign_key} = $new_value if defined $new_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_value 3 77 80
238 80 ▶ $self->{foreign_key} //= Geoffrey::Converter::SQLite::ForeignKey->new;
Condition66%2/3
$self->{"foreign_key"} //= "Geoffrey::Converter::SQLite::ForeignKey"->new
l!l&&r!l&&!r
7280
MC/DC0%0/2
$self->{"foreign_key"} //= "Geoffrey::Converter::SQLite::ForeignKey"->new
$self->{"foreign_key"} "Geoffrey::Converter::SQLite::ForeignKey"->new
Truth table66%2/3
A //= B
A= $self->{"foreign_key"}B= "Geoffrey::Converter::SQLite::ForeignKey"->new
ABresult
1X1
011
000
239 80 return $self->{foreign_key};
240 }
241  
242 33 ▶sub trigger {
Subroutine trigger: called CC=3 SCAR=11.0
Pod documented
243 33 my ( $self, $o_trigger ) = @_;
244 33 ▶ $self->{trigger} = $o_trigger if defined $o_trigger;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o_trigger 1 32 33
245 33 ▶ $self->{trigger} //= Geoffrey::Converter::SQLite::Trigger->new;
Condition66%2/3
$self->{"trigger"} //= "Geoffrey::Converter::SQLite::Trigger"->new
l!l&&r!l&&!r
2940
MC/DC0%0/2
$self->{"trigger"} //= "Geoffrey::Converter::SQLite::Trigger"->new
$self->{"trigger"} "Geoffrey::Converter::SQLite::Trigger"->new
Truth table66%2/3
A //= B
A= $self->{"trigger"}B= "Geoffrey::Converter::SQLite::Trigger"->new
ABresult
1X1
011
000
246 33 return $self->{trigger};
247 }
248  
249 15 ▶sub primary_key {
Subroutine primary_key: called CC=2 SCAR=7.0
Pod documented
250 15 my ($self) = @_;
251 15 ▶ $self->{primary_key} //= Geoffrey::Converter::SQLite::PrimaryKey->new;
Condition66%2/3
$self->{"primary_key"} //= "Geoffrey::Converter::SQLite::PrimaryKey"->new
l!l&&r!l&&!r
1140
MC/DC0%0/2
$self->{"primary_key"} //= "Geoffrey::Converter::SQLite::PrimaryKey"->new
$self->{"primary_key"} "Geoffrey::Converter::SQLite::PrimaryKey"->new
Truth table66%2/3
A //= B
A= $self->{"primary_key"}B= "Geoffrey::Converter::SQLite::PrimaryKey"->new
ABresult
1X1
011
000
252 15 return $self->{primary_key};
253 }
254  
255 19 ▶sub unique {
Subroutine unique: called CC=2 SCAR=7.0
Pod documented
256 19 my ($self) = @_;
257 19 ▶ $self->{unique} //= Geoffrey::Converter::SQLite::UniqueIndex->new;
Condition66%2/3
$self->{"unique"} //= "Geoffrey::Converter::SQLite::UniqueIndex"->new
l!l&&r!l&&!r
1540
MC/DC0%0/2
$self->{"unique"} //= "Geoffrey::Converter::SQLite::UniqueIndex"->new
$self->{"unique"} "Geoffrey::Converter::SQLite::UniqueIndex"->new
Truth table66%2/3
A //= B
A= $self->{"unique"}B= "Geoffrey::Converter::SQLite::UniqueIndex"->new
ABresult
1X1
011
000
258 19 return $self->{unique};
259 }
260  
261 19 1; # End of Geoffrey::Converter::SQLite
262  
263 __END__
« Geoffrey/Action/Constraint/Default.pm ↑ Summary Geoffrey/Role/Action.pm »