1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * sqlfilter.cpp --
|
---|
4 | * Copyright (C) 2008 DL Consulting Ltd
|
---|
5 | * Copyright (C) 2010 The New Zealand Digital Library Project
|
---|
6 | *
|
---|
7 | * A component of the Greenstone digital library software
|
---|
8 | * from the New Zealand Digital Library Project at the
|
---|
9 | * University of Waikato, New Zealand.
|
---|
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 |
|
---|
27 | #include "fileutil.h"
|
---|
28 | #include "sqlfilter.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | sqlfilterclass::sqlfilterclass ()
|
---|
32 | : filterclass()
|
---|
33 | {}
|
---|
34 |
|
---|
35 |
|
---|
36 | sqlfilterclass::~sqlfilterclass ()
|
---|
37 | {}
|
---|
38 |
|
---|
39 |
|
---|
40 | void sqlfilterclass::configure (const text_t &key, const text_tarray &cfgline)
|
---|
41 | {
|
---|
42 | filterclass::configure (key, cfgline);
|
---|
43 |
|
---|
44 | if (key == "indexstem")
|
---|
45 | {
|
---|
46 | indexstem = cfgline[0];
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | bool sqlfilterclass::init (ostream &logout)
|
---|
51 | {
|
---|
52 | outconvertclass text_t2ascii;
|
---|
53 |
|
---|
54 | if (!filterclass::init(logout)) return false;
|
---|
55 |
|
---|
56 | if (sql_db_ptr == NULL)
|
---|
57 | {
|
---|
58 | // most likely a configuration problem
|
---|
59 | logout << text_t2ascii << "configuration error: sqlfilter contains a null sqldbclass\n\n";
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (indexstem.empty())
|
---|
64 | {
|
---|
65 | indexstem = collection;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // get the filename for the database and make sure it exists
|
---|
69 | sql_db_filename = resolve_db_filename(indexstem,sql_db_ptr->getfileextension());
|
---|
70 | if (!file_exists(sql_db_filename))
|
---|
71 | {
|
---|
72 | logout << text_t2ascii << "warning: database \"" << sql_db_filename << "\" does not exist\n\n";
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | bool sqlfilterclass::connect_to_sqldb (FilterResponse_t &response,
|
---|
81 | comerror_t &err, ostream &logout)
|
---|
82 | {
|
---|
83 | outconvertclass text_t2ascii;
|
---|
84 |
|
---|
85 | response.clear();
|
---|
86 | err = noError;
|
---|
87 |
|
---|
88 | if (sql_db_ptr == NULL) {
|
---|
89 | // most likely a configuration problem
|
---|
90 | logout << text_t2ascii << "configuration error: sqlbrowsefilter contains a null sqldbclass\n\n";
|
---|
91 | err = configurationError;
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // open the database
|
---|
96 | sql_db_ptr->setlogout (&logout);
|
---|
97 | if (!sql_db_ptr->opendatabase (sql_db_filename, DB_READER, 100, false)) {
|
---|
98 | // most likely a system problem (we have already checked that the database exists)
|
---|
99 | logout << text_t2ascii << "system problem: open on database \"" << sql_db_filename << "\" failed\n\n";
|
---|
100 | err = systemProblem;
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | void sqlfilterclass::disconnect_from_sqldb ()
|
---|
109 | {
|
---|
110 | sql_db_ptr->closedatabase(); // Important that local library doesn't leave any files open
|
---|
111 | }
|
---|