[30318] | 1 | ###############################################################################
|
---|
| 2 | #
|
---|
| 3 | # DBDrivers/MSSQL.pm -- utility functions for writing to mssql databases
|
---|
| 4 | #
|
---|
| 5 | # A component of the Greenstone digital library software from the New Zealand
|
---|
| 6 | # Digital Library Project at the University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999-2015 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | # the terms of the GNU General Public License as published by the Free Software
|
---|
| 12 | # Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | # version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
| 17 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
| 18 | # more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License along with
|
---|
| 21 | # this program; if not, write to the Free Software Foundation, Inc., 675 Mass
|
---|
| 22 | # Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###############################################################################
|
---|
| 25 |
|
---|
| 26 | package DBDrivers::MSSQL;
|
---|
| 27 |
|
---|
| 28 | # Pragma
|
---|
| 29 | use strict;
|
---|
| 30 |
|
---|
| 31 | # Libraries
|
---|
| 32 | use Encode;
|
---|
| 33 | use DBDrivers::BaseDBDriver;
|
---|
| 34 |
|
---|
| 35 | sub BEGIN
|
---|
| 36 | {
|
---|
| 37 | @DBDrivers::MSSQL::ISA = ( 'DBDrivers::BaseDBDriver' );
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | sub new
|
---|
| 41 | {
|
---|
| 42 | my $class = shift(@_);
|
---|
| 43 | if ($^O !~ /cygwin|MSWin32/) {
|
---|
| 44 | print STDERR "Warning! Non-Windows operating system detected... good luck getting this to work.\n";
|
---|
| 45 | }
|
---|
| 46 | return bless ($self, $class);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | # -----------------------------------------------------------------------------
|
---|
| 50 | # MSSQL IMPLEMENTATION
|
---|
| 51 | # -----------------------------------------------------------------------------
|
---|
| 52 |
|
---|
| 53 | # The hard coded server connection thingy which should be place in some
|
---|
| 54 | # configuration file.
|
---|
| 55 | # If you have problem connecting to your MS SQL server:
|
---|
| 56 | # 1. Check if your MSSQL server has been started.
|
---|
| 57 | # 2. Check if the TCP/IP connection has been enabled.
|
---|
| 58 | # 3. Use telnet to the server
|
---|
| 59 | # (don't forget to specify the port, which can be found in the configuration manager)
|
---|
| 60 | # If none of the above helped, the you need to start googling then.
|
---|
| 61 | my $host = "localhost,1660"; # Need to look up your SQL server and see what port is it using.
|
---|
| 62 | my $user = "sa"; # Generally "sa" for default admin, but YMMV
|
---|
| 63 | my $pwd = "[When installing the MSSQL, you will be asked to input a password for the sa user, use that password]";
|
---|
| 64 | my $database = "[Create a database in MSSQL and use it here]";
|
---|
| 65 |
|
---|
| 66 | my $mssql_collection_name = "";
|
---|
| 67 | my $mssql_data_table_name = "";
|
---|
| 68 | my $mssql_document_metadata_table_name = "";
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | sub open_infodb_write_handle
|
---|
| 72 | {
|
---|
| 73 | my $infodb_file_path = shift(@_);
|
---|
| 74 |
|
---|
| 75 | # You might have to install the DBD::ADO module from CPAN
|
---|
| 76 | use DBI;
|
---|
| 77 | use DBD::ADO;
|
---|
| 78 | Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);
|
---|
| 79 |
|
---|
| 80 | # Create the unique name for the table
|
---|
| 81 | # We do not want to change the database for the current running index
|
---|
| 82 | # Therefore we use timestamp and collection short name to create an unqiue name
|
---|
| 83 | my $cur_time = time();
|
---|
| 84 | my $unique_key = $mssql_collection_name . "_" . $cur_time;
|
---|
| 85 | $mssql_data_table_name = "data_" . $unique_key;
|
---|
| 86 | $mssql_document_metadata_table_name = "document_metadata_" . $unique_key;
|
---|
| 87 | print STDERR "MSSQL: Creating unique table name. Unique ID:[" . $unique_key . "]\n";
|
---|
| 88 |
|
---|
| 89 | # Store these information into the infodbfile
|
---|
| 90 | open(FH, ">" . $infodb_file_path);
|
---|
| 91 | print FH "mss-host\t" . $host . "\n";
|
---|
| 92 | print FH "username\t" . $user . "\n";
|
---|
| 93 | print FH "password\t" . $pwd . "\n";
|
---|
| 94 | print FH "database\t" . $database . "\n";
|
---|
| 95 | print FH "tableid\t" . $unique_key . "\n";
|
---|
| 96 | close(FH);
|
---|
| 97 | print STDERR "MSSQL: Saving db info into :[" . $infodb_file_path . "]\n";
|
---|
| 98 |
|
---|
| 99 | # Make the connection
|
---|
| 100 | my $dsn = "Provider=SQLNCLI;Server=$host;Database=$database";
|
---|
| 101 | my $infodb_handle = DBI->connect("dbi:ADO:$dsn", $user, $pwd, { RaiseError => 1, AutoCommit => 1}) || return undef;
|
---|
| 102 | print STDERR "MSSQL: Connect to MS SQL database. DSN:[" . $dsn . "]\n";
|
---|
| 103 |
|
---|
| 104 | # Make sure the data table has been created.
|
---|
| 105 | my $data_table_checker_array = dbgetarray($infodb_handle, "SELECT name FROM sysobjects WHERE name = '" . $mssql_data_table_name . "' AND OBJECTPROPERTY(id,'IsUserTable') = 1");
|
---|
| 106 | if (scalar(@{$data_table_checker_array}) == 0)
|
---|
| 107 | {
|
---|
| 108 | dbquery($infodb_handle, "CREATE TABLE " . $mssql_data_table_name . " (one_key NVARCHAR(50) UNIQUE, one_value NVARCHAR(MAX))");
|
---|
| 109 | }
|
---|
| 110 | print STDERR "MSSQL: Making sure the data table(" . $mssql_data_table_name . ") exists\n";
|
---|
| 111 |
|
---|
| 112 | # Make sure the document_metadata table has been created.
|
---|
| 113 | 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");
|
---|
| 114 | if (scalar(@{$document_metadata_table_checker_array}) == 0)
|
---|
| 115 | {
|
---|
| 116 | 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))");
|
---|
| 117 | dbquery($infodb_handle, "CREATE INDEX dmd ON " . $mssql_document_metadata_table_name . "(docOID)");
|
---|
| 118 | }
|
---|
| 119 | print STDERR "MSSQL: Making sure the document_metadata table(" . $mssql_data_table_name . ") exists.\n";
|
---|
| 120 |
|
---|
| 121 | return $infodb_handle;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | sub close_infodb_write_handle
|
---|
| 126 | {
|
---|
| 127 | my $infodb_handle = shift(@_);
|
---|
| 128 |
|
---|
| 129 | $infodb_handle->disconnect();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | sub get_infodb_file_path
|
---|
| 134 | {
|
---|
| 135 | my $collection_name = shift(@_);
|
---|
| 136 | my $infodb_directory_path = shift(@_);
|
---|
| 137 |
|
---|
| 138 | my $infodb_file_extension = ".mssqldbinfo";
|
---|
| 139 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
| 140 |
|
---|
| 141 | # This will be used in the open_infodb_write_handle function
|
---|
| 142 | $mssql_collection_name = $collection_name;
|
---|
| 143 |
|
---|
| 144 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | sub read_infodb_file
|
---|
| 149 | {
|
---|
| 150 | my $infodb_file_path = shift(@_);
|
---|
| 151 | my $infodb_map = shift(@_);
|
---|
| 152 |
|
---|
| 153 | print STDERR "******* mssql::read_infodb_file() TO BE IMPLEMENTED!\n";
|
---|
| 154 | print STDERR "******* See sqlite.pm for comparable implementation that has been coded up!\n";
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | sub read_infodb_keys
|
---|
| 158 | {
|
---|
| 159 | my $infodb_file_path = shift(@_);
|
---|
| 160 | my $infodb_map = shift(@_);
|
---|
| 161 |
|
---|
| 162 | print STDERR "******* mssql::read_infodb_keys() TO BE IMPLEMENTED!\n";
|
---|
| 163 | print STDERR "******* See sqlite.pm for comparable implementation that has been coded up!\n";
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | sub write_infodb_entry
|
---|
| 168 | {
|
---|
| 169 | my $infodb_handle = shift(@_);
|
---|
| 170 | my $infodb_key = shift(@_);
|
---|
| 171 | my $infodb_map = shift(@_);
|
---|
| 172 |
|
---|
| 173 | # Add the key -> value mapping into the "data" table
|
---|
| 174 | my $infodb_entry_value = "";
|
---|
| 175 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 176 | {
|
---|
| 177 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
| 178 | {
|
---|
| 179 | $infodb_entry_value .= "<$infodb_value_key>" . &Encode::decode_utf8($infodb_value) . "\n";
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | # Prepare the query
|
---|
| 184 | my $safe_infodb_key = &mssql_safe($infodb_key);
|
---|
| 185 | my $query = "INSERT INTO " . $mssql_data_table_name . " (one_key, one_value) VALUES (N'" . $safe_infodb_key . "', N'" . &mssql_safe($infodb_entry_value) . "')";
|
---|
| 186 | dbquery($infodb_handle, $query);
|
---|
| 187 |
|
---|
| 188 | # If this infodb entry is for a document, add all the interesting document metadata to the
|
---|
| 189 | # "document_metadata" table (for use by the dynamic classifiers)
|
---|
| 190 | if ($infodb_key !~ /\./ && $infodb_entry_value =~ /\<doctype\>doc\n/)
|
---|
| 191 | {
|
---|
| 192 | dbquery($infodb_handle, "DELETE FROM " . $mssql_document_metadata_table_name . " WHERE docOID=N'" . $safe_infodb_key . "'");
|
---|
| 193 |
|
---|
| 194 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 195 | {
|
---|
| 196 | # We're not interested in most of the automatically added document metadata
|
---|
| 197 | next if ($infodb_value_key eq "archivedir" ||
|
---|
| 198 | $infodb_value_key eq "assocfilepath" ||
|
---|
| 199 | $infodb_value_key eq "childtype" ||
|
---|
| 200 | $infodb_value_key eq "contains" ||
|
---|
| 201 | $infodb_value_key eq "docnum" ||
|
---|
| 202 | $infodb_value_key eq "doctype" ||
|
---|
| 203 | $infodb_value_key eq "Encoding" ||
|
---|
| 204 | $infodb_value_key eq "FileSize" ||
|
---|
| 205 | $infodb_value_key eq "hascover" ||
|
---|
| 206 | $infodb_value_key eq "hastxt" ||
|
---|
| 207 | $infodb_value_key eq "lastmodified" ||
|
---|
| 208 | $infodb_value_key eq "metadataset" ||
|
---|
| 209 | $infodb_value_key eq "thistype" ||
|
---|
| 210 | $infodb_value_key =~ /^metadatafreq\-/ ||
|
---|
| 211 | $infodb_value_key =~ /^metadatalist\-/);
|
---|
| 212 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
| 213 | {
|
---|
| 214 | $infodb_handle->{LongReadLen} = 65535; # Added for the encoding issue
|
---|
| 215 | 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)) . "')";
|
---|
| 216 | dbquery($infodb_handle, $query);
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 | sub write_infodb_rawentry
|
---|
| 224 | {
|
---|
| 225 | my $infodb_handle = shift(@_);
|
---|
| 226 | my $infodb_key = shift(@_);
|
---|
| 227 | my $infodb_val = shift(@_);
|
---|
| 228 |
|
---|
| 229 | # Prepare the query
|
---|
| 230 | my $safe_infodb_key = &mssql_safe($infodb_key);
|
---|
| 231 | my $query = "INSERT INTO " . $mssql_data_table_name . " (one_key, one_value) VALUES (N'" . $safe_infodb_key . "', N'" . &mssql_safe($infodb_val) . "')";
|
---|
| 232 | dbquery($infodb_handle, $query);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 | sub set_infodb_entry
|
---|
| 237 | {
|
---|
| 238 | my $infodb_file_path = shift(@_);
|
---|
| 239 | my $infodb_key = shift(@_);
|
---|
| 240 | my $infodb_map = shift(@_);
|
---|
| 241 |
|
---|
| 242 | print STDERR "***** mssql::set_infodb_entry() not implemented yet!\n";
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | sub delete_infodb_entry
|
---|
| 246 | {
|
---|
| 247 | my $infodb_handle = shift(@_);
|
---|
| 248 | my $infodb_key = shift(@_);
|
---|
| 249 |
|
---|
| 250 | # Delete the key from the "data" table
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | # Prepare the query
|
---|
| 254 | my $safe_infodb_key = &mssql_safe($infodb_key);
|
---|
| 255 | my $query = "DELETE FROM " . $mssql_data_table_name . " WHERE one_key=N'" . $safe_infodb_key . "'";
|
---|
| 256 | dbquery($infodb_handle, $query);
|
---|
| 257 |
|
---|
| 258 | # If this infodb entry is for a document, add all the interesting document metadata to the
|
---|
| 259 | # "document_metadata" table (for use by the dynamic classifiers)
|
---|
| 260 | if ($infodb_key !~ /\./)
|
---|
| 261 | {
|
---|
| 262 | # Possible for there not to be a docOID matching this infodb_key
|
---|
| 263 | # (entries are only made when <doctype> == doc
|
---|
| 264 | # Attempt to delete it, and don't complain if one isn't found
|
---|
| 265 |
|
---|
| 266 | dbquery($infodb_handle, "DELETE FROM " . $mssql_document_metadata_table_name . " WHERE docOID=N'" . $safe_infodb_key . "'");
|
---|
| 267 |
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 |
|
---|
| 273 | sub mssql_safe
|
---|
| 274 | {
|
---|
| 275 | my $value = shift(@_);
|
---|
| 276 |
|
---|
| 277 | # Escape any single quotes in the value
|
---|
| 278 | $value =~ s/\'/\'\'/g;
|
---|
| 279 |
|
---|
| 280 | return $value;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 |
|
---|
| 284 | sub dbquery
|
---|
| 285 | {
|
---|
| 286 | my $infodb_handle = shift(@_);
|
---|
| 287 | my $sql_query = shift(@_);
|
---|
| 288 |
|
---|
| 289 | # Execute the SQL statement
|
---|
| 290 | my $statement_handle = $infodb_handle->prepare($sql_query);
|
---|
| 291 | $statement_handle->execute();
|
---|
| 292 | if ($statement_handle->err)
|
---|
| 293 | {
|
---|
| 294 | print STDERR "Error:" . $statement_handle->errstr . "\n";
|
---|
| 295 | return undef;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | return $statement_handle;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | sub dbgetarray
|
---|
| 303 | {
|
---|
| 304 | my $infodb_handle = shift(@_);
|
---|
| 305 | my $sql_query = shift(@_);
|
---|
| 306 |
|
---|
| 307 | my $statement_handle = dbquery($infodb_handle, $sql_query);
|
---|
| 308 | my $return_array = [];
|
---|
| 309 |
|
---|
| 310 | # Iterate through the results and push them into an array
|
---|
| 311 | if (!defined($statement_handle))
|
---|
| 312 | {
|
---|
| 313 | return [];
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | while ((my $temp_hash = $statement_handle->fetchrow_hashref()))
|
---|
| 317 | {
|
---|
| 318 | push(@$return_array, $temp_hash);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | return $return_array;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | 1;
|
---|