1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <stdio.h>
12 #include "dataset.h"
13 #if defined(HAS_MYSQL) || defined(HAS_MARIADB)
14 #include <mysql/mysql.h>
15 #endif
16 
17 namespace dbiplus {
18 /***************** Class MysqlDatabase definition ******************
19 
20        class 'MysqlDatabase' connects with MySQL-server
21 
22 ******************************************************************/
23 class MysqlDatabase: public Database {
24 protected:
25 /* connect descriptor */
26   MYSQL* conn;
27   bool _in_transaction;
28   int last_err;
29 
30 
31 public:
32 /* default constructor */
33   MysqlDatabase();
34 /* destructor */
35   ~MysqlDatabase() override;
36 
37   Dataset *CreateDataset() const override;
38 
39 /* func. returns connection handle with MySQL-server */
getHandle()40   MYSQL *getHandle() {  return conn; }
41 /* func. returns current status about MySQL-server connection */
42   int status() override;
43   int setErr(int err_code,const char * qry) override;
44 /* func. returns error message if error occurs */
45   const char *getErrorMsg() override;
46 
47 /* func. connects to database-server */
48   int connect(bool create) override;
49 /* func. disconnects from database-server */
50   void disconnect() override;
51 /* func. creates new database */
52   int create() override;
53 /* func. deletes database */
54   int drop() override;
55 /* check if database exists (ie has tables/views defined) */
56   bool exists() override;
57 
58 /* \brief copy database */
59   int copy(const char *backup_name) override;
60 
61 /* \brief drop all extra analytics from database */
62   int drop_analytics(void) override;
63 
64   long nextid(const char* seq_name) override;
65 
66 /* virtual methods for transaction */
67 
68   void start_transaction() override;
69   void commit_transaction() override;
70   void rollback_transaction() override;
71 
72 /* virtual methods for formatting */
73   std::string vprepare(const char *format, va_list args) override;
74 
in_transaction()75   bool in_transaction() override {return _in_transaction;};
76   int query_with_reconnect(const char* query);
77   void configure_connection();
78 
79 private:
80 
81   typedef struct StrAccum StrAccum;
82 
83   char et_getdigit(double *val, int *cnt);
84   void appendSpace(StrAccum *pAccum, int N);
85   void mysqlVXPrintf(StrAccum *pAccum, int useExtended, const char *fmt, va_list ap);
86   bool mysqlStrAccumAppend(StrAccum *p, const char *z, int N);
87   char * mysqlStrAccumFinish(StrAccum *p);
88   void mysqlStrAccumReset(StrAccum *p);
89   void mysqlStrAccumInit(StrAccum *p, char *zBase, int n, int mx);
90   std::string mysql_vmprintf(const char *zFormat, va_list ap);
91 };
92 
93 
94 
95 /***************** Class MysqlDataset definition *******************
96 
97        class 'MysqlDataset' does a query to MySQL-server
98 
99 ******************************************************************/
100 
101 class MysqlDataset : public Dataset {
102 protected:
103   MYSQL* handle();
104 
105 /* Makes direct queries to database */
106   virtual void make_query(StringList &_sql);
107 /* Makes direct inserts into database */
108   void make_insert() override;
109 /* Edit SQL */
110   void make_edit() override;
111 /* Delete SQL */
112   void make_deletion() override;
113 
114 
115 /* This function works only with MySQL database
116   Filling the fields information from select statement */
117   void fill_fields() override;
118 /* Changing field values during dataset navigation */
119   virtual void free_row();  // free the memory allocated for the current row
120 
121 public:
122 /* constructor */
123   MysqlDataset();
124   explicit MysqlDataset(MysqlDatabase *newDb);
125 
126 /* destructor */
127   ~MysqlDataset() override;
128 
129 /* set autorefresh boolean value (if true - refresh the data after edit()
130 or insert() operations default = false) */
131   void set_autorefresh(bool val);
132 
133 /* opens a query  & then sets a query results */
134   void open() override;
135   void open(const std::string &sql) override;
136 /* func. executes a query without results to return */
137   int  exec () override;
138   int  exec (const std::string &sql) override;
139   const void* getExecRes() override;
140 /* as open, but with our query exec Sql */
141   bool query(const std::string &query) override;
142 /* func. closes a query */
143   void close(void) override;
144 /* Cancel changes, made in insert or edit states of dataset */
145   void cancel() override;
146 /* last insert id */
147   int64_t lastinsertid() override;
148 /* sequence numbers */
149   long nextid(const char *seq_name) override;
150 /* sequence numbers */
151   int num_rows() override;
152 /* interrupt any pending database operation  */
153   void interrupt() override;
154 
155   bool bof() override;
156   bool eof() override;
157   void first() override;
158   void last() override;
159   void prev() override;
160   void next() override;
161 /* Go to record No (starting with 0) */
162   bool seek(int pos=0) override;
163 
164   bool dropIndex(const char *table, const char *index) override;
165 };
166 } //namespace
167 
168