3 * Description: See "connection.c"
5 * Comments: See "notice.txt" for copyright and license information.
9 #ifndef __CONNECTION_H__
10 #define __CONNECTION_H__
20 CONN_NOT_CONNECTED, /* Connection has not been established */
21 CONN_CONNECTED, /* Connection is up and has been
23 CONN_DOWN, /* Connection is broken */
24 CONN_EXECUTING /* the connection is currently executing a
28 /* These errors have general sql error state */
29 #define CONNECTION_SERVER_NOT_REACHED 101
30 #define CONNECTION_MSG_TOO_LONG 103
31 #define CONNECTION_COULD_NOT_SEND 104
32 #define CONNECTION_NO_SUCH_DATABASE 105
33 #define CONNECTION_BACKEND_CRAZY 106
34 #define CONNECTION_NO_RESPONSE 107
35 #define CONNECTION_SERVER_REPORTED_ERROR 108
36 #define CONNECTION_COULD_NOT_RECEIVE 109
37 #define CONNECTION_SERVER_REPORTED_WARNING 110
38 #define CONNECTION_NEED_PASSWORD 112
40 /* These errors correspond to specific SQL states */
41 #define CONN_INIREAD_ERROR 201
42 #define CONN_OPENDB_ERROR 202
43 #define CONN_STMT_ALLOC_ERROR 203
44 #define CONN_IN_USE 204
45 #define CONN_UNSUPPORTED_OPTION 205
46 /* Used by SetConnectoption to indicate unsupported options */
47 #define CONN_INVALID_ARGUMENT_NO 206
48 /* SetConnectOption: corresponds to ODBC--"S1009" */
49 #define CONN_TRANSACT_IN_PROGRES 207
50 #define CONN_NO_MEMORY_ERROR 208
51 #define CONN_NOT_IMPLEMENTED_ERROR 209
52 #define CONN_INVALID_AUTHENTICATION 210
53 #define CONN_AUTH_TYPE_UNSUPPORTED 211
54 #define CONN_UNABLE_TO_LOAD_DLL 212
56 #define CONN_OPTION_VALUE_CHANGED 213
57 #define CONN_VALUE_OUT_OF_RANGE 214
59 #define CONN_TRUNCATED 215
61 /* Conn_status defines */
62 #define CONN_IN_AUTOCOMMIT 0x01
63 #define CONN_IN_TRANSACTION 0x02
65 /* AutoCommit functions */
66 #define CC_set_autocommit_off(x) (x->transact_status &= ~CONN_IN_AUTOCOMMIT)
67 #define CC_set_autocommit_on(x) (x->transact_status |= CONN_IN_AUTOCOMMIT)
68 #define CC_is_in_autocommit(x) (x->transact_status & CONN_IN_AUTOCOMMIT)
70 /* Transaction in/not functions */
71 #define CC_set_in_trans(x) (x->transact_status |= CONN_IN_TRANSACTION)
72 #define CC_set_no_trans(x) (x->transact_status &= ~CONN_IN_TRANSACTION)
73 #define CC_is_in_trans(x) (x->transact_status & CONN_IN_TRANSACTION)
76 /* Authentication types */
78 #define AUTH_REQ_KRB4 1
79 #define AUTH_REQ_KRB5 2
80 #define AUTH_REQ_PASSWORD 3
81 #define AUTH_REQ_CRYPT 4
82 #define AUTH_REQ_MD5 5
83 #define AUTH_REQ_SCM_CREDS 6
85 /* Startup Packet sizes */
86 #define SM_DATABASE 64
92 /* Old 6.2 protocol defines */
93 #define NO_AUTHENTICATION 7
96 #define NAMEDATALEN 16
98 typedef unsigned int ProtocolVersion;
100 #define PG_PROTOCOL(major, minor) (((major) << 16) | (minor))
101 #define PG_PROTOCOL_LATEST PG_PROTOCOL(2, 0)
102 #define PG_PROTOCOL_63 PG_PROTOCOL(1, 0)
103 #define PG_PROTOCOL_62 PG_PROTOCOL(0, 0)
105 /* This startup packet is to support latest Postgres protocol (6.4, 6.3) */
106 typedef struct _StartupPacket
108 ProtocolVersion protoVersion;
109 char database[SM_DATABASE];
111 char options[SM_OPTIONS];
112 char unused[SM_UNUSED];
117 /* This startup packet is to support pre-Postgres 6.3 protocol */
118 typedef struct _StartupPacket6_2
120 unsigned int authtype;
121 char database[PATH_SIZE];
122 char user[NAMEDATALEN];
123 char options[ARGV_SIZE];
124 char execfile[ARGV_SIZE];
129 /* Structure to hold all the connection attributes for a specific
130 connection (used for both registry and file, DSN and DRIVER)
134 char dsn[MEDIUM_REGISTRY_LEN];
135 char desc[MEDIUM_REGISTRY_LEN];
136 char driver[MEDIUM_REGISTRY_LEN];
137 char server[MEDIUM_REGISTRY_LEN];
138 char database[MEDIUM_REGISTRY_LEN];
139 char username[MEDIUM_REGISTRY_LEN];
140 char password[MEDIUM_REGISTRY_LEN];
141 char conn_settings[LARGE_REGISTRY_LEN];
142 char protocol[SMALL_REGISTRY_LEN];
143 char port[SMALL_REGISTRY_LEN];
144 char onlyread[SMALL_REGISTRY_LEN];
145 char fake_oid_index[SMALL_REGISTRY_LEN];
146 char show_oid_column[SMALL_REGISTRY_LEN];
147 char row_versioning[SMALL_REGISTRY_LEN];
148 char show_system_tables[SMALL_REGISTRY_LEN];
149 char translation_dll[MEDIUM_REGISTRY_LEN];
150 char translation_option[SMALL_REGISTRY_LEN];
152 char disallow_premature;
153 char updatable_cursors;
154 GLOBAL_VALUES drivers; /* moved from driver's option */
157 /* Macro to determine is the connection using 6.2 protocol? */
158 #define PROTOCOL_62(conninfo_) (strncmp((conninfo_)->protocol, PG62, strlen(PG62)) == 0)
160 /* Macro to determine is the connection using 6.3 protocol? */
161 #define PROTOCOL_63(conninfo_) (strncmp((conninfo_)->protocol, PG63, strlen(PG63)) == 0)
164 * Macros to compare the server's version with a specified version
165 * 1st parameter: pointer to a ConnectionClass object
166 * 2nd parameter: major version number
167 * 3rd parameter: minor version number
169 #define SERVER_VERSION_GT(conn, major, minor) \
170 ((conn)->pg_version_major > major || \
171 ((conn)->pg_version_major == major && (conn)->pg_version_minor > minor))
172 #define SERVER_VERSION_GE(conn, major, minor) \
173 ((conn)->pg_version_major > major || \
174 ((conn)->pg_version_major == major && (conn)->pg_version_minor >= minor))
175 #define SERVER_VERSION_EQ(conn, major, minor) \
176 ((conn)->pg_version_major == major && (conn)->pg_version_minor == minor)
177 #define SERVER_VERSION_LE(conn, major, minor) (! SERVER_VERSION_GT(conn, major, minor))
178 #define SERVER_VERSION_LT(conn, major, minor) (! SERVER_VERSION_GE(conn, major, minor))
179 /*#if ! defined(HAVE_CONFIG_H) || defined(HAVE_STRINGIZE)*/
180 #define STRING_AFTER_DOT(string) (strchr(#string, '.') + 1)
182 #define STRING_AFTER_DOT(str) (strchr("str", '.') + 1)
185 * Simplified macros to compare the server's version with a
187 * Note: Never pass a variable as the second parameter.
188 * It must be a decimal constant of the form %d.%d .
190 #define PG_VERSION_GT(conn, ver) \
191 (SERVER_VERSION_GT(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
192 #define PG_VERSION_GE(conn, ver) \
193 (SERVER_VERSION_GE(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
194 #define PG_VERSION_EQ(conn, ver) \
195 (SERVER_VERSION_EQ(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
196 #define PG_VERSION_LE(conn, ver) (! PG_VERSION_GT(conn, ver))
197 #define PG_VERSION_LT(conn, ver) (! PG_VERSION_GE(conn, ver))
199 /* This is used to store cached table information in the connection */
202 QResultClass *result;
203 char name[MAX_TABLE_LEN + 1];
206 /* Translation DLL entry points */
208 #define DLLHANDLE HINSTANCE
210 #define WINAPI CALLBACK
211 #define DLLHANDLE void *
212 #define HINSTANCE void *
215 typedef BOOL (FAR WINAPI * DataSourceToDriverProc) (UDWORD,
226 typedef BOOL (FAR WINAPI * DriverToDataSourceProc) (UDWORD,
237 /******* The Connection handle ************/
238 struct ConnectionClass_
240 HENV henv; /* environment this connection was created
242 StatementOptions stmtOptions;
247 StatementClass **stmts;
253 long translation_option;
254 HINSTANCE translation_handle;
255 DataSourceToDriverProc DataSourceToDriver;
256 DriverToDataSourceProc DriverToDataSource;
257 Int2 driver_version; /* prepared for ODBC3.0 */
258 char transact_status;/* Is a transaction is currently in
260 char errormsg_created; /* has an informative error msg
262 char pg_version[MAX_INFO_STRING]; /* Version of PostgreSQL
263 * we're connected to -
265 float pg_version_number;
266 Int2 pg_version_major;
267 Int2 pg_version_minor;
270 char *client_encoding;
271 char *server_encoding;
272 #endif /* MULTIBYTE */
276 /* Accessor functions */
277 #define CC_get_socket(x) (x->sock)
278 #define CC_get_database(x) (x->connInfo.database)
279 #define CC_get_server(x) (x->connInfo.server)
280 #define CC_get_DSN(x) (x->connInfo.dsn)
281 #define CC_get_username(x) (x->connInfo.username)
282 #define CC_is_onlyread(x) (x->connInfo.onlyread[0] == '1')
285 /* for CC_DSN_info */
286 #define CONN_DONT_OVERWRITE 0
287 #define CONN_OVERWRITE 1
291 ConnectionClass *CC_Constructor(void);
292 char CC_Destructor(ConnectionClass *self);
293 int CC_cursor_count(ConnectionClass *self);
294 char CC_cleanup(ConnectionClass *self);
295 char CC_abort(ConnectionClass *self);
296 int CC_set_translation(ConnectionClass *self);
297 char CC_connect(ConnectionClass *self, char do_password);
298 char CC_add_statement(ConnectionClass *self, StatementClass *stmt);
299 char CC_remove_statement(ConnectionClass *self, StatementClass *stmt);
300 char CC_get_error(ConnectionClass *self, int *number, char **message);
301 QResultClass *CC_send_query(ConnectionClass *self, char *query, QueryInfo *qi);
302 void CC_clear_error(ConnectionClass *self);
303 char *CC_create_errormsg(ConnectionClass *self);
304 int CC_send_function(ConnectionClass *conn, int fnid, void *result_buf, int *actual_result_len, int result_is_int, LO_ARG *argv, int nargs);
305 char CC_send_settings(ConnectionClass *self);
306 void CC_lookup_lo(ConnectionClass *conn);
307 void CC_lookup_pg_version(ConnectionClass *conn);
308 void CC_initialize_pg_version(ConnectionClass *conn);
309 void CC_log_error(char *func, char *desc, ConnectionClass *self);
310 int CC_get_max_query_len(const ConnectionClass *self);