source: main/trunk/greenstone2/perllib/dbutil/sqlite.pm@ 21411

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

Separation of different database back-ends into individual files

File size: 5.9 KB
Line 
1###########################################################################
2#
3# dbutil::sqlite -- utility functions for writing to sqlite 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::sqlite;
28
29use strict;
30
31
32# -----------------------------------------------------------------------------
33# SQLITE IMPLEMENTATION
34# -----------------------------------------------------------------------------
35
36sub open_infodb_write_handle
37{
38 my $infodb_file_path = shift(@_);
39
40 my $sqlite3_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}, "sqlite3" . &util::get_os_exe());
41 my $infodb_handle = undef;
42 if (!-e "$sqlite3_exe" || !open($infodb_handle, "| \"$sqlite3_exe\" \"$infodb_file_path\""))
43 {
44 return undef;
45 }
46
47 print $infodb_handle "CREATE TABLE IF NOT EXISTS data (key TEXT PRIMARY KEY, value TEXT);\n";
48 print $infodb_handle "CREATE TABLE IF NOT EXISTS document_metadata (id INTEGER PRIMARY KEY, docOID TEXT, element TEXT, value TEXT);\n";
49
50 # This is crucial for efficiency when importing large amounts of data
51 print $infodb_handle "CREATE INDEX IF NOT EXISTS dmd ON document_metadata(docOID);\n";
52
53 # This is very important for efficiency, otherwise each command will be actioned one at a time
54 print $infodb_handle "BEGIN TRANSACTION;\n";
55
56 return $infodb_handle;
57}
58
59
60sub close_infodb_write_handle
61{
62 my $infodb_handle = shift(@_);
63
64 # Close the transaction we began after opening the file
65 print $infodb_handle "END TRANSACTION;\n";
66
67 # This is crucial for efficient queries on the database!
68 print $infodb_handle "CREATE INDEX IF NOT EXISTS dme ON document_metadata(element);\n";
69
70 close($infodb_handle);
71}
72
73
74sub get_infodb_file_path
75{
76 my $collection_name = shift(@_);
77 my $infodb_directory_path = shift(@_);
78
79 my $infodb_file_extension = ".db";
80 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
81 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
82}
83
84
85sub read_infodb_file
86{
87 my $infodb_file_path = shift(@_);
88 my $infodb_map = shift(@_);
89
90 # !! TO IMPLEMENT
91}
92
93
94sub write_infodb_entry
95{
96 my $infodb_handle = shift(@_);
97 my $infodb_key = shift(@_);
98 my $infodb_map = shift(@_);
99
100 # Add the key -> value mapping into the "data" table
101 my $infodb_entry_value = "";
102 foreach my $infodb_value_key (keys(%$infodb_map))
103 {
104 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
105 {
106 $infodb_entry_value .= "<$infodb_value_key>" . $infodb_value . "\n";
107 }
108 }
109
110 my $safe_infodb_key = &sqlite_safe($infodb_key);
111 print $infodb_handle "INSERT OR REPLACE INTO data (key, value) VALUES ('" . $safe_infodb_key . "', '" . &sqlite_safe($infodb_entry_value) . "');\n";
112
113 # If this infodb entry is for a document, add all the interesting document metadata to the
114 # "document_metadata" table (for use by the dynamic classifiers)
115 if ($infodb_key !~ /\./ && $infodb_entry_value =~ /\<doctype\>doc\n/)
116 {
117 print $infodb_handle "DELETE FROM document_metadata WHERE docOID='" . $safe_infodb_key . "';\n";
118
119 foreach my $infodb_value_key (keys(%$infodb_map))
120 {
121 # We're not interested in most of the automatically added document metadata
122 next if ($infodb_value_key eq "archivedir" ||
123 $infodb_value_key eq "assocfilepath" ||
124 $infodb_value_key eq "childtype" ||
125 $infodb_value_key eq "contains" ||
126 $infodb_value_key eq "docnum" ||
127 $infodb_value_key eq "doctype" ||
128 $infodb_value_key eq "Encoding" ||
129 $infodb_value_key eq "FileSize" ||
130 $infodb_value_key eq "hascover" ||
131 $infodb_value_key eq "hastxt" ||
132 $infodb_value_key eq "lastmodified" ||
133 $infodb_value_key eq "metadataset" ||
134 $infodb_value_key eq "thistype" ||
135 $infodb_value_key =~ /^metadatafreq\-/ ||
136 $infodb_value_key =~ /^metadatalist\-/);
137
138 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
139 {
140 print $infodb_handle "INSERT INTO document_metadata (docOID, element, value) VALUES ('" . $safe_infodb_key . "', '" . &sqlite_safe($infodb_value_key) . "', '" . &sqlite_safe($infodb_value) . "');\n";
141 }
142 }
143 }
144}
145
146
147
148sub delete_infodb_entry
149{
150 my $infodb_handle = shift(@_);
151 my $infodb_key = shift(@_);
152
153 # Delete the key from the "data" table
154
155 my $safe_infodb_key = &sqlite_safe($infodb_key);
156
157 print $infodb_handle "DELETE FROM data WHERE key='" . $safe_infodb_key . "';\n";
158
159 # If this infodb entry is for a document, delete the
160 # "document_metadata" table entry also (for use by the dynamic classifiers)
161 if ($infodb_key !~ /\./)
162 {
163 # Possible for there not to be a docOID matching this infodb_key
164 # (entries are only made when <doctype> == doc
165 # Attempt to delete it, and don't complain if one isn't found
166
167 print $infodb_handle "DELETE FROM document_metadata WHERE docOID='" . $safe_infodb_key . "';\n";
168
169 }
170}
171
172
173sub sqlite_safe
174{
175 my $value = shift(@_);
176
177 # Escape any single quotes in the value
178 $value =~ s/\'/\'\'/g;
179
180 return $value;
181}
182
183
1841;
Note: See TracBrowser for help on using the repository browser.