-
Notifications
You must be signed in to change notification settings - Fork 5
/
PGSQLConnection.h
249 lines (200 loc) · 8.35 KB
/
PGSQLConnection.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// PGSQLConnection.h
// PGSQLKit
//
// Created by Andy Satori on 5/8/07.
// Copyright 2007-2010 Druware Software Designs. All rights reserved.
//
/*!
@header PGSQLConnection
@abstract A Connection class that is the root of all data access in
PGSQLKit. Contextually, the PGSQLConnection encapsulates a
PQconnectdb() call and the results of that call.
@discussion The PGSQLConnection class provides the gateway to all of the
functionality in the library. The only class that is not
reached through the Connection class is the Login class, which
is a utility class intended to provide an easy and reusable
tool for obtaining a Connection class without reinventing the
login panel in every application.
The core functionality of this class wraps around the libpq
interface.
License
Copyright (c) 2005-2010, Druware Software Designs
All rights reserved.
Redistribution and use in binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
2. Neither the name of the Druware Software Designs nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
// #import <Cocoa/Cocoa.h>
#import "PGSQLRecordset.h"
/*!
@class
@abstract PGSQLConnection is the core class in the Kit. Using the
PGSQLConnection to create and use a database connection to
execute queries and return results from the database, everything
else in the kit stems from this core class.
@discussion The usual use of this class is to create a connection that is
used for the duration of the connection. A single connection
can support multiple queries, however, because of local storage
of the results, it is possible that memory could become a
concern if mulitple result sets are open as the same time.
*/
@interface PGSQLConnection : NSObject {
BOOL isConnected;
NSString *connectionString;
NSString *errorDescription;
NSMutableString *sqlLog;
NSStringEncoding defaultEncoding;
/* platform specific definitions */
BOOL logInfo;
BOOL logSQL;
void *pgconn;
NSString *host;
NSString *port;
NSString *options;
NSString *tty; // ignored now
NSString *dbName;
NSString *userName;
NSString *password;
NSString *sslMode; // allow, prefer, require
NSString *service; // service name
NSString *krbsrvName;
NSString *commandStatus;
}
/*!
@method
@abstract If a connection has been established, it will be maintained as
the defaultConnection.
@discussion The defaultConnection is treated as a singleton and will ALWAYS
be the first connection established in an instance of the
PGSQLKit framework. It will not span processes.
The defaultConnection will return an id as a PGSQLConnection *
to the the first connection made in the current session.
*/
+(id)defaultConnection;
#pragma mark -
#pragma mark Contructor / Destructor Functions
/*!
@function
@abstract Initialize the class. Though this is a standard to every Cocoa
class, the PGSQLConnection also uses this method to set a sane
base environment so as not presume compiler and runtime defaults.
@discussion During init, the PGSQLConnection clears and defaults all of the
properties in order to prevent unexpected values in any of the
parameters. Though this is not normally a concern, there are a
couple of defaults to be aware of:
host - defaults to "localhost"
port - defaults to 5432
dbName - defaults to "template1"
If the defaultConnection is not already assigned from a prior
connection, then the defaultConnection is also set to be the
current connection upon init.
*/
-(id)init;
-(void)dealloc;
#pragma mark -
#pragma mark Connection Management Functions
-(BOOL)close;
-(BOOL)connect;
-(void)connectAsync;
-(BOOL)reset;
-(NSMutableString *)makeConnectionString;
#pragma mark -
#pragma mark Sql Execution Functions
-(BOOL)execCommand:(NSString *)sql;
- (BOOL)execCommand:(NSString *)sql numberOfArguments:(int)nParams withParameters:(id)params,...;
-(void)execCommandAsync:(NSString *)sql;
- (PGSQLRecordset *)open:(NSString *)sql numberOfArguments:(int)nParams withParameters:(id)param, ...;
-(PGSQLRecordset *)open:(NSString *)sql;
-(void)openAsync:(NSString *)sql;
#pragma mark -
#pragma mark Utility Functions
-(NSData *)sqlDecodeData:(NSData *)toDecode;
-(NSString *)sqlEncodeData:(NSData *)toEncode;
-(NSString *)sqlEncodeString:(NSString *)toEncode;
#pragma mark -
#pragma mark Simple Accessors
-(BOOL)isConnected;
-(NSString *)connectionString;
-(void)setConnectionString:(NSString *)value;
-(NSString *)userName;
-(void)setUserName:(NSString *)value;
-(NSString *)password;
-(void)setPassword:(NSString *)value;
-(NSString *)server;
-(void)setServer:(NSString *)value;
-(NSString *)port;
-(void)setPort:(NSString *)value;
-(NSString *)databaseName;
-(void)setDatabaseName:(NSString *)value;
-(NSString *)lastError;
-(NSMutableString *)sqlLog;
-(void)appendSQLLog:(NSString *)value;
/*!
@function
@abstract Get the connection's defaultEncoding for all string operations
returning.
@discussion The default setting is NSMacOSRoman. While this default is
used to maintain existing functionality, this will be changed
NSUTF8StringEncoding when PostgreSQL9 is released.
@result returns the defaultEncoding as an NSSTringEncoding (
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSStringEncoding )
*/
-(NSStringEncoding)defaultEncoding;
/*!
@function
@abstract Set the defaultEncoding for all string operations on the current
connection
@discussion The default setting is NSMacOSRoman. While this default is
used to maintain existing functionality, this will be changed
NSUTF8StringEncoding when PostgreSQL9 is released.
@param value the defaultEncoding as an NSSTringEncoding (
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSStringEncoding )
@result void
*/
-(void)setDefaultEncoding:(NSStringEncoding)value;
/*!
@method
@abstract Provide a text based error (or information) about the status
of the last command.
@discussion Most of the execution methods of the Connection will set this
value to either nil or a string that contains the last error or
any messages returned by the server associated with the command.
The returns messages are not always errors. The may simply be
notes regarded the execution of the command from the server that
do not impact the result set itself.
*/
-(NSString *)lastCmdStatus;
#pragma mark -
#pragma mark Exported Constants
/*!
@const
@abstract Notification for use with async connections being established.
@discussion <#(description)#>
*/
FOUNDATION_EXPORT NSString * const PGSQLConnectionDidCompleteNotification;
/*!
@const
@abstract Notification for use with async command processing.
@discussion <#(description)#>
*/
FOUNDATION_EXPORT NSString * const PGSQLCommandDidCompleteNotification;
@end
static PGSQLConnection *globalPGSQLConnection;
#pragma unused(globalPGSQLConnection)