Merge pull request #116 from IronTetsubo/master
[vuplus_xbmc] / xbmc / dbwrappers / mysqldataset.h
1 /*
2  *      Copyright (C) 2005-2009 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #ifndef _MYSQLDATASET_H
23 #define _MYSQLDATASET_H
24
25 #include <stdio.h>
26 #include "dataset.h"
27 #include "mysql/mysql.h"
28
29 namespace dbiplus {
30 /***************** Class MysqlDatabase definition ******************
31
32        class 'MysqlDatabase' connects with MySQL-server
33
34 ******************************************************************/
35 class MysqlDatabase: public Database {
36 protected:
37 /* connect descriptor */
38   MYSQL* conn;
39   bool _in_transaction;
40   int last_err;
41
42
43 public:
44 /* default constructor */
45   MysqlDatabase();
46 /* destructor */
47   ~MysqlDatabase();
48
49   Dataset *CreateDataset() const;
50
51 /* func. returns connection handle with MySQL-server */
52   MYSQL *getHandle() {  return conn; }
53 /* func. returns current status about MySQL-server connection */
54   virtual int status();
55   virtual int setErr(int err_code,const char * qry);
56 /* func. returns error message if error occurs */
57   virtual const char *getErrorMsg();
58
59 /* func. connects to database-server */
60   virtual int connect(bool create);
61 /* func. disconnects from database-server */
62   virtual void disconnect();
63 /* func. creates new database */
64   virtual int create();
65 /* func. deletes database */
66   virtual int drop();
67 /* check if database exists (ie has tables/views defined) */
68   virtual bool exists();
69
70   virtual long nextid(const char* seq_name);
71
72 /* virtual methods for transaction */
73
74   virtual void start_transaction();
75   virtual void commit_transaction();
76   virtual void rollback_transaction();
77
78 /* virtual methods for formatting */
79   virtual std::string vprepare(const char *format, va_list args);
80
81   bool in_transaction() {return _in_transaction;};
82   int query_with_reconnect(const char* query);
83
84 private:
85
86   typedef struct StrAccum StrAccum;
87
88   char et_getdigit(double *val, int *cnt);
89   void appendSpace(StrAccum *pAccum, int N);
90   void mysqlVXPrintf(StrAccum *pAccum, int useExtended, const char *fmt, va_list ap);
91   void mysqlStrAccumAppend(StrAccum *p, const char *z, int N);
92   char * mysqlStrAccumFinish(StrAccum *p);
93   void mysqlStrAccumReset(StrAccum *p);
94   void mysqlStrAccumInit(StrAccum *p, char *zBase, int n, int mx);
95   char *mysql_vmprintf(const char *zFormat, va_list ap);
96
97 };
98
99
100
101 /***************** Class MysqlDataset definition *******************
102
103        class 'MysqlDataset' does a query to MySQL-server
104
105 ******************************************************************/
106
107 class MysqlDataset : public Dataset {
108 protected:
109 /* query results*/
110   result_set result;
111   result_set exec_res;
112   bool autorefresh;
113   char* errmsg;
114
115   MYSQL* handle();
116
117 /* Makes direct queries to database */
118   virtual void make_query(StringList &_sql);
119 /* Makes direct inserts into database */
120   virtual void make_insert();
121 /* Edit SQL */
122   virtual void make_edit();
123 /* Delete SQL */
124   virtual void make_deletion();
125
126
127 /* This function works only with MySQL database
128   Filling the fields information from select statement */
129   virtual void fill_fields();
130 /* Changing field values during dataset navigation */
131   virtual void free_row();  // free the memory allocated for the current row
132
133 public:
134 /* constructor */
135   MysqlDataset();
136   MysqlDataset(MysqlDatabase *newDb);
137
138 /* destructor */
139   ~MysqlDataset();
140
141 /* set autorefresh boolean value (if true - refresh the data after edit() 
142 or insert() operations default = false) */
143   void set_autorefresh(bool val);
144
145 /* opens a query  & then sets a query results */
146   virtual void open();
147   virtual void open(const std::string &sql);
148 /* func. executes a query without results to return */
149   virtual int  exec ();
150   virtual int  exec (const std::string &sql);
151   virtual const void* getExecRes();
152 /* as open, but with our query exept Sql */
153   virtual bool query(const char *query);
154   virtual bool query(const std::string &query);
155 /* func. closes a query */
156   virtual void close(void);
157 /* Cancel changes, made in insert or edit states of dataset */
158   virtual void cancel();
159 /* last insert id */
160   virtual int64_t lastinsertid();
161 /* sequence numbers */
162   virtual long nextid(const char *seq_name);
163 /* sequence numbers */
164   virtual int num_rows();
165 /* interupt any pending database operation  */
166   virtual void interrupt();
167
168   virtual bool bof();
169   virtual bool eof();
170   virtual void first();
171   virtual void last();
172   virtual void prev();
173   virtual void next();
174 /* Go to record No (starting with 0) */
175   virtual bool seek(int pos=0);
176
177 };
178 } //namespace
179 #endif