Use 'Flags' field in MSysObjects to recognize system tables

This commit is contained in:
whydoubt 2004-09-16 04:00:39 +00:00
parent d5f91ebe63
commit dd99bd0a68
16 changed files with 109 additions and 138 deletions

View File

@ -1,3 +1,20 @@
Wed Sep 15 22:59:26 CDT 2004 Jeff Smith <whydoubt@yahoo.com>
* include/mdbtools.h:
* src/gmdb/sql.c:
* src/gmdb/table.c:
* src/gmdb2/schema.c:
* src/gmdb2/sql.c:
* src/gmdb2/table.c:
* src/libmdb/catalog.c:
* src/libmdb/table.c:
* src/odbc/odbc.c:
* src/sql/mdbsql.c:
* src/util/mdb-check.c:
* src/util/mdb-header.c:
* src/util/mdb-schema.c:
* src/util/mdb-tables.c:
* HACKING: Use 'Flags' field in MSysObjects to recognize system tables
Wed Sep 15 07:33:34 CDT 2004 Jeff Smith <whydoubt@yahoo.com> Wed Sep 15 07:33:34 CDT 2004 Jeff Smith <whydoubt@yahoo.com>
* src/odbc/Makefile.am: Fix a bug in ODBC Makefile * src/odbc/Makefile.am: Fix a bug in ODBC Makefile

View File

@ -23,7 +23,9 @@ structures.
Page - A fixed size region within the file on a 2 or 4K boundry. All Page - A fixed size region within the file on a 2 or 4K boundry. All
data in the file exists inside pages. data in the file exists inside pages.
Catalog Table - Tables in Access generally starting with "MSys". See the TDEF System Table - Tables in Access generally starting with "MSys". The 'Flags'
field in the table's Catalog Entry will contain a flag in one
of two positions (0x80000000 or 0x00000002). See also the TDEF
(table definition) pages for "System Table" field. (table definition) pages for "System Table" field.
Catalog Entry - A row from the MSysObjects table describing another database Catalog Entry - A row from the MSysObjects table describing another database
object. The MSysObjects table definition page is always at object. The MSysObjects table definition page is always at

View File

@ -240,6 +240,7 @@ typedef struct {
int num_props; int num_props;
GArray *props; GArray *props;
GArray *columns; GArray *columns;
int flags;
} MdbCatalogEntry; } MdbCatalogEntry;
typedef struct { typedef struct {
@ -428,7 +429,8 @@ extern guint16 read_pg_if_16(MdbHandle *mdb, int *cur_pos);
extern guint32 read_pg_if_32(MdbHandle *mdb, int *cur_pos); extern guint32 read_pg_if_32(MdbHandle *mdb, int *cur_pos);
extern int read_pg_if(MdbHandle *mdb, int *cur_pos, int offset); extern int read_pg_if(MdbHandle *mdb, int *cur_pos, int offset);
extern guint16 read_pg_if_n(MdbHandle *mdb, unsigned char *buf, int *cur_pos, int len); extern guint16 read_pg_if_n(MdbHandle *mdb, unsigned char *buf, int *cur_pos, int len);
extern int mdb_is_user_table(MdbCatalogEntry *entry);
extern int mdb_is_system_table(MdbCatalogEntry *entry);
/* data.c */ /* data.c */
extern int mdb_bind_column_by_name(MdbTableDef *table, gchar *col_name, void *bind_ptr); extern int mdb_bind_column_by_name(MdbTableDef *table, gchar *col_name, void *bind_ptr);

View File

@ -264,20 +264,14 @@ int i;
MdbCatalogEntry *entry; MdbCatalogEntry *entry;
gchar *text[2]; gchar *text[2];
/* loop over each entry in the catalog */ /* add all user tables in catalog to tab */
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
if (mdb_is_user_table(entry)) {
/* if it's a table */
if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if (strncmp (entry->object_name, "MSys", 4)) {
/* add table to tab */
text[0] = entry->object_name; text[0] = entry->object_name;
text[1] = ""; text[1] = "";
gtk_ctree_insert_node(GTK_CTREE(sqlwin->ctree), NULL, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, FALSE); gtk_ctree_insert_node(GTK_CTREE(sqlwin->ctree), NULL, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, FALSE);
} }
} /* if MDB_TABLE */
} /* for */ } /* for */
} }
#else #else

