mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-11-26 10:29:27 +08:00
patch #856724/856342 and odds and ends.
This commit is contained in:
@@ -11,6 +11,7 @@ mdb-sql
|
|||||||
mdb-tables
|
mdb-tables
|
||||||
mdb-ver
|
mdb-ver
|
||||||
mdb-check
|
mdb-check
|
||||||
|
mdb-import
|
||||||
mdb-prop
|
mdb-prop
|
||||||
prcat
|
prcat
|
||||||
prdata
|
prdata
|
||||||
|
|||||||
@@ -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@
|
LIBS = $(GLIB_LIBS) $(READLINE_LIBS) @LEXLIB@
|
||||||
DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\"
|
DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\"
|
||||||
AM_CPPFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS)
|
AM_CPPFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS)
|
||||||
|
|||||||
@@ -56,14 +56,16 @@ main(int argc, char **argv)
|
|||||||
MdbColumn *col;
|
MdbColumn *col;
|
||||||
/* doesn't handle tables > 256 columns. Can that happen? */
|
/* doesn't handle tables > 256 columns. Can that happen? */
|
||||||
char *bound_values[256];
|
char *bound_values[256];
|
||||||
|
int bound_lens[256];
|
||||||
char *delimiter = ",";
|
char *delimiter = ",";
|
||||||
char *row_delimiter = "\n";
|
char *row_delimiter = "\n";
|
||||||
char header_row = 1;
|
char header_row = 1;
|
||||||
char quote_text = 1;
|
char quote_text = 1;
|
||||||
char insert_statements = 0;
|
char insert_statements = 0;
|
||||||
|
char sanitize = 0;
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
while ((opt=getopt(argc, argv, "HQd:D:R:I"))!=-1) {
|
while ((opt=getopt(argc, argv, "HQd:D:R:IS"))!=-1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'H':
|
case 'H':
|
||||||
header_row = 0;
|
header_row = 0;
|
||||||
@@ -83,6 +85,9 @@ main(int argc, char **argv)
|
|||||||
insert_statements = 1;
|
insert_statements = 1;
|
||||||
header_row = 0;
|
header_row = 0;
|
||||||
break;
|
break;
|
||||||
|
case 'S':
|
||||||
|
sanitize = 1;
|
||||||
|
break;
|
||||||
case 'D':
|
case 'D':
|
||||||
mdb_set_date_fmt(optarg);
|
mdb_set_date_fmt(optarg);
|
||||||
break;
|
break;
|
||||||
@@ -104,6 +109,7 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr," -R <delimiter> specify a row delimiter\n");
|
fprintf(stderr," -R <delimiter> specify a row delimiter\n");
|
||||||
fprintf(stderr," -I INSERT statements (instead of CSV)\n");
|
fprintf(stderr," -I INSERT statements (instead of CSV)\n");
|
||||||
fprintf(stderr," -D <format> set the date format (see strftime(3) for details)\n");
|
fprintf(stderr," -D <format> set the date format (see strftime(3) for details)\n");
|
||||||
|
fprintf(stderr," -S Sanitize names (replace spaces etc. with underscore)\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,6 +136,7 @@ main(int argc, char **argv)
|
|||||||
bound_values[j] = (char *) malloc(MDB_BIND_SIZE);
|
bound_values[j] = (char *) malloc(MDB_BIND_SIZE);
|
||||||
bound_values[j][0] = '\0';
|
bound_values[j][0] = '\0';
|
||||||
mdb_bind_column(table, j+1, bound_values[j]);
|
mdb_bind_column(table, j+1, bound_values[j]);
|
||||||
|
mdb_bind_len(table, j+1, &bound_lens[j]);
|
||||||
}
|
}
|
||||||
if (header_row) {
|
if (header_row) {
|
||||||
col=g_ptr_array_index(table->columns,0);
|
col=g_ptr_array_index(table->columns,0);
|
||||||
@@ -145,11 +152,11 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
if (insert_statements) {
|
if (insert_statements) {
|
||||||
fprintf(stdout, "INSERT INTO %s (",
|
fprintf(stdout, "INSERT INTO %s (",
|
||||||
sanitize_name(argv[optind + 1],1));
|
sanitize_name(argv[optind + 1],sanitize));
|
||||||
for (j=0;j<table->num_cols;j++) {
|
for (j=0;j<table->num_cols;j++) {
|
||||||
if (j>0) fprintf(stdout, ", ");
|
if (j>0) fprintf(stdout, ", ");
|
||||||
col=g_ptr_array_index(table->columns,j);
|
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 (");
|
fprintf(stdout, ") VALUES (");
|
||||||
}
|
}
|
||||||
@@ -159,7 +166,10 @@ main(int argc, char **argv)
|
|||||||
mdb_ole_read(mdb, col, bound_values[0], MDB_BIND_SIZE);
|
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,
|
quote_text,
|
||||||
col->col_type);
|
col->col_type);
|
||||||
|
|
||||||
@@ -170,7 +180,10 @@ main(int argc, char **argv)
|
|||||||
mdb_ole_read(mdb, col, bound_values[j], MDB_BIND_SIZE);
|
mdb_ole_read(mdb, col, bound_values[j], MDB_BIND_SIZE);
|
||||||
}
|
}
|
||||||
fprintf(stdout,"%s",delimiter);
|
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,
|
quote_text,
|
||||||
col->col_type);
|
col->col_type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,15 +25,14 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static char *sanitize_name(char *str, int sanitize);
|
static char *sanitize_name(char *str, int sanitize);
|
||||||
|
static void generate_table_schema(MdbCatalogEntry *entry, char *namespace, int sanitize);
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i, k;
|
int i;
|
||||||
MdbHandle *mdb;
|
MdbHandle *mdb;
|
||||||
MdbCatalogEntry *entry;
|
MdbCatalogEntry *entry;
|
||||||
MdbTableDef *table;
|
|
||||||
MdbColumn *col;
|
|
||||||
char *the_relation;
|
char *the_relation;
|
||||||
char *tabname = NULL;
|
char *tabname = NULL;
|
||||||
char *namespace = "";
|
char *namespace = "";
|
||||||
@@ -41,7 +40,11 @@ main (int argc, char **argv)
|
|||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf (stderr, "Usage: %s <file> [<backend>]\n",argv[0]);
|
fprintf (stderr, "Usage: [options] %s <file> [<backend>]\n",argv[0]);
|
||||||
|
fprintf (stderr, "where options are:\n");
|
||||||
|
fprintf (stderr, " -T <table> Only create schema for named table\n");
|
||||||
|
fprintf (stderr, " -N <namespace> Prefix identifiers with namespace\n");
|
||||||
|
fprintf (stderr, " -S Sanitize names (replace spaces etc. with underscore)\n");
|
||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,64 +83,33 @@ main (int argc, char **argv)
|
|||||||
exit(1);
|
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++)
|
/* loop over each entry in the catalog */
|
||||||
{
|
|
||||||
entry = g_ptr_array_index (mdb->catalog, i);
|
|
||||||
|
|
||||||
/* 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)
|
/* if it's a table */
|
||||||
{
|
|
||||||
/* skip the MSys tables */
|
if (entry->object_type == MDB_TABLE) {
|
||||||
if ((tabname && !strcmp(entry->object_name,tabname)) ||
|
/* skip the MSys tables */
|
||||||
(!tabname && strncmp (entry->object_name, "MSys", 4)))
|
if ((tabname && !strcmp(entry->object_name,tabname)) ||
|
||||||
{
|
(!tabname && strncmp (entry->object_name, "MSys", 4))) {
|
||||||
|
|
||||||
/* make sure it's a table (may be redundant) */
|
generate_table_schema(entry, namespace, s);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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, "\n\n");
|
||||||
fprintf (stdout, "-- CREATE ANY Relationships ...\n");
|
fprintf (stdout, "-- CREATE ANY Relationships ...\n");
|
||||||
fprintf (stdout, "\n");
|
fprintf (stdout, "\n");
|
||||||
@@ -147,10 +119,65 @@ main (int argc, char **argv)
|
|||||||
the_relation=mdb_get_relationships(mdb);
|
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)
|
static char *sanitize_name(char *str, int sanitize)
|
||||||
|
|||||||
Reference in New Issue
Block a user