diff --git a/src/util/.cvsignore b/src/util/.cvsignore index 9905598..0322d46 100644 --- a/src/util/.cvsignore +++ b/src/util/.cvsignore @@ -11,6 +11,7 @@ mdb-sql mdb-tables mdb-ver mdb-check +mdb-import mdb-prop prcat prdata diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 29330cb..87193f4 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -1,4 +1,4 @@ -bin_PROGRAMS = mdb-export mdb-array mdb-schema mdb-tables mdb-parsecsv mdb-header mdb-sql mdb-ver mdb-prop prtable prcat prdata prkkd prdump prole updrow prindex +bin_PROGRAMS = mdb-export mdb-array mdb-schema mdb-tables mdb-parsecsv mdb-header mdb-sql mdb-ver mdb-prop mdb-import prtable prcat prdata prkkd prdump prole updrow prindex LIBS = $(GLIB_LIBS) $(READLINE_LIBS) @LEXLIB@ DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\" AM_CPPFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) diff --git a/src/util/mdb-export.c b/src/util/mdb-export.c index 790aabc..2d44355 100644 --- a/src/util/mdb-export.c +++ b/src/util/mdb-export.c @@ -56,14 +56,16 @@ main(int argc, char **argv) MdbColumn *col; /* doesn't handle tables > 256 columns. Can that happen? */ char *bound_values[256]; + int bound_lens[256]; char *delimiter = ","; char *row_delimiter = "\n"; char header_row = 1; char quote_text = 1; char insert_statements = 0; + char sanitize = 0; int opt; - while ((opt=getopt(argc, argv, "HQd:D:R:I"))!=-1) { + while ((opt=getopt(argc, argv, "HQd:D:R:IS"))!=-1) { switch (opt) { case 'H': header_row = 0; @@ -83,6 +85,9 @@ main(int argc, char **argv) insert_statements = 1; header_row = 0; break; + case 'S': + sanitize = 1; + break; case 'D': mdb_set_date_fmt(optarg); break; @@ -104,6 +109,7 @@ main(int argc, char **argv) fprintf(stderr," -R specify a row delimiter\n"); fprintf(stderr," -I INSERT statements (instead of CSV)\n"); fprintf(stderr," -D set the date format (see strftime(3) for details)\n"); + fprintf(stderr," -S Sanitize names (replace spaces etc. with underscore)\n"); exit(1); } @@ -130,6 +136,7 @@ main(int argc, char **argv) bound_values[j] = (char *) malloc(MDB_BIND_SIZE); bound_values[j][0] = '\0'; mdb_bind_column(table, j+1, bound_values[j]); + mdb_bind_len(table, j+1, &bound_lens[j]); } if (header_row) { col=g_ptr_array_index(table->columns,0); @@ -145,11 +152,11 @@ main(int argc, char **argv) if (insert_statements) { fprintf(stdout, "INSERT INTO %s (", - sanitize_name(argv[optind + 1],1)); + sanitize_name(argv[optind + 1],sanitize)); for (j=0;jnum_cols;j++) { if (j>0) fprintf(stdout, ", "); col=g_ptr_array_index(table->columns,j); - fprintf(stdout,"%s", sanitize_name(col->name,1)); + fprintf(stdout,"%s", sanitize_name(col->name,sanitize)); } fprintf(stdout, ") VALUES ("); } @@ -159,7 +166,10 @@ main(int argc, char **argv) mdb_ole_read(mdb, col, bound_values[0], MDB_BIND_SIZE); } - print_col(bound_values[0], + if (insert_statements && !bound_lens[0]) + print_col("NULL",0,col->col_type); + else + print_col(bound_values[0], quote_text, col->col_type); @@ -170,7 +180,10 @@ main(int argc, char **argv) mdb_ole_read(mdb, col, bound_values[j], MDB_BIND_SIZE); } fprintf(stdout,"%s",delimiter); - print_col(bound_values[j], + if (insert_statements && !bound_lens[j]) + print_col("NULL",0,col->col_type); + else + print_col(bound_values[j], quote_text, col->col_type); } diff --git a/src/util/mdb-schema.c b/src/util/mdb-schema.c index b431931..4d434a7 100644 --- a/src/util/mdb-schema.c +++ b/src/util/mdb-schema.c @@ -25,15 +25,14 @@ #endif static char *sanitize_name(char *str, int sanitize); +static void generate_table_schema(MdbCatalogEntry *entry, char *namespace, int sanitize); int main (int argc, char **argv) { - int i, k; + int i; MdbHandle *mdb; MdbCatalogEntry *entry; - MdbTableDef *table; - MdbColumn *col; char *the_relation; char *tabname = NULL; char *namespace = ""; @@ -41,7 +40,11 @@ main (int argc, char **argv) int opt; if (argc < 2) { - fprintf (stderr, "Usage: %s []\n",argv[0]); + fprintf (stderr, "Usage: [options] %s []\n",argv[0]); + fprintf (stderr, "where options are:\n"); + fprintf (stderr, " -T Only create schema for named table\n"); + fprintf (stderr, " -N Prefix identifiers with namespace\n"); + fprintf (stderr, " -S Sanitize names (replace spaces etc. with underscore)\n"); exit (1); } @@ -80,64 +83,33 @@ main (int argc, char **argv) exit(1); } - /* loop over each entry in the catalog */ + /* Print out a little message to show that this came from mdb-tools. + I like to know how something is generated. DW */ + fprintf(stdout,"-------------------------------------------------------------\n"); + fprintf(stdout,"-- MDB Tools - A library for reading MS Access database files\n"); + fprintf(stdout,"-- Copyright (C) 2000-2004 Brian Bruns\n"); + fprintf(stdout,"-- Files in libmdb are licensed under LGPL and the utilities under\n"); + fprintf(stdout,"-- the GPL, see COPYING.LIB and COPYING files respectively.\n"); + fprintf(stdout,"-- Check out http://mdbtools.sourceforge.net\n"); + fprintf(stdout,"-------------------------------------------------------------\n\n"); - for (i=0; i < mdb->num_catalog; i++) - { - entry = g_ptr_array_index (mdb->catalog, i); + /* loop over each entry in the catalog */ - /* if it's a table */ + for (i=0; i < mdb->num_catalog; i++) { + entry = g_ptr_array_index (mdb->catalog, i); - if (entry->object_type == MDB_TABLE) - { - /* skip the MSys tables */ - if ((tabname && !strcmp(entry->object_name,tabname)) || - (!tabname && strncmp (entry->object_name, "MSys", 4))) - { + /* 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%s;\n", namespace, sanitize_name(entry->object_name,s)); - - /* create the table */ - fprintf (stdout, "CREATE TABLE %s%s\n", namespace, sanitize_name(entry->object_name,s)); - 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", sanitize_name(col->name,s), - mdb_get_coltype_string (mdb->default_backend, col->col_type)); - - if (col->col_size != 0 && - mdb_coltype_takes_length(mdb->default_backend, col->col_type)) { - - fprintf (stdout, " (%d)", col->col_size); + generate_table_schema(entry, namespace, s); } - - if (k < table->num_cols - 1) - fprintf (stdout, ", \n"); - else - fprintf (stdout, "\n"); - } + } + } - fprintf (stdout, ");\n"); - fprintf (stdout, "-- CREATE ANY INDEXES ...\n"); - fprintf (stdout, "\n"); - } - } - } - } fprintf (stdout, "\n\n"); fprintf (stdout, "-- CREATE ANY Relationships ...\n"); fprintf (stdout, "\n"); @@ -147,10 +119,65 @@ main (int argc, char **argv) the_relation=mdb_get_relationships(mdb); } - mdb_free_handle (mdb); - mdb_exit(); - exit(0); + mdb_free_handle (mdb); + mdb_exit(); + + exit(0); +} +static void +generate_table_schema(MdbCatalogEntry *entry, char *namespace, int sanitize) +{ + MdbTableDef *table; + MdbHandle *mdb = entry->mdb; + 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)); + + /* create the table */ + fprintf (stdout, "CREATE TABLE %s%s\n", namespace, sanitize_name(entry->object_name,sanitize)); + 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 (i = 0; i < table->num_cols; i++) { + col = g_ptr_array_index (table->columns, i); + + fprintf (stdout, "\t%s\t\t\t%s", sanitize_name(col->name,sanitize), + mdb_get_coltype_string (mdb->default_backend, col->col_type)); + + if (mdb_coltype_takes_length(mdb->default_backend, + col->col_type)) { + + /* more portable version from DW patch */ + if (col->col_size == 0) + fprintf (stdout, " (255)"); + else + fprintf (stdout, " (%d)", col->col_size); + } + + if (i < table->num_cols - 1) + fprintf (stdout, ", \n"); + else + fprintf (stdout, "\n"); + } /* for */ + + fprintf (stdout, ");\n"); + fprintf (stdout, "-- CREATE ANY INDEXES ...\n"); + fprintf (stdout, "\n"); + } static char *sanitize_name(char *str, int sanitize)