summaryrefslogtreecommitdiff
path: root/navit/data/poi_geodownload/libmdb/backend.c
blob: 902805c3e99650ea70713dffaf1577e1fbf2e1cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* MDB Tools - A library for reading MS Access database files
 * Copyright (C) 2000 Brian Bruns
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
** functions to deal with different backend database engines
*/

#include "mdbtools.h"

#ifdef DMALLOC
#include "dmalloc.h"
#endif

static int is_init;
static GHashTable *mdb_backends;

   /*    Access data types */
static MdbBackendType mdb_access_types[] = {
		{"Unknown 0x00", 0,0,0 },
		{"Boolean", 0,0,0},
		{"Byte", 0,0,0},
		{"Integer", 0,0,0},
		{"Long Integer", 0,0,0},
		{"Currency", 0,0,0},
		{"Single", 0,0,0},
		{"Double", 0,0,0},
		{"DateTime (Short)", 0,0,1},
		{"Unknown 0x09", 0,0,0},
		{"Text", 1,0,1},
		{"OLE", 1,0,1},
		{"Memo/Hyperlink",1,0,1},
		{"Unknown 0x0d",0,0,0},
		{"Unknown 0x0e",0,0,0},
		{"Replication ID",0,0,0},
		{"Numeric",1,1,0}
};

/*    Oracle data types */
static MdbBackendType mdb_oracle_types[] = {
		{"Oracle_Unknown 0x00",0,0,0},
		{"NUMBER",1,0,0},
		{"NUMBER",1,0,0},
		{"NUMBER",1,0,0},
		{"NUMBER",1,0,0},
		{"NUMBER",1,0,0},
		{"FLOAT",0,0,0},
		{"FLOAT",0,0,0},
		{"DATE",0,0,0},
		{"Oracle_Unknown 0x09",0,0,0},
		{"VARCHAR2",1,0,1},
		{"BLOB",1,0,1},
		{"CLOB",1,0,1},
		{"Oracle_Unknown 0x0d",0,0,0},
		{"Oracle_Unknown 0x0e",0,0,0},
		{"NUMBER",1,0,0},
		{"NUMBER",1,0,0},
};

/*    Sybase/MSSQL data types */
static MdbBackendType mdb_sybase_types[] = {
		{"Sybase_Unknown 0x00",0,0,0},
		{"bit",0,0,0},
		{"char",1,0,1},
		{"smallint",0,0,0},
		{"int",0,0,0},
		{"money",0,0,0},
		{"real",0,0,0},
		{"float",0,0,0},
		{"smalldatetime",0,0,0},
		{"Sybase_Unknown 0x09",0,0,0},
		{"varchar",1,0,1},
		{"varbinary",1,0,1},
		{"text",1,0,1},
		{"Sybase_Unknown 0x0d",0,0,0},
		{"Sybase_Unknown 0x0e",0,0,0},
		{"Sybase_Replication ID",0,0,0},
		{"numeric",1,1,0},
};

/*    Postgres data types */
static MdbBackendType mdb_postgres_types[] = {
		{"Postgres_Unknown 0x00",0,0,0},
		{"Bool",0,0,0},
		{"Int2",0,0,0},
		{"Int4",0,0,0},
		{"Int8",0,0,0},
		{"Money",0,0,0},
		{"Float4",0,0,0},
		{"Float8",0,0,0},
		{"Timestamp",0,0,0},
		{"Postgres_Unknown 0x09",0,0,0},
		{"Char",1,0,1},
		{"Postgres_Unknown 0x0b",0,0,0},
		{"Postgres_Unknown 0x0c",0,0,0},
		{"Postgres_Unknown 0x0d",0,0,0},
		{"Postgres_Unknown 0x0e",0,0,0},
		{"Serial",0,0,0},
		{"Postgres_Unknown 0x10",0,0,0},
};
/*    MySQL data types */
static MdbBackendType mdb_mysql_types[] = {
		{"Text",1,0,1},
		{"char",0,0,0},
		{"int",0,0,0},
		{"int",0,0,0},
		{"int",0,0,0},
		{"float",0,0,0},
		{"float",0,0,0},
		{"float",0,0,0},
		{"date",0,0,1},
		{"varchar",1,0,1},
		{"varchar",1,0,1},
		{"varchar",1,0,1},
		{"text",1,0,1},
		{"blob",0,0,0},
		{"text",1,0,1},
		{"numeric",1,1,0},
		{"numeric",1,1,0},
};

static gboolean mdb_drop_backend(gpointer key, gpointer value, gpointer data);

char *mdb_get_coltype_string(MdbBackend *backend, int col_type)
{
	static char buf[16];

	if (col_type > 0x10 ) {
   		// return NULL;
		snprintf(buf,sizeof(buf), "type %04x", col_type);
		return buf;
	} else {
		return backend->types_table[col_type].name;
	}
}

