~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/storage/sqlite3/sqlite3_storage.h

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    sqlite3_storage.h - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
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.
 
17
    
 
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.
 
22
    
 
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.
 
26
    
 
27
    $Id: sqlite3_storage.h 1343 2007-06-04 14:06:56Z lww $
 
28
*/
 
29
 
 
30
/// \file sqlite3_storage.h
 
31
///\brief Definitions of the Sqlite3Storage, Sqlite3Result, Sqlite3Row and SLTask classes.
 
32
 
 
33
#ifdef HAVE_SQLITE3
 
34
 
 
35
#ifndef __SQLITE3_STORAGE_H__
 
36
#define __SQLITE3_STORAGE_H__
 
37
 
 
38
#include <sqlite3.h>
 
39
 
 
40
#include "storage/sql_storage.h"
 
41
#include "sync.h"
 
42
 
 
43
class Sqlite3Storage;
 
44
class Sqlite3Result;
 
45
 
 
46
/// \brief A virtual class that represents a task to be done by the sqlite3 thread.
 
47
class SLTask : public zmm::Object
 
48
{
 
49
public:
 
50
    /// \brief Instantiate a task
 
51
    SLTask();
 
52
    
 
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;
 
56
    
 
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.
 
59
    bool is_running();
 
60
    
 
61
    /// \brief modify the creator of the task using the supplied pthread_mutex and pthread_cond, that the task is finished
 
62
    void sendSignal();
 
63
    
 
64
    void sendSignal(zmm::String error);
 
65
    
 
66
    void waitForTask();
 
67
    
 
68
    zmm::String getError() { return error; }
 
69
    
 
70
protected:
 
71
    /// \brief true as long as the task is not finished
 
72
    ///
 
73
    /// The value is set by the constructor to true and then to false be sendSignal()
 
74
    bool running;
 
75
    zmm::Ref<Cond> cond;
 
76
    zmm::Ref<Mutex> mutex;
 
77
    zmm::String error;
 
78
};
 
79
 
 
80
#ifdef AUTO_CREATE_DATABASE
 
81
/// \brief A task for the sqlite3 thread to inititally create the database.
 
82
class SLInitTask : public SLTask
 
83
{
 
84
public:
 
85
    /// \brief Constructor for the sqlite3 init task
 
86
    SLInitTask() : SLTask() {}
 
87
    virtual void run(sqlite3 *db, Sqlite3Storage *sl);
 
88
};
 
89
#endif
 
90
 
 
91
 
 
92
/// \brief A task for the sqlite3 thread to do a SQL select.
 
93
class SLSelectTask : public SLTask
 
94
{
 
95
public:
 
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); };
 
101
protected:
 
102
    /// \brief The SQL query string
 
103
    const char *query;
 
104
    /// \brief The Sqlite3Result
 
105
    zmm::Ref<Sqlite3Result> pres;
 
106
};
 
107
 
 
108
/// \brief A task for the sqlite3 thread to do a SQL exec.
 
109
class SLExecTask : public SLTask
 
110
{
 
111
public:
 
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; }
 
117
protected:
 
118
    /// \brief The SQL query string
 
119
    const char *query;
 
120
    
 
121
    int lastInsertId;
 
122
    bool getLastInsertIdFlag;
 
123
};
 
124
 
 
125
/// \brief The Storage class for using SQLite3
 
126
class Sqlite3Storage : private SQLStorage
 
127
{
 
128
private:
 
129
    Sqlite3Storage();
 
130
    friend zmm::Ref<Storage> Storage::createInstance();
 
131
    virtual void init();
 
132
    virtual void shutdown();
 
133
    
 
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);
 
144
    
 
145
    void _exec(const char *query);
 
146
    
 
147
    zmm::String startupError;
 
148
    
 
149
    zmm::String getError(zmm::String query, zmm::String error, sqlite3 *db);
 
150
    
 
151
    static void *staticThreadProc(void *arg);
 
152
    void threadProc();
 
153
    
 
154
    void addTask(zmm::Ref<SLTask> task);
 
155
    
 
156
    pthread_t sqliteThread;
 
157
    zmm::Ref<Cond> cond;
 
158
    zmm::Ref<Mutex> sqliteMutex;
 
159
    
 
160
    /// \brief is set to true by shutdown() if the sqlite3 thread should terminate
 
161
    bool shutdownFlag;
 
162
    
 
163
    /// \brief the tasks to be done by the sqlite3 thread
 
164
    zmm::Ref<zmm::ObjectQueue<SLTask> > taskQueue;
 
165
    bool taskQueueOpen;
 
166
    
 
167
    virtual void threadCleanup() {}
 
168
    virtual bool threadCleanupRequired() { return false; }
 
169
    
 
170
    inline void signal() { cond->signal(); }
 
171
    
 
172
    friend class SLSelectTask;
 
173
    friend class SLExecTask;
 
174
    friend class SLInitTask;
 
175
};
 
176
 
 
177
/// \brief Represents a result of a sqlite3 select
 
178
class Sqlite3Result : public SQLResult
 
179
{
 
180
private:
 
181
    Sqlite3Result();
 
182
    virtual ~Sqlite3Result();
 
183
    virtual zmm::Ref<SQLRow> nextRow();
 
184
    virtual unsigned long long getNumRows() { return nrow; }
 
185
    
 
186
    char **table;
 
187
    char **row;
 
188
    
 
189
    int cur_row;
 
190
    
 
191
    int nrow;
 
192
    int ncolumn;
 
193
    
 
194
    friend class SLSelectTask;
 
195
    friend class Sqlite3Row;
 
196
    friend class Sqlite3Storage;
 
197
};
 
198
 
 
199
/// \brief Represents a row of a result of a sqlite3 select
 
200
class Sqlite3Row : public SQLRow
 
201
{
 
202
private:
 
203
    Sqlite3Row(char **row, zmm::Ref<SQLResult> sqlResult);
 
204
    inline virtual char* col_c_str(int index) { return row[index]; }
 
205
    char **row;
 
206
    zmm::Ref<Sqlite3Result> res;
 
207
    
 
208
    friend class Sqlite3Result;
 
209
};
 
210
 
 
211
#endif // __SQLITE3_STORAGE_H__
 
212
 
 
213
#endif // HAVE_SQLITE3