View File

@ -66,8 +66,7 @@ MdbCatalogEntry *entry;
selected_child = child_num; selected_child = child_num;
for (i=0;i<mdb->num_catalog;i++) { for (i=0;i<mdb->num_catalog;i++) {
entry = g_ptr_array_index(mdb->catalog,i); entry = g_ptr_array_index(mdb->catalog,i);
if (entry->object_type==MDB_TABLE && if (mdb_is_user_table(entry)) {
strncmp(entry->object_name,"MSys",4)) {
if (j==child_num) { if (j==child_num) {
selected_table = i; selected_table = i;
} }
@ -192,17 +191,11 @@ void gmdb_table_populate(MdbHandle *mdb)
int i; int i;
MdbCatalogEntry *entry; MdbCatalogEntry *entry;
/* loop over each entry in the catalog */ /* add all user tables in catalog to tab */
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
if (mdb_is_user_table(entry)) {
/* if it's a table */
if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if (strncmp (entry->object_name, "MSys", 4)) {
/* add table to tab */
gmdb_table_add_icon(entry->object_name); gmdb_table_add_icon(entry->object_name);
} }
} /* if MDB_TABLE */
} /* for */ } /* for */
} }

View File

@ -58,16 +58,15 @@ char *the_relation;
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
/* if it's a table */ if (entry->object_type != MDB_TABLE)
continue;
/* Do not show system tables if table name is not specified */
if (mdb_is_system_table(entry) && !strlen(tabname))
continue;
/* If object name does not match the table specified */
if (strlen(tabname) && strcmp(entry->object_name, tabname))
continue;
if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if ((strlen(tabname) && !strcmp(entry->object_name,tabname)) ||
(!strlen(tabname) && strncmp (entry->object_name, "MSys", 4))) {
/* make sure it's a table (may be redundant) */
if (!strcmp (mdb_get_objtype_string (entry->object_type), "Table")) {
/* drop the table if it exists */ /* drop the table if it exists */
if (drops=='Y') if (drops=='Y')
fprintf(outfile, "DROP TABLE %s;\n", entry->object_name); fprintf(outfile, "DROP TABLE %s;\n", entry->object_name);
@ -101,9 +100,6 @@ char *the_relation;
fprintf (outfile, "\n);\n"); fprintf (outfile, "\n);\n");
fprintf (outfile, "-- CREATE ANY INDEXES ...\n"); fprintf (outfile, "-- CREATE ANY INDEXES ...\n");
fprintf (outfile, "\n"); fprintf (outfile, "\n");
}
}
}
} }
fprintf (outfile, "\n\n"); fprintf (outfile, "\n\n");
@ -183,18 +179,12 @@ gmdb_schema_new_cb(GtkWidget *w, gpointer data)
combo = glade_xml_get_widget (schemawin_xml, "table_combo"); combo = glade_xml_get_widget (schemawin_xml, "table_combo");
glist = g_list_append(glist, ALL_TABLES); glist = g_list_append(glist, ALL_TABLES);
/* loop over each entry in the catalog */ /* add all user tables in catalog to list */
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
if (mdb_is_user_table(entry)) {
/* if it's a table */
if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if (strncmp (entry->object_name, "MSys", 4)) {
/* add table to list */
glist = g_list_append(glist, entry->object_name); glist = g_list_append(glist, entry->object_name);
} }
} /* if MDB_TABLE */
} /* for */ } /* for */
gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist); gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
g_list_free(glist); g_list_free(glist);

View File

