source: main/trunk/greenstone2/common-src/src/lib/sqldbclass.cpp@ 22057

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

base class for sql operations

File size: 2.8 KB
Line 
1/**********************************************************************
2 *
3 * sqldbclass.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 "sqldbclass.h"
27
28sqldbclass::sqldbclass()
29{
30}
31
32sqldbclass::~sqldbclass()
33{
34}
35
36
37
38// returns array of document OIDs
39text_tarray sqldbclass::get_documents_where (const text_t& sql_initial_cmd,
40 const text_t& sort_by)
41{
42 text_tarray document_OIDs;
43
44 // Get the entries in the "document_metadata" table where the 'sql_cmd'
45 // parameter is used to control what is located
46 text_t sql_cmd = sql_initial_cmd;
47
48 // If we're sorting the documents by a certain metadata element, extend the SQL command to do this
49 if (sort_by != "")
50 {
51 text_tlist sbterms;
52 splitword(sort_by.begin(), sort_by.end(), "/", sbterms);
53
54 text_t tags_in = "";
55
56 while (!sbterms.empty()) {
57 text_t tag = sbterms.front();
58 sbterms.pop_front();
59
60 if (!tag.empty()) {
61
62 if (tag.size()>3 && (substr(tag.begin(), tag.begin()+3) == "ex.")) {
63 tag = substr (tag.begin()+3, tag.end());
64 }
65
66 if (!tags_in.empty()) {
67 tags_in += ",";
68 }
69
70 tags_in += "'" + sql_safe(tag) + "'";
71 }
72 }
73
74 sql_cmd = "SELECT DISTINCT docOID FROM (" + sql_cmd + ") LEFT JOIN (SELECT docOID,value FROM document_metadata WHERE element IN (" + tags_in + ")) USING (docOID) ORDER by value";
75 }
76
77 cerr << "**** sql cmd = " << sql_cmd << endl;
78
79 // Perform the SQL request
80 vector<text_tmap> sql_results;
81 if (!sqlgetarray(sql_cmd, sql_results) || sql_results.size() == 0)
82 {
83 return document_OIDs;
84 }
85
86 // Iterate through the documents and add them to the array to be returned
87 vector<text_tmap>::iterator sql_results_iterator = sql_results.begin();
88 while (sql_results_iterator != sql_results.end())
89 {
90 text_tmap sql_result = (*sql_results_iterator);
91 document_OIDs.push_back(sql_result["docOID"]);
92 sql_results_iterator++;
93 }
94
95 return document_OIDs;
96}
97
98
Note: See TracBrowser for help on using the repository browser.