Merge pull request #5039 from CEikermann/patch-1
[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 /* sets a new host name */
65   virtual void setHostName(const char *newHost);
66 /* sets a database name */
67   virtual void setDatabase(const char *newDb);
68
69 /* func. connects to database-server */
70
71   virtual int connect(bool create);
72 /* func. disconnects from database-server */
73   virtual void disconnect();
74 /* func. creates new database */
75   virtual int create();
76 /* func. deletes database */
77   virtual int drop();
78 /* check if database exists (ie has tables/views defined) */
79   virtual bool exists();
80
81 /* \brief copy database */
82   virtual int copy(const char *backup_name);
83
84 /* \brief drop all extra analytics from database */
85   virtual int drop_analytics(void);
86
87   virtual long nextid(const char* seq_name);
88
89 /* virtual methods for transaction */
90
91   virtual void start_transaction();
92   virtual void commit_transaction();
93   virtual void rollback_transaction();
94
95 /* virtual methods for formatting */
96   virtual std::string vprepare(const char *format, va_list args);
97
98   bool in_transaction() {return _in_transaction;};      
99
100 };
101
102
103
104 /***************** Class SqliteDataset definition *******************
105
106        class 'SqliteDataset' does a query to SQLite-server
107
108 ******************************************************************/
109
110 class SqliteDataset : public Dataset {
111 protected:
112   sqlite3* handle();
113
114 /* Makes direct queries to database */
115   virtual void make_query(StringList &_sql);
116 /* Makes direct inserts into database */
117   virtual void make_insert();
118 /* Edit SQL */
119   virtual void make_edit();
120 /* Delete SQL */
121   virtual void make_deletion();
122
123   //static int sqlite_callback(void* res_ptr,int ncol, char** reslt, char** cols);
124
125 /* This function works only with MySQL database
126   Filling the fields information from select statement */
127   virtual void fill_fields();
128 /* Changing field values during dataset navigation */
129   virtual void free_row();  // free the memory allocated for the current row
130
131 public:
132 /* constructor */
133   SqliteDataset();
134   SqliteDataset(SqliteDatabase *newDb);
135
136 /* destructor */
137   ~SqliteDataset();
138
139 /* set autorefresh boolean value (if true - refresh the data after edit() 
140 or insert() operations default = false) */
141   void set_autorefresh(bool val);
142
143 /* opens a query  & then sets a query results */
144   virtual void open();
145   virtual void open(const std::string &sql);
146 /* func. executes a query without results to return */
147   virtual int  exec ();
148   virtual int  exec (const std::string &sql);
149   virtual const void* getExecRes();
150 /* as open, but with our query exept Sql */
151   virtual bool query(const char *query);
152   virtual bool query(const std::string &query);
153 /* func. closes a query */
154   virtual void close(void);
155 /* Cancel changes, made in insert or edit states of dataset */
156   virtual void cancel();
157 /* last inserted id */
158   virtual int64_t lastinsertid();
159 /* sequence numbers */
160   virtual long nextid(const char *seq_name);
161 /* sequence numbers */
162   virtual int num_rows();
163 /* interupt any pending database operation  */
164   virtual void interrupt();
165
166   virtual bool bof();
167   virtual bool eof();
168   virtual void first();
169   virtual void last();
170   virtual void prev();
171   virtual void next();
172 /* Go to record No (starting with 0) */
173   virtual bool seek(int pos=0);
174
175   virtual bool dropIndex(const char *table, const char *index);
176 };
177 } //namespace
178 #endif