void mdb_table_dump(MdbCatalogEntry *entry) { MdbTableDef *table; MdbColumn *col; int coln; MdbIndex *idx; MdbHandle *mdb = entry->mdb; unsigned int i, bitn; guint32 pgnum; table = mdb_read_table(entry); fprintf(stdout,"definition page = %lu\n",entry->table_pg); fprintf(stdout,"number of datarows = %d\n",table->num_rows); fprintf(stdout,"number of columns = %d\n",table->num_cols); fprintf(stdout,"number of indices = %d\n",table->num_real_idxs); mdb_read_columns(table); mdb_read_indices(table); for (i=0;i<table->num_cols;i++) { col = g_ptr_array_index(table->columns,i); fprintf(stdout,"column %d Name: %-20s Type: %s(%d)\n", i, col->name, mdb_get_coltype_string(mdb->default_backend, col->col_type), col->col_size); } for (i=0;i<table->num_idxs;i++) { idx = g_ptr_array_index (table->indices, i); mdb_index_dump(table, idx); } if (table->usage_map) { printf("pages reserved by this object\n"); printf("usage map pg %" G_GUINT32_FORMAT "\n", table->map_base_pg); printf("free map pg %" G_GUINT32_FORMAT "\n", table->freemap_base_pg); pgnum = mdb_get_int32(table->usage_map,1); /* the first 5 bytes of the usage map mean something */ coln = 0; for (i=5;i<table->map_sz;i++) { for (bitn=0;bitn<8;bitn++) { if (table->usage_map[i] & 1 << bitn) { coln++; printf("%6" G_GUINT32_FORMAT, pgnum); if (coln==10) { printf("\n"); coln = 0; } else { printf(" "); } } pgnum++; } } printf("\n"); } }
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; }
MdbTableDef *mdb_read_table_by_name(MdbHandle *mdb, gchar *table_name, int obj_type) { unsigned int i; MdbCatalogEntry *entry; mdb_read_catalog(mdb, obj_type); for (i=0; i<mdb->num_catalog; i++) { entry = g_ptr_array_index(mdb->catalog, i); if (!strcasecmp(entry->object_name, table_name)) return mdb_read_table(entry); } return NULL; }
main(int argc, char **argv) { int rows; int i; unsigned char buf[2048]; MdbHandle *mdb; MdbCatalogEntry *entry; MdbTableDef *table; GList *l; if (argc<2) { fprintf(stderr,"Usage: %s <file> <table>\n",argv[0]); exit(1); } mdb_init(); mdb = mdb_open(argv[1]); mdb_read_catalog(mdb, MDB_TABLE); for (i=0;i<mdb->num_catalog;i++) { entry = g_ptr_array_index(mdb->catalog,i); if (entry->object_type == MDB_TABLE && !strcmp(entry->object_name,argv[2])) { table = mdb_read_table(entry); mdb_read_columns(table); mdb_data_dump(table); } } mdb_free_handle(mdb); mdb_exit(); exit(0); }
static void generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *dbnamespace, guint32 export_options) { MdbTableDef *table; MdbHandle *mdb = entry->mdb; MdbColumn *col; unsigned int i; char* quoted_table_name; char* quoted_name; MdbProperties *props; const char *prop_value; quoted_table_name = mdb->default_backend->quote_schema_name(dbnamespace, entry->object_name); /* drop the table if it exists */ if (export_options & MDB_SHEXP_DROPTABLE) fprintf (outfile, mdb->default_backend->drop_statement, quoted_table_name); /* create the table */ fprintf (outfile, "CREATE TABLE %s\n", quoted_table_name); fprintf (outfile, " (\n"); table = mdb_read_table (entry); /* get the columns */ mdb_read_columns (table); /* loop over the columns, dumping the names and types */ for (i = 0; i < table->num_cols; i++) { col = g_ptr_array_index (table->columns, i); quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name); fprintf (outfile, "\t%s\t\t\t%s", quoted_name, mdb_get_colbacktype_string (col)); free(quoted_name); if (mdb_colbacktype_takes_length(col)) { /* more portable version from DW patch */ if (col->col_size == 0) fputs(" (255)", outfile); else fprintf(outfile, " (%d)", col->col_size); } if (export_options & MDB_SHEXP_CST_NOTNULL) { if (col->col_type == MDB_BOOL) { /* access booleans are never null */ fputs(" NOT NULL", outfile); } else { const gchar *not_null = mdb_col_get_prop(col, "Required"); if (not_null && not_null[0]=='y') fputs(" NOT NULL", outfile); } } if (export_options & MDB_SHEXP_DEFVALUES) { int done = 0; if (col->props) { gchar *defval = g_hash_table_lookup(col->props->hash, "DefaultValue"); if (defval) { size_t def_len = strlen(defval); fputs(" DEFAULT ", outfile); /* ugly hack to detect the type */ if (defval[0]=='"' && defval[def_len-1]=='"') { /* this is a string */ gchar *output_default = malloc(def_len-1); gchar *output_default_escaped = malloc(def_len-1); memcpy(output_default, defval+1, def_len-2); output_default[def_len-2] = 0; output_default_escaped = quote_with_squotes(output_default); fputs(output_default_escaped, outfile); g_free(output_default_escaped); free(output_default); } else if (!strcmp(defval, "Yes")) fputs("TRUE", outfile); else if (!strcmp(defval, "No")) fputs("FALSE", outfile); else if (!strcasecmp(defval, "date()")) { if (!strcmp(mdb_col_get_prop(col, "Format"), "Short Date")) fputs(mdb->default_backend->short_now, outfile); else fputs(mdb->default_backend->long_now, outfile); } else fputs(defval, outfile); done = 1; } } if (!done && col->col_type == MDB_BOOL) /* access booleans are false by default */ fputs(" DEFAULT FALSE", outfile); } if (i < table->num_cols - 1) fputs(", \n", outfile); else fputs("\n", outfile); } /* for */ fputs(");\n", outfile); /* Add the constraints on columns */ for (i = 0; i < table->num_cols; i++) { col = g_ptr_array_index (table->columns, i); props = col->props; if (!props) continue; quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name); if (export_options & MDB_SHEXP_CST_NOTEMPTY) { prop_value = mdb_col_get_prop(col, "AllowZeroLength"); if (prop_value && prop_value[0]=='n') fprintf(outfile, mdb->default_backend->constaint_not_empty_statement, quoted_table_name, quoted_name); } if (export_options & MDB_SHEXP_COMMENTS) { prop_value = mdb_col_get_prop(col, "Description"); if (prop_value) { char *comment = quote_with_squotes(prop_value); fprintf(outfile, mdb->default_backend->column_comment_statement, quoted_table_name, quoted_name, comment); free(comment); } } free(quoted_name); } /* Add the constraints on table */ if (export_options & MDB_SHEXP_COMMENTS) { prop_value = mdb_table_get_prop(table, "Description"); if (prop_value) { char *comment = quote_with_squotes(prop_value); fprintf(outfile, mdb->default_backend->table_comment_statement, quoted_table_name, comment); free(comment); } } fputc('\n', outfile); if (export_options & MDB_SHEXP_INDEXES) // prints all the indexes of that table mdb_print_indexes(outfile, table, dbnamespace); free(quoted_table_name); mdb_free_tabledef (table); }
int main (int argc, char **argv) { unsigned int i, j, k; unsigned int unsupported = 0; MdbHandle *mdb; MdbCatalogEntry *entry; MdbTableDef *table; MdbColumn *col; FILE *typesfile; FILE *headerfile; FILE *cfile; if (argc < 2) { fprintf (stderr, "Usage: %s <file>\n",argv[0]); exit (1); } mdb_init(); /* open the database */ mdb = mdb_open (argv[1], MDB_NOFLAGS); if (!mdb) { mdb_exit(); exit(1); } typesfile = fopen ("types.h", "w"); headerfile = fopen ("dumptypes.h", "w"); cfile = fopen ("dumptypes.c", "w"); copy_header (typesfile); copy_header (headerfile); fprintf (headerfile, "#include \"types.h\"\n"); copy_header (cfile); fprintf (cfile, "#include <stdio.h>\n"); fprintf (cfile, "#include \"dumptypes.h\"\n"); /* read the catalog */ 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 (!mdb_is_user_table(entry)) continue; fprintf (typesfile, "typedef struct _%s\n", entry->object_name); fprintf (typesfile, "{\n"); fprintf (headerfile, "void dump_%s (%s x);\n", entry->object_name, entry->object_name); fprintf (cfile, "void dump_%s (%s x)\n{\n", entry->object_name, entry->object_name); fprintf (cfile, "\tfprintf (stdout, \"**************** %s ****************\\n\");\n", entry->object_name); table = mdb_read_table (entry); /* get the columns */ mdb_read_columns (table); /* loop over the columns, dumping the names and types */ for (k = 0; k < table->num_cols; k++) { col = g_ptr_array_index (table->columns, k); fprintf (cfile, "\tfprintf (stdout, \"x."); for (j = 0; j < strlen (col->name); j++) { fprintf (cfile, "%c", tolower (col->name [j])); } fprintf (cfile, " = \");\n"); switch (col->col_type) { case MDB_INT: fprintf (typesfile, "\tint\t"); fprintf (cfile, "\tdump_int (x."); break; case MDB_LONGINT: fprintf (typesfile, "\tlong\t"); fprintf (cfile, "\tdump_long (x."); break; case MDB_TEXT: case MDB_MEMO: fprintf (typesfile, "\tchar *\t"); fprintf (cfile, "\tdump_string (x."); break; default: unsupported = 1; break; } for (j = 0; j < strlen (col->name); j++) { fprintf (typesfile, "%c", tolower (col->name [j])); fprintf (cfile, "%c", tolower (col->name [j])); } fprintf (typesfile, ";\n"); fprintf (cfile, ");\n"); } fprintf (typesfile, "\n} %s ;\n", entry->object_name); fprintf (typesfile, "\n"); fprintf (cfile, "}\n\n"); mdb_free_tabledef(table); } fclose (headerfile); fclose (typesfile); fclose (cfile); mdb_close (mdb); mdb_exit(); if (unsupported) fputs("ERROR: unsupported type.\n", stderr); exit(unsupported); }
void gmdb_table_export_button_cb(GtkWidget *w, gpointer data) { gchar *file_path; FILE *outfile; gchar *bound_data[256]; MdbTableDef *table; MdbColumn *col; int i; int need_headers = 0; int need_quote = 0; gchar delimiter[11]; gchar quotechar; gchar lineterm[5]; gchar *str; int rows=0; GtkWidget *exportwin, *dlg; gmdb_export_get_delimiter(exportwin_xml, delimiter, 10); gmdb_export_get_lineterm(exportwin_xml, lineterm, 5); need_quote = gmdb_export_get_quote(exportwin_xml); quotechar = gmdb_export_get_quotechar(exportwin_xml); need_headers = gmdb_export_get_headers(exportwin_xml); file_path = gmdb_export_get_filepath(exportwin_xml); // printf("file path %s\n",file_path); if ((outfile=fopen(file_path, "w"))==NULL) { GtkWidget* dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, _("Unable to open file.")); gtk_dialog_run (GTK_DIALOG (dlg)); gtk_widget_destroy (dlg); return; } /* read table */ table = mdb_read_table(cat_entry); mdb_read_columns(table); mdb_rewind_table(table); for (i=0;i<table->num_cols;i++) { /* bind columns */ bound_data[i] = (char *) g_malloc0(MDB_BIND_SIZE); mdb_bind_column(table, i+1, bound_data[i], NULL); /* display column titles */ col=g_ptr_array_index(table->columns,i); if (need_headers) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); fprintf(outfile,"%s", col->name); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); } } if (need_headers) fprintf(outfile,lineterm); /* fetch those rows! */ while(mdb_fetch_row(table)) { for (i=0;i<table->num_cols;i++) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); fprintf(outfile,"%s", bound_data[i]); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); } fprintf(outfile,lineterm); rows++; } /* free the memory used to bind */ for (i=0;i<table->num_cols;i++) { g_free(bound_data[i]); } fclose(outfile); exportwin = glade_xml_get_widget (exportwin_xml, "export_dialog"); gtk_widget_destroy(exportwin); dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, _("%d rows successfully exported."), rows); gtk_dialog_run (GTK_DIALOG (dlg)); gtk_widget_destroy (dlg); }
void gmdb_table_export_button_cb(GtkWidget *w, gpointer data) { gchar *file_path; FILE *outfile; gchar *bound_data[256]; MdbTableDef *table; MdbColumn *col; int i; int need_headers = 0; int need_quote = 0; gchar delimiter[11]; gchar quotechar; gchar lineterm[5]; gchar *str; int rows=0; char msg[100]; GtkWidget *exportwin; gmdb_export_get_delimiter(exportwin_xml, delimiter, 10); gmdb_export_get_lineterm(exportwin_xml, lineterm, 5); need_quote = gmdb_export_get_quote(exportwin_xml); quotechar = gmdb_export_get_quotechar(exportwin_xml); need_headers = gmdb_export_get_headers(exportwin_xml); file_path = gmdb_export_get_filepath(exportwin_xml); // printf("file path %s\n",file_path); if ((outfile=fopen(file_path, "w"))==NULL) { gnome_warning_dialog("Unable to Open File!"); return; } /* read table */ table = mdb_read_table(cat_entry); mdb_read_columns(table); mdb_rewind_table(table); for (i=0;i<table->num_cols;i++) { /* bind columns */ bound_data[i] = (char *) malloc(MDB_BIND_SIZE); bound_data[i][0] = '\0'; mdb_bind_column(table, i+1, bound_data[i]); /* display column titles */ col=g_ptr_array_index(table->columns,i); if (need_headers) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); fprintf(outfile,"%s", col->name); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); } } if (need_headers) fprintf(outfile,lineterm); /* fetch those rows! */ while(mdb_fetch_row(table)) { for (i=0;i<table->num_cols;i++) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); fprintf(outfile,"%s", bound_data[i]); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); } fprintf(outfile,lineterm); rows++; } /* free the memory used to bind */ for (i=0;i<table->num_cols;i++) { free(bound_data[i]); } fclose(outfile); exportwin = glade_xml_get_widget (exportwin_xml, "export_dialog"); gtk_widget_destroy(exportwin); sprintf(msg,"%d Rows exported successfully.\n", rows); gnome_ok_dialog(msg); }
main (int argc, char **argv) { int i, j, k; MdbHandle *mdb; MdbCatalogEntry *entry; MdbTableDef *table; MdbColumn *col; char *the_relation; char *tabname = NULL; int opt; if (argc < 2) { fprintf (stderr, "Usage: %s <file> [<backend>]\n",argv[0]); exit (1); } while ((opt=getopt(argc, argv, "T:"))!=-1) { switch (opt) { case 'T': tabname = (char *) malloc(strlen(optarg)+1); strcpy(tabname, optarg); break; } } mdb_init(); /* open the database */ mdb = mdb_open (argv[optind]); if (argc - optind >2) { if (!mdb_set_default_backend(mdb, argv[optind + 1])) { fprintf(stderr,"Invalid backend type\n"); mdb_exit(); exit(1); } } /* read the catalog */ 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 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))) { /* 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 */ fprintf (stdout, "DROP TABLE %s;\n", entry->object_name); /* create the table */ fprintf (stdout, "CREATE TABLE %s\n", entry->object_name); fprintf (stdout, " (\n"); table = mdb_read_table (entry); /* get the columns */ mdb_read_columns (table); /* loop over the columns, dumping the names and types */ for (k = 0; k < table->num_cols; k++) { col = g_ptr_array_index (table->columns, k); fprintf (stdout, "\t%s\t\t\t%s", col->name, mdb_get_coltype_string (mdb->default_backend, col->col_type)); if (col->col_size != 0) fprintf (stdout, " (%d)", col->col_size); if (k < table->num_cols - 1) fprintf (stdout, ", \n"); else fprintf (stdout, "\n"); } fprintf (stdout, "\n);\n"); fprintf (stdout, "-- CREATE ANY INDEXES ...\n"); fprintf (stdout, "\n"); } } } } fprintf (stdout, "\n\n"); fprintf (stdout, "-- CREATE ANY Relationships ...\n"); fprintf (stdout, "\n"); the_relation=mdb_get_relationships(mdb); while (the_relation[0] != '\0') { fprintf(stdout,"%s\n",the_relation); the_relation=mdb_get_relationships(mdb); } mdb_free_handle (mdb); mdb_exit(); exit(0); }
/* functions */ GtkWidget * gmdb_table_data_new(MdbCatalogEntry *entry) { MdbTableDef *table; MdbColumn *col; GtkWidget *clist; GtkWidget *scroll; int i, rownum; long row, maxrow; gchar *bound_data[256]; GMdbDataWindow *dataw = NULL; /* do we have an active window for this object? if so raise it */ for (i=0;i<g_list_length(window_list);i++) { dataw = g_list_nth_data(window_list, i); if (!strcmp(dataw->table_name, entry->object_name)) { gdk_window_raise (dataw->window->window); return dataw->window; } } dataw = g_malloc(sizeof(GMdbDataWindow)); strcpy(dataw->table_name, entry->object_name); dataw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(dataw->window), entry->object_name); gtk_widget_set_usize(dataw->window, 300,200); gtk_widget_set_uposition(dataw->window, 50,50); gtk_widget_show(dataw->window); gtk_signal_connect (GTK_OBJECT (dataw->window), "delete_event", GTK_SIGNAL_FUNC (gmdb_table_data_close), dataw); scroll = gtk_scrolled_window_new(NULL,NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_widget_show (scroll); gtk_container_add(GTK_CONTAINER(dataw->window), scroll); /* read table */ table = mdb_read_table(entry); mdb_read_columns(table); mdb_rewind_table(table); clist = gtk_clist_new(table->num_cols); gtk_widget_show(clist); gtk_container_add(GTK_CONTAINER(scroll),clist); for (i=0;i<table->num_cols;i++) { /* bind columns */ bound_data[i] = (char *) g_malloc0(MDB_BIND_SIZE); mdb_bind_column(table, i+1, bound_data[i], NULL); /* display column titles */ col=g_ptr_array_index(table->columns,i); gtk_clist_set_column_title(GTK_CLIST(clist), i, col->name); } gtk_clist_column_titles_show(GTK_CLIST(clist)); maxrow = gmdb_prefs_get_maxrows(); /* fetch those rows! */ row = 0; while(mdb_fetch_row(table) && (!maxrow || (row < maxrow))) { row++; rownum = gtk_clist_append(GTK_CLIST(clist), bound_data); } /* free the memory used to bind */ for (i=0;i<table->num_cols;i++) { g_free(bound_data[i]); } /* add this one to the window list */ window_list = g_list_append(window_list, dataw); return dataw->window; }