[15699] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # dbutil.pm -- utility functions for writing to different databases
|
---|
| 4 | # Copyright (C) 2008 DL Consulting Ltd
|
---|
| 5 | #
|
---|
| 6 | # A component of the Greenstone digital library software
|
---|
| 7 | # from the New Zealand Digital Library Project at the
|
---|
| 8 | # University of Waikato, New Zealand.
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
| 25 |
|
---|
| 26 | package dbutil;
|
---|
| 27 |
|
---|
[17476] | 28 | use Encode;
|
---|
[15699] | 29 | use strict;
|
---|
| 30 |
|
---|
| 31 |
|
---|
[17087] | 32 | sub open_infodb_write_handle
|
---|
| 33 | {
|
---|
[17476] | 34 | my $infodb_type = shift(@_);
|
---|
| 35 | my $infodb_file_path = shift(@_);
|
---|
| 36 |
|
---|
| 37 | if ($infodb_type eq "sqlite")
|
---|
| 38 | {
|
---|
| 39 | return &open_infodb_write_handle_sqlite($infodb_file_path);
|
---|
| 40 | }
|
---|
| 41 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 42 | {
|
---|
| 43 | return &open_infodb_write_handle_gdbm_txtgz($infodb_file_path);
|
---|
| 44 | }
|
---|
| 45 | elsif ($infodb_type eq "mssql")
|
---|
| 46 | {
|
---|
| 47 | return &open_infodb_write_handle_mssql($infodb_file_path);
|
---|
| 48 | }
|
---|
[17087] | 49 |
|
---|
[17476] | 50 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
| 51 | return &open_infodb_write_handle_gdbm($infodb_file_path);
|
---|
[17087] | 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
[16176] | 55 | sub close_infodb_write_handle
|
---|
| 56 | {
|
---|
[17476] | 57 | my $infodb_type = shift(@_);
|
---|
| 58 | my $infodb_handle = shift(@_);
|
---|
[16176] | 59 |
|
---|
[17476] | 60 | if ($infodb_type eq "sqlite")
|
---|
| 61 | {
|
---|
| 62 | return &close_infodb_write_handle_sqlite($infodb_handle);
|
---|
| 63 | }
|
---|
| 64 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 65 | {
|
---|
| 66 | return &close_infodb_write_handle_gdbm_txtgz($infodb_handle);
|
---|
| 67 | }
|
---|
| 68 | elsif ($infodb_type eq "mssql")
|
---|
| 69 | {
|
---|
| 70 | return &close_infodb_write_handle_mssql($infodb_handle);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
| 74 | return &close_infodb_write_handle_gdbm($infodb_handle);
|
---|
[17104] | 75 | }
|
---|
[16176] | 76 |
|
---|
| 77 |
|
---|
[15727] | 78 | sub get_default_infodb_type
|
---|
| 79 | {
|
---|
[17476] | 80 | # The default is GDBM so everything works the same for existing collections
|
---|
| 81 | # To use something else, specify the "infodbtype" in the collection's collect.cfg file
|
---|
| 82 | return "gdbm";
|
---|
[15727] | 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
[15710] | 86 | sub get_infodb_file_path
|
---|
| 87 | {
|
---|
[17476] | 88 | my $infodb_type = shift(@_);
|
---|
| 89 | my $collection_name = shift(@_);
|
---|
| 90 | my $infodb_directory_path = shift(@_);
|
---|
[15710] | 91 |
|
---|
[17476] | 92 | if ($infodb_type eq "sqlite")
|
---|
| 93 | {
|
---|
| 94 | return &get_infodb_file_path_sqlite($collection_name, $infodb_directory_path);
|
---|
| 95 | }
|
---|
| 96 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 97 | {
|
---|
| 98 | return &get_infodb_file_path_gdbm_txtgz($collection_name, $infodb_directory_path);
|
---|
| 99 | }
|
---|
| 100 | elsif ($infodb_type eq "mssql")
|
---|
| 101 | {
|
---|
| 102 | #==================================================================================================#
|
---|
| 103 | # Updated by Jeffrey (2008/08/25 Monday)
|
---|
| 104 | # After look into the run-time code, it seems we should still create a database file.
|
---|
| 105 | # Since the run-time code is always try to read a database file, the easiest way here is not
|
---|
| 106 | # to change the whole structure, but to give whatever the system is looking for.
|
---|
| 107 | #==================================================================================================#
|
---|
| 108 | # Added by Jeffrey (2008/08/15 Friday)
|
---|
| 109 | # No file path required for MS SQL, it is a server-client connection.
|
---|
| 110 | # At the moment the information is hard coded in open_infodb_write_handle_mssql
|
---|
| 111 | # the this might need some tidy up sometime.
|
---|
| 112 | #==================================================================================================#
|
---|
| 113 | return &get_infodb_file_path_mssql($collection_name, $infodb_directory_path);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
| 117 | return &get_infodb_file_path_gdbm($collection_name, $infodb_directory_path);
|
---|
[15710] | 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
[15705] | 121 | sub read_infodb_file
|
---|
| 122 | {
|
---|
[17476] | 123 | my $infodb_type = shift(@_);
|
---|
| 124 | my $infodb_file_path = shift(@_);
|
---|
| 125 | my $infodb_map = shift(@_);
|
---|
[15705] | 126 |
|
---|
[17476] | 127 | if ($infodb_type eq "sqlite")
|
---|
| 128 | {
|
---|
| 129 | return &read_infodb_file_sqlite($infodb_file_path, $infodb_map);
|
---|
| 130 | }
|
---|
| 131 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 132 | {
|
---|
| 133 | return &read_infodb_file_gdbm_txtgz($infodb_file_path, $infodb_map);
|
---|
| 134 | }
|
---|
| 135 | elsif ($infodb_type eq "mssql")
|
---|
| 136 | {
|
---|
| 137 | return &read_infodb_file_mssql($infodb_file_path, $infodb_map);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
| 141 | return &read_infodb_file_gdbm($infodb_file_path, $infodb_map);
|
---|
[15705] | 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
[15699] | 145 | sub write_infodb_entry
|
---|
| 146 | {
|
---|
[17476] | 147 | my $infodb_type = shift(@_);
|
---|
| 148 | my $infodb_handle = shift(@_);
|
---|
| 149 | my $infodb_key = shift(@_);
|
---|
| 150 | my $infodb_map = shift(@_);
|
---|
[15699] | 151 |
|
---|
[17476] | 152 | if ($infodb_type eq "sqlite")
|
---|
| 153 | {
|
---|
| 154 | return &write_infodb_entry_sqlite($infodb_handle, $infodb_key, $infodb_map);
|
---|
| 155 | }
|
---|
| 156 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 157 | {
|
---|
| 158 | return &write_infodb_entry_gdbm_txtgz($infodb_handle, $infodb_key, $infodb_map);
|
---|
| 159 | }
|
---|
| 160 | elsif ($infodb_type eq "mssql")
|
---|
| 161 | {
|
---|
| 162 | return &write_infodb_entry_mssql($infodb_handle, $infodb_key, $infodb_map);
|
---|
| 163 | }
|
---|
[15725] | 164 |
|
---|
[17476] | 165 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
| 166 | return &write_infodb_entry_gdbm($infodb_handle, $infodb_key, $infodb_map);
|
---|
[15699] | 167 | }
|
---|
| 168 |
|
---|
[15705] | 169 |
|
---|
| 170 |
|
---|
[17087] | 171 | # -----------------------------------------------------------------------------
|
---|
| 172 | # GDBM TXT-GZ IMPLEMENTATION
|
---|
| 173 | # -----------------------------------------------------------------------------
|
---|
[15705] | 174 |
|
---|
[17087] | 175 | sub open_infodb_write_handle_gdbm_txtgz
|
---|
[16176] | 176 | {
|
---|
[17476] | 177 | # Keep infodb in GDBM neutral form => save data as compressed text file,
|
---|
| 178 | # read for txt2db to be run on it later (i.e. by the runtime system,
|
---|
| 179 | # first time the collection is ever accessed). This makes it easier
|
---|
| 180 | # distribute pre-built collections to various architectures.
|
---|
| 181 | #
|
---|
| 182 | # NB: even if two architectures are little endian (e.g. Intel and
|
---|
| 183 | # ARM procesors) GDBM does *not* guarantee that the database generated on
|
---|
| 184 | # one will work on the other
|
---|
[17087] | 185 |
|
---|
[17476] | 186 | my $infodb_file_path = shift(@_);
|
---|
[17087] | 187 |
|
---|
[17476] | 188 | # Greenstone ships with gzip for windows, on $PATH
|
---|
[17087] | 189 |
|
---|
[17476] | 190 | my $infodb_file_handle = undef;
|
---|
| 191 | if (!open($infodb_file_handle, "| gzip - > \"$infodb_file_path\""))
|
---|
| 192 | {
|
---|
| 193 | return undef;
|
---|
| 194 | }
|
---|
[17087] | 195 |
|
---|
[17476] | 196 | return $infodb_file_handle;
|
---|
[17087] | 197 | }
|
---|
| 198 |
|
---|
[17104] | 199 |
|
---|
[17087] | 200 | sub close_infodb_write_handle_gdbm_txtgz
|
---|
| 201 | {
|
---|
[17476] | 202 | my $infodb_handle = shift(@_);
|
---|
[16176] | 203 |
|
---|
[17476] | 204 | close($infodb_handle);
|
---|
[16176] | 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
[17087] | 208 | sub get_infodb_file_path_gdbm_txtgz
|
---|
[15710] | 209 | {
|
---|
[17476] | 210 | my $collection_name = shift(@_);
|
---|
| 211 | my $infodb_directory_path = shift(@_);
|
---|
[15710] | 212 |
|
---|
[17476] | 213 | my $infodb_file_name = &util::get_dirsep_tail($collection_name).".txt.gz";
|
---|
| 214 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
[15710] | 215 | }
|
---|
| 216 |
|
---|
| 217 |
|
---|
[17087] | 218 | sub read_infodb_file_gdbm_txtgz
|
---|
[15711] | 219 | {
|
---|
[17476] | 220 | my $infodb_file_path = shift(@_);
|
---|
| 221 | my $infodb_map = shift(@_);
|
---|
[15711] | 222 |
|
---|
[17476] | 223 | my $cmd = "gzip --decompress \"$infodb_file_path\"";
|
---|
[15711] | 224 |
|
---|
[17476] | 225 | open (PIPEIN, "$cmd |")
|
---|
| 226 | || die "Error: Couldn't open pipe from gzip: $!\n $cmd\n";
|
---|
[15711] | 227 |
|
---|
[17476] | 228 | my $infodb_line = "";
|
---|
| 229 | my $infodb_key = "";
|
---|
| 230 | my $infodb_value = "";
|
---|
| 231 | while (defined ($infodb_line = <PIPEIN>))
|
---|
| 232 | {
|
---|
| 233 | if ($infodb_line =~ /^\[([^\]]+)\]$/)
|
---|
[15705] | 234 | {
|
---|
[17476] | 235 | $infodb_key = $1;
|
---|
[15705] | 236 | }
|
---|
[17476] | 237 | elsif ($infodb_line =~ /^-{70}$/)
|
---|
| 238 | {
|
---|
| 239 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
| 240 | $infodb_key = "";
|
---|
| 241 | $infodb_value = "";
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
| 244 | {
|
---|
| 245 | $infodb_value .= $infodb_line;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
[15705] | 248 |
|
---|
[17476] | 249 | close (PIPEIN);
|
---|
[15705] | 250 | }
|
---|
| 251 |
|
---|
[15699] | 252 |
|
---|
[17087] | 253 | sub write_infodb_entry_gdbm_txtgz
|
---|
[15699] | 254 | {
|
---|
[17087] | 255 |
|
---|
[17476] | 256 | my $infodb_handle = shift(@_);
|
---|
| 257 | my $infodb_key = shift(@_);
|
---|
| 258 | my $infodb_map = shift(@_);
|
---|
| 259 |
|
---|
| 260 | print $infodb_handle "[$infodb_key]\n";
|
---|
| 261 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 262 | {
|
---|
| 263 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
[15699] | 264 | {
|
---|
[17476] | 265 | if ($infodb_value =~ /-{70,}/)
|
---|
| 266 | {
|
---|
| 267 | # if value contains 70 or more hyphens in a row we need to escape them
|
---|
| 268 | # to prevent txt2db from treating them as a separator
|
---|
| 269 | $infodb_value =~ s/-/&\#045;/gi;
|
---|
| 270 | }
|
---|
| 271 | print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
|
---|
[15699] | 272 | }
|
---|
[17476] | 273 | }
|
---|
| 274 | print $infodb_handle '-' x 70, "\n";
|
---|
[15699] | 275 | }
|
---|
| 276 |
|
---|
| 277 |
|
---|
[15722] | 278 |
|
---|
[17087] | 279 | # -----------------------------------------------------------------------------
|
---|
| 280 | # GDBM IMPLEMENTATION
|
---|
| 281 | # -----------------------------------------------------------------------------
|
---|
[15722] | 282 |
|
---|
[17087] | 283 | sub open_infodb_write_handle_gdbm
|
---|
[16176] | 284 | {
|
---|
[17476] | 285 | my $infodb_file_path = shift(@_);
|
---|
[16176] | 286 |
|
---|
[17476] | 287 | my $txt2db_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}, "txt2db" . &util::get_os_exe());
|
---|
| 288 | my $infodb_file_handle = undef;
|
---|
| 289 | if (!-e "$txt2db_exe" || !open($infodb_file_handle, "| \"$txt2db_exe\" \"$infodb_file_path\""))
|
---|
| 290 | {
|
---|
| 291 | return undef;
|
---|
| 292 | }
|
---|
[16178] | 293 |
|
---|
[17476] | 294 | return $infodb_file_handle;
|
---|
[17087] | 295 | }
|
---|
[16223] | 296 |
|
---|
[17105] | 297 |
|
---|
[17087] | 298 | sub close_infodb_write_handle_gdbm
|
---|
| 299 | {
|
---|
[17476] | 300 | my $infodb_handle = shift(@_);
|
---|
[17087] | 301 |
|
---|
[17476] | 302 | close($infodb_handle);
|
---|
[16176] | 303 | }
|
---|
| 304 |
|
---|
| 305 |
|
---|
[17087] | 306 | sub get_infodb_file_path_gdbm
|
---|
[15722] | 307 | {
|
---|
[17476] | 308 | my $collection_name = shift(@_);
|
---|
| 309 | my $infodb_directory_path = shift(@_);
|
---|
[15722] | 310 |
|
---|
[17476] | 311 | my $infodb_file_extension = (&util::is_little_endian() ? ".ldb" : ".bdb");
|
---|
| 312 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
| 313 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
[15722] | 314 | }
|
---|
| 315 |
|
---|
| 316 |
|
---|
[17087] | 317 | sub read_infodb_file_gdbm
|
---|
| 318 | {
|
---|
[17476] | 319 | my $infodb_file_path = shift(@_);
|
---|
| 320 | my $infodb_map = shift(@_);
|
---|
[17087] | 321 |
|
---|
[18441] | 322 | open (PIPEIN, "db2txt \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
|
---|
[17476] | 323 | my $infodb_line = "";
|
---|
| 324 | my $infodb_key = "";
|
---|
| 325 | my $infodb_value = "";
|
---|
| 326 | while (defined ($infodb_line = <PIPEIN>))
|
---|
| 327 | {
|
---|
| 328 | if ($infodb_line =~ /^\[([^\]]+)\]$/)
|
---|
[17087] | 329 | {
|
---|
[17476] | 330 | $infodb_key = $1;
|
---|
[17087] | 331 | }
|
---|
[17476] | 332 | elsif ($infodb_line =~ /^-{70}$/)
|
---|
| 333 | {
|
---|
| 334 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
| 335 | $infodb_key = "";
|
---|
| 336 | $infodb_value = "";
|
---|
| 337 | }
|
---|
| 338 | else
|
---|
| 339 | {
|
---|
| 340 | $infodb_value .= $infodb_line;
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
[17087] | 343 |
|
---|
[17476] | 344 | close (PIPEIN);
|
---|
[17087] | 345 | }
|
---|
| 346 |
|
---|
[18441] | 347 | sub read_infodb_keys_gdbm
|
---|
| 348 | {
|
---|
| 349 | my $infodb_file_path = shift(@_);
|
---|
| 350 | my $infodb_map = shift(@_);
|
---|
| 351 |
|
---|
| 352 | open (PIPEIN, "gdbmkeys \"$infodb_file_path\" |") || die "couldn't open pipe from gdbmkeys \$infodb_file_path\"\n";
|
---|
| 353 | my $infodb_line = "";
|
---|
| 354 | my $infodb_key = "";
|
---|
| 355 | my $infodb_value = "";
|
---|
| 356 | while (defined ($infodb_line = <PIPEIN>))
|
---|
| 357 | {
|
---|
| 358 | chomp $infodb_line; # remove end of line
|
---|
| 359 |
|
---|
| 360 | $infodb_map->{$infodb_line} = 1;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | close (PIPEIN);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
[17087] | 366 |
|
---|
| 367 | sub write_infodb_entry_gdbm
|
---|
| 368 | {
|
---|
[17476] | 369 | # With infodb_handle already set up, works the same as _gdbm_txtgz version
|
---|
| 370 | write_infodb_entry_gdbm_txtgz(@_);
|
---|
[17087] | 371 | }
|
---|
| 372 |
|
---|
| 373 |
|
---|
| 374 |
|
---|
| 375 | # -----------------------------------------------------------------------------
|
---|
| 376 | # SQLITE IMPLEMENTATION
|
---|
| 377 | # -----------------------------------------------------------------------------
|
---|
| 378 |
|
---|
[15722] | 379 | sub open_infodb_write_handle_sqlite
|
---|
| 380 | {
|
---|
[17476] | 381 | my $infodb_file_path = shift(@_);
|
---|
[15722] | 382 |
|
---|
[17476] | 383 | my $sqlite3_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}, "sqlite3" . &util::get_os_exe());
|
---|
| 384 | my $infodb_handle = undef;
|
---|
| 385 | if (!-e "$sqlite3_exe" || !open($infodb_handle, "| \"$sqlite3_exe\" \"$infodb_file_path\""))
|
---|
| 386 | {
|
---|
| 387 | return undef;
|
---|
| 388 | }
|
---|
[15722] | 389 |
|
---|
[17476] | 390 | print $infodb_handle "CREATE TABLE IF NOT EXISTS data (key TEXT PRIMARY KEY, value TEXT);\n";
|
---|
| 391 | print $infodb_handle "CREATE TABLE IF NOT EXISTS document_metadata (id INTEGER PRIMARY KEY, docOID TEXT, element TEXT, value TEXT);\n";
|
---|
[15722] | 392 |
|
---|
[17476] | 393 | # This is crucial for efficiency when importing large amounts of data
|
---|
| 394 | print $infodb_handle "CREATE INDEX IF NOT EXISTS dmd ON document_metadata(docOID);\n";
|
---|
[16240] | 395 |
|
---|
[17476] | 396 | # This is very important for efficiency, otherwise each command will be actioned one at a time
|
---|
| 397 | print $infodb_handle "BEGIN TRANSACTION;\n";
|
---|
[16178] | 398 |
|
---|
[17476] | 399 | return $infodb_handle;
|
---|
[15722] | 400 | }
|
---|
| 401 |
|
---|
| 402 |
|
---|
[17087] | 403 | sub close_infodb_write_handle_sqlite
|
---|
| 404 | {
|
---|
[17476] | 405 | my $infodb_handle = shift(@_);
|
---|
[17087] | 406 |
|
---|
[17476] | 407 | # Close the transaction we began after opening the file
|
---|
| 408 | print $infodb_handle "END TRANSACTION;\n";
|
---|
[17087] | 409 |
|
---|
[17476] | 410 | # This is crucial for efficient queries on the database!
|
---|
| 411 | print $infodb_handle "CREATE INDEX IF NOT EXISTS dme ON document_metadata(element);\n";
|
---|
[17087] | 412 |
|
---|
[17476] | 413 | close($infodb_handle);
|
---|
[17087] | 414 | }
|
---|
| 415 |
|
---|
| 416 |
|
---|
| 417 | sub get_infodb_file_path_sqlite
|
---|
| 418 | {
|
---|
[17476] | 419 | my $collection_name = shift(@_);
|
---|
| 420 | my $infodb_directory_path = shift(@_);
|
---|
[17087] | 421 |
|
---|
[17476] | 422 | my $infodb_file_extension = ".db";
|
---|
| 423 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
| 424 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
[17087] | 425 | }
|
---|
| 426 |
|
---|
| 427 |
|
---|
[15746] | 428 | sub read_infodb_file_sqlite
|
---|
| 429 | {
|
---|
[17476] | 430 | my $infodb_file_path = shift(@_);
|
---|
| 431 | my $infodb_map = shift(@_);
|
---|
[15746] | 432 |
|
---|
[17476] | 433 | # !! TO IMPLEMENT
|
---|
[15746] | 434 | }
|
---|
| 435 |
|
---|
| 436 |
|
---|
[15722] | 437 | sub write_infodb_entry_sqlite
|
---|
| 438 | {
|
---|
[17476] | 439 | my $infodb_handle = shift(@_);
|
---|
| 440 | my $infodb_key = shift(@_);
|
---|
| 441 | my $infodb_map = shift(@_);
|
---|
[15722] | 442 |
|
---|
[17476] | 443 | # Add the key -> value mapping into the "data" table
|
---|
| 444 | my $infodb_entry_value = "";
|
---|
| 445 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 446 | {
|
---|
| 447 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
[15722] | 448 | {
|
---|
[17476] | 449 | $infodb_entry_value .= "<$infodb_value_key>" . $infodb_value . "\n";
|
---|
[15722] | 450 | }
|
---|
[17476] | 451 | }
|
---|
[15722] | 452 |
|
---|
[17476] | 453 | my $safe_infodb_key = &sqlite_safe($infodb_key);
|
---|
| 454 | print $infodb_handle "INSERT OR REPLACE INTO data (key, value) VALUES ('" . $safe_infodb_key . "', '" . &sqlite_safe($infodb_entry_value) . "');\n";
|
---|
[15748] | 455 |
|
---|
[17476] | 456 | # If this infodb entry is for a document, add all the interesting document metadata to the
|
---|
| 457 | # "document_metadata" table (for use by the dynamic classifiers)
|
---|
| 458 | if ($infodb_key !~ /\./ && $infodb_entry_value =~ /\<doctype\>doc\n/)
|
---|
| 459 | {
|
---|
| 460 | print $infodb_handle "DELETE FROM document_metadata WHERE docOID='" . $safe_infodb_key . "';\n";
|
---|
| 461 |
|
---|
| 462 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
[15748] | 463 | {
|
---|
[17476] | 464 | # We're not interested in most of the automatically added document metadata
|
---|
| 465 | next if ($infodb_value_key eq "archivedir" ||
|
---|
| 466 | $infodb_value_key eq "assocfilepath" ||
|
---|
| 467 | $infodb_value_key eq "childtype" ||
|
---|
| 468 | $infodb_value_key eq "contains" ||
|
---|
| 469 | $infodb_value_key eq "docnum" ||
|
---|
| 470 | $infodb_value_key eq "doctype" ||
|
---|
| 471 | $infodb_value_key eq "Encoding" ||
|
---|
| 472 | $infodb_value_key eq "FileSize" ||
|
---|
| 473 | $infodb_value_key eq "hascover" ||
|
---|
| 474 | $infodb_value_key eq "hastxt" ||
|
---|
| 475 | $infodb_value_key eq "lastmodified" ||
|
---|
| 476 | $infodb_value_key eq "metadataset" ||
|
---|
| 477 | $infodb_value_key eq "thistype" ||
|
---|
| 478 | $infodb_value_key =~ /^metadatafreq\-/ ||
|
---|
| 479 | $infodb_value_key =~ /^metadatalist\-/);
|
---|
[15748] | 480 |
|
---|
[17476] | 481 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
| 482 | {
|
---|
| 483 | print $infodb_handle "INSERT INTO document_metadata (docOID, element, value) VALUES ('" . $safe_infodb_key . "', '" . &sqlite_safe($infodb_value_key) . "', '" . &sqlite_safe($infodb_value) . "');\n";
|
---|
| 484 | }
|
---|
[15748] | 485 | }
|
---|
[17476] | 486 | }
|
---|
[15747] | 487 | }
|
---|
| 488 |
|
---|
| 489 |
|
---|
| 490 | sub sqlite_safe
|
---|
| 491 | {
|
---|
[17476] | 492 | my $value = shift(@_);
|
---|
[15747] | 493 |
|
---|
[17476] | 494 | # Escape any single quotes in the value
|
---|
| 495 | $value =~ s/\'/\'\'/g;
|
---|
[15722] | 496 |
|
---|
[17476] | 497 | return $value;
|
---|
[15722] | 498 | }
|
---|
| 499 |
|
---|
| 500 |
|
---|
[17476] | 501 |
|
---|
| 502 | # ----------------------------------------------------------------------------------------
|
---|
| 503 | # MSSQL IMPLEMENTATION
|
---|
| 504 | # ----------------------------------------------------------------------------------------
|
---|
| 505 |
|
---|
| 506 | my $mssql_collection_name = "";
|
---|
| 507 | my $mssql_data_table_name = "";
|
---|
| 508 | my $mssql_document_metadata_table_name = "";
|
---|
| 509 |
|
---|
| 510 |
|
---|
| 511 | sub open_infodb_write_handle_mssql
|
---|
| 512 | {
|
---|
| 513 | my $infodb_file_path = shift(@_);
|
---|
| 514 |
|
---|
| 515 | # You might have to install the DBD::ADO module from CPAN
|
---|
| 516 | #================================================================#
|
---|
| 517 | # Uncomment this if you want to use MSSQL!!!
|
---|
| 518 | # By the way, MSSQL only works on a Windows machine...
|
---|
| 519 | #================================================================#
|
---|
| 520 | #use DBI;
|
---|
| 521 | #use DBD::ADO;
|
---|
| 522 | #Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);
|
---|
| 523 |
|
---|
| 524 | # The hard coded server connection thingy which should be placed
|
---|
| 525 | # in some configuration file.
|
---|
| 526 | # If you have problem connecting to your MS SQL server:
|
---|
| 527 | # 1. Check if your MSSQL server has been started.
|
---|
| 528 | # 2. Check if the TCP/IP connection has been enabled.
|
---|
| 529 | # 3. Use telnet to the server
|
---|
| 530 | # (don't forget to specify the port, which can be found in the configuration manager)
|
---|
| 531 | # If none of the above helped, the you need to start googling then.
|
---|
| 532 | my $host = "localhost,1660"; # Need to look up your SQL server and see what port is it using.
|
---|
| 533 | my $user = "sa";
|
---|
| 534 | my $pwd = "[When installing the MSSQL, you will be asked to input a password for the sa user, use that password]";
|
---|
| 535 | my $database = "[Create a database in MSSQL and use it here]";
|
---|
| 536 |
|
---|
| 537 | # Create the unique name for the table
|
---|
| 538 | # We do not want to change the database for the current running index
|
---|
| 539 | # Therefore we use timestamp and collection short name to create an unqiue name
|
---|
| 540 | my $cur_time = time();
|
---|
| 541 | my $unique_key = $mssql_collection_name . "_" . $cur_time;
|
---|
| 542 | $mssql_data_table_name = "data_" . $unique_key;
|
---|
| 543 | $mssql_document_metadata_table_name = "document_metadata_" . $unique_key;
|
---|
| 544 | print STDERR "MSSQL: Creating unique table name. Unique ID:[" . $unique_key . "]\n";
|
---|
| 545 |
|
---|
| 546 | # Store these information into the infodbfile
|
---|
| 547 | open(FH, ">" . $infodb_file_path);
|
---|
| 548 | print FH "mss-host\t" . $host . "\n";
|
---|
| 549 | print FH "username\t" . $user . "\n";
|
---|
| 550 | print FH "password\t" . $pwd . "\n";
|
---|
| 551 | print FH "database\t" . $database . "\n";
|
---|
| 552 | print FH "tableid\t" . $unique_key . "\n";
|
---|
| 553 | close(FH);
|
---|
| 554 | print STDERR "MSSQL: Saving db info into :[" . $infodb_file_path . "]\n";
|
---|
| 555 |
|
---|
| 556 | # Make the connection
|
---|
| 557 | my $dsn = "Provider=SQLNCLI;Server=$host;Database=$database";
|
---|
| 558 | my $infodb_handle = DBI->connect("dbi:ADO:$dsn", $user, $pwd, { RaiseError => 1, AutoCommit => 1}) || return undef;
|
---|
| 559 | print STDERR "MSSQL: Connect to MS SQL database. DSN:[" . $dsn . "]\n";
|
---|
| 560 |
|
---|
| 561 | # Make sure the data table has been created.
|
---|
| 562 | my $data_table_checker_array = dbgetarray($infodb_handle, "SELECT name FROM sysobjects WHERE name = '" . $mssql_data_table_name . "' AND OBJECTPROPERTY(id,'IsUserTable') = 1");
|
---|
| 563 | if (scalar(@{$data_table_checker_array}) == 0)
|
---|
| 564 | {
|
---|
| 565 | dbquery($infodb_handle, "CREATE TABLE " . $mssql_data_table_name . " (one_key NVARCHAR(50) UNIQUE, one_value NVARCHAR(MAX))");
|
---|
| 566 | }
|
---|
| 567 | print STDERR "MSSQL: Making sure the data table(" . $mssql_data_table_name . ") exists\n";
|
---|
| 568 |
|
---|
| 569 | # Make sure the document_metadata table has been created.
|
---|
| 570 | my $document_metadata_table_checker_array = dbgetarray($infodb_handle, "SELECT name FROM sysobjects WHERE name = '" . $mssql_document_metadata_table_name . "' AND OBJECTPROPERTY(id,'IsUserTable') = 1");
|
---|
| 571 | if (scalar(@{$document_metadata_table_checker_array}) == 0)
|
---|
| 572 | {
|
---|
| 573 | dbquery($infodb_handle, "CREATE TABLE " . $mssql_document_metadata_table_name . " (id INTEGER IDENTITY(1,1) PRIMARY KEY, docOID NVARCHAR(50), element NVARCHAR(MAX), value NVARCHAR(MAX))");
|
---|
| 574 | dbquery($infodb_handle, "CREATE INDEX dmd ON " . $mssql_document_metadata_table_name . "(docOID)");
|
---|
| 575 | }
|
---|
| 576 | print STDERR "MSSQL: Making sure the document_metadata table(" . $mssql_data_table_name . ") exists.\n";
|
---|
| 577 |
|
---|
| 578 | return $infodb_handle;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 |
|
---|
| 582 | sub close_infodb_write_handle_mssql
|
---|
| 583 | {
|
---|
| 584 | my $infodb_handle = shift(@_);
|
---|
| 585 |
|
---|
| 586 | $infodb_handle->disconnect();
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 |
|
---|
| 590 | sub get_infodb_file_path_mssql
|
---|
| 591 | {
|
---|
| 592 | my $collection_name = shift(@_);
|
---|
| 593 | my $infodb_directory_path = shift(@_);
|
---|
| 594 |
|
---|
| 595 | my $infodb_file_extension = ".mssqldbinfo";
|
---|
| 596 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
| 597 |
|
---|
| 598 | # This will be used in the open_infodb_write_handle_mssql function
|
---|
| 599 | $mssql_collection_name = $collection_name;
|
---|
| 600 |
|
---|
| 601 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 |
|
---|
| 605 | sub read_infodb_file_mssql
|
---|
| 606 | {
|
---|
| 607 | my $infodb_file_path = shift(@_);
|
---|
| 608 | my $infodb_map = shift(@_);
|
---|
| 609 |
|
---|
| 610 | # !! TO IMPLEMENT
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 |
|
---|
| 614 | sub write_infodb_entry_mssql
|
---|
| 615 | {
|
---|
| 616 | my $infodb_handle = shift(@_);
|
---|
| 617 | my $infodb_key = shift(@_);
|
---|
| 618 | my $infodb_map = shift(@_);
|
---|
| 619 |
|
---|
| 620 | # Add the key -> value mapping into the "data" table
|
---|
| 621 | my $infodb_entry_value = "";
|
---|
| 622 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 623 | {
|
---|
| 624 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
| 625 | {
|
---|
| 626 | $infodb_entry_value .= "<$infodb_value_key>" . &Encode::decode_utf8($infodb_value) . "\n";
|
---|
| 627 | }
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | # Prepare the query
|
---|
| 631 | my $safe_infodb_key = &mssql_safe($infodb_key);
|
---|
| 632 | my $query = "INSERT INTO " . $mssql_data_table_name . " (one_key, one_value) VALUES (N'" . $safe_infodb_key . "', N'" . &mssql_safe($infodb_entry_value) . "')";
|
---|
| 633 | dbquery($infodb_handle, $query);
|
---|
| 634 |
|
---|
| 635 | # If this infodb entry is for a document, add all the interesting document metadata to the
|
---|
| 636 | # "document_metadata" table (for use by the dynamic classifiers)
|
---|
| 637 | if ($infodb_key !~ /\./ && $infodb_entry_value =~ /\<doctype\>doc\n/)
|
---|
| 638 | {
|
---|
| 639 | dbquery($infodb_handle, "DELETE FROM " . $mssql_document_metadata_table_name . " WHERE docOID=N'" . $safe_infodb_key . "'");
|
---|
| 640 |
|
---|
| 641 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 642 | {
|
---|
| 643 | # We're not interested in most of the automatically added document metadata
|
---|
| 644 | next if ($infodb_value_key eq "archivedir" ||
|
---|
| 645 | $infodb_value_key eq "assocfilepath" ||
|
---|
| 646 | $infodb_value_key eq "childtype" ||
|
---|
| 647 | $infodb_value_key eq "contains" ||
|
---|
| 648 | $infodb_value_key eq "docnum" ||
|
---|
| 649 | $infodb_value_key eq "doctype" ||
|
---|
| 650 | $infodb_value_key eq "Encoding" ||
|
---|
| 651 | $infodb_value_key eq "FileSize" ||
|
---|
| 652 | $infodb_value_key eq "hascover" ||
|
---|
| 653 | $infodb_value_key eq "hastxt" ||
|
---|
| 654 | $infodb_value_key eq "lastmodified" ||
|
---|
| 655 | $infodb_value_key eq "metadataset" ||
|
---|
| 656 | $infodb_value_key eq "thistype" ||
|
---|
| 657 | $infodb_value_key =~ /^metadatafreq\-/ ||
|
---|
| 658 | $infodb_value_key =~ /^metadatalist\-/);
|
---|
| 659 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
| 660 | {
|
---|
| 661 | $infodb_handle->{LongReadLen} = 65535; # Added for the encoding issue
|
---|
| 662 | my $query = "INSERT INTO " . $mssql_document_metadata_table_name . " (docOID, element, value) VALUES (N'" . $safe_infodb_key . "', N'" . &mssql_safe(&Encode::decode_utf8($infodb_value_key)) . "', N'" . &mssql_safe(&Encode::decode_utf8($infodb_value)) . "')";
|
---|
| 663 | dbquery($infodb_handle, $query);
|
---|
| 664 | }
|
---|
| 665 | }
|
---|
| 666 | }
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 |
|
---|
| 670 | sub mssql_safe
|
---|
| 671 | {
|
---|
| 672 | my $value = shift(@_);
|
---|
| 673 |
|
---|
| 674 | # Escape any single quotes in the value
|
---|
| 675 | $value =~ s/\'/\'\'/g;
|
---|
| 676 |
|
---|
| 677 | return $value;
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 |
|
---|
| 681 | sub dbquery
|
---|
| 682 | {
|
---|
| 683 | my $infodb_handle = shift(@_);
|
---|
| 684 | my $sql_query = shift(@_);
|
---|
| 685 |
|
---|
| 686 | # Execute the SQL statement
|
---|
| 687 | my $statement_handle = $infodb_handle->prepare($sql_query);
|
---|
| 688 | $statement_handle->execute();
|
---|
| 689 | if ($statement_handle->err)
|
---|
| 690 | {
|
---|
| 691 | print STDERR "Error:" . $statement_handle->errstr . "\n";
|
---|
| 692 | return undef;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | return $statement_handle;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 |
|
---|
| 699 | sub dbgetarray
|
---|
| 700 | {
|
---|
| 701 | my $infodb_handle = shift(@_);
|
---|
| 702 | my $sql_query = shift(@_);
|
---|
| 703 |
|
---|
| 704 | my $statement_handle = dbquery($infodb_handle, $sql_query);
|
---|
| 705 | my $return_array = [];
|
---|
| 706 |
|
---|
| 707 | # Iterate through the results and push them into an array
|
---|
| 708 | if (!defined($statement_handle))
|
---|
| 709 | {
|
---|
| 710 | return [];
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | while ((my $temp_hash = $statement_handle->fetchrow_hashref()))
|
---|
| 714 | {
|
---|
| 715 | push(@$return_array, $temp_hash);
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | return $return_array;
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 |
|
---|
[15699] | 722 | 1;
|
---|