1 /*
2  * libdbi - database independent abstraction layer for C.
3  * Copyright (C) 2001-2003, David Parker and Mark Tobenkin.
4  * http://libdbi.sourceforge.net
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * $Id: dbi-dev.h,v 1.49 2013/01/08 23:54:30 mhoenicka Exp $
21  */
22 
23 #ifndef __DBI_DEV_H__
24 #define __DBI_DEV_H__
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include "dirent.h" /* DIR definition */
31 #include <dbi/dbi.h> /* for dbi_conn_error_handler_func */
32 
33 /*********************
34  * SQL RELATED TYPES *
35  *********************/
36 
37 /* to fool the compiler into letting us use the following structs before they're actually defined: */
38 typedef struct dbi_driver_s *dbi_driver_t_pointer;
39 typedef struct dbi_inst_s *dbi_inst_t_pointer;
40 typedef struct dbi_conn_s *dbi_conn_t_pointer;
41 typedef struct _field_binding_s *_field_binding_t_pointer;
42 
43 typedef union dbi_data_u {
44 	char d_char;
45 	short d_short;
46         int d_long; /* misnomer */
47 	long long d_longlong;
48 	float d_float;
49 	double d_double;
50 	char *d_string;
51 	time_t d_datetime;
52 } dbi_data_t;
53 
54 typedef struct dbi_row_s {
55 	dbi_data_t *field_values;
56 	size_t *field_sizes; /* strlen() for strings, 0 otherwise */
57 	unsigned char *field_flags; /* field-specific metadata for this particular row */
58 } dbi_row_t;
59 
60 typedef struct dbi_result_s {
61 	dbi_conn_t_pointer conn;
62 	void *result_handle; /* will be typecast into conn-specific type */
63 	unsigned long long numrows_matched; /* set immediately after query */
64 	unsigned long long numrows_affected;
65 	_field_binding_t_pointer field_bindings;
66 
67 	unsigned int numfields; /* can be zero or NULL until first fetchrow */
68 	char **field_names;
69 	unsigned short *field_types;
70 	unsigned int *field_attribs;
71 
72 	enum { NOTHING_RETURNED, ROWS_RETURNED } result_state;
73 	dbi_row_t **rows; /* array of filled rows, elements set to NULL if not fetched yet */
74 	unsigned long long currowidx;
75 } dbi_result_t;
76 
77 typedef struct _field_binding_s {
78 	void (*helper_function)(_field_binding_t_pointer);
79 	dbi_result_t *result;
80 	const char *fieldname;
81 	void *bindto;
82 	struct _field_binding_s *next;
83 } _field_binding_t;
84 
85 /***************************************
86  * DRIVER INFRASTRUCTURE RELATED TYPES *
87  ***************************************/
88 
89 typedef struct dbi_info_s {
90 	const char *name; /* all lowercase letters and numbers, no spaces */
91 	const char *description; /* one or two short sentences, no newlines */
92 	const char *maintainer; /* Full Name <fname@fooblah.com> */
93 	const char *url; /* where this driver came from (if maintained by a third party) */
94 	const char *version;
95 	const char *date_compiled;
96 } dbi_info_t;
97 
98 typedef struct _capability_s {
99 	char *name;
100 	int value;
101 	struct _capability_s *next;
102 } _capability_t;
103 
104 typedef struct dbi_option_s {
105 	char *key;
106 	char *string_value;
107 	int numeric_value; /* use this for port and other numeric settings */
108 	struct dbi_option_s *next;
109 } dbi_option_t;
110 
111 typedef struct dbi_functions_s {
112 	void (*register_driver)(const dbi_info_t **, const char ***, const char ***);
113 	int (*initialize)(dbi_driver_t_pointer);
114 	int (*finalize)(dbi_driver_t_pointer);
115 	int (*connect)(dbi_conn_t_pointer);
116 	int (*disconnect)(dbi_conn_t_pointer);
117 	int (*fetch_row)(dbi_result_t *, unsigned long long);
118 	int (*free_query)(dbi_result_t *);
119         int (*goto_row)(dbi_result_t *, unsigned long long, unsigned long long);
120 	int (*get_socket)(dbi_conn_t_pointer);
121 	const char *(*get_encoding)(dbi_conn_t_pointer);
122 	dbi_result_t *(*list_dbs)(dbi_conn_t_pointer, const char *);
123 	dbi_result_t *(*list_tables)(dbi_conn_t_pointer, const char *, const char *);
124 	dbi_result_t *(*query)(dbi_conn_t_pointer, const char *);
125 	dbi_result_t *(*query_null)(dbi_conn_t_pointer, const unsigned char *, size_t);
126         int (*transaction_begin)(dbi_conn_t_pointer);
127         int (*transaction_commit)(dbi_conn_t_pointer);
128         int (*transaction_rollback)(dbi_conn_t_pointer);
129         int (*savepoint)(dbi_conn_t_pointer, const char *);
130         int (*rollback_to_savepoint)(dbi_conn_t_pointer, const char *);
131         int (*release_savepoint)(dbi_conn_t_pointer, const char *);
132 	size_t (*quote_string)(dbi_driver_t_pointer, const char *, char *);
133 	size_t (*conn_quote_string)(dbi_conn_t_pointer, const char *, char *);
134 	size_t (*quote_binary)(dbi_conn_t_pointer, const unsigned char *, size_t, unsigned char **);
135         const char *(*encoding_to_iana)(const char *);
136         const char *(*encoding_from_iana)(const char *);
137         char *(*get_engine_version)(dbi_conn_t_pointer,char *);
138 	const char *(*select_db)(dbi_conn_t_pointer, const char *);
139 	int (*geterror)(dbi_conn_t_pointer, int *, char **);
140 	unsigned long long (*get_seq_last)(dbi_conn_t_pointer, const char *);
141 	unsigned long long (*get_seq_next)(dbi_conn_t_pointer, const char *);
142 	int (*ping)(dbi_conn_t_pointer);
143 } dbi_functions_t;
144 
145 typedef struct dbi_custom_function_s {
146 	const char *name;
147 	void *function_pointer;
148 	struct dbi_custom_function_s *next;
149 } dbi_custom_function_t;
150 
151 typedef struct dbi_driver_s {
152 	void *dlhandle;
153 	char *filename; /* full pathname */
154 	const dbi_info_t *info;
155 	dbi_functions_t *functions;
156 	dbi_custom_function_t *custom_functions;
157 	const char **reserved_words;
158 	_capability_t *caps;
159 	dbi_inst_t_pointer dbi_inst; /* engine instance we are called from */
160 	struct dbi_driver_s *next;
161 } dbi_driver_t;
162 
163 typedef struct dbi_conn_s {
164 	dbi_driver_t *driver; /* generic unchanging attributes shared by all instances of this conn */
165 	dbi_option_t *options;
166 	_capability_t *caps;
167 	void *connection; /* will be typecast into conn-specific type */
168 	char *current_db;
169 	dbi_error_flag error_flag;
170 	int error_number; /*XXX*/
171 	char *error_message; /*XXX*/
172 	char *full_errmsg;
173 	dbi_conn_error_handler_func error_handler;
174 	void *error_handler_argument;
175 	dbi_result_t **results; /* for garbage-collector-mandated result disjoins */
176 	int results_used;
177 	int results_size;
178 	struct dbi_conn_s *next; /* so libdbi can unload all conns at exit */
179 } dbi_conn_t;
180 
181 unsigned int _isolate_attrib(unsigned int attribs, unsigned int rangemin, unsigned int rangemax);
182 void _error_handler(dbi_conn_t *conn, dbi_error_flag errflag);
183 void _reset_conn_error(dbi_conn_t *conn);
184 void _verbose_handler(dbi_conn_t *conn, const char* fmt, ...);
185 void _logquery(dbi_conn_t *conn, const char* fmt, ...);
186 void _logquery_null(dbi_conn_t *conn, const char* statement, size_t st_length);
187 int _disjoin_from_conn(dbi_result_t *result);
188 void _set_field_flag(dbi_row_t *row, unsigned int fieldidx, unsigned char flag, unsigned char value);
189 int _get_field_flag(dbi_row_t *row, unsigned int fieldidx, unsigned char flag);
190 size_t _dirent_buf_size(DIR * dirp);
191 
192 
193 /******************************
194  * DBI INSTANCE RELATED TYPES *
195  ******************************/
196 typedef struct dbi_inst_s {
197 	dbi_driver_t *rootdriver;
198 	dbi_conn_t *rootconn;
199 	int dbi_verbosity;
200 } dbi_inst_t;
201 
202 
203 #ifdef __cplusplus
204 }
205 #endif /* __cplusplus */
206 
207 #endif	/* __DBI_DEV_H__ */
208