Merge pull request #116 from IronTetsubo/master
[vuplus_xbmc] / xbmc / dbwrappers / sqlitedataset.h
1 /**********************************************************************
2  * Copyright (c) 2004, Leo Seib, Hannover
3  *
4  * Project:SQLiteDataset C++ Dynamic Library
5  * Module: SQLiteDataset class header file
6  * Author: Leo Seib      E-Mail: leoseib@web.de
7  * Begin: 5/04/2002
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  *
27  **********************************************************************/
28
29 #ifndef _SQLITEDATASET_H
30 #define _SQLITEDATASET_H
31
32 #include <stdio.h>
33 #include "dataset.h"
34 #include <sqlite3.h>
35
36 namespace dbiplus {
37 /***************** Class SqliteDatabase definition ******************
38
39        class 'SqliteDatabase' connects with Sqlite-server
40
41 ******************************************************************/
42 class SqliteDatabase: public Database {
43 protected:
44 /* connect descriptor */
45   sqlite3 *conn;
46   bool _in_transaction;
47   int last_err;
48
49 public:
50 /* default constructor */
51   SqliteDatabase();
52 /* destructor */
53   ~SqliteDatabase();
54
55   Dataset *CreateDataset() const; 
56
57 /* func. returns connection handle with SQLite-server */
58   sqlite3 *getHandle() {  return conn; }
59 /* func. returns current status about SQLite-server connection */
60   virtual int status();
61   virtual int setErr(int err_code,const char * qry);
62 /* func. returns error message if error occurs */
63   virtual const char *getErrorMsg();
64   
65 /* func. connects to database-server */
66   virtual int connect(bool create);
67 /* func. disconnects from database-server */
68   virtual void disconnect();
69 /* func. creates new database */
70   virtual int create();
71 /* func. deletes database */
72   virtual int drop();
73 /* check if database exists (ie has tables/views defined) */
74   virtual bool exists();
75
76   virtual long nextid(const char* seq_name);
77
78 /* virtual methods for transaction */
79
80   virtual void start_transaction();
81   virtual void commit_transaction();
82   virtual void rollback_transaction();
83
84 /* virtual methods for formatting */
85   virtual std::string vprepare(const char *format, va_list args);
86
87   bool in_transaction() {return _in_transaction;};      
88
89 };
90
91
92
93 /***************** Class SqliteDataset definition *******************
94
95        class 'SqliteDataset' does a query to SQLite-server
96
97 ******************************************************************/
98
99 class SqliteDataset : public Dataset {
100 protected:
101 /* query results*/
102   result_set result;
103   result_set exec_res;
104   bool autorefresh;
105   char* errmsg;
106   
107   sqlite3* handle();
108
109 /* Makes direct queries to database */
110   virtual void make_query(StringList &_sql);
111 /* Makes direct inserts into database */
112   virtual void make_insert();
113 /* Edit SQL */
114   virtual void make_edit();
115 /* Delete SQL */
116   virtual void make_deletion();
117
118   //static int sqlite_callback(void* res_ptr,int ncol, char** reslt, char** cols);
119
120 /* This function works only with MySQL database
121   Filling the fields information from select statement */
122   virtual void fill_fields();
123 /* Changing field values during dataset navigation */
124   virtual void free_row();  // free the memory allocated for the current row
125
126 public:
127 /* constructor */
128   SqliteDataset();
129   SqliteDataset(SqliteDatabase *newDb);
130
131 /* destructor */
132   ~SqliteDataset();
133
134 /* set autorefresh boolean value (if true - refresh the data after edit() 
135 or insert() operations default = false) */
136   void set_autorefresh(bool val);
137
138 /* opens a query  & then sets a query results */
139   virtual void open();
140   virtual void open(const std::string &sql);
141 /* func. executes a query without results to return */
142   virtual int  exec ();
143   virtual int  exec (const std::string &sql);
144   virtual const void* getExecRes();
145 /* as open, but with our query exept Sql */
146   virtual bool query(const char *query);
147   virtual bool query(const std::string &query);
148 /* func. closes a query */
149   virtual void close(void);
150 /* Cancel changes, made in insert or edit states of dataset */
151   virtual void cancel();
152 /* last inserted id */
153   virtual int64_t lastinsertid();
154 /* sequence numbers */
155   virtual long nextid(const char *seq_name);
156 /* sequence numbers */
157   virtual int num_rows();
158 /* interupt any pending database operation  */
159   virtual void interrupt();
160
161   virtual bool bof();
162   virtual bool eof();
163   virtual void first();
164   virtual void last();
165   virtual void prev();
166   virtual void next();
167 /* Go to record No (starting with 0) */
168   virtual bool seek(int pos=0);
169
170
171 };
172 } //namespace
173 #endif