mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-04-05 20:31:00 +08:00
Major revisions to 'backend' code
This commit is contained in:
parent
8e5b7ee293
commit
402e6be097
@ -1,3 +1,10 @@
|
||||
Fri Jun 11 08:52:14 CDT 2004 Jeff Smith <whydoubt@yahoo.com>
|
||||
* src/libmdb/data.c:
|
||||
* src/libmdb/backend.c:
|
||||
* src/gmdb2/schema.c:
|
||||
* src/util/mdb-schema.c:
|
||||
* include/mdbtools.h: Major revisions to 'backend' code
|
||||
|
||||
Wed Jun 2 07:31:04 CDT 2004 Jeff Smith <whydoubt@yahoo.com>
|
||||
* src/libmdb/file.c: Improve mdb_open's error handling
|
||||
|
||||
|
@ -448,7 +448,7 @@ extern void buffer_dump(const unsigned char* buf, int start, int end);
|
||||
extern char *mdb_get_coltype_string(MdbBackend *backend, int col_type);
|
||||
extern int mdb_coltype_takes_length(MdbBackend *backend, int col_type);
|
||||
extern void mdb_init_backends();
|
||||
extern void mdb_register_backend(MdbBackend *backend, char *backend_name);
|
||||
extern void mdb_register_backend(MdbBackendType *backend, char *backend_name);
|
||||
extern void mdb_remove_backends();
|
||||
extern int mdb_set_default_backend(MdbHandle *mdb, char *backend_name);
|
||||
extern char *mdb_get_relationships(MdbHandle *mdb);
|
||||
|
@ -110,10 +110,9 @@ char *the_relation;
|
||||
if (relation=='Y') {
|
||||
fprintf (outfile, "-- CREATE ANY Relationships ...\n");
|
||||
fprintf (outfile, "\n");
|
||||
the_relation=mdb_get_relationships(mdb);
|
||||
while (the_relation[0] != '\0') {
|
||||
while ((the_relation=mdb_get_relationships(mdb)) != NULL) {
|
||||
fprintf(outfile,"%s\n",the_relation);
|
||||
the_relation=mdb_get_relationships(mdb);
|
||||
g_free(the_relation);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,10 +27,11 @@
|
||||
#include "dmalloc.h"
|
||||
#endif
|
||||
|
||||
GHashTable *mdb_backends;
|
||||
static int is_init;
|
||||
static GHashTable *mdb_backends;
|
||||
|
||||
/* Access data types */
|
||||
MdbBackendType mdb_access_types[] = {
|
||||
static MdbBackendType mdb_access_types[] = {
|
||||
{"Unknown 0x00", 0,0,0 },
|
||||
{"Boolean", 0,0,0},
|
||||
{"Byte", 0,0,0},
|
||||
@ -46,13 +47,13 @@ MdbBackendType mdb_access_types[] = {
|
||||
{"Memo/Hyperlink",1,0,1},
|
||||
{"Unknown 0x0d",0,0,0},
|
||||
{"Unknown 0x0e",0,0,0},
|
||||
{"Replication ID",0,0,0},
|
||||
{"Replication ID",0,0,0},
|
||||
{"Numeric",1,1,0}
|
||||
};
|
||||
|
||||
/* Oracle data types */
|
||||
MdbBackendType mdb_oracle_types[] = {
|
||||
{"Oracle_Unknown 0x00",0,0,0},
|
||||
static MdbBackendType mdb_oracle_types[] = {
|
||||
{"Oracle_Unknown 0x00",0,0,0},
|
||||
{"NUMBER",1,0,0},
|
||||
{"NUMBER",1,0,0},
|
||||
{"NUMBER",1,0,0},
|
||||
@ -67,13 +68,13 @@ MdbBackendType mdb_oracle_types[] = {
|
||||
{"CLOB",1,0,1},
|
||||
{"Oracle_Unknown 0x0d",0,0,0},
|
||||
{"Oracle_Unknown 0x0e",0,0,0},
|
||||
{"NUMBER",1,0,0},
|
||||
{"NUMBER",1,0,0},
|
||||
{"NUMBER",1,0,0},
|
||||
{"NUMBER",1,0,0},
|
||||
};
|
||||
|
||||
/* Sybase/MSSQL data types */
|
||||
MdbBackendType mdb_sybase_types[] = {
|
||||
{"Sybase_Unknown 0x00",0,0,0},
|
||||
static MdbBackendType mdb_sybase_types[] = {
|
||||
{"Sybase_Unknown 0x00",0,0,0},
|
||||
{"bit",0,0,0},
|
||||
{"char",1,0,1},
|
||||
{"smallint",0,0,0},
|
||||
@ -88,13 +89,13 @@ MdbBackendType mdb_sybase_types[] = {
|
||||
{"text",1,0,1},
|
||||
{"Sybase_Unknown 0x0d",0,0,0},
|
||||
{"Sybase_Unknown 0x0e",0,0,0},
|
||||
{"Sybase_Replication ID",0,0,0},
|
||||
{"Sybase_Replication ID",0,0,0},
|
||||
{"numeric",1,1,0},
|
||||
};
|
||||
|
||||
/* Postgres data types */
|
||||
MdbBackendType mdb_postgres_types[] = {
|
||||
{"Postgres_Unknown 0x00",0,0,0},
|
||||
static MdbBackendType mdb_postgres_types[] = {
|
||||
{"Postgres_Unknown 0x00",0,0,0},
|
||||
{"Bool",0,0,0},
|
||||
{"Int2",0,0,0},
|
||||
{"Int4",0,0,0},
|
||||
@ -113,8 +114,8 @@ MdbBackendType mdb_postgres_types[] = {
|
||||
{"Postgres_Unknown 0x10",0,0,0},
|
||||
};
|
||||
/* MySQL data types */
|
||||
MdbBackendType mdb_mysql_types[] = {
|
||||
{"Text",1,0,1},
|
||||
static MdbBackendType mdb_mysql_types[] = {
|
||||
{"Text",1,0,1},
|
||||
{"char",0,0,0},
|
||||
{"int",0,0,0},
|
||||
{"int",0,0,0},
|
||||
@ -129,16 +130,10 @@ MdbBackendType mdb_mysql_types[] = {
|
||||
{"text",1,0,1},
|
||||
{"blob",0,0,0},
|
||||
{"text",1,0,1},
|
||||
{"numeric",1,1,0},
|
||||
{"numeric",1,1,0},
|
||||
{"numeric",1,1,0},
|
||||
};
|
||||
|
||||
char *bound_values[MDB_MAX_COLS];
|
||||
char *relationships[4];
|
||||
MdbColumn *col;
|
||||
MdbCatalogEntry *entry;
|
||||
MdbTableDef *table;
|
||||
int did_first;
|
||||
|
||||
char *mdb_get_coltype_string(MdbBackend *backend, int col_type)
|
||||
{
|
||||
@ -155,7 +150,7 @@ char *mdb_get_coltype_string(MdbBackend *backend, int col_type)
|
||||
|
||||
int mdb_coltype_takes_length(MdbBackend *backend, int col_type)
|
||||
{
|
||||
return backend->types_table[col_type].needs_length;
|
||||
return backend->types_table[col_type].needs_length;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -168,30 +163,19 @@ void mdb_init_backends()
|
||||
|
||||
mdb_backends = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
|
||||
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
|
||||
backend->types_table = mdb_access_types;
|
||||
mdb_register_backend(backend, "access");
|
||||
|
||||
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
|
||||
backend->types_table = mdb_sybase_types;
|
||||
mdb_register_backend(backend, "sybase");
|
||||
|
||||
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
|
||||
backend->types_table = mdb_oracle_types;
|
||||
mdb_register_backend(backend, "oracle");
|
||||
|
||||
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
|
||||
backend->types_table = mdb_postgres_types;
|
||||
mdb_register_backend(backend, "postgres");
|
||||
|
||||
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
|
||||
backend->types_table = mdb_mysql_types;
|
||||
mdb_register_backend(backend, "mysql");
|
||||
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(MdbBackend *backend, char *backend_name)
|
||||
void mdb_register_backend(MdbBackendType *backend_type, char *backend_name)
|
||||
{
|
||||
g_hash_table_insert(mdb_backends,backend_name,backend);
|
||||
MdbBackend *backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
|
||||
backend->types_table = backend_type;
|
||||
g_hash_table_insert(mdb_backends, backend_name, backend);
|
||||
}
|
||||
|
||||
static gboolean mdb_drop_backend(gpointer key, gpointer value, gpointer data)
|
||||
{
|
||||
MdbBackend *backend = (MdbBackend *)value;
|
||||
@ -211,99 +195,98 @@ int mdb_set_default_backend(MdbHandle *mdb, char *backend_name)
|
||||
if (backend) {
|
||||
mdb->default_backend = backend;
|
||||
mdb->backend_name = (char *) g_strdup(backend_name);
|
||||
did_first = 0;
|
||||
is_init = 0;
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
do_first (MdbHandle *mdb)
|
||||
{
|
||||
int i, k;
|
||||
|
||||
mdb_read_catalog (mdb, MDB_TABLE);
|
||||
|
||||
/* loop over each entry in the catalog */
|
||||
for (i=0; i < mdb->num_catalog; i++) {
|
||||
entry = g_ptr_array_index (mdb->catalog, i);
|
||||
if ((entry->object_type == MDB_TABLE) &&
|
||||
(strncmp (entry->object_name, "MSysRelationships", 17) == 0))
|
||||
{
|
||||
table = mdb_read_table (entry);
|
||||
if ( table->num_rows > 0 ) {
|
||||
mdb_read_columns(table);
|
||||
mdb_rewind_table(table);
|
||||
for (k=0;k<table->num_cols;k++) {
|
||||
bound_values[k] = (char *) g_malloc(MDB_BIND_SIZE);
|
||||
bound_values[k][0] = '\0';
|
||||
mdb_bind_column(table,k+1,bound_values[k]);
|
||||
}
|
||||
relationships[0] = (char *) g_malloc(256); /* child column */
|
||||
relationships[1] = (char *) g_malloc(256); /* child table */
|
||||
relationships[2] = (char *) g_malloc(256); /* parent column */
|
||||
relationships[3] = (char *) g_malloc(256); /* parent table */
|
||||
} /* if num_rows > 0 */
|
||||
did_first = 1;
|
||||
return;
|
||||
} /* if MSysRelationships */
|
||||
} /* for */
|
||||
}
|
||||
|
||||
char *mdb_get_relationships(MdbHandle *mdb) {
|
||||
|
||||
int k;
|
||||
static char text[255];
|
||||
|
||||
/*
|
||||
* generate relationships by "reading" the MSysRelationships table
|
||||
* generate 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
|
||||
*/
|
||||
sprintf(text,"%c",0);
|
||||
if ( did_first == 0)
|
||||
do_first(mdb);
|
||||
if (table->cur_row < table->num_rows) {
|
||||
if (mdb_fetch_row(table)) {
|
||||
relationships[0][0] = '\0';
|
||||
relationships[1][0] = '\0';
|
||||
relationships[2][0] = '\0';
|
||||
relationships[3][0] = '\0';
|
||||
for (k=0;k<table->num_cols;k++) {
|
||||
col=g_ptr_array_index(table->columns,k);
|
||||
if (strncmp(col->name,"szColumn",8) == 0)
|
||||
strcpy(relationships[0],bound_values[k]);
|
||||
else if (strncmp(col->name,"szObject",8) == 0)
|
||||
strcpy(relationships[1],bound_values[k]);
|
||||
else if (strncmp(col->name,"szReferencedColumn",18) == 0)
|
||||
strcpy(relationships[2],bound_values[k]);
|
||||
else if (strncmp(col->name,"szReferencedObject",18) == 0)
|
||||
strcpy(relationships[3],bound_values[k]);
|
||||
}
|
||||
if (strncmp(mdb->backend_name,"oracle",6) == 0) {
|
||||
sprintf(text,"alter table %s add constraint %s_%s foreign key (%s) \
|
||||
references %s(%s)",
|
||||
relationships[1],relationships[3],relationships[1],
|
||||
relationships[0],relationships[3],relationships[2]);
|
||||
} else {
|
||||
sprintf(text,"-- relationships are not supported for %s",
|
||||
mdb->backend_name);
|
||||
} /* else */
|
||||
} /* got a row */
|
||||
} else {
|
||||
for (k=0;k<table->num_cols;k++) {
|
||||
g_free(bound_values[k]);
|
||||
}
|
||||
g_free(relationships[0]);
|
||||
g_free(relationships[1]);
|
||||
g_free(relationships[2]);
|
||||
g_free(relationships[3]);
|
||||
did_first = 0;
|
||||
}
|
||||
return text;
|
||||
char *mdb_get_relationships(MdbHandle *mdb)
|
||||
{
|
||||
unsigned int i;
|
||||
gchar *text; /* 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) {
|
||||
MdbCatalogEntry *entry;
|
||||
|
||||
mdb_read_catalog (mdb, MDB_TABLE);
|
||||
|
||||
/* loop over each entry in the catalog */
|
||||
for (i=0; i < mdb->num_catalog; i++) {
|
||||
entry = g_ptr_array_index (mdb->catalog, i);
|
||||
if ((entry->object_type == MDB_TABLE) &&
|
||||
(strncmp (entry->object_name, "MSysRelationships", 17) == 0))
|
||||
break;
|
||||
}
|
||||
if (i == mdb->num_catalog) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
table = mdb_read_table (entry);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -59,12 +59,13 @@ mdb_bind_column_by_name(MdbTableDef *table, gchar *col_name, void *bind_ptr)
|
||||
|
||||
for (i=0;i<table->num_cols;i++) {
|
||||
col=g_ptr_array_index(table->columns,i);
|
||||
if (!strcmp(col->name,col_name))
|
||||
col_num = col->col_num + 1;
|
||||
if (!strcmp(col->name,col_name)) {
|
||||
col_num = col->col_num + 1;
|
||||
mdb_bind_column(table, col_num, bind_ptr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mdb_bind_column(table, col_num, bind_ptr);
|
||||
|
||||
return col_num;
|
||||
}
|
||||
void mdb_bind_len(MdbTableDef *table, int col_num, int *len_ptr)
|
||||
|
@ -114,10 +114,9 @@ main (int argc, char **argv)
|
||||
fprintf (stdout, "\n\n");
|
||||
fprintf (stdout, "-- CREATE ANY Relationships ...\n");
|
||||
fprintf (stdout, "\n");
|
||||
the_relation=mdb_get_relationships(mdb);
|
||||
while (the_relation[0] != '\0') {
|
||||
while ((the_relation=mdb_get_relationships(mdb)) != NULL) {
|
||||
fprintf(stdout,"%s\n",the_relation);
|
||||
the_relation=mdb_get_relationships(mdb);
|
||||
g_free(the_relation);
|
||||
}
|
||||
|
||||
g_free(namespace);
|
||||
|
Loading…
Reference in New Issue
Block a user