1 /* src/interfaces/ecpg/ecpglib/extern.h */
2 
3 #ifndef _ECPG_LIB_EXTERN_H
4 #define _ECPG_LIB_EXTERN_H
5 
6 #include "libpq-fe.h"
7 #include "sqlca.h"
8 #include "sqlda-native.h"
9 #include "sqlda-compat.h"
10 #include "ecpg_config.h"
11 
12 #ifndef CHAR_BIT
13 #include <limits.h>
14 #endif
15 #ifdef LOCALE_T_IN_XLOCALE
16 #include <xlocale.h>
17 #endif
18 
19 enum COMPAT_MODE
20 {
21 	ECPG_COMPAT_PGSQL = 0, ECPG_COMPAT_INFORMIX, ECPG_COMPAT_INFORMIX_SE, ECPG_COMPAT_ORACLE
22 };
23 
24 extern bool ecpg_internal_regression_mode;
25 
26 #define INFORMIX_MODE(X) ((X) == ECPG_COMPAT_INFORMIX || (X) == ECPG_COMPAT_INFORMIX_SE)
27 #define ORACLE_MODE(X) ((X) == ECPG_COMPAT_ORACLE)
28 
29 enum ARRAY_TYPE
30 {
31 	ECPG_ARRAY_ERROR, ECPG_ARRAY_NOT_SET, ECPG_ARRAY_ARRAY, ECPG_ARRAY_VECTOR, ECPG_ARRAY_NONE
32 };
33 
34 #define ECPG_IS_ARRAY(X) ((X) == ECPG_ARRAY_ARRAY || (X) == ECPG_ARRAY_VECTOR)
35 
36 /* A generic varchar type. */
37 struct ECPGgeneric_varchar
38 {
39 	int			len;
40 	char		arr[FLEXIBLE_ARRAY_MEMBER];
41 };
42 
43 /*
44  * type information cache
45  */
46 
47 struct ECPGtype_information_cache
48 {
49 	struct ECPGtype_information_cache *next;
50 	int			oid;
51 	enum ARRAY_TYPE isarray;
52 };
53 
54 /* structure to store one statement */
55 struct statement
56 {
57 	int			lineno;
58 	char	   *command;
59 	char	   *name;
60 	struct connection *connection;
61 	enum COMPAT_MODE compat;
62 	bool		force_indicator;
63 	enum ECPG_statement_type statement_type;
64 	bool		questionmarks;
65 	struct variable *inlist;
66 	struct variable *outlist;
67 #ifdef HAVE_USELOCALE
68 	locale_t	clocale;
69 	locale_t	oldlocale;
70 #else
71 	char	   *oldlocale;
72 #ifdef HAVE__CONFIGTHREADLOCALE
73 	int			oldthreadlocale;
74 #endif
75 #endif
76 	int			nparams;
77 	char	  **paramvalues;
78 	PGresult   *results;
79 };
80 
81 /* structure to store prepared statements for a connection */
82 struct prepared_statement
83 {
84 	char	   *name;
85 	bool		prepared;
86 	struct statement *stmt;
87 	struct prepared_statement *next;
88 };
89 
90 /* structure to store connections */
91 struct connection
92 {
93 	char	   *name;
94 	PGconn	   *connection;
95 	bool		autocommit;
96 	struct ECPGtype_information_cache *cache_head;
97 	struct prepared_statement *prep_stmts;
98 	struct connection *next;
99 };
100 
101 /* structure to store descriptors */
102 struct descriptor
103 {
104 	char	   *name;
105 	PGresult   *result;
106 	struct descriptor *next;
107 	int			count;
108 	struct descriptor_item *items;
109 };
110 
111 struct descriptor_item
112 {
113 	int			num;
114 	char	   *data;
115 	int			indicator;
116 	int			length;
117 	int			precision;
118 	int			scale;
119 	int			type;
120 	struct descriptor_item *next;
121 };
122 
123 struct variable
124 {
125 	enum ECPGttype type;
126 	void	   *value;
127 	void	   *pointer;
128 	long		varcharsize;
129 	long		arrsize;
130 	long		offset;
131 	enum ECPGttype ind_type;
132 	void	   *ind_value;
133 	void	   *ind_pointer;
134 	long		ind_varcharsize;
135 	long		ind_arrsize;
136 	long		ind_offset;
137 	struct variable *next;
138 };
139 
140 struct var_list
141 {
142 	int			number;
143 	void	   *pointer;
144 	struct var_list *next;
145 };
146 
147 extern struct var_list *ivlist;
148 
149 /* Here are some methods used by the lib. */
150 
151 bool		ecpg_add_mem(void *ptr, int lineno);
152 
153 bool ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
154 			  enum ECPGttype, char *, char *, long, long, long,
155 			  enum ARRAY_TYPE, enum COMPAT_MODE, bool);
156 
157 #ifdef ENABLE_THREAD_SAFETY
158 void		ecpg_pthreads_init(void);
159 #endif
160 struct connection *ecpg_get_connection(const char *);
161 char	   *ecpg_alloc(long, int);
162 char	   *ecpg_auto_alloc(long, int);
163 char	   *ecpg_realloc(void *, long, int);
164 void		ecpg_free(void *);
165 bool		ecpg_init(const struct connection *, const char *, const int);
166 char	   *ecpg_strdup(const char *, int);
167 const char *ecpg_type_name(enum ECPGttype);
168 int			ecpg_dynamic_type(Oid);
169 int			sqlda_dynamic_type(Oid, enum COMPAT_MODE);
170 void		ecpg_free_auto_mem(void);
171 void		ecpg_clear_auto_mem(void);
172 
173 struct descriptor *ecpggetdescp(int, char *);
174 
175 struct descriptor *ecpg_find_desc(int line, const char *name);
176 
177 struct prepared_statement *ecpg_find_prepared_statement(const char *,
178 							 struct connection *, struct prepared_statement **);
179 
180 bool ecpg_store_result(const PGresult *results, int act_field,
181 				  const struct statement *stmt, struct variable *var);
182 bool		ecpg_store_input(const int, const bool, const struct variable *, char **, bool);
183 void		ecpg_free_params(struct statement *stmt, bool print);
184 bool ecpg_do_prologue(int, const int, const int, const char *, const bool,
185 				 enum ECPG_statement_type, const char *, va_list,
186 				 struct statement **);
187 bool		ecpg_build_params(struct statement *);
188 bool		ecpg_autostart_transaction(struct statement *stmt);
189 bool		ecpg_execute(struct statement *stmt);
190 bool		ecpg_process_output(struct statement *, bool);
191 void		ecpg_do_epilogue(struct statement *);
192 bool ecpg_do(const int, const int, const int, const char *, const bool,
193 		const int, const char *, va_list);
194 
195 bool		ecpg_check_PQresult(PGresult *, int, PGconn *, enum COMPAT_MODE);
196 void		ecpg_raise(int line, int code, const char *sqlstate, const char *str);
197 void		ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat);
198 char	   *ecpg_prepared(const char *, struct connection *);
199 bool		ecpg_deallocate_all_conn(int lineno, enum COMPAT_MODE c, struct connection *conn);
200 void		ecpg_log(const char *format,...) pg_attribute_printf(1, 2);
201 bool		ecpg_auto_prepare(int, const char *, const int, char **, const char *);
202 void		ecpg_init_sqlca(struct sqlca_t *sqlca);
203 
204 struct sqlda_compat *ecpg_build_compat_sqlda(int, PGresult *, int, enum COMPAT_MODE);
205 void		ecpg_set_compat_sqlda(int, struct sqlda_compat **, const PGresult *, int, enum COMPAT_MODE);
206 struct sqlda_struct *ecpg_build_native_sqlda(int, PGresult *, int, enum COMPAT_MODE);
207 void		ecpg_set_native_sqlda(int, struct sqlda_struct **, const PGresult *, int, enum COMPAT_MODE);
208 
209 #ifdef ENABLE_NLS
210 extern char *ecpg_gettext(const char *msgid) pg_attribute_format_arg(1);
211 #else
212 #define ecpg_gettext(x) (x)
213 #endif
214 
215 /* SQLSTATE values generated or processed by ecpglib (intentionally
216  * not exported -- users should refer to the codes directly) */
217 
218 #define ECPG_SQLSTATE_NO_DATA				"02000"
219 #define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS	"07001"
220 #define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS		"07002"
221 #define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION	"07006"
222 #define ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX		"07009"
223 #define ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION	"08001"
224 #define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST		"08003"
225 #define ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN	"08007"
226 #define ECPG_SQLSTATE_CARDINALITY_VIOLATION "21000"
227 #define ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER "22002"
228 #define ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION		"25001"
229 #define ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION		"25P01"
230 #define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME	"26000"
231 #define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME	"33000"
232 #define ECPG_SQLSTATE_INVALID_CURSOR_NAME	"34000"
233 #define ECPG_SQLSTATE_SYNTAX_ERROR			"42601"
234 #define ECPG_SQLSTATE_DATATYPE_MISMATCH		"42804"
235 #define ECPG_SQLSTATE_DUPLICATE_CURSOR		"42P03"
236 
237 /* implementation-defined internal errors of ecpg */
238 #define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR	"YE000"
239 #define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY	"YE001"
240 
241 #endif							/* _ECPG_LIB_EXTERN_H */
242