forked from Tencent/phxsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphxsqlclientbase.h
More file actions
151 lines (105 loc) · 4.1 KB
/
Copy pathphxsqlclientbase.h
File metadata and controls
151 lines (105 loc) · 4.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
/*
Tencent is pleased to support the open source community by making PhxSQL available.
Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the GNU General Public License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/GPL-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#pragma once
#include <vector>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "mysql.h"
#include "phxsqlclienterrdef.h"
namespace phxsql {
typedef struct tagMySQLEndPoint {
std::string m_sMySQLIP;
int32_t m_iPort;
std::string ToString() {
char sBuf[64] = { 0 };
snprintf(sBuf, sizeof(sBuf), "%s:%u", m_sMySQLIP.c_str(), m_iPort);
return std::string(sBuf);
}
std::string ToString() const {
return ((tagMySQLEndPoint *) this)->ToString();
}
} MySQLEndPoint_t;
typedef struct tagMySQLProp {
MySQLEndPoint_t tEndPoint;
char pcUser[64];
char pcPass[64];
char pcDB[256];
char pcNULL[1];
char iClientFlag;
MYSQL connection;
bool bConnected;
} MySQLProp_t;
class PhxSQLClientConfig {
public:
PhxSQLClientConfig();
virtual ~PhxSQLClientConfig();
virtual void SetMySQLEndPoints(const std::vector<MySQLEndPoint_t> & vecEndPoints);
virtual void ResetEndPoints();
const std::vector<MySQLEndPoint_t> & GetEndPoints();
int GetConnTimeout();
void SetConnTimeout(int iConnTimeout);
int GetReadTimeout();
void SetReadTimeout(int iReadTimeout);
protected:
std::vector<MySQLEndPoint_t> m_vecMySQLEndPoint;
int m_iConnTimeout;
int m_iReadTimeout;
};
class PhxSQLClientDisasterStrategy {
public:
PhxSQLClientDisasterStrategy();
virtual ~PhxSQLClientDisasterStrategy();
public:
virtual int ReportEndPoint(const MySQLEndPoint_t & tEndPoint, int iRet, int iCostInMS);
virtual int GetEndPoint(const std::vector<MySQLEndPoint_t> & vecMySQLEndPoints,
const MySQLEndPoint_t & tPreviousEndPoint, MySQLEndPoint_t & tEndPoint);
};
//MySQLConnect() is called, it will new another one.
//waning!! it is not thread-safe
class PhxSQLClientBase {
public:
PhxSQLClientBase(PhxSQLClientConfig * poConfig, PhxSQLClientDisasterStrategy * poDisasterStrategy,
const char *pcUser, const char *pcPasswd, const char *pcDB, uint32_t iClientFlag);
virtual ~PhxSQLClientBase();
PhxSQLClientConfig * GetConfig();
void SetConfig(PhxSQLClientConfig * poConfig);
PhxSQLClientDisasterStrategy * GetDisasterStrategy();
void SetDisasterStrategy(PhxSQLClientDisasterStrategy * poDisasterStrategy);
//mysql api
public:
MYSQL * GetMySQLFD();
//Connect Mysql, if connection exists, just return OK
int Connect();
int SetOption(mysql_option option, const char *arg);
//substitute of mysql_query()
int Query(const char* szSqlString);
//substitute of mysql_query(), bPingFirst = true will cause a mysql_ping() before query,
//which will cost one more RTT and maybe create another connection, be carefull if u need this feature.
//After bPingFirst is set, u don't need to invoke Connect() before Query().
int Query(const char* szSqlString, bool bPingFirst);
int Ping();
void CloseConnection();
const MySQLEndPoint_t & GetConnectEndPoint();
private:
void InitLibrary();
void InitProperty(MySQLProp_t * poProperty);
protected:
PhxSQLClientConfig * m_poConfig;
MySQLProp_t * m_poMySQLProp;
PhxSQLClientDisasterStrategy * m_poDisasterStrategy;
};
class PhxSQLClient_Random : public PhxSQLClientBase {
public:
PhxSQLClient_Random(const char *pcUser, const char *pcPasswd, const char *pcDB, uint32_t iClientFlag);
virtual ~PhxSQLClient_Random();
};
}
;