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