patch default_values from Nirgal Vourgère

This commit is contained in:
Brian Bruns 2011-02-16 18:59:25 -05:00
parent 39277312d4
commit d794c6e2a1
6 changed files with 122 additions and 41 deletions

View File

@ -14,6 +14,8 @@ OPTIONS
--no-drop-table Don't issue DROP TABLE statement. This is the default. --no-drop-table Don't issue DROP TABLE statement. This is the default.
--not-null Issue NOT NULL constraints. This is the default. --not-null Issue NOT NULL constraints. This is the default.
--no-not-null Don't issue NOT NULL constraints. --no-not-null Don't issue NOT NULL constraints.
--default-values Issue DEFAULT values.
--no-default-values Don't issue DEFAULT values. This is the default.
--not-empty Issue CHECK <> '' constraints. --not-empty Issue CHECK <> '' constraints.
--no-not-empty Don't issue CHECK <> '' constraints. This is the default. --no-not-empty Don't issue CHECK <> '' constraints. This is the default.
--indexes Export INDEXes. This is the default. --indexes Export INDEXes. This is the default.

View File

@ -159,9 +159,10 @@ enum {
MDB_SHEXP_CST_NOTNULL = 1<<1, /* generate NOT NULL constraints */ MDB_SHEXP_CST_NOTNULL = 1<<1, /* generate NOT NULL constraints */
MDB_SHEXP_CST_NOTEMPTY = 1<<2, /* <>'' constraints */ MDB_SHEXP_CST_NOTEMPTY = 1<<2, /* <>'' constraints */
MDB_SHEXP_COMMENTS = 1<<3, /* export comments on columns & tables */ MDB_SHEXP_COMMENTS = 1<<3, /* export comments on columns & tables */
MDB_SHEXP_INDEXES = 1<<4, /* export indices */ MDB_SHEXP_DEFVALUES = 1<<4, /* export default values */
MDB_SHEXP_RELATIONS = 1<<5, /* export relation (foreign keys) */ MDB_SHEXP_INDEXES = 1<<5, /* export indices */
MDB_SHEXP_SANITIZE = 1<<6 /* clean up names */ MDB_SHEXP_RELATIONS = 1<<6, /* export relation (foreign keys) */
MDB_SHEXP_SANITIZE = 1<<7 /* clean up names */
}; };
#define MDB_SHEXP_DEFAULT (MDB_SHEXP_CST_NOTNULL | MDB_SHEXP_COMMENTS | MDB_SHEXP_INDEXES | MDB_SHEXP_RELATIONS) #define MDB_SHEXP_DEFAULT (MDB_SHEXP_CST_NOTNULL | MDB_SHEXP_COMMENTS | MDB_SHEXP_INDEXES | MDB_SHEXP_RELATIONS)
@ -184,6 +185,8 @@ typedef struct {
MdbBackendType *types_table; MdbBackendType *types_table;
MdbBackendType *type_shortdate; MdbBackendType *type_shortdate;
MdbBackendType *type_autonum; MdbBackendType *type_autonum;
const char *short_now;
const char *long_now;
const char *charset_statement; const char *charset_statement;
const char *drop_statement; const char *drop_statement;
const char *constaint_not_empty_statement; const char *constaint_not_empty_statement;
@ -487,7 +490,7 @@ extern const MdbBackendType* mdb_get_colbacktype(const MdbColumn *col);
extern const char* mdb_get_colbacktype_string(const MdbColumn *col); extern const char* mdb_get_colbacktype_string(const MdbColumn *col);
extern int mdb_colbacktype_takes_length(const MdbColumn *col); extern int mdb_colbacktype_takes_length(const MdbColumn *col);
extern void mdb_init_backends(); extern void mdb_init_backends();
extern void mdb_register_backend(char *backend_name, guint32 capabilities, MdbBackendType *backend_type, MdbBackendType *type_shortdate, MdbBackendType *type_autonum, const char *charset_statement, const char *drop_statement, const char *constaint_not_empty_statement, const char *column_comment_statement, const char *table_comment_statement, gchar* (*quote_schema_name)(const gchar*, const gchar*)); extern void mdb_register_backend(char *backend_name, guint32 capabilities, MdbBackendType *backend_type, MdbBackendType *type_shortdate, MdbBackendType *type_autonum, const char *short_now, const char *long_now, const char *charset_statement, const char *drop_statement, const char *constaint_not_empty_statement, const char *column_comment_statement, const char *table_comment_statement, gchar* (*quote_schema_name)(const gchar*, const gchar*));
extern void mdb_remove_backends(); extern void mdb_remove_backends();
extern int mdb_set_default_backend(MdbHandle *mdb, const char *backend_name); extern int mdb_set_default_backend(MdbHandle *mdb, const char *backend_name);
extern void mdb_print_schema(MdbHandle *mdb, FILE *outfile, char *tabname, char *namespace, guint32 export_options); extern void mdb_print_schema(MdbHandle *mdb, FILE *outfile, char *tabname, char *namespace, guint32 export_options);

View File

@ -15,7 +15,7 @@
<widget class="GtkTable" id="table1"> <widget class="GtkTable" id="table1">
<property name="visible">True</property> <property name="visible">True</property>
<property name="border_width">33</property> <property name="border_width">33</property>
<property name="n_rows">9</property> <property name="n_rows">10</property>
<property name="n_columns">2</property> <property name="n_columns">2</property>
<property name="column_spacing">30</property> <property name="column_spacing">30</property>
<property name="row_spacing">8</property> <property name="row_spacing">8</property>
@ -247,8 +247,8 @@
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="right_attach">2</property> <property name="right_attach">2</property>
<property name="top_attach">8</property> <property name="top_attach">9</property>
<property name="bottom_attach">9</property> <property name="bottom_attach">10</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options"></property> <property name="y_options"></property>
</packing> </packing>
@ -266,27 +266,8 @@
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="right_attach">2</property> <property name="right_attach">2</property>
<property name="top_attach">7</property> <property name="top_attach">8</property>
<property name="bottom_attach">8</property> <property name="bottom_attach">9</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkCheckButton" id="cstnotempty_checkbox">
<property name="label" translatable="yes">Include Not Empty constraints</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">5</property>
<property name="bottom_attach">6</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options"></property> <property name="y_options"></property>
</packing> </packing>
@ -304,8 +285,8 @@
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="right_attach">2</property> <property name="right_attach">2</property>
<property name="top_attach">6</property> <property name="top_attach">7</property>
<property name="bottom_attach">7</property> <property name="bottom_attach">8</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options"></property> <property name="y_options"></property>
</packing> </packing>
@ -325,6 +306,47 @@
<child> <child>
<placeholder/> <placeholder/>
</child> </child>
<child>
<placeholder/>
</child>
<child>
<widget class="GtkCheckButton" id="cstnotempty_checkbox">
<property name="label" translatable="yes">Include Not Empty constraints</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">6</property>
<property name="bottom_attach">7</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkCheckButton" id="defaults_checkbox1">
<property name="label" translatable="yes">Include Default values</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">5</property>
<property name="bottom_attach">6</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
</widget> </widget>
<packing> <packing>
<property name="fill">False</property> <property name="fill">False</property>

View File

@ -40,6 +40,7 @@ static struct {
{ "cstnotnull_checkbox", MDB_SHEXP_CST_NOTNULL }, { "cstnotnull_checkbox", MDB_SHEXP_CST_NOTNULL },
{ "cstnotempty_checkbox", MDB_SHEXP_CST_NOTEMPTY}, { "cstnotempty_checkbox", MDB_SHEXP_CST_NOTEMPTY},
{ "comments_checkbox", MDB_SHEXP_COMMENTS}, { "comments_checkbox", MDB_SHEXP_COMMENTS},
{ "defaults_checkbox", MDB_SHEXP_DEFVALUES },
{ "index_checkbox", MDB_SHEXP_INDEXES}, { "index_checkbox", MDB_SHEXP_INDEXES},
{ "rel_checkbox", MDB_SHEXP_RELATIONS} { "rel_checkbox", MDB_SHEXP_RELATIONS}
}; };

