1 /*!
2   \file include/dbmi.h
3 
4   \brief Main header of \ref dbmilib
5 
6   (C) 1999-2009, 2011 by the GRASS Development Team
7 
8   This program is free software under the GNU General Public License
9   (>=v2). Read the file COPYING that comes with GRASS for details.
10 */
11 
12 #ifndef GRASS_DBMI_H
13 #define GRASS_DBMI_H
14 
15 #include <stdio.h>
16 #include <grass/gis.h>
17 
18 #define DB_VERSION "0"
19 
20 #ifdef HAVE_SQLITE
21 #define DB_DEFAULT_DRIVER "sqlite"
22 #else
23 #define DB_DEFAULT_DRIVER "dbf"
24 #endif
25 
26 /* DB Prodedure Numbers */
27 #define DB_PROC_VERSION                 999
28 
29 #define DB_PROC_CLOSE_DATABASE		101
30 #define DB_PROC_CREATE_DATABASE		102
31 #define DB_PROC_DELETE_DATABASE		103
32 #define DB_PROC_FIND_DATABASE		104
33 #define DB_PROC_LIST_DATABASES		105
34 #define DB_PROC_OPEN_DATABASE		106
35 #define DB_PROC_SHUTDOWN_DRIVER		107
36 
37 #define DB_PROC_CLOSE_CURSOR		201
38 #define DB_PROC_DELETE			202
39 #define DB_PROC_FETCH			203
40 #define DB_PROC_INSERT			204
41 #define DB_PROC_OPEN_INSERT_CURSOR	205
42 #define DB_PROC_OPEN_SELECT_CURSOR	206
43 #define DB_PROC_OPEN_UPDATE_CURSOR	207
44 #define DB_PROC_UPDATE			208
45 #define DB_PROC_ROWS			209
46 #define DB_PROC_BIND_UPDATE		220
47 #define DB_PROC_BIND_INSERT		221
48 
49 #define DB_PROC_EXECUTE_IMMEDIATE	301
50 #define DB_PROC_BEGIN_TRANSACTION	302
51 #define DB_PROC_COMMIT_TRANSACTION	303
52 
53 #define DB_PROC_CREATE_TABLE		401
54 #define DB_PROC_DESCRIBE_TABLE		402
55 #define DB_PROC_DROP_TABLE		403
56 #define DB_PROC_LIST_TABLES		404
57 #define DB_PROC_ADD_COLUMN		405
58 #define DB_PROC_DROP_COLUMN		406
59 #define DB_PROC_GRANT_ON_TABLE		407
60 
61 #define DB_PROC_CREATE_INDEX		701
62 #define DB_PROC_LIST_INDEXES		702
63 #define DB_PROC_DROP_INDEX		703
64 
65 /* Unix file permissions */
66 #define DB_PERM_R	01
67 #define DB_PERM_W	02
68 #define DB_PERM_X	04
69 
70 /* DB Error codes */
71 #define DB_OK		 0
72 #define DB_FAILED	 1
73 #define DB_NOPROC	 2
74 #define DB_MEMORY_ERR	-1
75 #define DB_PROTOCOL_ERR	-2
76 #define DB_EOF		-1
77 
78 /* dbColumn.sqlDataType */
79 #define DB_SQL_TYPE_UNKNOWN          0
80 
81 #define DB_SQL_TYPE_CHARACTER        1
82 #define DB_SQL_TYPE_SMALLINT         2
83 #define DB_SQL_TYPE_INTEGER          3
84 #define DB_SQL_TYPE_REAL             4
85 #define DB_SQL_TYPE_DOUBLE_PRECISION 6
86 #define DB_SQL_TYPE_DECIMAL          7
87 #define DB_SQL_TYPE_NUMERIC          8
88 #define DB_SQL_TYPE_DATE             9
89 #define DB_SQL_TYPE_TIME            10
90 #define DB_SQL_TYPE_TIMESTAMP       11
91 #define DB_SQL_TYPE_INTERVAL        12
92 #define DB_SQL_TYPE_TEXT            13	/* length not defined */
93 
94 #define DB_SQL_TYPE_SERIAL          21
95 
96 /* these are OR'ed (|) with the TIMESTAMP and INTERVAL type */
97 #define DB_YEAR              0x4000
98 #define DB_MONTH             0x2000
99 #define DB_DAY               0x1000
100 #define DB_HOUR              0x0800
101 #define DB_MINUTE            0x0400
102 #define DB_SECOND            0x0200
103 #define DB_FRACTION          0x0100
104 #define DB_DATETIME_MASK     0xFF00
105 
106 /* dbColumn.CDataType */
107 #define DB_C_TYPE_STRING          1
108 #define DB_C_TYPE_INT             2
109 #define DB_C_TYPE_DOUBLE          3
110 #define DB_C_TYPE_DATETIME        4
111 
112 /* fetch positions */
113 #define DB_CURRENT		1
114 #define DB_NEXT			2
115 #define DB_PREVIOUS		3
116 #define DB_FIRST		4
117 #define DB_LAST			5
118 
119 /* cursor modes/types */
120 #define DB_READONLY		1
121 #define DB_INSERT		2
122 #define DB_UPDATE		3
123 #define DB_SEQUENTIAL		0
124 #define DB_SCROLL		1
125 #define DB_INSENSITIVE		4
126 
127 /* privilege modes */
128 #define DB_GRANTED		1
129 #define DB_NOT_GRANTED		-1
130 
131 /* Privileges */
132 #define DB_PRIV_SELECT       0x01
133 
134 #define DB_GROUP             0x01
135 #define DB_PUBLIC            0x02
136 
137 /* default value modes */
138 #define DB_DEFINED	1
139 #define DB_UNDEFINED	2
140 
141 /* static buffer for SQL statements */
142 #define DB_SQL_MAX      8192
143 
144 typedef void *dbAddress;
145 typedef int dbToken;
146 
147 typedef struct _db_string
148 {
149     char *string;
150     int nalloc;
151 } dbString;
152 
153 typedef struct _dbmscap
154 {
155     char driverName[256];	/* symbolic name for the dbms system */
156     char startup[256];		/* command to run the driver */
157     char comment[256];		/* comment field             */
158     struct _dbmscap *next;	/* linked list               */
159 } dbDbmscap;
160 
161 typedef struct _db_dirent
162 {
163     dbString name;		/* file/dir name             */
164     int isdir;			/* bool: name is a directory */
165     int perm;			/* permissions               */
166 } dbDirent;
167 
168 typedef struct _db_driver
169 {
170     dbDbmscap dbmscap;		/* dbmscap entry for this driver */
171     FILE *send, *recv;		/* i/o to-from driver            */
172     int pid;			/* process id of the driver      */
173 } dbDriver;
174 
175 typedef struct _db_handle
176 {
177     dbString dbName;		/* database name               */
178     /* dbString dbPath; *//* directory containing dbName */
179     dbString dbSchema;		/* database schema */
180 } dbHandle;
181 
182 typedef struct _db_date_time
183 {
184     char current;
185     int year;
186     int month;
187     int day;
188     int hour;
189     int minute;
190     double seconds;
191 } dbDateTime;
192 
193 typedef struct _db_value
194 {
195     char isNull;
196     int i;
197     double d;
198     dbString s;
199     dbDateTime t;
200 } dbValue;
201 
202 typedef struct _db_column
203 {
204     dbString columnName;
205     dbString description;
206     int sqlDataType;
207     int hostDataType;
208     dbValue value;
209     int dataLen;
210     int precision;
211     int scale;
212     char nullAllowed;
213     char hasDefaultValue;
214     char useDefaultValue;
215     dbValue defaultValue;
216     int select;
217     int update;
218 } dbColumn;
219 
220 typedef struct _db_table
221 {
222     dbString tableName;
223     dbString description;
224     int numColumns;
225     dbColumn *columns;
226     int priv_insert;
227     int priv_delete;
228 } dbTable;
229 
230 typedef struct _db_cursor
231 {
232     dbToken token;
233     dbDriver *driver;
234     dbTable *table;
235     short *column_flags;
236     int type;
237     int mode;
238 } dbCursor;
239 
240 typedef struct _db_index
241 {
242     dbString indexName;
243     dbString tableName;
244     int numColumns;
245     dbString *columnNames;
246     char unique;
247 } dbIndex;
248 
249 typedef struct _db_driver_state
250 {
251     char *dbname;
252     char *dbschema;
253     int open;
254     int ncursors;
255     dbCursor **cursor_list;
256 } dbDriverState;
257 
258 /* category value (integer) */
259 typedef struct
260 {
261     int cat;			/* category */
262     int val;			/* value */
263 } dbCatValI;
264 
265 /* category value */
266 typedef struct
267 {
268     int cat;			/* category */
269     int isNull;
270     union
271     {
272 	int i;
273 	double d;
274 	/* s and t were added 22.8.2005, both are pointers,
275 	 * they so should not take more than 8 bytes.
276 	 * It would be better to add dbString, not pointer,
277 	 * But it could be > 8 bytes on some systems */
278 	dbString *s;
279 	dbDateTime *t;
280     } val;
281 } dbCatVal;
282 
283 /* category value array */
284 typedef struct
285 {
286     int n_values;
287     int alloc;
288     int ctype;			/* C type of values stored in array DB_C_TYPE_* */
289     dbCatVal *value;
290 } dbCatValArray;
291 
292 /* parameters of connection */
293 typedef struct _db_connection
294 {
295     char *driverName;
296     char *hostName;
297     char *databaseName;
298     char *schemaName;
299     char *port;
300     char *user;
301     char *password;
302     char *keycol;		/* name of default key column */
303     char *group;		/* default group to which select privilege is granted */
304 } dbConnection;
305 
306 /* reclass rule */
307 typedef struct
308 {
309     int count;			/* number of defined rules */
310     int alloc;			/* size of allocated array */
311     char *table;		/* table name */
312     char *key;			/* key column name */
313     int *cat;			/* array of new category numbers */
314     char **where;		/* array of SQL WHERE conditions */
315     char **label;		/* array of new category labels */
316 } dbRclsRule;
317 
318 #include <grass/defs/dbmi.h>
319 
320 #endif
321