1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * sqlqueryfilter.cpp --
|
---|
4 | * Copyright (C) 2010 New Zealand Digital Library Project
|
---|
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 | #include "sqlqueryfilter.h"
|
---|
27 |
|
---|
28 | sqlqueryfilterclass::sqlqueryfilterclass ()
|
---|
29 | : sqlfilterclass()
|
---|
30 | {
|
---|
31 | FilterOption_t filtopt;
|
---|
32 |
|
---|
33 | // -- onePerQuery StartResults integer
|
---|
34 | filtopt.clear();
|
---|
35 | filtopt.name = "StartResults";
|
---|
36 | filtopt.type = FilterOption_t::integert;
|
---|
37 | filtopt.repeatable = FilterOption_t::onePerQuery;
|
---|
38 | filtopt.defaultValue = "1";
|
---|
39 | filtopt.validValues.push_back("1");
|
---|
40 | filtopt.validValues.push_back("1000");
|
---|
41 | filterOptions["StartResults"] = filtopt;
|
---|
42 |
|
---|
43 | // -- onePerQuery EndResults integer
|
---|
44 | filtopt.clear();
|
---|
45 | filtopt.name = "EndResults";
|
---|
46 | filtopt.type = FilterOption_t::integert;
|
---|
47 | filtopt.repeatable = FilterOption_t::onePerQuery;
|
---|
48 | filtopt.defaultValue = "10";
|
---|
49 | filtopt.validValues.push_back("-1");
|
---|
50 | filtopt.validValues.push_back("1000");
|
---|
51 | filterOptions["EndResults"] = filtopt;
|
---|
52 |
|
---|
53 | // -- onePerQuery Maxdocs integer
|
---|
54 | filtopt.clear();
|
---|
55 | filtopt.name = "Maxdocs";
|
---|
56 | filtopt.type = FilterOption_t::integert;
|
---|
57 | filtopt.repeatable = FilterOption_t::onePerQuery;
|
---|
58 | filtopt.defaultValue = "200";
|
---|
59 | filtopt.validValues.push_back("-1");
|
---|
60 | filtopt.validValues.push_back("1000");
|
---|
61 | filterOptions["Maxdocs"] = filtopt;
|
---|
62 |
|
---|
63 | // -- IndexFieldDomain, enumerated, used to list available fields (from)
|
---|
64 | filtopt.clear();
|
---|
65 | filtopt.name = "IndexFieldDomain";
|
---|
66 | filtopt.type = FilterOption_t::enumeratedt;
|
---|
67 | filtopt.repeatable = FilterOption_t::onePerTerm;
|
---|
68 | filtopt.defaultValue = "";
|
---|
69 | filterOptions["IndexFieldDomain"] = filtopt;
|
---|
70 |
|
---|
71 | // -- IndexFieldRange, enumerated, used to list available fields (to)
|
---|
72 | // Equivalent to IndexField in full text search filter
|
---|
73 | filtopt.clear();
|
---|
74 | filtopt.name = "IndexFieldRange";
|
---|
75 | filtopt.type = FilterOption_t::enumeratedt;
|
---|
76 | filtopt.repeatable = FilterOption_t::onePerTerm;
|
---|
77 | filtopt.defaultValue = "";
|
---|
78 | filterOptions["IndexFieldRange"] = filtopt;
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | sqlqueryfilterclass::~sqlqueryfilterclass ()
|
---|
84 | {}
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | void sqlqueryfilterclass::configure (const text_t &key,
|
---|
90 | const text_tarray &cfgline)
|
---|
91 | {
|
---|
92 | sqlfilterclass::configure(key, cfgline);
|
---|
93 |
|
---|
94 | if (key == "indexfieldmap") {
|
---|
95 | indexfieldmap.importmap (cfgline);
|
---|
96 |
|
---|
97 | // update the list of indexes in the filter information
|
---|
98 | text_tarray from_options;
|
---|
99 | indexfieldmap.getfromarray (from_options);
|
---|
100 | filterOptions["IndexFieldDomain"].validValues = from_options;
|
---|
101 |
|
---|
102 | text_tarray to_options;
|
---|
103 | indexfieldmap.gettoarray (to_options);
|
---|
104 | filterOptions["IndexFieldRange"].validValues = to_options;
|
---|
105 |
|
---|
106 | } else if (key == "defaultindex") {
|
---|
107 |
|
---|
108 | filterOptions["IndexFieldDomain"].defaultValue = cfgline[0];
|
---|
109 |
|
---|
110 | indexfieldmap.from2to (cfgline[0],
|
---|
111 | filterOptions["IndexFieldRange"].defaultValue);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | bool sqlqueryfilterclass::init (ostream &logout)
|
---|
116 | {
|
---|
117 | if (!sqlfilterclass::init(logout)) {
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (filterOptions["IndexFieldRange"].defaultValue.empty()) {
|
---|
122 | // use first index in map as default if no default is set explicitly
|
---|
123 |
|
---|
124 | text_tarray fromarray;
|
---|
125 | indexfieldmap.getfromarray(fromarray);
|
---|
126 |
|
---|
127 | text_tarray toarray;
|
---|
128 | indexfieldmap.gettoarray(fromarray);
|
---|
129 |
|
---|
130 | if (fromarray.size()) {
|
---|
131 | filterOptions["IndexFieldDomain"].defaultValue = fromarray[0];
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (toarray.size()) {
|
---|
135 | filterOptions["IndexFieldRange"].defaultValue = toarray[0];
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | return true;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void sqlqueryfilterclass::filter (const FilterRequest_t &request,
|
---|
143 | FilterResponse_t &response,
|
---|
144 | comerror_t &err, ostream &logout)
|
---|
145 | {
|
---|
146 | outconvertclass text_t2ascii;
|
---|
147 |
|
---|
148 | if (!connect_to_sqldb(response,err,logout)) {
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | text_t sql_where = "";
|
---|
153 | text_t sort_by_metadata_element_name = "";
|
---|
154 | OptionValue_tarray::const_iterator options_iterator = request.filterOptions.begin();
|
---|
155 | while (options_iterator != request.filterOptions.end())
|
---|
156 | {
|
---|
157 | if ((*options_iterator).name == "SQLWhere") {
|
---|
158 | sql_where = (*options_iterator).value;
|
---|
159 | }
|
---|
160 | if ((*options_iterator).name == "SortField") {
|
---|
161 | sort_by_metadata_element_name = (*options_iterator).value;
|
---|
162 | }
|
---|
163 | options_iterator++;
|
---|
164 | }
|
---|
165 |
|
---|
166 | text_tarray document_OIDs = sql_db_ptr->get_documents_where (sql_where, sort_by_metadata_element_name);
|
---|
167 | int numDocs = document_OIDs.size();
|
---|
168 |
|
---|
169 | // Fill in response.docInfo with the document OIDs
|
---|
170 | text_tarray::iterator document_OID_iterator = document_OIDs.begin();
|
---|
171 | while (document_OID_iterator != document_OIDs.end())
|
---|
172 | {
|
---|
173 | ResultDocInfo_t document_result_doc;
|
---|
174 | document_result_doc.OID = *document_OID_iterator;
|
---|
175 | response.docInfo.push_back (document_result_doc);
|
---|
176 |
|
---|
177 | document_OID_iterator++;
|
---|
178 | }
|
---|
179 |
|
---|
180 | disconnect_from_sqldb();
|
---|
181 |
|
---|
182 | response.numDocs = numDocs;
|
---|
183 | response.isApprox = Exact;
|
---|
184 |
|
---|
185 | }
|
---|