Quiq/SQLite.pm

filter: Statementstmt 88.5% 77 / 87 Branchbran 45.8% 11 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 79.2% 103 / 130 CC 16 SCAR 25.5
CC
16
Cov
78%
recreateDatabase
22.6
exportData
2.0
importData
2.0
CRAP 12.8
« Quiq/Debug.pm ↑ Summary Quiq/Html/Pygments.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::SQLite - Operationen auf einer SQLite-Datenbank
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::SQLite;
18 1 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '1.240';
25  
26 1 ▶use Quiq::Database::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Quiq::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # -----------------------------------------------------------------------------
32  
33 =head1 METHODS
34  
35 =head2 Klassenmethoden
36  
37 =head3 exportData() - Exportiere SQLite Tabellendaten in Verzeichnis
38  
39 =head4 Synopsis
40  
41 $class->exportData($dbFile,$exportDir);
42  
43 =head4 Arguments
44  
45 =over 4
46  
47 =item $dbFile
48  
49 SQLite Datenbank-Datei.
50  
51 =item $exportDir
52  
53 Verzeichnis, in das die Tabellendaten exportiert werden.
54  
55 =back
56  
57 =head4 Description
58  
59 Exportiere die Tabellendaten der SQLite-Datenbank $dbFile in
60 Verzeichnis $exportDir.
61  
62 =head4 Example
63  
64 Quiq::SQLite->exportData('~/var/myapp/myapp.db','/tmp/myapp');
65  
66 =cut
67  
68 # -----------------------------------------------------------------------------
69  
70 2 ▶sub exportData {
Subroutine exportData: called CC=2 SCAR=6.9
Pod documented
71 2 my ($class,$dbFile,$exportDir) = @_;
72  
73 # Exportiere die Tabellendaten
74  
75 2 my $udl = "dbi#sqlite:$dbFile";
76 2 my $db = Quiq::Database::Connection->new($udl,-utf8=>1);
77  
78 2 my @tables = $db->values(
79 -select => 'name',
80 -from => 'sqlite_master',
81 -where,
82 type => 'table',
83 "tbl_name NOT LIKE 'sqlite_%'",
84 -orderBy => 'name',
85 );
86  
87 2 for my $table (@tables) {
88 1 $db->exportTable($table,"$exportDir/$table.dat");
89 }
90  
91 2 $db->disconnect;
92  
93 2 return;
94 }
95  
96 # -----------------------------------------------------------------------------
97  
98 =head3 importData() - Importiere SQLite Datenbank aus Verzeichnis
99  
100 =head4 Synopsis
101  
102 $class->importData($dbFile,$importDir);
103  
104 =head4 Arguments
105  
106 =over 4
107  
108 =item $dbFile
109  
110 SQLite Datenbank-Datei.
111  
112 =item $exportDir
113  
114 Verzeichnis, aus dem die Tabellendaten importiert werden.
115  
116 =back
117  
118 =head4 Description
119  
120 Importiere die Tabellendaten der SQLite-Datenbank $dbFile aus
121 Verzeichnis $importDir.
122  
123 =head4 Example
124  
125 Quiq::SQLite->importData('~/var/myapp/myapp.db','/tmp/myapp');
126  
127 =cut
128  
129 # -----------------------------------------------------------------------------
130  
131 2 ▶sub importData {
Subroutine importData: called CC=2 SCAR=6.9
Pod documented
132 2 my ($class,$dbFile,$importDir) = @_;
133  
134 # Importiere Tabellendaten
135  
136 2 my $udl = "dbi#sqlite:$dbFile";
137 2 my $db = Quiq::Database::Connection->new($udl,-utf8=>1);
138 2 $db->sql('PRAGMA foreign_keys = OFF');
139  
140 2 for my $file (Quiq::Path->glob("$importDir/*.dat")) {
141 1 my ($table) = $file =~ m|/([^/]+).dat$|;
142 1 $db->delete($table);
143 1 $db->importTable($table,$file);
144 }
145 2 $db->disconnect(1);
146  
147 2 return;
148 }
149  
150 # -----------------------------------------------------------------------------
151  
152 =head3 recreateDatabase() - Erzeuge SQLite Datenbank neu
153  
154 =head4 Synopsis
155  
156 $class->recreateDatabase($dbFile,$exportDir,@opt,$sub);
157  
158 =head4 Arguments
159  
160 =over 4
161  
162 =item $dbFile
163  
164 SQLite Datenbank-Datei.
165  
166 =item $exportDir
167  
168 Verzeichnis, in das die Tabellendaten und Datenbank-Datei gesichert
169 werden. Schlägt die Neuerzeugung fehl, müssen die Tabellendaten
170 eventuell bearbeitet und die Neuerzeugung wiederholt werden.
171 Die ursprüngliche Datenbank kann bei Bedarf wieder hergestellt
172 werden, da sie zuvor ebenfalls in das Exportverzeichnis gesichert
173 wurde (s.u.).
174  
175 =item $sub
176  
177 Refenz auf die Subroutine, die das Schema auf einer I<leeren>
178 Datenbank erzeugt. Als einzigen Parameter wird $dbFile
179 an die Subroutine übergeben.
180  
181 $class->recreateDatabase('~/var/myapp/myapp.db','/tmp/myapp',sub {
182 my $dbFile = shift;
183
184 my $db = %<Quiq::Database::Connection->new("dbi#sqlite:$dbFile",
185 -utf8 => 1,
186 );
187
188 # via $db alle Schemaobjekte erzeugen,
189 # aber keine Daten importieren!
190 ...
191
192 return;
193 });
194  
195 =back
196  
197 =head4 Options
198  
199 =over 4
200  
201 =item -interactive => $bool (Default: 1)
202  
203 Stelle Rückfragen an den Benutzer.
204  
205 =back
206  
207 =head4 Description
208  
209 Erzeuge die Datenbank $dbFile via Subroutine $sub erstmalig oder neu.
210 Dies erfolgt in folgenden Schritten:
211  
212 =over 4
213  
214 =item 1.
215  
216 Tabellendaten in Exportverzeichnis exportieren
217  
218 =item 2.
219  
220 Datenbank $dbFile in Exportverzeichnis kopieren (sichern)
221  
222 =item 3.
223  
224 Datenbank $dbFile leeren
225  
226 =item 4.
227  
228 Datenbank-Strukturen via $sub erzeugen
229  
230 =item 5.
231  
232 die unter 2. exportierten Daten importieren
233  
234 =item 6.
235  
236 Exportverzeichnis löschen (falls in den Schritten 4. bis 6.
237 kein Fehler aufgetreten ist)
238  
239 =back
240  
241 Die Schritte 1. und 2. finden nur nach Rückfrage statt, wenn
242 das Exportverzeichnis bereits existiert. Das Exportverzeichnis existiert
243 typischerweise nur, wenn ein vorheriger Neuerzeugungsversuch
244 fehlgeschlagen ist.
245  
246 =cut
247  
248 # -----------------------------------------------------------------------------
249  
250 2 ▶sub recreateDatabase {
Subroutine recreateDatabase: called CC=14 SCAR=31.2
Pod documented
251 2 my $class = shift;
252 # @_: $dbFile,$exportDir,@opt,$sub
253  
254 # Optionen und Argumente
255  
256 2 my $interactive = 1;
257  
258 2 my $argA = $class->parameters(3,3,\@_,
259 -interactive => \$interactive,
260 );
261 2 my ($dbFile,$exportDir,$sub) = @$argA;
262  
263 # Operation ausführen
264  
265 2 my $p = Quiq::Path->new;
266  
267 # Exportiere Tabellendaten und sichere Datenbank. Wenn das
268 # Exportverzeichnis bereits existiert, stellen wir eine
269 # Rückfrage, denn es könnte der erneute Versuch nach einem
270 # fehlgeschlagenen Import sein.
271  
272 2 my $export = 1;
273 2 ▶ if ($p->exists($exportDir) && $interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->exists($exportDir) and $interactive) { } 0 2 2
Condition33%1/3
$p->exists($exportDir) and $interactive
!ll&&!rl&&r
200
MC/DC0%0/2
$p->exists($exportDir) and $interactive
$p->exists($exportDir) $interactive
Truth table33%1/3
A and B
A= $p->exists($exportDir)B= $interactive
ABresult
0X0
100
111
274 0 my $answ = Quiq::Terminal->askUser(
275 "ExportDir $exportDir already exists. Delete and export again?",
276 -values => 'y/n',
277 -default => 'n',
278 );
279 0 ▶ if ($answ eq 'n') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answ eq "n") 0 0 0
280 0 $export = 0;
281 }
282 }
283 else {
284 2 say "Creating exportdir $exportDir ...";
285 2 $p->mkdir($exportDir,-recursive=>1);
286 }
287  
288 2 ▶ if ($export) {
Branch: 1 branch
BranchTrueFalseTotal
if ($export) 2 0 2
289 2 ▶ if ($interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interactive) 0 2 2
290 0 say "Exporting table data to $exportDir ...";
291 }
292 2 $class->exportData($dbFile,$exportDir);
293  
294 2 my $saveDir = '~/tmp';
295 2 $p->mkdir($saveDir,-recursive=>1);
296 2 my (undef,undef,$basename,$ext) = $p->split($dbFile);
297 2 my $now = POSIX::strftime('%Y%m%d-%H%M%S',localtime);
298 2 my $destFile = sprintf '%s/%s-%s.%s',$saveDir,$basename,$now,$ext;
299 2 ▶ if ($interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interactive) 0 2 2
300 0 say "Saving database $dbFile to $destFile ...";
301 }
302 2 $p->copy($dbFile,$destFile,-preserve=>1);
303 }
304  
305 # Erzeuge Datenbank neu und importiere Tabellendaten
306  
307 2 my $status = eval {
308 2 ▶ if ($interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interactive) 0 2 2
309 0 say "Truncating database $dbFile ...";
310 }
311 2 $p->truncate($dbFile);
312 2 ▶ if ($interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interactive) 0 2 2
313 0 say "Creating tables etc. ...";
314 }
315 2 $sub->($dbFile);
316 2 my $answ = Quiq::Terminal->askUser(
317 "Ready to import data from $exportDir?",
318 -values => '(y)es,(a)bort',
319 -default => 'y',
320 -automatic => !$interactive,
321 );
322 2 ▶ if ($answ eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answ eq "y") 2 0 2
323 2 ▶ if ($interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interactive) 0 2 2
324 0 say "Importing table data from $exportDir ...";
325 }
326 2 $class->importData($dbFile,$exportDir);
327 }
328 2 return $answ;
329 };
330 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
331 0 $class->throw(
332 'SQLITE-00001: Recreation of database failed',
333 Database => $dbFile,
334 ExportDir => $exportDir,
335 Error => $@,
336 );
337 }
338 2 ▶ if ($status eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "y") 2 0 2
339 # Wenn alles geklappt hat, löschen wir das Exportverzeichnis
340  
341 2 ▶ if ($interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interactive) 0 2 2
342 0 say "Deleting $exportDir ...";
343 }
344 2 $p->delete($exportDir);
345 }
346  
347 2 return;
348 }
349  
350 # -----------------------------------------------------------------------------
351  
352 =head1 VERSION
353  
354 1.240
355  
356 =head1 AUTHOR
357  
358 Frank Seitz, L<http://fseitz.de/>
359  
360 =head1 COPYRIGHT
361  
362 Copyright (C) 2026 Frank Seitz
363  
364 =head1 LICENSE
365  
366 This code is free software; you can redistribute it and/or modify
367 it under the same terms as Perl itself.
368  
369 =cut
370  
371 # -----------------------------------------------------------------------------
372  
373 1 1;
374  
375 # eof
« Quiq/Debug.pm ↑ Summary Quiq/Html/Pygments.pm »