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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
package sqlLayer;
use strict;
use warnings;
use Data::Dumper;
use DBI;
use phonebook;
sub new( $ $ $ ) {
my ($class, $dbHandle, $dbType, $version) = @_;
my $this = {};
bless $this, $class;
$this->{'dbh'} = $dbHandle;
$this->{'type'} = $dbType;
#default to 0, perfect compatablity with C primary xml parsing
# 1 = compatibility of C combo parsing
# 2 = multilingual support and printers_byname in drivers
$this->{'version'} = 0;
$this->{'version'} = $version if(defined($version));
my $phonebook = phonebook->new($this->{'version'});
$this->{'printerPhonebook'} = $phonebook->printer();
$this->{'driverPhonebook'} = $phonebook->driver();
$this->{'optionPhonebook'} = $phonebook->option();
$this->{'schemaPhonebook'} = $phonebook->schema();
return $this;
}
#TODO: Move this and the version in xmlParse to a helper lib
sub getCleanId {
my ($id) = @_;
$id =~ s/^[^\/]*\///;
#remove everything before the leading slash
return $id;
}
sub getSchema {
my ($this, $table) = @_;
return $this->{'schemaPhonebook'}{$table};
}
sub initDatabase {
my ($this) = @_;
#Create the Tables
foreach my $table (keys %{$this->{'schemaPhonebook'}}) {
my $schema = $this->getSchema($table);
my $sth = $this->{'dbh'}->prepare($schema);
$sth->execute();
}
$this->{'dbh'}->commit();
}
sub getSortedKeys {
my ($this, $data) = @_;
my @keys;
foreach my $key (keys %{$data}) {
push(@keys, $key);
}
@keys = sort(@keys);
#create a string that sums the contents of @keys, this allows us to identify
#similar sets of keys
my $sumString;
foreach my $key (@keys) {
$sumString .= $key;
}
return (\@keys, $sumString);
}
sub getPreparedStatement {
my ($this, $wantedTable, $data) = @_;
my ($sortedKeys, $summedKeys) = $this->getSortedKeys($data);
#use cached copy if present
if (defined($this->{'cache'}{'preparedStatements'}{$wantedTable}{$summedKeys})) {
return $this->{'cache'}{'preparedStatements'}{$wantedTable}{$summedKeys};
}
#Create the SQL
my $statementBegining = "INSERT INTO ".$wantedTable."(";
my $tableKeys = join(",", @{$sortedKeys});
my $statementMiddle = ") VALUES (";
my $bindValues;
for(my $i = 0; $i < @{$sortedKeys}; $i++) {
$bindValues .= ',' if ($i > 0);
$bindValues .= '?';
}
my $statementEnd = ");";
my $statementSql = $statementBegining . $tableKeys .
$statementMiddle . $bindValues . $statementEnd;
my $sth = $this->{'dbh'}->prepare($statementSql);
#add the sth to our cache
$this->{'cache'}{'preparedStatements'}{$wantedTable}{$summedKeys} = $sth;
return $sth;
}
sub setPreparedData {
my ($this, $data, $phonebook) = @_;
#compute the namespace from the phonebook
my $nodePath = $phonebook->[0][0];#the nodepath of the first element
$nodePath =~ m!^/([^/]*)/!;
my $namespace = $1;
#We sort the data by table and rename if needed.
foreach my $element (@{$phonebook}) {
my ($ignore, $source, $mainGroup, $table, $destination, $minorGroup) = @{$element};
$destination = $source if (!$destination);
#Only sort if the table is defined and if the destination key has not
#been set, does not work for complex types
if($table && !exists $data->{'prepared'}{$table}{$destination}) {
if($minorGroup == 4) {
if($namespace eq 'option') {
$data->{'prepared'}{$table}{$destination} = getCleanId($data->{$source});
}
} elsif($minorGroup == 7) {#multilingual comments
foreach my $lang (keys %{$data->{$source}}) {
if($namespace eq 'printer') {
my %preparedComment;
$preparedComment{'id'} = $data->{'id'};
$preparedComment{'lang'} = $lang;
$preparedComment{'comments'} = $data->{'comments'}{$lang};
push(@{$data->{'prepared'}{$table}{$destination}}, \%preparedComment);
} elsif ($namespace eq 'option') {
my %preparedComment;
$preparedComment{'id'} = getCleanId($data->{'idx'});
$preparedComment{'lang'} = $lang;
$preparedComment{'longname'} = $data->{'comments'}{$lang};
push(@{$data->{'prepared'}{$table}{$destination}}, \%preparedComment);
}
}
} elsif($mainGroup == 13) { #Languages
if($namespace eq 'printer') {
# A complex data key must not be undef
# since there is no direct key in the table for it
next() if !defined($data->{$source});
foreach my $lang (@{$data->{$source}}) {
my $name = $lang->{'name'};
$data->{'prepared'}{$table}{$name} = 1;
if(defined($lang->{'level'}) && !($name eq 'proprietary') ) {
$data->{'prepared'}{$table}{$name.'_level'} = $lang->{'level'};
}
}
}
} elsif( defined($data->{$source})) {
$data->{'prepared'}{$table}{$destination} = $data->{$source};
}
}
}
}
sub insertData {
my ($this, $data, $table) = @_;
my $sth = $this->getPreparedStatement($table, $data);
#Sort data alphabetically, required for prepared statements
my ($sortedKeys) = $this->getSortedKeys($data);
my @sortedData;
foreach my $key (@{$sortedKeys}) {
push(@sortedData, $data->{$key});
}
if( !$sth->execute(@sortedData) ) {
print Dumper($data);
die $this->{'dbh'}->errstr . "\n------\n";
}
}
sub insertTranslationData {
my ($this, $data, $table) = @_;
foreach my $comment (@{ $data->{'prepared'}{$table}{'comments'} }) {
$this->insertData($comment, $table);
}
}
sub pushOption {
my ($this, $option) = @_;
$this->setPreparedData($option, $this->{'optionPhonebook'});
$this->insertData($option->{'prepared'}{'options'},'options');
$this->insertTranslationData($option, 'options_translation');
$this->{'dbh'}->commit;
}
sub pushPrinter {
my ($this, $printer) = @_;
$this->setPreparedData($printer, $this->{'printerPhonebook'});
$this->insertData($printer->{'prepared'}{'printer'},'printer');
$this->insertTranslationData($printer, 'printer_translation');
$this->{'dbh'}->commit;
}
1;
|