GPtrArray *mdb_read_catalog (MdbHandle *mdb, int objtype) { MdbCatalogEntry *entry, msysobj; MdbTableDef *table; char obj_id[256]; char obj_name[256]; char obj_type[256]; char obj_flags[256]; int type; if (!mdb) return NULL; if (mdb->catalog) mdb_free_catalog(mdb); mdb->catalog = g_ptr_array_new(); mdb->num_catalog = 0; /* dummy up a catalog entry so we may read the table def */ memset(&msysobj, 0, sizeof(MdbCatalogEntry)); msysobj.mdb = mdb; msysobj.object_type = MDB_TABLE; msysobj.table_pg = 2; strcpy(msysobj.object_name, "MSysObjects"); /* mdb_table_dump(&msysobj); */ table = mdb_read_table(&msysobj); if (!table) return NULL; mdb_read_columns(table); mdb_bind_column_by_name(table, "Id", obj_id, NULL); mdb_bind_column_by_name(table, "Name", obj_name, NULL); mdb_bind_column_by_name(table, "Type", obj_type, NULL); mdb_bind_column_by_name(table, "Flags", obj_flags, NULL); mdb_rewind_table(table); while (mdb_fetch_row(table)) { type = atoi(obj_type); if (objtype==MDB_ANY || type == objtype) { entry = (MdbCatalogEntry *) g_malloc0(sizeof(MdbCatalogEntry)); entry->mdb = mdb; 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); } } mdb_free_tabledef(table); return mdb->catalog; }
void dump_ole(MdbTableDef *table, char *colname, char *sargname) { char ole_data[200000]; int len; MdbSarg sarg; char *sargcol, *sargop, *sargval; mdb_bind_column_by_name(table, colname, ole_data, &len); if (sargname) { sargcol = strtok(sargname," "); sargop = strtok(NULL," "); sargval = strtok(NULL," "); printf("col %s op %s val %s\n",sargcol,sargop,sargval); sarg.op = MDB_EQUAL; /* only support = for now, sorry */ strcpy(sarg.value.s, sargval); mdb_add_sarg_by_name(table, sargcol, &sarg); } mdb_rewind_table(table); while (mdb_fetch_row(table)) { mdb_buffer_dump(ole_data, 0, len); printf("---\n"); } }
int main(int argc, char **argv) { MdbHandle *mdb; MdbTableDef *table; char *colname, *tabname; char *colval; int colnum; char *sargname = NULL; char *updstr = NULL; char data[255]; int len; if (argc<4) { fprintf(stderr,"Usage: %s <file> <table> <sargs> <updstr>\n",argv[0]); exit(1); } mdb = mdb_open(argv[1], MDB_WRITABLE); tabname = argv[2]; sargname = argv[3]; updstr = g_strdup(argv[4]); table = mdb_read_table_by_name(mdb, tabname, MDB_TABLE); if (table) { mdb_read_columns(table); mdb_read_indices(table); printf("updstr %s\n",updstr); colname = strtok(updstr,"="); colval = strtok(NULL,"="); colnum = mdb_bind_column_by_name(table, colname, data, &len); printf("column %d\n", colnum); read_to_row(table, sargname); printf("current value of %s is %s, changing to %s\n", colname, data, colval); len = strlen(colval); strcpy(data,colval); mdb_update_row(table); mdb_free_tabledef(table); } mdb_close(mdb); return 0; }
/** * mdb_get_relationships * @mdb: Handle to open MDB database file * @tablename: Name of the table to process. Process all tables if NULL. * * 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. * 'grbit' contains integrity constraints. * * 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. * Returns NULL on last iteration. * The caller is responsible for freeing this string. */ static char * mdb_get_relationships(MdbHandle *mdb, const gchar *dbnamespace, const char* tablename) { unsigned int i; gchar *text = NULL; /* String to be returned */ static char *bound[5]; /* Bound values */ static MdbTableDef *table; /* Relationships table */ int backend = 0; /* Backends: 1=oracle, 2=postgres */ char *quoted_table_1, *quoted_column_1, *quoted_table_2, *quoted_column_2, *constraint_name, *quoted_constraint_name; long grbit; if (!strcmp(mdb->backend_name, "oracle")) { backend = 1; } else if (!strcmp(mdb->backend_name, "postgres")) { backend = 2; } else { if (is_init == 0) { /* the first time through */ is_init = 1; return (char *) g_strconcat( "-- relationships are not implemented for ", mdb->backend_name, "\n", 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)) { fprintf(stderr, "No MSysRelationships\n"); return NULL; } mdb_read_columns(table); for (i=0;i<5;i++) { bound[i] = (char *) g_malloc0(MDB_BIND_SIZE); } mdb_bind_column_by_name(table, "szColumn", bound[0], NULL); mdb_bind_column_by_name(table, "szObject", bound[1], NULL); mdb_bind_column_by_name(table, "szReferencedColumn", bound[2], NULL); mdb_bind_column_by_name(table, "szReferencedObject", bound[3], NULL); mdb_bind_column_by_name(table, "grbit", bound[4], NULL); mdb_rewind_table(table); is_init = 1; } else { if (!table) { fprintf(stderr, "table is NULL\n"); } if (table->cur_row >= table->num_rows) { /* past the last row */ for (i=0;i<5;i++) g_free(bound[i]); is_init = 0; return NULL; } } while (1) { if (!mdb_fetch_row(table)) { for (i=0;i<5;i++) g_free(bound[i]); is_init = 0; return NULL; } if (!tablename || !strcmp(bound[1], tablename)) break; } quoted_table_1 = mdb->default_backend->quote_schema_name(dbnamespace, bound[1]); quoted_column_1 = mdb->default_backend->quote_schema_name(dbnamespace, bound[0]); quoted_table_2 = mdb->default_backend->quote_schema_name(dbnamespace, bound[3]); quoted_column_2 = mdb->default_backend->quote_schema_name(dbnamespace, bound[2]); grbit = atoi(bound[4]); constraint_name = g_strconcat(bound[1], "_", bound[0], "_fk", NULL); quoted_constraint_name = mdb->default_backend->quote_schema_name(dbnamespace, constraint_name); free(constraint_name); if (grbit & 0x00000002) { text = g_strconcat( "-- Relationship from ", quoted_table_1, " (", quoted_column_1, ")" " to ", quoted_table_2, "(", quoted_column_2, ")", " does not enforce integrity.\n", NULL); } else { switch (backend) { case 1: /* oracle */ case 2: /* postgres */ text = g_strconcat( "ALTER TABLE ", quoted_table_1, " ADD CONSTRAINT ", quoted_constraint_name, " FOREIGN KEY (", quoted_column_1, ")" " REFERENCES ", quoted_table_2, "(", quoted_column_2, ")", (grbit & 0x00000100) ? " ON UPDATE CASCADE" : "", (grbit & 0x00001000) ? " ON DELETE CASCADE" : "", ";\n", NULL); break; } } free(quoted_table_1); free(quoted_column_1); free(quoted_table_2); free(quoted_column_2); free(quoted_constraint_name); return (char *)text; }
int main(int argc, char **argv) { MdbHandle *mdb; MdbTableDef *table; gchar name[256]; gchar *propColName; void *buf; int col_num; int found = 0; if (argc < 3) { fprintf(stderr,"Usage: %s <file> <object name> [<prop col>]\n", argv[0]); return 1; } if (argc < 4) propColName = "LvProp"; else propColName = argv[3]; mdb_init(); mdb = mdb_open(argv[1], MDB_NOFLAGS); if (!mdb) { mdb_exit(); return 1; } table = mdb_read_table_by_name(mdb, "MSysObjects", MDB_ANY); if (!table) { mdb_close(mdb); mdb_exit(); return 1; } mdb_read_columns(table); mdb_rewind_table(table); mdb_bind_column_by_name(table, "Name", name, NULL); buf = g_malloc(MDB_BIND_SIZE); col_num = mdb_bind_column_by_name(table, propColName, buf, NULL); if (col_num < 1) { g_free(buf); mdb_free_tabledef(table); mdb_close(mdb); mdb_exit(); printf("Column %s not found in MSysObjects!\n", argv[3]); return 1; } while(mdb_fetch_row(table)) { if (!strcmp(name, argv[2])) { found = 1; break; } } if (found) { MdbColumn *col = g_ptr_array_index(table->columns, col_num-1); size_t size; void *kkd = mdb_ole_read_full(mdb, col, &size); dump_kkd(mdb, kkd, size); free(kkd); } g_free(buf); mdb_free_tabledef(table); mdb_close(mdb); mdb_exit(); return 0; }