@ -693,20 +693,14 @@ GtkTreeIter *iter2;
GtkWidget *tree = glade_xml_get_widget(xml, "sql_treeview"); GtkWidget *tree = glade_xml_get_widget(xml, "sql_treeview");
GtkTreeStore *store = (GtkTreeStore *) gtk_tree_view_get_model(GTK_TREE_VIEW(tree)); GtkTreeStore *store = (GtkTreeStore *) gtk_tree_view_get_model(GTK_TREE_VIEW(tree));
/* loop over each entry in the catalog */ /* add all user tables in catalog to tab */
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
if (mdb_is_user_table(entry)) {
/* if it's a table */
if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if (strncmp (entry->object_name, "MSys", 4)) {
/* add table to tab */
iter2 = g_malloc(sizeof(GtkTreeIter)); iter2 = g_malloc(sizeof(GtkTreeIter));
gtk_tree_store_append(store, iter2, NULL); gtk_tree_store_append(store, iter2, NULL);
gtk_tree_store_set(store, iter2, 0, entry->object_name, -1); gtk_tree_store_set(store, iter2, 0, entry->object_name, -1);
} }
} /* if MDB_TABLE */
} /* for */ } /* for */
} }
#else #else

View File

@ -215,17 +215,11 @@ void gmdb_table_populate(MdbHandle *mdb)
int i; int i;
MdbCatalogEntry *entry; MdbCatalogEntry *entry;
/* loop over each entry in the catalog */ /* add all user tables in catalog to tab */
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
if (mdb_is_user_table(entry)) {
/* if it's a table */
if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if (strncmp (entry->object_name, "MSys", 4)) {
/* add table to tab */
gmdb_table_add_icon(entry->object_name); gmdb_table_add_icon(entry->object_name);
} }
} /* if MDB_TABLE */
} /* for */ } /* for */
} }

View File

@ -65,6 +65,7 @@ GPtrArray *mdb_read_catalog (MdbHandle *mdb, int objtype)
char obj_id[256]; char obj_id[256];
char obj_name[256]; char obj_name[256];
char obj_type[256]; char obj_type[256];
char obj_flags[256];
int type; int type;
if (mdb->catalog) mdb_free_catalog(mdb); if (mdb->catalog) mdb_free_catalog(mdb);
@ -88,6 +89,7 @@ GPtrArray *mdb_read_catalog (MdbHandle *mdb, int objtype)
mdb_bind_column_by_name(table, "Id", obj_id); mdb_bind_column_by_name(table, "Id", obj_id);
mdb_bind_column_by_name(table, "Name", obj_name); mdb_bind_column_by_name(table, "Name", obj_name);
mdb_bind_column_by_name(table, "Type", obj_type); mdb_bind_column_by_name(table, "Type", obj_type);
mdb_bind_column_by_name(table, "Flags", obj_flags);
mdb_rewind_table(table); mdb_rewind_table(table);
@ -101,6 +103,7 @@ GPtrArray *mdb_read_catalog (MdbHandle *mdb, int objtype)
strcpy(entry->object_name, obj_name); strcpy(entry->object_name, obj_name);
entry->object_type = (type & 0x7F); entry->object_type = (type & 0x7F);
entry->table_pg = atol(obj_id) & 0x00FFFFFF; entry->table_pg = atol(obj_id) & 0x00FFFFFF;
entry->flags = atol(obj_flags);
mdb->num_catalog++; mdb->num_catalog++;
g_ptr_array_add(mdb->catalog, entry); g_ptr_array_add(mdb->catalog, entry);
} }

View File

@ -355,3 +355,14 @@ guint32 pgnum;
printf("\n"); printf("\n");
} }
} }
int mdb_is_user_table(MdbCatalogEntry *entry)
{
return ((entry->object_type == MDB_TABLE)
&& !(entry->flags & 0x80000002)) ? 1 : 0;
}
int mdb_is_system_table(MdbCatalogEntry *entry)
{
return ((entry->object_type == MDB_TABLE)
&& (entry->flags & 0x80000002)) ? 1 : 0;
}

View File