View File

@ -1,5 +1,5 @@
/* MDB Tools - A library for reading MS Access database files /* MDB Tools - A library for reading MS Access database files
* Copyright (C) 2000 Brian Bruns * Copyright (C) 2000-2011 Brian Bruns and others
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public * modify it under the terms of the GNU Library General Public
@ -255,7 +255,7 @@ quote_schema_name_rquotes_merge(const gchar* schema, const gchar *name)
} }
static gchar* static gchar*
quote_with_squotes(gchar* value) quote_with_squotes(const gchar* value)
{ {
return quote_generic(value, '\'', '\''); return quote_generic(value, '\'', '\'');
} }
@ -337,8 +337,9 @@ void mdb_init_backends()
mdb_backends = g_hash_table_new(g_str_hash, g_str_equal); mdb_backends = g_hash_table_new(g_str_hash, g_str_equal);
mdb_register_backend("access", mdb_register_backend("access",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_SANITIZE, MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_DEFVALUES|MDB_SHEXP_SANITIZE,
mdb_access_types, NULL, NULL, mdb_access_types, NULL, NULL,
"Date()", "Date()",
"-- That file uses encoding %s\n", "-- That file uses encoding %s\n",
"DROP TABLE %s;\n", "DROP TABLE %s;\n",
NULL, NULL,
@ -346,8 +347,9 @@ void mdb_init_backends()
NULL, NULL,
quote_schema_name_bracket_merge); quote_schema_name_bracket_merge);
mdb_register_backend("sybase", mdb_register_backend("sybase",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_SANITIZE, MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_DEFVALUES|MDB_SHEXP_SANITIZE,
mdb_sybase_types, &mdb_sybase_shortdate_type, NULL, mdb_sybase_types, &mdb_sybase_shortdate_type, NULL,
"getdate()", "getdate()",
"-- That file uses encoding %s\n", "-- That file uses encoding %s\n",
"DROP TABLE %s;\n", "DROP TABLE %s;\n",
"ALTER TABLE %s ADD CHECK (%s <>'');\n", "ALTER TABLE %s ADD CHECK (%s <>'');\n",
@ -355,8 +357,9 @@ void mdb_init_backends()
"COMMENT ON TABLE %s IS %s;\n", "COMMENT ON TABLE %s IS %s;\n",
quote_schema_name_dquote); quote_schema_name_dquote);
mdb_register_backend("oracle", mdb_register_backend("oracle",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_COMMENTS|MDB_SHEXP_INDEXES|MDB_SHEXP_RELATIONS|MDB_SHEXP_SANITIZE, MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_COMMENTS|MDB_SHEXP_INDEXES|MDB_SHEXP_RELATIONS|MDB_SHEXP_DEFVALUES|MDB_SHEXP_SANITIZE,
mdb_oracle_types, &mdb_oracle_shortdate_type, NULL, mdb_oracle_types, &mdb_oracle_shortdate_type, NULL,
"current_date", "sysdate",
"-- That file uses encoding %s\n", "-- That file uses encoding %s\n",
"DROP TABLE %s;\n", "DROP TABLE %s;\n",
NULL, NULL,
@ -364,8 +367,9 @@ void mdb_init_backends()
"COMMENT ON TABLE %s IS %s;\n", "COMMENT ON TABLE %s IS %s;\n",
quote_schema_name_dquote); quote_schema_name_dquote);
mdb_register_backend("postgres", mdb_register_backend("postgres",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_INDEXES|MDB_SHEXP_RELATIONS|MDB_SHEXP_SANITIZE, MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_INDEXES|MDB_SHEXP_RELATIONS|MDB_SHEXP_DEFVALUES|MDB_SHEXP_SANITIZE,
mdb_postgres_types, &mdb_postgres_shortdate_type, &mdb_postgres_serial_type, mdb_postgres_types, &mdb_postgres_shortdate_type, &mdb_postgres_serial_type,
"current_date", "now()",
"SET client_encoding = '%s';\n", "SET client_encoding = '%s';\n",
"DROP TABLE IF EXISTS %s;\n", "DROP TABLE IF EXISTS %s;\n",
"ALTER TABLE %s ADD CHECK (%s <>'');\n", "ALTER TABLE %s ADD CHECK (%s <>'');\n",
@ -373,8 +377,9 @@ void mdb_init_backends()
"COMMENT ON TABLE %s IS %s;\n", "COMMENT ON TABLE %s IS %s;\n",
quote_schema_name_dquote); quote_schema_name_dquote);
mdb_register_backend("mysql", mdb_register_backend("mysql",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_SANITIZE, MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_DEFVALUES|MDB_SHEXP_SANITIZE,
mdb_mysql_types, &mdb_mysql_shortdate_type, NULL, mdb_mysql_types, &mdb_mysql_shortdate_type, NULL,
"current_date", "now()",
"-- That file uses encoding %s\n", "-- That file uses encoding %s\n",
"DROP TABLE IF EXISTS %s;\n", "DROP TABLE IF EXISTS %s;\n",
"ALTER TABLE %s ADD CHECK (%s <>'');\n", "ALTER TABLE %s ADD CHECK (%s <>'');\n",
@ -382,13 +387,15 @@ void mdb_init_backends()
"COMMENT ON TABLE %s IS %s;\n", "COMMENT ON TABLE %s IS %s;\n",
quote_schema_name_rquotes_merge); quote_schema_name_rquotes_merge);
} }
void mdb_register_backend(char *backend_name, guint32 capabilities, MdbBackendType *backend_type, MdbBackendType *type_shortdate, MdbBackendType *type_autonum, const char *charset_statement, const char *drop_statement, const char *constaint_not_empty_statement, const char *column_comment_statement, const char *table_comment_statement, gchar* (*quote_schema_name)(const gchar*, const gchar*)) void mdb_register_backend(char *backend_name, guint32 capabilities, MdbBackendType *backend_type, MdbBackendType *type_shortdate, MdbBackendType *type_autonum, const char *short_now, const char *long_now, const char *charset_statement, const char *drop_statement, const char *constaint_not_empty_statement, const char *column_comment_statement, const char *table_comment_statement, gchar* (*quote_schema_name)(const gchar*, const gchar*))
{ {
MdbBackend *backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend)); MdbBackend *backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
backend->capabilities = capabilities; backend->capabilities = capabilities;
backend->types_table = backend_type; backend->types_table = backend_type;
backend->type_shortdate = type_shortdate; backend->type_shortdate = type_shortdate;
backend->type_autonum = type_autonum; backend->type_autonum = type_autonum;
backend->short_now = short_now;
backend->long_now = long_now;
backend->charset_statement = charset_statement; backend->charset_statement = charset_statement;
backend->drop_statement = drop_statement; backend->drop_statement = drop_statement;
backend->constaint_not_empty_statement = constaint_not_empty_statement; backend->constaint_not_empty_statement = constaint_not_empty_statement;
@ -661,7 +668,7 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *namespace, gu
char* quoted_name; char* quoted_name;
int sanitize = export_options & MDB_SHEXP_SANITIZE; int sanitize = export_options & MDB_SHEXP_SANITIZE;
MdbProperties *props; MdbProperties *props;
char *prop_value; const char *prop_value;
if (sanitize) if (sanitize)
quoted_table_name = sanitize_name(entry->object_name); quoted_table_name = sanitize_name(entry->object_name);
@ -712,6 +719,44 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *namespace, gu
fputs(" NOT NULL", outfile); 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) if (i < table->num_cols - 1)
fputs(", \n", outfile); fputs(", \n", outfile);
else else
@ -722,8 +767,6 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *namespace, gu
/* Add the constraints on columns */ /* Add the constraints on columns */
for (i = 0; i < table->num_cols; i++) { for (i = 0; i < table->num_cols; i++) {
const gchar *prop_value;
col = g_ptr_array_index (table->columns, i); col = g_ptr_array_index (table->columns, i);
props = col->props; props = col->props;
if (!props) if (!props)

View File

@ -52,6 +52,8 @@ main (int argc, char **argv)
{"namespace", 1, NULL, 'N'}, {"namespace", 1, NULL, 'N'},
{"drop-table", 0, NULL, 0}, {"drop-table", 0, NULL, 0},
{"no-drop-table", 0, NULL, 0}, {"no-drop-table", 0, NULL, 0},
{"default-values", 0, NULL, 0},
{"no-default-values", 0, NULL, 0},
{"not-null", 0, NULL, 0}, {"not-null", 0, NULL, 0},
{"no-not-null", 0, NULL, 0}, {"no-not-null", 0, NULL, 0},
{"not-empty", 0, NULL, 0}, {"not-empty", 0, NULL, 0},
@ -88,6 +90,14 @@ main (int argc, char **argv)
export_options &= ~MDB_SHEXP_CST_NOTNULL; export_options &= ~MDB_SHEXP_CST_NOTNULL;
break; break;
} }
if (!strcmp(long_options[option_index].name, "default-values")) {
export_options |= MDB_SHEXP_DEFVALUES;
break;
}
if (!strcmp(long_options[option_index].name, "no-default-values")) {
export_options &= ~MDB_SHEXP_DEFVALUES;
break;
}
if (!strcmp(long_options[option_index].name, "not-empty")) { if (!strcmp(long_options[option_index].name, "not-empty")) {
export_options |= MDB_SHEXP_CST_NOTEMPTY; export_options |= MDB_SHEXP_CST_NOTEMPTY;
break; break;