source: gsdl/trunk/lib/sqliteclass.cpp@ 15601

Last change on this file since 15601 was 15601, checked in by mdewsnip, 16 years ago

(Adding new DB support) Started adding sqliteclass internals, beginning with an sqlexec() function.

File size: 4.0 KB
Line 
1/**********************************************************************
2 *
3 * sqliteclass.cpp --
4 * Copyright (C) 2008 DL Consulting Ltd
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 "sqliteclass.h"
27
28
29#define SQLITE_MAX_RETRIES 8
30
31
32sqliteclass::~sqliteclass()
33{
34 closedatabase();
35}
36
37
38// returns true if opened
39bool sqliteclass::opendatabase(const text_t &filename, int mode, int num_retrys,
40#ifdef __WIN32__
41 bool need_filelock
42#else
43 bool
44#endif
45 )
46{
47 // Check if we've already got the database open
48 if (sqlitefile != NULL)
49 {
50 if (openfile == filename) return true;
51 else closedatabase();
52 }
53
54 char *filename_cstr = filename.getcstr();
55 sqlitefile = NULL;
56 sqlite3_open(filename_cstr, &sqlitefile);
57 delete[] filename_cstr;
58
59 if (sqlitefile == NULL && logout != NULL)
60 {
61 outconvertclass text_t2ascii;
62 (*logout) << text_t2ascii << "database open failed on: " << filename << "\n";
63 }
64
65 return (sqlitefile != NULL);
66}
67
68
69void sqliteclass::closedatabase()
70{
71 if (sqlitefile == NULL) return;
72
73 sqlite3_close(sqlitefile);
74 sqlitefile = NULL;
75 openfile.clear();
76}
77
78
79// returns true on success
80bool sqliteclass::getinfo(const text_t& key, infodbclass &info)
81{
82 // !! TO IMPLEMENT
83 return false;
84}
85
86
87// returns true if exists
88bool sqliteclass::exists(const text_t& key)
89{
90 // !! TO IMPLEMENT
91 return false;
92}
93
94
95// returns true on success
96bool sqliteclass::setinfo(const text_t &key, const infodbclass &info)
97{
98 if (sqlitefile == NULL) return false;
99
100 // !! TO IMPLEMENT
101 return false;
102}
103
104
105void sqliteclass::deletekey(const text_t &key)
106{
107 if (sqlitefile == NULL) return;
108
109 // !! TO IMPLEMENT
110}
111
112
113text_t sqliteclass::getfirstkey()
114{
115 if (sqlitefile == NULL) return g_EmptyText;
116
117 // !! TO IMPLEMENT
118 return g_EmptyText;
119}
120
121
122text_t sqliteclass::getnextkey(const text_t &key)
123{
124 if (sqlitefile == NULL || key.empty()) return g_EmptyText;
125
126 // !! TO IMPLEMENT
127 return g_EmptyText;
128}
129
130
131// sqlexec simply executes the given sql statement - it doesn't obtain a
132// result set - returns true if the sql statement was executed successfully
133// logerrors may be set to false to prevent this method from logging an
134// error if it fails - this is useful in some circumstances when we execute
135// an sql command specifically to see if it fails (e.g. when testing if a
136// table exists in the db)
137bool sqliteclass::sqlexec(const text_t &sql_cmd, bool logerrors)
138{
139 char *sql_cmd_cstr = sql_cmd.getcstr();
140
141 int rv = 0;
142 int tries = 0;
143 while ((rv = sqlite3_exec(sqlitefile, sql_cmd_cstr, NULL, NULL, NULL)) == SQLITE_BUSY)
144 {
145 sleep(1000);
146 tries++;
147 if (tries > SQLITE_MAX_RETRIES)
148 {
149 outconvertclass text_t2ascii;
150 (*logout) << text_t2ascii << "max_retries exceeded for sql query: " << sql_cmd << "\n";
151 break;
152 }
153 }
154
155 delete[] sql_cmd_cstr;
156
157 if (rv == SQLITE_OK) return true;
158
159 if (logerrors)
160 {
161 outconvertclass text_t2ascii;
162 (*logout) << text_t2ascii << "Error executing sql statement: " << sql_cmd << "\n";
163 }
164
165 return false;
166}
167
168
169// sleep for the given number of milliseconds
170void sleep(int m)
171{
172#ifdef __WIN32__
173 Sleep(m);
174#else
175 usleep(m);
176#endif
177}
Note: See TracBrowser for help on using the repository browser.