[go: up one dir, main page]

Menu

[r303]: / cmysql.h  Maximize  Restore  History

Download this file

207 lines (186 with data), 6.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* Marcion
Copyright (C) 2009 - 2011 Milan Konvicka
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program */
#ifndef CMYSQL_H
#define CMYSQL_H
#include <stdio.h>
#include <QString>
#include <mysql.h>
#include <QTextStream>
#include <QFile>
#include "ctranslit.h"
#include "outstr.h"
#define MQUERY(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
if(m_msg()) m_msg()->MsgMsg(QObject::tr("executing query '")+MQTEXT+"' ..."); \
if(!MQ.exec()) \
{ \
m_msg()->MsgErr(MQ.lastError()); \
return; \
}
#define MQUERY_ENABL_MSG(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
messages->MsgMsg(QObject::tr("executing query '")+MQTEXT+"' ..."); \
if(!MQ.exec()) \
{ \
messages->MsgErr(MQ.lastError()); \
messages->setMsgDisabled(false); \
return; \
}
/*#define MQUERY_ENABL_MSG_RESTC(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
messages->MsgMsg("executing query \""+MQTEXT+"\" ..."); \
if(!MQ.exec()) \
{ \
messages->MsgErr(MQ.lastError()); \
messages->setMsgDisabled(false); \
RESTCURSOR \
return; \
}*/
/*#define MQUERY_RESTC(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
messages->MsgMsg("executing query \""+MQTEXT+"\" ..."); \
if(!MQ.exec()) \
{ \
QApplication::restoreOverrideCursor(); \
messages->MsgErr(MQ.lastError()); \
return; \
}*/
#define MQUERY_RF(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
messages->MsgMsg(QObject::tr("executing query '")+MQTEXT+"' ..."); \
if(!MQ.exec()) \
{ \
messages->MsgErr(MQ.lastError()); \
return false; \
}
/*#define MQUERY_RF_RESTC(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
messages->MsgMsg("executing query \""+MQTEXT+"\" ..."); \
if(!MQ.exec()) \
{ \
RESTCURSOR \
messages->MsgErr(MQ.lastError()); \
return false; \
}*/
#define MQUERY_GETFIRST(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
if(m_msg()) m_msg()->MsgMsg(QObject::tr("executing query '")+MQTEXT+"' ..."); \
if(!MQ.exec()) \
{ \
if(m_msg()) m_msg()->MsgErr(MQ.lastError()); \
return; \
} \
if(!MQ.first()) \
{ \
if(m_msg()) m_msg()->MsgErr(QObject::tr("no row returned")); \
return; \
}
#define MQUERY_GETFIRST_RF(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
messages->MsgMsg(QObject::tr("executing query '")+MQTEXT+"' ..."); \
if(!MQ.exec()) \
{ \
messages->MsgErr(MQ.lastError()); \
return false; \
} \
if(!MQ.first()) \
{ \
messages->MsgErr(QObject::tr("no row returned")); \
return false; \
}
/*#define MQUERY_GETFIRST_RF_RESTC(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
messages->MsgMsg("executing query \""+MQTEXT+"\" ..."); \
if(!MQ.exec()) \
{ \
RESTCURSOR \
messages->MsgErr(MQ.lastError()); \
return false; \
} \
if(!MQ.first()) \
{ \
RESTCURSOR \
messages->MsgErr("no row returned"); \
return false; \
}*/
#define MQUERY_RF_NOMSG(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
if(!MQ.exec()) \
{ \
messages->MsgErr(MQTEXT+"\n"+MQ.lastError()); \
return false; \
}
/*#define MQUERY_RF_NOMSG_RESTC(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
if(!MQ.exec()) \
{ \
RESTCURSOR \
messages->MsgErr(MQTEXT+"\n"+MQ.lastError()); \
return false; \
}*/
#define MQUERY_NOMSG(MQ,MQTEXT) \
CMySql MQ(MQTEXT); \
if(!MQ.exec()) \
{ \
messages->MsgErr(MQTEXT+"\n"+MQ.lastError()); \
}
class CMySql
{
public:
enum Dbase {Local,Remote};
CMySql(Dbase dbase=Local);
CMySql(QString const & command,Dbase dbase=Local);
~CMySql();
static bool connect(QString const & host,
QString const & user,
QString const & password,
QString const & db,
unsigned int port),
connect_local(
QString const & host,
QString const & user,
QString const & password,
QString const & db,
unsigned int port),
connect_local();
static void close();
bool exec(QString const & command);
bool exec();
bool first(),next();
bool isNULL(int index) const;
QString value(int index) const;
char * data(int index) const;
QString escapedValue(int index) const;
QString lastError() const;
static QString last_Error();
unsigned int size() const;
unsigned int fieldCount() const;
QString fieldName() const;
bool hasResult() const;
int fileToBlob(char const * cmd_begin,
char const * cmd_end,
QString const & filename);
QString realEscape(const char *,unsigned long length) const;
//static bool setVar(QString const & name,QString const & value);
static QString lastConnectError() {return last_error;}
static MYSQL * mysql,* r_mysql,*m_init,*m_r_init;
static QString const delimiter;
private:
QString command;
MYSQL_ROW row;
MYSQL_RES * result;
QString error;
static QString last_error;
Dbase dbase;
inline MYSQL * p_mysql() const {return dbase==Local?mysql:r_mysql;}
};
#endif // CMYSQL_H