/
SPMySQLConnection.h
200 lines (163 loc) · 6.01 KB
/
SPMySQLConnection.h
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
//
// SPMySQLConnection.h
// SPMySQLFramework
//
// Created by Rowan Beentje (rowan.beent.je) on January 8, 2012
// Copyright (c) 2012 Rowan Beentje. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// More info at <https://github.com/sequelpro/sequelpro>
@class SPMySQLKeepAliveTimer;
@interface SPMySQLConnection : NSObject {
// Delegate
NSObject <SPMySQLConnectionDelegate> *delegate;
BOOL delegateSupportsWillQueryString;
BOOL delegateSupportsConnectionLost;
BOOL delegateQueryLogging; // Defaults to YES if protocol implemented
// Basic connection details
NSString *host;
NSString *username;
NSString *password;
NSUInteger port;
BOOL useSocket;
NSString *socketPath;
// SSL connection details
BOOL useSSL;
NSString *sslKeyFilePath;
NSString *sslCertificatePath;
NSString *sslCACertificatePath;
NSString *sslCipherList;
// MySQL connection details and state
struct st_mysql *mySQLConnection;
SPMySQLConnectionState state;
BOOL connectedWithSSL;
BOOL userTriggeredDisconnect;
pthread_t reconnectingThread;
uint64_t initialConnectTime;
unsigned long mysqlConnectionThreadId;
// Connection proxy
NSObject <SPMySQLConnectionProxy> *proxy;
SPMySQLConnectionProxyState previousProxyState;
BOOL proxyStateChangeNotificationsIgnored;
// Connection lock to prevent non-thread-safe query misuse
NSConditionLock *connectionLock;
// Currently selected database
NSString *database, *databaseToRestore;
// Delegate connection lost decisions
NSUInteger reconnectionRetryAttempts;
SPMySQLConnectionLostDecision lastDelegateDecisionForLostConnection;
NSLock *delegateDecisionLock;
// Timeout and keep-alive
NSUInteger timeout;
BOOL useKeepAlive;
SPMySQLKeepAliveTimer *keepAliveTimer;
CGFloat keepAliveInterval;
uint64_t lastKeepAliveTime;
NSUInteger keepAlivePingFailures;
volatile NSThread *keepAliveThread;
volatile BOOL keepAlivePingThreadActive;
BOOL keepAliveLastPingBlocked;
// Encoding details - and also a record of any previous encoding to allow
// switching back and forth
NSString *encoding, *encodingToRestore;
NSStringEncoding stringEncoding;
BOOL encodingUsesLatin1Transport, encodingUsesLatin1TransportToRestore;
NSString *previousEncoding;
BOOL previousEncodingUsesLatin1Transport;
// Server details
NSString *serverVariableVersion;
unsigned long serverVersionNumber;
// Error state for the last query or connection state
NSUInteger queryErrorID;
NSString *queryErrorMessage;
NSString *querySqlstate;
// Query details
unsigned long long lastQueryAffectedRowCount;
unsigned long long lastQueryInsertID;
// Query cancellation details
BOOL lastQueryWasCancelled;
BOOL lastQueryWasCancelledUsingReconnect;
// Timing details
uint64_t lastConnectionUsedTime;
double lastQueryExecutionTime;
// Maximum query size
NSUInteger maxQuerySize;
BOOL maxQuerySizeIsEditable;
BOOL maxQuerySizeEditabilityChecked;
NSUInteger queryActionShouldRestoreMaxQuerySize;
// Queries
BOOL retryQueriesOnConnectionFailure;
SPMySQLClientFlags clientFlags;
NSString *_debugLastConnectedEvent;
}
#pragma mark -
#pragma mark Synthesized properties
@property (readwrite, retain) NSString *host;
@property (readwrite, retain) NSString *username;
@property (readwrite, retain) NSString *password;
@property (readwrite, assign) NSUInteger port;
@property (readwrite, assign) BOOL useSocket;
@property (readwrite, retain) NSString *socketPath;
@property (readwrite, assign) BOOL useSSL;
@property (readwrite, retain) NSString *sslKeyFilePath;
@property (readwrite, retain) NSString *sslCertificatePath;
@property (readwrite, retain) NSString *sslCACertificatePath;
/**
* List of supported ciphers for SSL/TLS connections.
* This is a colon-separated string of names as used by
* `openssl ciphers`. The order of entries specifies
* their preference (earlier = better).
* A value of nil (default) means SPMySQL will use its built-in cipher list.
*/
@property (readwrite, retain) NSString *sslCipherList;
@property (readwrite, assign) NSUInteger timeout;
@property (readwrite, assign) BOOL useKeepAlive;
@property (readwrite, assign) CGFloat keepAliveInterval;
@property (readonly) unsigned long mysqlConnectionThreadId;
@property (readwrite, assign) BOOL retryQueriesOnConnectionFailure;
@property (readwrite, assign) BOOL delegateQueryLogging;
@property (readwrite, assign) BOOL lastQueryWasCancelled;
/**
* The mysql client capability flags to set when connecting.
* See CLIENT_* in mysql.h
*/
@property (readwrite, assign, nonatomic) SPMySQLClientFlags clientFlags;
- (void)addClientFlags:(SPMySQLClientFlags)opts;
- (void)removeClientFlags:(SPMySQLClientFlags)opts;
#pragma mark -
#pragma mark Connection and disconnection
- (BOOL)connect;
- (BOOL)reconnect;
- (void)disconnect;
#pragma mark -
#pragma mark Connection state
- (BOOL)isConnected;
- (BOOL)isConnectedViaSSL;
- (BOOL)checkConnection;
- (BOOL)checkConnectionIfNecessary;
- (double)timeConnected;
- (BOOL)userTriggeredDisconnect;
#pragma mark -
#pragma mark Connection utility
+ (NSString *)findSocketPath;
@end