Add database comments in mysql exports

This commit is contained in:
Mihai Draghicioiu 2014-08-20 21:50:03 +02:00 committed by Nirgal Vourgère
parent eaa9af89c3
commit a2587a6830
2 changed files with 47 additions and 12 deletions

View File

@ -190,7 +190,9 @@ typedef struct {
const char *drop_statement;
const char *constaint_not_empty_statement;
const char *column_comment_statement;
const char *per_column_comment_statement;
const char *table_comment_statement;
const char *per_table_comment_statement;
gchar* (*quote_schema_name)(const gchar*, const gchar*);
} MdbBackend;
@ -489,7 +491,7 @@ extern const MdbBackendType* mdb_get_colbacktype(const MdbColumn *col);
extern const char* mdb_get_colbacktype_string(const MdbColumn *col);
extern int mdb_colbacktype_takes_length(const MdbColumn *col);
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 *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_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 *per_column_comment_statement, const char *table_comment_statement, const char *per_table_comment_statement, gchar* (*quote_schema_name)(const gchar*, const gchar*));
extern void mdb_remove_backends();
extern int mdb_set_default_backend(MdbHandle *mdb, const char *backend_name);
extern void mdb_print_schema(MdbHandle *mdb, FILE *outfile, char *tabname, char *dbnamespace, guint32 export_options);

View File

@ -322,6 +322,8 @@ void mdb_init_backends()
NULL,
NULL,
NULL,
NULL,
NULL,
quote_schema_name_bracket_merge);
mdb_register_backend("sybase",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_DEFVALUES,
@ -331,7 +333,9 @@ void mdb_init_backends()
"DROP TABLE %s;\n",
"ALTER TABLE %s ADD CHECK (%s <>'');\n",
"COMMENT ON COLUMN %s.%s IS %s;\n",
NULL,
"COMMENT ON TABLE %s IS %s;\n",
NULL,
quote_schema_name_dquote);
mdb_register_backend("oracle",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_COMMENTS|MDB_SHEXP_INDEXES|MDB_SHEXP_RELATIONS|MDB_SHEXP_DEFVALUES,
@ -341,7 +345,9 @@ void mdb_init_backends()
"DROP TABLE %s;\n",
NULL,
"COMMENT ON COLUMN %s.%s IS %s;\n",
NULL,
"COMMENT ON TABLE %s IS %s;\n",
NULL,
quote_schema_name_dquote);
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_DEFVALUES,
@ -351,20 +357,24 @@ void mdb_init_backends()
"DROP TABLE IF EXISTS %s;\n",
"ALTER TABLE %s ADD CHECK (%s <>'');\n",
"COMMENT ON COLUMN %s.%s IS %s;\n",
NULL,
"COMMENT ON TABLE %s IS %s;\n",
NULL,
quote_schema_name_dquote);
mdb_register_backend("mysql",
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_DEFVALUES,
MDB_SHEXP_DROPTABLE|MDB_SHEXP_CST_NOTNULL|MDB_SHEXP_CST_NOTEMPTY|MDB_SHEXP_COMMENTS|MDB_SHEXP_INDEXES|MDB_SHEXP_DEFVALUES,
mdb_mysql_types, &mdb_mysql_shortdate_type, NULL,
"current_date", "now()",
"-- That file uses encoding %s\n",
"DROP TABLE IF EXISTS %s;\n",
"ALTER TABLE %s ADD CHECK (%s <>'');\n",
"COMMENT ON COLUMN %s.%s IS %s;\n",
"COMMENT ON TABLE %s IS %s;\n",
NULL,
"COMMENT %s",
NULL,
"COMMENT %s",
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 *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*))
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 *per_column_comment_statement, const char *table_comment_statement, const char *per_table_comment_statement, gchar* (*quote_schema_name)(const gchar*, const gchar*))
{
MdbBackend *backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
backend->capabilities = capabilities;
@ -377,7 +387,9 @@ void mdb_register_backend(char *backend_name, guint32 capabilities, MdbBackendTy
backend->drop_statement = drop_statement;
backend->constaint_not_empty_statement = constaint_not_empty_statement;
backend->column_comment_statement = column_comment_statement;
backend->per_column_comment_statement = per_column_comment_statement;
backend->table_comment_statement = table_comment_statement;
backend->per_table_comment_statement = per_table_comment_statement;
backend->quote_schema_name = quote_schema_name;
g_hash_table_insert(mdb_backends, backend_name, backend);
}
@ -440,7 +452,7 @@ mdb_print_indexes(FILE* outfile, MdbTableDef *table, char *dbnamespace)
MdbIndex *idx;
MdbColumn *col;
if (strcmp(mdb->backend_name, "postgres")) {
if (strcmp(mdb->backend_name, "postgres") && strcmp(mdb->backend_name, "mysql")) {
fprintf(outfile, "-- Indexes are not implemented for %s\n\n", mdb->backend_name);
return;
}
@ -663,12 +675,23 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *dbnamespace,
free(quoted_name);
if (mdb_colbacktype_takes_length(col)) {
/* more portable version from DW patch */
if (col->col_size == 0)
fputs(" (255)", outfile);
fputs(" (255)", outfile);
else
fprintf(outfile, " (%d)", col->col_size);
fprintf(outfile, " (%d)", col->col_size);
}
if (mdb->default_backend->per_column_comment_statement && export_options & MDB_SHEXP_COMMENTS) {
prop_value = mdb_col_get_prop(col, "Description");
if (prop_value) {
char *comment = quote_with_squotes(prop_value);
fputs(" ", outfile);
fprintf(outfile,
mdb->default_backend->per_column_comment_statement,
comment);
free(comment);
}
}
if (export_options & MDB_SHEXP_CST_NOTNULL) {
@ -725,7 +748,17 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *dbnamespace,
fputs("\n", outfile);
} /* for */
fputs(");\n", outfile);
fputs(")", outfile);
if (mdb->default_backend->per_table_comment_statement && export_options & MDB_SHEXP_COMMENTS) {
prop_value = mdb_table_get_prop(table, "Description");
if (prop_value) {
char *comment = quote_with_squotes(prop_value);
fputs(" ", outfile);
fprintf(outfile, mdb->default_backend->per_table_comment_statement, comment);
free(comment);
}
}
fputs(";\n", outfile);
/* Add the constraints on columns */
for (i = 0; i < table->num_cols; i++) {
@ -744,7 +777,7 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *dbnamespace,
quoted_table_name, quoted_name);
}
if (export_options & MDB_SHEXP_COMMENTS) {
if (mdb->default_backend->column_comment_statement && export_options & MDB_SHEXP_COMMENTS) {
prop_value = mdb_col_get_prop(col, "Description");
if (prop_value) {
char *comment = quote_with_squotes(prop_value);
@ -759,7 +792,7 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *dbnamespace,
}
/* Add the constraints on table */
if (export_options & MDB_SHEXP_COMMENTS) {
if (mdb->default_backend->table_comment_statement && export_options & MDB_SHEXP_COMMENTS) {
prop_value = mdb_table_get_prop(table, "Description");
if (prop_value) {
char *comment = quote_with_squotes(prop_value);