3
MediaTomb - http://www.mediatomb.cc/
5
sqlite3_storage.h - this file is part of MediaTomb.
7
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
10
Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
11
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
Leonhard Wimmer <leo@mediatomb.cc>
14
MediaTomb is free software; you can redistribute it and/or modify
15
it under the terms of the GNU General Public License version 2
16
as published by the Free Software Foundation.
18
MediaTomb is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
GNU General Public License for more details.
23
You should have received a copy of the GNU General Public License
24
version 2 along with MediaTomb; if not, write to the Free Software
25
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
27
$Id: sqlite3_storage.h 1343 2007-06-04 14:06:56Z lww $
30
/// \file sqlite3_storage.h
31
///\brief Definitions of the Sqlite3Storage, Sqlite3Result, Sqlite3Row and SLTask classes.
35
#ifndef __SQLITE3_STORAGE_H__
36
#define __SQLITE3_STORAGE_H__
40
#include "storage/sql_storage.h"
46
/// \brief A virtual class that represents a task to be done by the sqlite3 thread.
47
class SLTask : public zmm::Object
50
/// \brief Instantiate a task
53
/// \brief run the sqlite3 task
54
/// \param sl The instance of Sqlite3Storage to do the queries with.
55
virtual void run(sqlite3 *db, Sqlite3Storage *sl) = 0;
57
/// \brief returns true if the task is not completed
58
/// \return true if the task is not completed yet, false if the task is finished and the results are ready.
61
/// \brief modify the creator of the task using the supplied pthread_mutex and pthread_cond, that the task is finished
64
void sendSignal(zmm::String error);
68
zmm::String getError() { return error; }
71
/// \brief true as long as the task is not finished
73
/// The value is set by the constructor to true and then to false be sendSignal()
76
zmm::Ref<Mutex> mutex;
80
#ifdef AUTO_CREATE_DATABASE
81
/// \brief A task for the sqlite3 thread to inititally create the database.
82
class SLInitTask : public SLTask
85
/// \brief Constructor for the sqlite3 init task
86
SLInitTask() : SLTask() {}
87
virtual void run(sqlite3 *db, Sqlite3Storage *sl);
92
/// \brief A task for the sqlite3 thread to do a SQL select.
93
class SLSelectTask : public SLTask
96
/// \brief Constructor for the sqlite3 select task
97
/// \param query The SQL query string
98
SLSelectTask(const char *query);
99
virtual void run(sqlite3 *db, Sqlite3Storage *sl);
100
inline zmm::Ref<SQLResult> getResult() { return RefCast(pres, SQLResult); };
102
/// \brief The SQL query string
104
/// \brief The Sqlite3Result
105
zmm::Ref<Sqlite3Result> pres;
108
/// \brief A task for the sqlite3 thread to do a SQL exec.
109
class SLExecTask : public SLTask
112
/// \brief Constructor for the sqlite3 exec task
113
/// \param query The SQL query string
114
SLExecTask(const char *query, bool getLastInsertId);
115
virtual void run(sqlite3 *db, Sqlite3Storage *sl);
116
inline int getLastInsertId() { return lastInsertId; }
118
/// \brief The SQL query string
122
bool getLastInsertIdFlag;
125
/// \brief The Storage class for using SQLite3
126
class Sqlite3Storage : private SQLStorage
130
friend zmm::Ref<Storage> Storage::createInstance();
132
virtual void shutdown();
134
virtual zmm::String quote(zmm::String str);
135
virtual inline zmm::String quote(int val) { return zmm::String::from(val); }
136
virtual inline zmm::String quote(unsigned int val) { return zmm::String::from(val); }
137
virtual inline zmm::String quote(long val) { return zmm::String::from(val); }
138
virtual inline zmm::String quote(unsigned long val) { return zmm::String::from(val); }
139
virtual inline zmm::String quote(bool val) { return zmm::String(val ? '1' : '0'); }
140
virtual inline zmm::String quote(char val) { return quote(zmm::String(val)); }
141
virtual zmm::Ref<SQLResult> select(const char *query, int length);
142
virtual int exec(const char *query, int length, bool getLastInsertId = false);
143
virtual void storeInternalSetting(zmm::String key, zmm::String value);
145
void _exec(const char *query);
147
zmm::String startupError;
149
zmm::String getError(zmm::String query, zmm::String error, sqlite3 *db);
151
static void *staticThreadProc(void *arg);
154
void addTask(zmm::Ref<SLTask> task);
156
pthread_t sqliteThread;
158
zmm::Ref<Mutex> sqliteMutex;
160
/// \brief is set to true by shutdown() if the sqlite3 thread should terminate
163
/// \brief the tasks to be done by the sqlite3 thread
164
zmm::Ref<zmm::ObjectQueue<SLTask> > taskQueue;
167
virtual void threadCleanup() {}
168
virtual bool threadCleanupRequired() { return false; }
170
inline void signal() { cond->signal(); }
172
friend class SLSelectTask;
173
friend class SLExecTask;
174
friend class SLInitTask;
177
/// \brief Represents a result of a sqlite3 select
178
class Sqlite3Result : public SQLResult
182
virtual ~Sqlite3Result();
183
virtual zmm::Ref<SQLRow> nextRow();
184
virtual unsigned long long getNumRows() { return nrow; }
194
friend class SLSelectTask;
195
friend class Sqlite3Row;
196
friend class Sqlite3Storage;
199
/// \brief Represents a row of a result of a sqlite3 select
200
class Sqlite3Row : public SQLRow
203
Sqlite3Row(char **row, zmm::Ref<SQLResult> sqlResult);
204
inline virtual char* col_c_str(int index) { return row[index]; }
206
zmm::Ref<Sqlite3Result> res;
208
friend class Sqlite3Result;
211
#endif // __SQLITE3_STORAGE_H__
213
#endif // HAVE_SQLITE3