int mdb_coltype_takes_length(MdbBackend *backend, int col_type)
{
	return backend->types_table[col_type].needs_length;
}

/**
 * mdb_init_backends
 *
 * Initializes the mdb_backends hash and loads the builtin backends.
 * Use mdb_remove_backends() to destroy this hash when done.
 */
void mdb_init_backends(void)
{
	mdb_backends = g_hash_table_new(g_str_hash, g_str_equal);

	mdb_register_backend(mdb_access_types, "access");
	mdb_register_backend(mdb_sybase_types, "sybase");
	mdb_register_backend(mdb_oracle_types, "oracle");
	mdb_register_backend(mdb_postgres_types, "postgres");
	mdb_register_backend(mdb_mysql_types, "mysql");
}
void mdb_register_backend(MdbBackendType *backend_type, char *backend_name)
{
	MdbBackend *backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
	backend->types_table = backend_type;
	g_hash_table_insert(mdb_backends, backend_name, backend);
}

/**
 * mdb_remove_backends
 *
 * Removes all entries from and destroys the mdb_backends hash.
 */
void mdb_remove_backends(void)
{
	g_hash_table_foreach_remove(mdb_backends, mdb_drop_backend, NULL);
	g_hash_table_destroy(mdb_backends);
}
static gboolean mdb_drop_backend(gpointer key, gpointer value, gpointer data)
{
	MdbBackend *backend = (MdbBackend *)value;
	g_free (backend);
	return TRUE;
}

/**
 * mdb_set_default_backend
 * @mdb: Handle to open MDB database file
 * @backend_name: Name of the backend to set as default
 *
 * Sets the default backend of the handle @mdb to @backend_name.
 *
 * Returns: 1 if successful, 0 if unsuccessful.
 */
int mdb_set_default_backend(MdbHandle *mdb, char *backend_name)
{
	MdbBackend *backend;

	backend = (MdbBackend *) g_hash_table_lookup(mdb_backends, backend_name);
	if (backend) {
		mdb->default_backend = backend;
		mdb->backend_name = (char *) g_strdup(backend_name);
		is_init = 0;
		return 1;
	} else {
		return 0;
	}
}

/**
 * mdb_get_relationships
 * @mdb: Handle to open MDB database file
 *
 * Generates relationships by reading the MSysRelationships table.
 *   'szColumn' contains the column name of the child table.
 *   'szObject' contains the table name of the child table.
 *   'szReferencedColumn' contains the column name of the parent table.
 *   'szReferencedObject' contains the table name of the parent table.
 *
 * Returns: a string stating that relationships are not supported for the
 *   selected backend, or a string containing SQL commands for setting up
 *   the relationship, tailored for the selected backend.  The caller is
 *   responsible for freeing this string.
 */
char *mdb_get_relationships(MdbHandle *mdb)
{
	unsigned int i;
	gchar *text = NULL;  /* String to be returned */
	static char *bound[4];  /* Bound values */
	static MdbTableDef *table;  /* Relationships table */
	int backend = 0;  /* Backends: 1=oracle */

	if (strncmp(mdb->backend_name,"oracle",6) == 0) {
		backend = 1;
	} else {
		if (is_init == 0) { /* the first time through */
			is_init = 1;
			return (char *) g_strconcat(
				"-- relationships are not supported for ",
				mdb->backend_name, NULL);
		} else { /* the second time through */
			is_init = 0;
			return NULL;
		}
	}

	if (is_init == 0) {
		table = mdb_read_table_by_name(mdb, "MSysRelationships", MDB_TABLE);
		if ((!table) || (table->num_rows == 0)) {
			return NULL;
		}

		mdb_read_columns(table);
		for (i=0;i<4;i++) {
			bound[i] = (char *) g_malloc0(MDB_BIND_SIZE);
		}
		mdb_bind_column_by_name(table, "szColumn", bound[0]);
		mdb_bind_column_by_name(table, "szObject", bound[1]);
		mdb_bind_column_by_name(table, "szReferencedColumn", bound[2]);
		mdb_bind_column_by_name(table, "szReferencedObject", bound[3]);
		mdb_rewind_table(table);

		is_init = 1;
	}
	else if (table->cur_row >= table->num_rows) {  /* past the last row */
		for (i=0;i<4;i++)
			g_free(bound[i]);
		is_init = 0;
		return NULL;
	}

	if (!mdb_fetch_row(table)) {
		for (i=0;i<4;i++)
			g_free(bound[i]);
		is_init = 0;
		return NULL;
	}

	switch (backend) {
	  case 1:  /* oracle */
		text = g_strconcat("alter table ", bound[1],
			" add constraint ", bound[3], "_", bound[1],
			" foreign key (", bound[0], ")"
			" references ", bound[3], "(", bound[2], ")", NULL);
		break;
	}

	return (char *)text;
}