@ -32,7 +32,7 @@
#include "connectparams.h" #include "connectparams.h"
static char software_version[] = "$Id: odbc.c,v 1.21 2004/09/14 00:13:20 whydoubt Exp $"; static char software_version[] = "$Id: odbc.c,v 1.22 2004/09/16 04:00:45 whydoubt Exp $";
static void *no_unused_var_warn[] = {software_version, static void *no_unused_var_warn[] = {software_version,
no_unused_var_warn}; no_unused_var_warn};
@ -153,7 +153,7 @@ SQLRETURN SQL_API SQLDriverConnect(
ConnectParams* params; ConnectParams* params;
SQLRETURN ret; SQLRETURN ret;
TRACE("DriverConnect"); TRACE("SQLDriverConnect");
strcpy (lastError, ""); strcpy (lastError, "");
@ -191,7 +191,7 @@ SQLRETURN SQL_API SQLBrowseConnect(
SQLSMALLINT cbConnStrOutMax, SQLSMALLINT cbConnStrOutMax,
SQLSMALLINT FAR *pcbConnStrOut) SQLSMALLINT FAR *pcbConnStrOut)
{ {
TRACE("BrowseConnect"); TRACE("SQLBrowseConnect");
return SQL_SUCCESS; return SQL_SUCCESS;
} }
@ -1542,15 +1542,17 @@ SQLRETURN SQL_API SQLTables(
mdb_sql_add_temp_col(sql, ttable, 4, "REMARKS", MDB_TEXT, 254, 0); mdb_sql_add_temp_col(sql, ttable, 4, "REMARKS", MDB_TEXT, 254, 0);
mdb_temp_columns_end(ttable); mdb_temp_columns_end(ttable);
/* TODO: Sort the return list by TYPE, CAT, SCHEM, NAME */
for (i=0; i<mdb->num_catalog; i++) { for (i=0; i<mdb->num_catalog; i++) {
entry = g_ptr_array_index(mdb->catalog, i); entry = g_ptr_array_index(mdb->catalog, i);
switch (entry->object_type) {
case MDB_TABLE: ttype = 1; break; if (mdb_is_user_table(entry))
case MDB_SYSTEM_TABLE: ttype = 2; break; ttype = 0;
case MDB_QUERY: ttype = 3; break; else if (mdb_is_system_table(entry))
default: ttype = 0; break; ttype = 1;
} else if (entry->object_type == MDB_QUERY)
if (!ttype) ttype = 2;
else
continue; continue;
/* Set all fields to NULL */ /* Set all fields to NULL */
@ -1559,7 +1561,7 @@ SQLRETURN SQL_API SQLTables(
} }
ts2 = mdb_ascii2unicode(mdb, entry->object_name, 0, 100, t2); ts2 = mdb_ascii2unicode(mdb, entry->object_name, 0, 100, t2);
ts3 = mdb_ascii2unicode(mdb, table_types[ttype-1], 0, 100, t3); ts3 = mdb_ascii2unicode(mdb, table_types[ttype], 0, 100, t3);
FILL_FIELD(&fields[2], t2, ts2); FILL_FIELD(&fields[2], t2, ts2);
FILL_FIELD(&fields[3], t3, ts3); FILL_FIELD(&fields[3], t3, ts3);

View File

@ -506,12 +506,10 @@ void mdb_sql_listtables(MdbSQL *sql)
ttable = mdb_create_temp_table(mdb, "#listtables"); ttable = mdb_create_temp_table(mdb, "#listtables");
mdb_sql_add_temp_col(sql, ttable, 0, "Tables", MDB_TEXT, 30, 0); mdb_sql_add_temp_col(sql, ttable, 0, "Tables", MDB_TEXT, 30, 0);
/* loop over each entry in the catalog */ /* add all user tables in catalog to list */
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
/* only list user tables */ if (mdb_is_user_table(entry)) {
if ((entry->object_type == MDB_TABLE)
&& (strncmp (entry->object_name, "MSys", 4))) {
//col = g_ptr_array_index(table->columns,0); //col = g_ptr_array_index(table->columns,0);
tmpsiz = mdb_ascii2unicode(mdb, entry->object_name, 0, 100, tmpstr); tmpsiz = mdb_ascii2unicode(mdb, entry->object_name, 0, 100, tmpstr);
mdb_fill_temp_field(&fields[0],tmpstr, tmpsiz, 0,0,0,0); mdb_fill_temp_field(&fields[0],tmpstr, tmpsiz, 0,0,0,0);

View File

@ -79,10 +79,8 @@ int opt;
/* if it's a table */ /* if it's a table */
if (entry->object_type == MDB_TABLE) { if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if ((tabname && !strcmp(entry->object_name,tabname)) || if ((tabname && !strcmp(entry->object_name,tabname)) ||
(!tabname )) { (!tabname /* && mdb_is_user_table(entry) */)) {
// && strncmp (entry->object_name, "MSys", 4))) {
int ret; int ret;
table = mdb_read_table(entry); table = mdb_read_table(entry);

View File

@ -80,18 +80,8 @@ FILE *cfile;
{ {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
/* if it's a table */ if (!mdb_is_user_table(entry))
continue;
if (entry->object_type == MDB_TABLE)
{
/* skip the MSys tables */
if (strncmp (entry->object_name, "MSys", 4))
{
/* make sure it's a table (may be redundant) */
if (!strcmp (mdb_get_objtype_string (entry->object_type), "Table"))
{
fprintf (typesfile, "typedef struct _%s\n", entry->object_name); fprintf (typesfile, "typedef struct _%s\n", entry->object_name);
fprintf (typesfile, "{\n"); fprintf (typesfile, "{\n");
@ -148,9 +138,6 @@ FILE *cfile;
mdb_free_tabledef(table); mdb_free_tabledef(table);
} }
}
}
}
fclose (typesfile); fclose (typesfile);
fclose (cfile); fclose (cfile);

View File

@ -61,9 +61,6 @@ main (int argc, char **argv)
break; break;
} }
} }
if (!namespace) {
namespace = (char *) g_strdup("");
}
mdb_init(); mdb_init();
@ -94,18 +91,11 @@ main (int argc, char **argv)
fprintf(stdout,"-- Check out http://mdbtools.sourceforge.net\n"); fprintf(stdout,"-- Check out http://mdbtools.sourceforge.net\n");
fprintf(stdout,"-------------------------------------------------------------\n\n"); fprintf(stdout,"-------------------------------------------------------------\n\n");
/* loop over each entry in the catalog */
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
/* if it's a table */
if (entry->object_type == MDB_TABLE) { if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */ if ((tabname && !strcmp(entry->object_name, tabname))
if ((tabname && !strcmp(entry->object_name,tabname)) || || (!tabname && mdb_is_user_table(entry))) {
(!tabname && strncmp (entry->object_name, "MSys", 4))) {
generate_table_schema(entry, namespace, s); generate_table_schema(entry, namespace, s);
} }
} }
@ -134,16 +124,13 @@ generate_table_schema(MdbCatalogEntry *entry, char *namespace, int sanitize)
unsigned int i; unsigned int i;
MdbColumn *col; MdbColumn *col;
/* make sure it's a table (may be redundant) */
if (strcmp (mdb_get_objtype_string (entry->object_type), "Table"))
return;
/* drop the table if it exists */ /* drop the table if it exists */
fprintf (stdout, "DROP TABLE %s%s;\n", namespace, sanitize_name(entry->object_name,sanitize)); fprintf (stdout, "DROP TABLE %s%s;\n", (namespace) ? namespace : "",
sanitize_name(entry->object_name, sanitize));
/* create the table */ /* create the table */
fprintf (stdout, "CREATE TABLE %s%s\n", namespace, sanitize_name(entry->object_name,sanitize)); fprintf (stdout, "CREATE TABLE %s%s\n", (namespace) ? namespace : "",
sanitize_name(entry->object_name, sanitize));
fprintf (stdout, " (\n"); fprintf (stdout, " (\n");
table = mdb_read_table (entry); table = mdb_read_table (entry);

View File

@ -136,10 +136,11 @@ main (int argc, char **argv)
for (i=0; i < mdb->num_catalog; i++) { for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i); entry = g_ptr_array_index (mdb->catalog, i);
/* if it's a table */ if (entry->object_type != objtype)
if (entry->object_type == objtype) { continue;
/* skip the MSys tables */ if (skip_sys && mdb_is_system_table(entry))
if (!skip_sys || strncmp (entry->object_name, "MSys", 4)) { continue;
if (line_break) if (line_break)
fprintf (stdout, "%s\n", entry->object_name); fprintf (stdout, "%s\n", entry->object_name);
else if (delimiter) else if (delimiter)
@ -147,8 +148,6 @@ main (int argc, char **argv)
else else
fprintf (stdout, "%s ", entry->object_name); fprintf (stdout, "%s ", entry->object_name);
} }
}
}
if (!line_break) if (!line_break)
fprintf (stdout, "\n"); fprintf (stdout, "\n");