Manager/Sql.pm

filter: Statementstmt 18.2% 21 / 115 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.2% 6 / 17 Podpod 0.0% 0 / 10 totaltotal 16.0% 27 / 168 CC 14 SCAR 26.7
CC
14
Cov
15%
_version
42.0
create_table_sql
20.0
generate_table
12.0
CRAP 14.5
« Script/View.pm ↑ Summary Script/ForeignKey.pm »
1 package Daje::Workflow::GenerateSQL::Manager::Sql;
2 1 ▶use Mojo::Base 'Daje::Workflow::GenerateSQL::Base::Common', -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # Daje::Generate::Sql::SqlManager Generatee Database and table related SQL scripts from JSON file
5 #
6 # Synopsis
7 # ========
8 #
9 # use Daje::Generate::Sql::SqlManager;
10 #
11 # my $table = Daje::Generate::Sql::SqlManager->new(
12 # json => $json,
13 # template => $template,
14 # );
15 #
16 # METHODS
17 # =======
18 # $table->generate_table();
19 # my $script = $table->sql();
20 #
21 #
22 # LICENSE
23 # =======
24 # Daje::Generate::Sql::SqlManager (the distribution) is licensed under the same terms as Perl.
25 #
26 # AUTHOR
27 # ======
28 # Jan Eskilsson
29 #
30 # COPYRIGHT
31 # =========
32 # Copyright (C) 2024 Jan Eskilsson.
33 #
34  
35 1 our $VERSION = '0.01';
36  
37 1 has 'context';
38  
39  
40 1 ▶use Daje::Workflow::GenerateSQL::Script::Fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Daje::Workflow::GenerateSQL::Script::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Daje::Workflow::GenerateSQL::Script::ForeignKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Daje::Workflow::GenerateSQL::Script::Sql;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Daje::Workflow::GenerateSQL::Script::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 0 ▶sub generate_table($self) {
Subroutine generate_table: not called CC=3 SCAR=24.8
Pod undocumented
47 0 my $sections = "";
48 0 my $json_arr = $self->json;
49 0 my $length = scalar @{$json_arr};
50 0 ▶ for (my $i = 0; $i < $length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
51 0 my $json = @{$json_arr}[$i];
52 0 ▶ if (exists($json->{version})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $json->{"version"}) 0 0 0
53 0 $sections .= $self->_version($json->{version});
54 }
55 }
56 0 $self->set_sql($self->create_file($sections));
57 0 return ;
58 }
59  
60 0 ▶sub _version($self, $version) {
Subroutine _version: not called CC=6 SCAR=37.4
61 0 my $sql = "";
62 0 my $sections = "";
63 0 my $length = scalar @{$version};
64 0 ▶ for (my $i = 0; $i < $length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
65 0 ▶ if(exists(@{$version}[$i]->{tables})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists @{$version;}[$i]->{"tables"}) 0 0 0
66 0 my $tables = @{$version}[$i]->{tables};
67 0 my $len = scalar @{$tables};
68 0 ▶ for(my $j = 0; $j < $len; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $len) 0 0 0
69 0 my $table = $self->shift_section($tables);
70 0 $sql .= $self->create_table_sql($table);
71 }
72 }
73 0 ▶ if (exists(@{$version}[$i]->{views})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists @{$version;}[$i]->{"views"}) 0 0 0
74 0 my $views = @{$version}[$i]->{views};
75 0 my $len = scalar @{$views};
76 0 ▶ for(my $j = 0; $j < $len; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $len) 0 0 0
77 0 my $view = $self->shift_section($views);
78 0 $sql .= $self->create_view_sql($view);
79 }
80 }
81 0 $sections .= $self->create_section($sql, @{$version}[$i]->{number});
82 }
83 0 return $sections
84 }
85  
86 0 ▶sub create_file($self, $sections) {
Subroutine create_file: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my $file = $self->templates->get_data_section('file');
88 0 my $date = localtime();
89 0 $file =~ s/<<date>>/$date/ig;
90 0 $file =~ s/<<sections>>/$sections/ig;
91  
92 0 return $file;
93 }
94  
95 0 ▶sub create_section($self, $sql, $number) {
Subroutine create_section: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my $section = $self->templates->get_data_section('section');
97 0 $section =~ s/<<version>>/$number/ig;
98 0 $section =~ s/<<table>>/$sql/ig;
99 0 return $section;
100 }
101  
102 0 ▶sub create_view_sql($self, $view) {
Subroutine create_view_sql: not called CC=1 SCAR=6.9
Pod undocumented
103 my $sql = Daje::Workflow::GenerateSQL::Script::View->new(
104 json => $view->{view},,
105 0 templates => $self->templates,
106 )->generate();
107  
108 0 return $sql
109 }
110  
111 0 ▶sub create_table_sql($self, $table) {
Subroutine create_table_sql: not called CC=4 SCAR=30.0
Pod undocumented
112 0 my $result = "";
113 0 my $fields = '';
114 0 my $indexes = '';
115 0 my $foreignkeys = "";
116 0 my $sql = "";
117  
118 0 my $name = $table->{table}->{name};
119 0 ▶ if (exists($table->{table}->{fields})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table->{"table"}{"fields"}) 0 0 0
120 0 $fields = $self->create_fields($table->{table});
121 0 $foreignkeys = $self->create_fkeys($table->{table}, $name);
122 }
123 0 my $test = $table->{table}->{index};
124 0 ▶ if (exists($table->{table}->{index})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table->{"table"}{"index"}) 0 0 0
125 $indexes = $self->create_index($table->{table})
126 0 }
127  
128 0 ▶ if (exists($table->{table}->{sql})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table->{"table"}{"sql"}) 0 0 0
129 0 $sql = $self->create_sql($table->{table}, $name)
130 }
131  
132 0 my $template = $self->fill_template($name, $fields, $foreignkeys, $indexes, $sql);
133  
134 0 return $template;
135  
136 }
137  
138 0 ▶sub create_sql($self, $json, $tablename) {
Subroutine create_sql: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my $sql_stmt = Daje::Workflow::GenerateSQL::Script::Sql->new(
140 json => $json,
141 templates => $self->templates,
142 tablename => $tablename,
143 );
144 0 $sql_stmt->create_sql();
145 0 return $sql_stmt->sql();
146 }
147  
148 0 ▶sub fill_template($self, $name, $fields, $foreignkeys, $indexes, $sql) {
Subroutine fill_template: not called CC=3 SCAR=24.8
Pod undocumented
149 0 my $template = $self->templates->get_data_section('table');
150 0 $template =~ s/<<fields>>/$fields/ig;
151 0 $template =~ s/<<tablename>>/$name/ig;
152 0 ▶ if(exists($foreignkeys->{template_fkey})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $foreignkeys->{'template_fkey'}) { } 0 0 0
153 0 $template =~ s/<<foregin_keys>>/$foreignkeys->{template_fkey}/ig;
154 } else {
155 0 $template =~ s/<<foregin_keys>>//ig;
156 }
157 0 ▶ if(exists($foreignkeys->{template_ind})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $foreignkeys->{"template_ind"}) 0 0 0
158 0 $indexes .= "" . $foreignkeys->{template_ind};
159 }
160  
161 0 $template =~ s/<<indexes>>/$indexes/ig;
162 0 $template =~ s/<<sql>>/$sql/ig;
163  
164 0 return $template;
165 }
166  
167 0 ▶sub create_fields($self, $json) {
Subroutine create_fields: not called CC=1 SCAR=6.9
Pod undocumented
168 0 my $fields = Daje::Workflow::GenerateSQL::Script::Fields->new(
169 json => $json,
170 templates => $self->templates,
171 error => $self->error,
172 );
173  
174 0 $fields->create_fields();
175 0 my $sql = $fields->sql;
176  
177 0 return $sql;
178 }
179  
180 0 ▶sub create_index($self, $json) {
Subroutine create_index: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my $test = 1;
182 0 my $templates= $self->templates;
183 my $index = Daje::Workflow::GenerateSQL::Script::Index->new(
184 json => $json,
185 templates => $templates,
186 tablename => $json->{name},
187 0 error => $self->error,
188 );
189  
190 0 $index->create_index();
191 0 my $sql = $index->sql;
192 0 return $sql;
193 }
194  
195 0 ▶sub create_fkeys($self, $json, $table_name) {
Subroutine create_fkeys: not called CC=2 SCAR=17.9
Pod undocumented
196 0 my $foreign_keys = {};
197 0 my $foreign_key = Daje::Workflow::GenerateSQL::Script::ForeignKey->new(
198 json => $json,
199 templates => $self->templates,
200 tablename => $table_name,
201 error => $self->error,
202 );
203 0 $foreign_key->create_foreign_keys();
204 0 ▶ if ($foreign_key->created() == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foreign_key->created == 1) 0 0 0
205 0 $foreign_keys = $foreign_key->templates();
206 }
207 0 return $foreign_keys;
208 }
209  
210  
211  
212  
213 1 1;
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226 #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
227  
228 =head1 NAME
229  
230 Daje::Workflow::GenerateSQL::Manager::Sql
231  
232  
233 =head1 DESCRIPTION
234  
235 Daje::Generate::Sql::SqlManager Generatee Database and table related SQL scripts from JSON file
236  
237  
238  
239 =head1 REQUIRES
240  
241 L<Daje::Generate::Sql::SqlManager>
242  
243 L<Daje::Workflow::GenerateSQL::Script::Sql>
244  
245 L<Daje::Workflow::GenerateSQL::Script::ForeignKey>
246  
247 L<Daje::Workflow::GenerateSQL::Script::Index>
248  
249 L<Daje::Workflow::GenerateSQL::Script::Fields>
250  
251 L<Mojo::Base>
252  
253  
254 =head1 METHODS
255  
256 $table->generate_table();
257 my $script = $table->sql();
258  
259  
260  
261  
262 =head1 Synopsis
263  
264  
265 use Daje::Generate::Sql::SqlManager;
266  
267 my $table = Daje::Generate::Sql::SqlManager->new(
268 json => $json,
269 template => $template,
270 );
271  
272  
273  
274 =head1 AUTHOR
275  
276 Jan Eskilsson
277  
278  
279  
280 =head1 COPYRIGHT
281  
282 Copyright (C) 2024 Jan Eskilsson.
283  
284  
285  
286 =head1 LICENSE
287  
288 Daje::Generate::Sql::SqlManager (the distribution) is licensed under the same terms as Perl.
289  
290  
291  
292 =cut
293  
« Script/View.pm ↑ Summary Script/ForeignKey.pm »