]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ssqlite3.cc
Merge pull request #7852 from Habbie/sqlite-wal
[thirdparty/pdns.git] / pdns / ssqlite3.cc
1 /* SQLite backend for PowerDNS
2 * Copyright (C) 2003, Michel Stol <michel@powerdns.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
7 *
8 * Additionally, the license of this program contains a special
9 * exception which allows to distribute the program in binary form when
10 * it is linked against OpenSSL.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <string>
26 #include <sstream>
27 #include "ssqlite3.hh"
28 #include <iostream>
29 #include <fstream>
30 #include "pdns/logger.hh"
31 #include "misc.hh"
32 #include "utility.hh"
33 #include <unistd.h>
34
35 /*
36 ** Set all the parameters in the compiled SQL statement to NULL.
37 *
38 * copied from sqlite 3.3.6 // cmouse
39 */
40 int pdns_sqlite3_clear_bindings(sqlite3_stmt *pStmt){
41 int i;
42 int rc = SQLITE_OK;
43 for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
44 rc = sqlite3_bind_null(pStmt, i);
45 }
46 return rc;
47 }
48
49 class SSQLite3Statement: public SSqlStatement
50 {
51 public:
52 SSQLite3Statement(SSQLite3 *db, bool dolog, const string& query) :
53 d_query(query),
54 d_db(db),
55 d_dolog(dolog)
56 {
57 }
58
59 int name2idx(const string& name) {
60 string zName = string(":")+name;
61 prepareStatement();
62 return sqlite3_bind_parameter_index(d_stmt, zName.c_str());
63 // XXX: support @ and $?
64 }
65
66 SSqlStatement* bind(const string& name, bool value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int(d_stmt, idx, value ? 1 : 0); }; return this; }
67 SSqlStatement* bind(const string& name, int value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int(d_stmt, idx, value); }; return this; }
68 SSqlStatement* bind(const string& name, uint32_t value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
69 SSqlStatement* bind(const string& name, long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
70 SSqlStatement* bind(const string& name, unsigned long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
71 SSqlStatement* bind(const string& name, long long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; };
72 SSqlStatement* bind(const string& name, unsigned long long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
73 SSqlStatement* bind(const string& name, const std::string& value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_text(d_stmt, idx, value.c_str(), value.size(), SQLITE_TRANSIENT); }; return this; }
74 SSqlStatement* bindNull(const string& name) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_null(d_stmt, idx); }; return this; }
75
76 SSqlStatement* execute() {
77 prepareStatement();
78 if (d_dolog) {
79 g_log<<Logger::Warning<< "Query "<<((long)(void*)this)<<": " << d_query << endl;
80 d_dtime.set();
81 }
82 int attempts = d_db->inTransaction(); // try only once
83 while(attempts < 2 && (d_rc = sqlite3_step(d_stmt)) == SQLITE_BUSY) attempts++;
84
85 if (d_rc != SQLITE_ROW && d_rc != SQLITE_DONE) {
86 // failed.
87 releaseStatement();
88 if (d_rc == SQLITE_CANTOPEN)
89 throw SSqlException(string("CANTOPEN error in sqlite3, often caused by unwritable sqlite3 db *directory*: ")+string(sqlite3_errmsg(d_db->db())));
90 throw SSqlException(string("Error while retrieving SQLite query results: ")+string(sqlite3_errmsg(d_db->db())));
91 }
92 if(d_dolog)
93 g_log<<Logger::Warning<< "Query "<<((long)(void*)this)<<": "<<d_dtime.udiffNoReset()<<" usec to execute"<<endl;
94 return this;
95 }
96 bool hasNextRow() {
97 if(d_dolog && d_rc != SQLITE_ROW) {
98 g_log<<Logger::Warning<< "Query "<<((long)(void*)this)<<": "<<d_dtime.udiffNoReset()<<" total usec to last row"<<endl;
99 }
100 return d_rc == SQLITE_ROW;
101 }
102
103 SSqlStatement* nextRow(row_t& row) {
104 row.clear();
105 int numCols = sqlite3_column_count(d_stmt);
106 row.reserve(numCols); // preallocate memory
107 // Another row received, process it.
108 for ( int i=0; i<numCols; i++)
109 {
110 if (sqlite3_column_type(d_stmt,i) == SQLITE_NULL) {
111 row.push_back("");
112 } else {
113 const char *pData = (const char*) sqlite3_column_text(d_stmt, i);
114 row.push_back(string(pData, sqlite3_column_bytes(d_stmt, i)));
115 }
116 }
117 d_rc = sqlite3_step(d_stmt);
118 return this;
119 }
120
121 SSqlStatement* getResult(result_t& result) {
122 result.clear();
123 while(hasNextRow()) {
124 row_t row;
125 nextRow(row);
126 result.push_back(row);
127 }
128 return this;
129 }
130
131 SSqlStatement* reset() {
132 sqlite3_reset(d_stmt);
133 #if SQLITE_VERSION_NUMBER >= 3003009
134 sqlite3_clear_bindings(d_stmt);
135 #else
136 pdns_sqlite3_clear_bindings(d_stmt);
137 #endif
138 return this;
139 }
140
141 ~SSQLite3Statement() {
142 // deallocate if necessary
143 releaseStatement();
144 }
145
146 const string& getQuery() { return d_query; };
147 private:
148 string d_query;
149 DTime d_dtime;
150 sqlite3_stmt* d_stmt{nullptr};
151 SSQLite3* d_db{nullptr};
152 int d_rc{0};
153 bool d_dolog;
154 bool d_prepared{false};
155
156 void prepareStatement() {
157 const char *pTail;
158
159 if (d_prepared) return;
160 #if SQLITE_VERSION_NUMBER >= 3003009
161 if (sqlite3_prepare_v2(d_db->db(), d_query.c_str(), -1, &d_stmt, &pTail ) != SQLITE_OK)
162 #else
163 if (sqlite3_prepare(d_db->db(), d_query.c_str(), -1, &d_stmt, &pTail ) != SQLITE_OK)
164 #endif
165 {
166 releaseStatement();
167 throw SSqlException(string("Unable to compile SQLite statement : '")+d_query+"': "+sqlite3_errmsg(d_db->db()));
168 }
169 if (pTail && strlen(pTail)>0)
170 g_log<<Logger::Warning<<"Sqlite3 command partially processed. Unprocessed part: "<<pTail<<endl;
171 d_prepared = true;
172 }
173
174 void releaseStatement() {
175 if (d_stmt)
176 sqlite3_finalize(d_stmt);
177 d_stmt = nullptr;
178 d_prepared = false;
179 }
180 };
181
182 // Constructor.
183 SSQLite3::SSQLite3( const std::string & database, const std::string & journalmode, bool creat )
184 {
185 if (access( database.c_str(), F_OK ) == -1){
186 if (!creat)
187 throw sPerrorException( "SQLite database '"+database+"' does not exist yet" );
188 } else {
189 if (creat)
190 throw sPerrorException( "SQLite database '"+database+"' already exists" );
191 }
192
193 if ( sqlite3_open( database.c_str(), &m_pDB)!=SQLITE_OK )
194 throw sPerrorException( "Could not connect to the SQLite database '" + database + "'" );
195 m_dolog = 0;
196 m_in_transaction = false;
197 sqlite3_busy_handler(m_pDB, busyHandler, 0);
198
199 if(journalmode.length())
200 execute("PRAGMA journal_mode="+journalmode);
201 }
202
203 void SSQLite3::setLog(bool state)
204 {
205 m_dolog=state;
206 }
207
208 // Destructor.
209 SSQLite3::~SSQLite3()
210 {
211 int ret;
212 for(int n = 0; n < 2 ; ++n) {
213 if((ret =sqlite3_close( m_pDB )) != SQLITE_OK) {
214 if(n || ret != SQLITE_BUSY) { // if we have SQLITE_BUSY, and a working m_Pstmt, try finalize
215 cerr<<"Unable to close down sqlite connection: "<<ret<<endl;
216 abort();
217 }
218 }
219 else
220 break;
221 }
222 }
223
224 std::unique_ptr<SSqlStatement> SSQLite3::prepare(const string& query, int nparams __attribute__((unused))) {
225 return std::unique_ptr<SSqlStatement>(new SSQLite3Statement(this, m_dolog, query));
226 }
227
228 void SSQLite3::execute(const string& query) {
229 char *errmsg;
230 int rc;
231 if (sqlite3_exec(m_pDB, query.c_str(), NULL, NULL, &errmsg) == SQLITE_BUSY) {
232 if (m_in_transaction) {
233 throw("Failed to execute query: " + string(errmsg));
234 } else {
235 if ((rc = sqlite3_exec(m_pDB, query.c_str(), NULL, NULL, &errmsg) != SQLITE_OK) && rc != SQLITE_DONE && rc != SQLITE_ROW)
236 throw("Failed to execute query: " + string(errmsg));
237 }
238 }
239 }
240
241 int SSQLite3::busyHandler(void*, int)
242 {
243 Utility::usleep(1000);
244 return 1;
245 }
246
247 void SSQLite3::startTransaction() {
248 execute("begin");
249 m_in_transaction = true;
250 }
251
252 void SSQLite3::rollback() {
253 execute("rollback");
254 m_in_transaction = false;
255 }
256
257 void SSQLite3::commit() {
258 execute("commit");
259 m_in_transaction = false;
260 }
261
262 // Constructs a SSqlException object.
263 SSqlException SSQLite3::sPerrorException( const std::string & reason )
264 {
265 return SSqlException( reason );
266 }