]> granicus.if.org Git - postgresql/blob - src/interfaces/odbc/connection.h
Provide some initial support for building the ODBC driver for
[postgresql] / src / interfaces / odbc / connection.h
1 /* File:                        connection.h
2  *
3  * Description:         See "connection.c"
4  *
5  * Comments:            See "notice.txt" for copyright and license information.
6  *
7  */
8
9 #ifndef __CONNECTION_H__
10 #define __CONNECTION_H__
11
12 #include "psqlodbc.h"
13
14 #include <stdlib.h>
15 #include <string.h>
16
17
18 typedef enum
19 {
20         CONN_NOT_CONNECTED,                     /* Connection has not been established */
21         CONN_CONNECTED,                         /* Connection is up and has been
22                                                                  * established */
23         CONN_DOWN,                                      /* Connection is broken */
24         CONN_EXECUTING                          /* the connection is currently executing a
25                                                                  * statement */
26 } CONN_Status;
27
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
39
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
55
56 #define CONN_OPTION_VALUE_CHANGED                                       213
57 #define CONN_VALUE_OUT_OF_RANGE                                         214
58
59 #define CONN_TRUNCATED                                                          215
60
61 /* Conn_status defines */
62 #define CONN_IN_AUTOCOMMIT                                                      0x01
63 #define CONN_IN_TRANSACTION                                                     0x02
64
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)
69
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)
74
75
76 /* Authentication types */
77 #define AUTH_REQ_OK                                                                     0
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
84
85 /*      Startup Packet sizes */
86 #define SM_DATABASE                                                                     64
87 #define SM_USER                                                                         32
88 #define SM_OPTIONS                                                                      64
89 #define SM_UNUSED                                                                       64
90 #define SM_TTY                                                                          64
91
92 /*      Old 6.2 protocol defines */
93 #define NO_AUTHENTICATION                                                       7
94 #define PATH_SIZE                                                                       64
95 #define ARGV_SIZE                                                                       64
96 #define NAMEDATALEN                                                                     16
97
98 typedef unsigned int ProtocolVersion;
99
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)
104
105 /*      This startup packet is to support latest Postgres protocol (6.4, 6.3) */
106 typedef struct _StartupPacket
107 {
108         ProtocolVersion protoVersion;
109         char            database[SM_DATABASE];
110         char            user[SM_USER];
111         char            options[SM_OPTIONS];
112         char            unused[SM_UNUSED];
113         char            tty[SM_TTY];
114 } StartupPacket;
115
116
117 /*      This startup packet is to support pre-Postgres 6.3 protocol */
118 typedef struct _StartupPacket6_2
119 {
120         unsigned int authtype;
121         char            database[PATH_SIZE];
122         char            user[NAMEDATALEN];
123         char            options[ARGV_SIZE];
124         char            execfile[ARGV_SIZE];
125         char            tty[PATH_SIZE];
126 }                       StartupPacket6_2;
127
128
129 /*      Structure to hold all the connection attributes for a specific
130         connection (used for both registry and file, DSN and DRIVER)
131 */
132 typedef struct
133 {
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];
151         char            focus_password;
152         char            disallow_premature;
153         char            updatable_cursors;
154         GLOBAL_VALUES   drivers; /* moved from driver's option */
155 } ConnInfo;
156
157 /*      Macro to determine is the connection using 6.2 protocol? */
158 #define PROTOCOL_62(conninfo_)          (strncmp((conninfo_)->protocol, PG62, strlen(PG62)) == 0)
159
160 /*      Macro to determine is the connection using 6.3 protocol? */
161 #define PROTOCOL_63(conninfo_)          (strncmp((conninfo_)->protocol, PG63, strlen(PG63)) == 0)
162
163 /*
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
168  */
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)
181 /*#else
182 #define STRING_AFTER_DOT(str)   (strchr("str", '.') + 1)
183 #endif*/
184 /*
185  *      Simplified macros to compare the server's version with a
186  *              specified version
187  *      Note: Never pass a variable as the second parameter.
188  *                It must be a decimal constant of the form %d.%d .
189  */
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))
198
199 /*      This is used to store cached table information in the connection */
200 struct col_info
201 {
202         QResultClass *result;
203         char            name[MAX_TABLE_LEN + 1];
204 };
205
206  /* Translation DLL entry points */
207 #ifdef WIN32
208 #define DLLHANDLE HINSTANCE
209 #else
210 #define WINAPI CALLBACK
211 #define DLLHANDLE void *
212 #define HINSTANCE void *
213 #endif
214
215 typedef BOOL (FAR WINAPI * DataSourceToDriverProc) (UDWORD,
216                                                                                                                                 SWORD,
217                                                                                                                                 PTR,
218                                                                                                                                 SDWORD,
219                                                                                                                                 PTR,
220                                                                                                                                 SDWORD,
221                                                                                                                         SDWORD FAR *,
222                                                                                                                          UCHAR FAR *,
223                                                                                                                                 SWORD,
224                                                                                                                         SWORD FAR *);
225
226 typedef BOOL (FAR WINAPI * DriverToDataSourceProc) (UDWORD,
227                                                                                                                                 SWORD,
228                                                                                                                                 PTR,
229                                                                                                                                 SDWORD,
230                                                                                                                                 PTR,
231                                                                                                                                 SDWORD,
232                                                                                                                         SDWORD FAR *,
233                                                                                                                          UCHAR FAR *,
234                                                                                                                                 SWORD,
235                                                                                                                         SWORD FAR *);
236
237 /*******        The Connection handle   ************/
238 struct ConnectionClass_
239 {
240         HENV            henv;                   /* environment this connection was created
241                                                                  * on */
242         StatementOptions stmtOptions;
243         char       *errormsg;
244         int                     errornumber;
245         CONN_Status status;
246         ConnInfo        connInfo;
247         StatementClass **stmts;
248         int                     num_stmts;
249         SocketClass *sock;
250         int                     lobj_type;
251         int                     ntables;
252         COL_INFO  **col_info;
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
259                                                                  * progress */
260         char            errormsg_created;               /* has an informative error msg
261                                                                                  * been created?  */
262         char            pg_version[MAX_INFO_STRING];    /* Version of PostgreSQL
263                                                                                                  * we're connected to -
264                                                                                                  * DJP 25-1-2001 */
265         float           pg_version_number;
266         Int2            pg_version_major;
267         Int2            pg_version_minor;
268         char            ms_jet;
269 #ifdef  MULTIBYTE
270         char            *client_encoding;
271         char            *server_encoding;
272 #endif /* MULTIBYTE */
273 };
274
275
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')
283
284
285 /*      for CC_DSN_info */
286 #define CONN_DONT_OVERWRITE             0
287 #define CONN_OVERWRITE                  1
288
289
290 /*      prototypes */
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);
311
312 #endif