-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfoomatic-xml-to-sqlite.in
84 lines (65 loc) · 2.15 KB
/
foomatic-xml-to-sqlite.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!@PERL@
# -*- perl -*-
# foomatic-xml-to-sqlite takes the xml library and creates a stand alone sqlite
# database usable by Foomatic::DB as a backend. Any existing database is
# deleted. On a quad core AMD the process takes about one minute.
use strict;
use warnings;
use Data::Dumper;
use DBI;
use Foomatic::DB;
use Foomatic::Defaults;
use Foomatic::filters::sql::to;
my $dbFile = "$libdir/db/openprinting.db";
if ($#ARGV > -1) {
if ($ARGV[0] eq "-h" || $ARGV[0] eq '--help') {
print STDERR "
Usage: foomatic-xml-to-sqlite.pl [\"SQlite_DB_Path\"]
SQlite_DB_Path: By default we export to openprinting.db in foomatic-db
optionaly a different destination db can be specified. Note: when using a
non-default path there is no guaranty that DB.pm will use the
XML backend.
\"$libdir/db/openprinting.db\"
Example:
foomatic-xml-to-sqlite.pl '../foomatic-db/db/openprinting.db'
";
exit(1);
} else {
$dbFile = $ARGV[0] if defined($ARGV[0]);
}
}
# Delete any existing database.
unlink($dbFile);
# Foomatic::DB uses a sqlite database if present, thus the file must be deleted
# while we init our foomatic::db. This will only work for the default db
# location.
my $db = new Foomatic::DB;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbFile","","");
my $sqlLayer = Foomatic::filters::sql::to->new($dbh, 'sqlite', 2);
$sqlLayer->initDatabase();# Creates tables
my @options = <$libdir/db/source/opt/*.xml>;
my @printers = <$libdir/db/source/printer/*.xml>;
my @drivers = <$libdir/db/source/driver/*.xml>;
my $now = time;
my $i = 1;
for my $driver (@drivers) {
printf("Xml #: %004d - driver - $driver\n", $i);
$driver = $db->get_driver($driver, 2);
$sqlLayer->pushDriver($driver, 2);
$i++;
}
for my $option (@options) {
printf("Xml #: %004d - option - $option\n", $i);
$option = $db->get_option( $option, 2);
$sqlLayer->pushOption( $option, 2);
$i++;
}
for my $printer (@printers) {
printf("Xml #: %004d - printer - $printer\n", $i);
$printer = $db->get_printer($printer, 2);
$sqlLayer->pushPrinter($printer, 2);
$i++;
}
$now = time - $now;
printf("\n\nTotal running time: %02d:%02d:%02d\n\n", int($now / 3600), int(($now % 3600) / 60),
int($now % 60));