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>
* src/odbc/Makefile.am: Fix a bug in ODBC Makefile

View File

@ -23,8 +23,10 @@ structures.
Page - A fixed size region within the file on a 2 or 4K boundry. All
data in the file exists inside pages.
Catalog Table - Tables in Access generally starting with "MSys". See the TDEF
(table definition) pages for "System Table" field.
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.
Catalog Entry - A row from the MSysObjects table describing another database
object. The MSysObjects table definition page is always at
page 2 of the database, and a phony tdef structure is

View File

@ -240,6 +240,7 @@ typedef struct {
int num_props;
GArray *props;
GArray *columns;
int flags;
} MdbCatalogEntry;
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 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 int mdb_is_user_table(MdbCatalogEntry *entry);
extern int mdb_is_system_table(MdbCatalogEntry *entry);
/* data.c */
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;
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++) {
entry = g_ptr_array_index (mdb->catalog, i);
/* 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[1] = "";
gtk_ctree_insert_node(GTK_CTREE(sqlwin->ctree), NULL, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, FALSE);
}
} /* if MDB_TABLE */
if (mdb_is_user_table(entry)) {
text[0] = entry->object_name;
text[1] = "";
gtk_ctree_insert_node(GTK_CTREE(sqlwin->ctree), NULL, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, FALSE);
}
} /* for */
}
#else

View File

@ -66,8 +66,7 @@ MdbCatalogEntry *entry;
selected_child = child_num;
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,"MSys",4)) {
if (mdb_is_user_table(entry)) {
if (j==child_num) {
selected_table = i;
}
@ -192,17 +191,11 @@ void gmdb_table_populate(MdbHandle *mdb)
int i;
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++) {
entry = g_ptr_array_index (mdb->catalog, i);
/* 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);
}
} /* if MDB_TABLE */
if (mdb_is_user_table(entry)) {
gmdb_table_add_icon(entry->object_name);
}
} /* for */
}

View File

@ -56,18 +56,17 @@ char *the_relation;
mdb_set_default_backend(mdb,backend);
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 */
if (drops=='Y')
fprintf(outfile, "DROP TABLE %s;\n", entry->object_name);
@ -101,10 +100,7 @@ char *the_relation;
fprintf (outfile, "\n);\n");
fprintf (outfile, "-- CREATE ANY INDEXES ...\n");
fprintf (outfile, "\n");
}
}
}
}
}
fprintf (outfile, "\n\n");
if (relation=='Y') {
@ -183,18 +179,12 @@ gmdb_schema_new_cb(GtkWidget *w, gpointer data)
combo = glade_xml_get_widget (schemawin_xml, "table_combo");
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++) {
entry = g_ptr_array_index (mdb->catalog, i);
/* 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);
}
} /* if MDB_TABLE */
if (mdb_is_user_table(entry)) {
glist = g_list_append(glist, entry->object_name);
}
} /* for */
gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
g_list_free(glist);

View File

@ -693,20 +693,14 @@ GtkTreeIter *iter2;
GtkWidget *tree = glade_xml_get_widget(xml, "sql_treeview");
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++) {
entry = g_ptr_array_index (mdb->catalog, i);
/* 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));
gtk_tree_store_append(store, iter2, NULL);
gtk_tree_store_set(store, iter2, 0, entry->object_name, -1);
}
} /* if MDB_TABLE */
if (mdb_is_user_table(entry)) {
iter2 = g_malloc(sizeof(GtkTreeIter));
gtk_tree_store_append(store, iter2, NULL);
gtk_tree_store_set(store, iter2, 0, entry->object_name, -1);
}
} /* for */
}
#else

View File

@ -215,17 +215,11 @@ void gmdb_table_populate(MdbHandle *mdb)
int i;
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++) {
entry = g_ptr_array_index (mdb->catalog, i);
/* 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);
}
} /* if MDB_TABLE */
if (mdb_is_user_table(entry)) {
gmdb_table_add_icon(entry->object_name);
}
} /* for */
}

View File

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

View File

