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

Last change on this file since 22067 was 22067, checked in by ak19, 14 years ago
  1. More changes to makefiles: rm JDBMWrapper.jar and jdbm.jar on clean. 2. DB files (argdb, users, key, history) now not only for gdbm but to work with other db types like jdbm, sqlite and mssql.
File size: 2.8 KB
RevLine 
[22057]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
[22067]28sqldbclass::sqldbclass(const text_t& gsdlhome)
29 : dbclass(gsdlhome)
[22057]30{
31}
32
33sqldbclass::~sqldbclass()
34{
35}
36
37
38
39// returns array of document OIDs
40text_tarray sqldbclass::get_documents_where (const text_t& sql_initial_cmd,
41 const text_t& sort_by)
42{
43 text_tarray document_OIDs;
44
45 // Get the entries in the "document_metadata" table where the 'sql_cmd'
46 // parameter is used to control what is located
47 text_t sql_cmd = sql_initial_cmd;
48
49 // If we're sorting the documents by a certain metadata element, extend the SQL command to do this
50 if (sort_by != "")
51 {
52 text_tlist sbterms;
53 splitword(sort_by.begin(), sort_by.end(), "/", sbterms);
54
55 text_t tags_in = "";
56
57 while (!sbterms.empty()) {
58 text_t tag = sbterms.front();
59 sbterms.pop_front();
60
61 if (!tag.empty()) {
62
63 if (tag.size()>3 && (substr(tag.begin(), tag.begin()+3) == "ex.")) {
64 tag = substr (tag.begin()+3, tag.end());
65 }
66
67 if (!tags_in.empty()) {
68 tags_in += ",";
69 }
70
71 tags_in += "'" + sql_safe(tag) + "'";
72 }
73 }
74
75 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";
76 }
77
78 cerr << "**** sql cmd = " << sql_cmd << endl;
79
80 // Perform the SQL request
81 vector<text_tmap> sql_results;
82 if (!sqlgetarray(sql_cmd, sql_results) || sql_results.size() == 0)
83 {
84 return document_OIDs;
85 }
86
87 // Iterate through the documents and add them to the array to be returned
88 vector<text_tmap>::iterator sql_results_iterator = sql_results.begin();
89 while (sql_results_iterator != sql_results.end())
90 {
91 text_tmap sql_result = (*sql_results_iterator);
92 document_OIDs.push_back(sql_result["docOID"]);
93 sql_results_iterator++;
94 }
95
96 return document_OIDs;
97}
98
99
Note: See TracBrowser for help on using the repository browser.