source: main/trunk/greenstone2/perllib/dbutil/mssql.pm@ 21856

Last change on this file since 21856 was 21856, checked in by davidb, 14 years ago

read_infodb_entry() implemented for sqlite. new write_infodb_rawentry() added for main dbutil.dm class

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