@ -355,3 +355,14 @@ guint32 pgnum;
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"
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,
no_unused_var_warn};
@ -153,7 +153,7 @@ SQLRETURN SQL_API SQLDriverConnect(
ConnectParams* params;
SQLRETURN ret;
TRACE("DriverConnect");
TRACE("SQLDriverConnect");
strcpy (lastError, "");
@ -191,7 +191,7 @@ SQLRETURN SQL_API SQLBrowseConnect(
SQLSMALLINT cbConnStrOutMax,
SQLSMALLINT FAR *pcbConnStrOut)
{
TRACE("BrowseConnect");
TRACE("SQLBrowseConnect");
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_temp_columns_end(ttable);
/* TODO: Sort the return list by TYPE, CAT, SCHEM, NAME */
for (i=0; i<mdb->num_catalog; i++) {
entry = g_ptr_array_index(mdb->catalog, i);
switch (entry->object_type) {
case MDB_TABLE: ttype = 1; break;
case MDB_SYSTEM_TABLE: ttype = 2; break;
case MDB_QUERY: ttype = 3; break;
default: ttype = 0; break;
}
if (!ttype)
if (mdb_is_user_table(entry))
ttype = 0;
else if (mdb_is_system_table(entry))
ttype = 1;
else if (entry->object_type == MDB_QUERY)
ttype = 2;
else
continue;
/* Set all fields to NULL */
@ -1559,7 +1561,7 @@ SQLRETURN SQL_API SQLTables(
}
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[3], t3, ts3);

View File

@ -506,12 +506,10 @@ void mdb_sql_listtables(MdbSQL *sql)
ttable = mdb_create_temp_table(mdb, "#listtables");
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++) {
entry = g_ptr_array_index (mdb->catalog, i);
/* only list user tables */
if ((entry->object_type == MDB_TABLE)
&& (strncmp (entry->object_name, "MSys", 4))) {
if (mdb_is_user_table(entry)) {
//col = g_ptr_array_index(table->columns,0);
tmpsiz = mdb_ascii2unicode(mdb, entry->object_name, 0, 100, tmpstr);
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 (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if ((tabname && !strcmp(entry->object_name,tabname)) ||
(!tabname )) {
// && strncmp (entry->object_name, "MSys", 4))) {
(!tabname /* && mdb_is_user_table(entry) */)) {
int ret;
table = mdb_read_table(entry);

View File

@ -80,18 +80,8 @@ FILE *cfile;
{
entry = g_ptr_array_index (mdb->catalog, i);
/* if it's a table */
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"))
{
if (!mdb_is_user_table(entry))
continue;
fprintf (typesfile, "typedef struct _%s\n", entry->object_name);
fprintf (typesfile, "{\n");
@ -147,9 +137,6 @@ FILE *cfile;
fprintf (cfile, "}\n\n");
mdb_free_tabledef(table);
}
}
}
}
fclose (typesfile);

View File

@ -61,9 +61,6 @@ main (int argc, char **argv)
break;
}
}
if (!namespace) {
namespace = (char *) g_strdup("");
}
mdb_init();
@ -94,19 +91,12 @@ main (int argc, char **argv)
fprintf(stdout,"-- Check out http://mdbtools.sourceforge.net\n");
fprintf(stdout,"-------------------------------------------------------------\n\n");
/* loop over each entry in the catalog */
for (i=0; i < mdb->num_catalog; i++) {
entry = g_ptr_array_index (mdb->catalog, i);
/* if it's a table */
if (entry->object_type == MDB_TABLE) {
/* skip the MSys tables */
if ((tabname && !strcmp(entry->object_name,tabname)) ||
(!tabname && strncmp (entry->object_name, "MSys", 4))) {
generate_table_schema(entry, namespace, s);
if ((tabname && !strcmp(entry->object_name, tabname))
|| (!tabname && mdb_is_user_table(entry))) {
generate_table_schema(entry, namespace, s);
}
}
}
@ -134,16 +124,13 @@ generate_table_schema(MdbCatalogEntry *entry, char *namespace, int sanitize)
unsigned int i;
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 */
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 */
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");
table = mdb_read_table (entry);

View File

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