]> granicus.if.org Git - postgresql/blob - src/interfaces/odbc/statement.h
834eb005b3d6e6be793fd8ce9ccd245cc7a2aa1c
[postgresql] / src / interfaces / odbc / statement.h
1 /* File:                        statement.h
2  *
3  * Description:         See "statement.c"
4  *
5  * Comments:            See "notice.txt" for copyright and license information.
6  *
7  */
8
9 #ifndef __STATEMENT_H__
10 #define __STATEMENT_H__
11
12 #include "psqlodbc.h"
13 #include "bind.h"
14
15 #ifndef WIN32
16 #include "iodbc.h"
17 #include "isql.h"
18 #else
19 #include <windows.h>
20 #include <sql.h>
21 #endif
22
23
24 #ifndef FALSE
25 #define FALSE   (BOOL)0
26 #endif
27 #ifndef TRUE
28 #define TRUE    (BOOL)1
29 #endif
30
31 typedef enum
32 {
33         STMT_ALLOCATED,                         /* The statement handle is allocated, but
34                                                                  * not used so far */
35         STMT_READY,                                     /* the statement is waiting to be executed */
36         STMT_PREMATURE,                         /* ODBC states that it is legal to call
37                                                                  * e.g. SQLDescribeCol before a call to
38                                                                  * SQLExecute, but after SQLPrepare. To
39                                                                  * get all the necessary information in
40                                                                  * such a case, we simply execute the
41                                                                  * query _before_ the actual call to
42                                                                  * SQLExecute, so that statement is
43                                                                  * considered to be "premature". */
44         STMT_FINISHED,                          /* statement execution has finished */
45         STMT_EXECUTING                          /* statement execution is still going on */
46 } STMT_Status;
47
48 #define STMT_ROW_VERSION_CHANGED                                        (-4)
49 #define STMT_POS_BEFORE_RECORDSET                                       (-3)
50 #define STMT_TRUNCATED                                                  (-2)
51 #define STMT_INFO_ONLY                                                  (-1)    /* not an error message,
52                                                                                                                  * just a notification
53                                                                                                                  * to be returned by
54                                                                                                                  * SQLError */
55 #define STMT_OK                                                                 0               /* will be interpreted
56                                                                                                                  * as "no error pending" */
57 #define STMT_EXEC_ERROR                                                 1
58 #define STMT_STATUS_ERROR                                               2
59 #define STMT_SEQUENCE_ERROR                                             3
60 #define STMT_NO_MEMORY_ERROR                                    4
61 #define STMT_COLNUM_ERROR                                               5
62 #define STMT_NO_STMTSTRING                                              6
63 #define STMT_ERROR_TAKEN_FROM_BACKEND                   7
64 #define STMT_INTERNAL_ERROR                                             8
65 #define STMT_STILL_EXECUTING                                    9
66 #define STMT_NOT_IMPLEMENTED_ERROR                              10
67 #define STMT_BAD_PARAMETER_NUMBER_ERROR                 11
68 #define STMT_OPTION_OUT_OF_RANGE_ERROR                  12
69 #define STMT_INVALID_COLUMN_NUMBER_ERROR                13
70 #define STMT_RESTRICTED_DATA_TYPE_ERROR                 14
71 #define STMT_INVALID_CURSOR_STATE_ERROR                 15
72 #define STMT_OPTION_VALUE_CHANGED                               16
73 #define STMT_CREATE_TABLE_ERROR                                 17
74 #define STMT_NO_CURSOR_NAME                                             18
75 #define STMT_INVALID_CURSOR_NAME                                19
76 #define STMT_INVALID_ARGUMENT_NO                                20
77 #define STMT_ROW_OUT_OF_RANGE                                   21
78 #define STMT_OPERATION_CANCELLED                                22
79 #define STMT_INVALID_CURSOR_POSITION                    23
80 #define STMT_VALUE_OUT_OF_RANGE                                 24
81 #define STMT_OPERATION_INVALID                                  25
82 #define STMT_PROGRAM_TYPE_OUT_OF_RANGE                  26
83 #define STMT_BAD_ERROR                                                  27
84 #define STMT_INVALID_OPTION_IDENTIFIER                                  28
85
86 /* statement types */
87 enum
88 {
89         STMT_TYPE_UNKNOWN = -2,
90         STMT_TYPE_OTHER = -1,
91         STMT_TYPE_SELECT = 0,
92         STMT_TYPE_INSERT,
93         STMT_TYPE_UPDATE,
94         STMT_TYPE_DELETE,
95         STMT_TYPE_CREATE,
96         STMT_TYPE_ALTER,
97         STMT_TYPE_DROP,
98         STMT_TYPE_GRANT,
99         STMT_TYPE_REVOKE,
100         STMT_TYPE_PROCCALL
101 };
102
103 #define STMT_UPDATE(stmt)       (stmt->statement_type > STMT_TYPE_SELECT)
104
105
106 /*      Parsing status */
107 enum
108 {
109         STMT_PARSE_NONE = 0,
110         STMT_PARSE_COMPLETE,
111         STMT_PARSE_INCOMPLETE,
112         STMT_PARSE_FATAL,
113 };
114
115 /*      Result style */
116 enum
117 {
118         STMT_FETCH_NONE = 0,
119         STMT_FETCH_NORMAL,
120         STMT_FETCH_EXTENDED,
121 };
122
123 typedef struct
124 {
125         COL_INFO   *col_info;           /* cached SQLColumns info for this table */
126         char            name[MAX_TABLE_LEN + 1];
127         char            alias[MAX_TABLE_LEN + 1];
128 } TABLE_INFO;
129
130 typedef struct
131 {
132         TABLE_INFO *ti;                         /* resolve to explicit table names */
133         int                     precision;
134         int                     scale;
135         int                     display_size;
136         int                     length;
137         int                     type;
138         char            nullable;
139         char            func;
140         char            expr;
141         char            quote;
142         char            dquote;
143         char            numeric;
144         char            dot[MAX_TABLE_LEN + 1];
145         char            name[MAX_COLUMN_LEN + 1];
146         char            alias[MAX_COLUMN_LEN + 1];
147 } FIELD_INFO;
148
149
150 /********       Statement Handle        ***********/
151 struct StatementClass_
152 {
153         ConnectionClass *hdbc;          /* pointer to ConnectionClass this
154                                                                  * statement belongs to */
155         QResultClass *result;           /* result of the current statement */
156         HSTMT FAR  *phstmt;
157         StatementOptions options;
158
159         STMT_Status status;
160         char       *errormsg;
161         int                     errornumber;
162
163         /* information on bindings */
164         BindInfoClass *bindings;        /* array to store the binding information */
165         BindInfoClass bookmark;
166         int                     bindings_allocated;
167
168         /* information on statement parameters */
169         int                     parameters_allocated;
170         ParameterInfoClass *parameters;
171
172         Int4            currTuple;              /* current absolute row number (GetData,
173                                                                  * SetPos, SQLFetch) */
174         int                     save_rowset_size;               /* saved rowset size in case of
175                                                                                  * change/FETCH_NEXT */
176         int                     rowset_start;   /* start of rowset (an absolute row
177                                                                  * number) */
178         int                     bind_row;               /* current offset for Multiple row/column
179                                                                  * binding */
180         int                     last_fetch_count;               /* number of rows retrieved in
181                                                                                  * last fetch/extended fetch */
182         int                     current_col;    /* current column for GetData -- used to
183                                                                  * handle multiple calls */
184         int                     lobj_fd;                /* fd of the current large object */
185
186         char       *statement;          /* if non--null pointer to the SQL
187                                                                  * statement that has been executed */
188
189         TABLE_INFO **ti;
190         FIELD_INFO **fi;
191         int                     nfld;
192         int                     ntab;
193
194         int                     parse_status;
195
196         int                     statement_type; /* According to the defines above */
197         int                     data_at_exec;   /* Number of params needing SQLPutData */
198         int                     current_exec_param;             /* The current parameter for
199                                                                                  * SQLPutData */
200
201         char            put_data;               /* Has SQLPutData been called yet? */
202
203         char            errormsg_created;               /* has an informative error msg
204                                                                                  * been created?  */
205         char            manual_result;  /* Is the statement result manually built? */
206         char            prepare;                /* is this statement a prepared statement
207                                                                  * or direct */
208
209         char            internal;               /* Is this statement being called
210                                                                  * internally? */
211
212         char            cursor_name[MAX_CURSOR_LEN + 1];
213
214         char            *stmt_with_params;      /* statement after
215                                                                                                                  * parameter
216                                                                                                                  * substitution */
217         int             stmt_size_limit;        
218
219         char            pre_executing;  /* This statement is prematurely executing */
220         char            inaccurate_result;      /* Current status is PREMATURE but
221                                                                                  * result is inaccurate */
222         char            errormsg_malloced;      /* Current error message is malloed (not in a static variable) ? */
223         char            miscinfo;
224 };
225
226 #define SC_get_conn(a)    (a->hdbc)
227 #define SC_get_Result(a)  (a->result);
228
229 /*      options for SC_free_params() */
230 #define STMT_FREE_PARAMS_ALL                            0
231 #define STMT_FREE_PARAMS_DATA_AT_EXEC_ONLY      1
232
233 /*      misc info */
234 #define SC_set_pre_executable(a) (a->miscinfo |= 1L)
235 #define SC_no_pre_executable(a) (a->miscinfo &= ~1L)
236 #define SC_is_pre_executable(a) ((a->miscinfo & 1L) != 0)
237 #define SC_set_fetchcursor(a)   (a->miscinfo |= 2L)
238 #define SC_no_fetchcursor(a)    (a->miscinfo &= ~2L)
239 #define SC_is_fetchcursor(a)    ((a->miscinfo & 2L) != 0)
240
241 /*      Statement prototypes */
242 StatementClass *SC_Constructor(void);
243 void            InitializeStatementOptions(StatementOptions *opt);
244 char            SC_Destructor(StatementClass *self);
245 int                     statement_type(char *statement);
246 char            parse_statement(StatementClass *stmt);
247 void            SC_pre_execute(StatementClass *self);
248 char            SC_unbind_cols(StatementClass *self);
249 char            SC_recycle_statement(StatementClass *self);
250
251 void            SC_clear_error(StatementClass *self);
252 char            SC_get_error(StatementClass *self, int *number, char **message);
253 char       *SC_create_errormsg(StatementClass *self);
254 RETCODE         SC_execute(StatementClass *self);
255 RETCODE         SC_fetch(StatementClass *self);
256 void            SC_free_params(StatementClass *self, char option);
257 void            SC_log_error(char *func, char *desc, StatementClass *self);
258 unsigned long SC_get_bookmark(StatementClass *self);
259
260
261 #endif