mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-06-28 15:39:02 +08:00
Add index and primary key output to the sqlite schema writer backend. (#402)
This commit is contained in:
parent
9aa68a6ba1
commit
0e77b68e76
@ -401,7 +401,7 @@ void mdb_init_backends(MdbHandle *mdb)
|
|||||||
"COMMENT %s",
|
"COMMENT %s",
|
||||||
quote_schema_name_rquotes_merge);
|
quote_schema_name_rquotes_merge);
|
||||||
mdb_register_backend(mdb, "sqlite",
|
mdb_register_backend(mdb, "sqlite",
|
||||||
MDB_SHEXP_DROPTABLE|MDB_SHEXP_DEFVALUES|MDB_SHEXP_BULK_INSERT,
|
MDB_SHEXP_DROPTABLE|MDB_SHEXP_DEFVALUES|MDB_SHEXP_BULK_INSERT|MDB_SHEXP_INDEXES|MDB_SHEXP_CST_NOTNULL,
|
||||||
mdb_sqlite_types, NULL, NULL,
|
mdb_sqlite_types, NULL, NULL,
|
||||||
"date('now')", "date('now')",
|
"date('now')", "date('now')",
|
||||||
"%Y-%m-%d %H:%M:%S",
|
"%Y-%m-%d %H:%M:%S",
|
||||||
@ -571,7 +571,47 @@ mdb_get_index_name(int backend, MdbTableDef *table, MdbIndex *idx)
|
|||||||
|
|
||||||
return index_name;
|
return index_name;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* mdb_print_pk - print primary key constraint
|
||||||
|
* @output: Where to print the sql
|
||||||
|
* @table: Table to process
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
mdb_print_pk_if_sqlite(FILE *outfile, MdbTableDef *table)
|
||||||
|
{
|
||||||
|
unsigned int i, j;
|
||||||
|
MdbHandle *mdb = table->entry->mdb;
|
||||||
|
MdbIndex *idx;
|
||||||
|
MdbColumn *col;
|
||||||
|
char *quoted_name;
|
||||||
|
// this is only necessary for sqlite
|
||||||
|
if (strcmp(mdb->backend_name, "sqlite") != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (table->indices==NULL)
|
||||||
|
mdb_read_indices(table);
|
||||||
|
|
||||||
|
for (i = 0; i < table->num_idxs; i++) {
|
||||||
|
idx = g_ptr_array_index(table->indices, i);
|
||||||
|
if (idx->index_type == 1) {
|
||||||
|
fprintf(outfile, "\t, PRIMARY KEY (");
|
||||||
|
for (j = 0; j < idx->num_keys; j++) {
|
||||||
|
if (j)
|
||||||
|
fprintf(outfile, ", ");
|
||||||
|
col = g_ptr_array_index(table->columns, idx->key_col_num[j] - 1);
|
||||||
|
quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name);
|
||||||
|
quoted_name = mdb_normalise_and_replace(mdb, "ed_name);
|
||||||
|
fprintf(outfile, "%s", quoted_name);
|
||||||
|
if (idx->index_type != 1 && idx->key_col_order[j])
|
||||||
|
/* no DESC for primary keys */
|
||||||
|
fprintf(outfile, " DESC");
|
||||||
|
|
||||||
|
g_free(quoted_name);
|
||||||
|
}
|
||||||
|
fprintf(outfile, ")\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* mdb_print_indexes
|
* mdb_print_indexes
|
||||||
* @output: Where to print the sql
|
* @output: Where to print the sql
|
||||||
@ -596,12 +636,15 @@ mdb_print_indexes(FILE* outfile, MdbTableDef *table, char *dbnamespace)
|
|||||||
backend = MDB_BACKEND_MYSQL;
|
backend = MDB_BACKEND_MYSQL;
|
||||||
} else if (!strcmp(mdb->backend_name, "oracle")) {
|
} else if (!strcmp(mdb->backend_name, "oracle")) {
|
||||||
backend = MDB_BACKEND_ORACLE;
|
backend = MDB_BACKEND_ORACLE;
|
||||||
|
} else if (!strcmp(mdb->backend_name, "sqlite")) {
|
||||||
|
backend = MDB_BACKEND_SQLITE;
|
||||||
} else {
|
} else {
|
||||||
fprintf(outfile, "-- Indexes are not implemented for %s\n\n", mdb->backend_name);
|
fprintf(outfile, "-- Indexes are not implemented for %s\n\n", mdb->backend_name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read indexes */
|
/* read indexes */
|
||||||
|
if (table->indices==NULL)
|
||||||
mdb_read_indices(table);
|
mdb_read_indices(table);
|
||||||
|
|
||||||
fprintf (outfile, "-- CREATE INDEXES ...\n");
|
fprintf (outfile, "-- CREATE INDEXES ...\n");
|
||||||
@ -613,6 +656,9 @@ mdb_print_indexes(FILE* outfile, MdbTableDef *table, char *dbnamespace)
|
|||||||
idx = g_ptr_array_index (table->indices, i);
|
idx = g_ptr_array_index (table->indices, i);
|
||||||
if (idx->index_type==2)
|
if (idx->index_type==2)
|
||||||
continue;
|
continue;
|
||||||
|
/* Sqlite3 primary keys have to be issued as a table constraint */
|
||||||
|
if (idx->index_type == 1 && backend == MDB_BACKEND_SQLITE)
|
||||||
|
continue;
|
||||||
|
|
||||||
index_name = mdb_get_index_name(backend, table, idx);
|
index_name = mdb_get_index_name(backend, table, idx);
|
||||||
switch (backend) {
|
switch (backend) {
|
||||||
@ -630,7 +676,10 @@ mdb_print_indexes(FILE* outfile, MdbTableDef *table, char *dbnamespace)
|
|||||||
}
|
}
|
||||||
|
|
||||||
quoted_name = mdb_normalise_and_replace(mdb, "ed_name);
|
quoted_name = mdb_normalise_and_replace(mdb, "ed_name);
|
||||||
|
if (idx->num_keys == 0) {
|
||||||
|
fprintf(outfile, "-- WARNING: found no keys for index %s - ignored\n", quoted_name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (idx->index_type==1) {
|
if (idx->index_type==1) {
|
||||||
switch (backend) {
|
switch (backend) {
|
||||||
case MDB_BACKEND_ORACLE:
|
case MDB_BACKEND_ORACLE:
|
||||||
@ -645,6 +694,7 @@ mdb_print_indexes(FILE* outfile, MdbTableDef *table, char *dbnamespace)
|
|||||||
switch (backend) {
|
switch (backend) {
|
||||||
case MDB_BACKEND_ORACLE:
|
case MDB_BACKEND_ORACLE:
|
||||||
case MDB_BACKEND_POSTGRES:
|
case MDB_BACKEND_POSTGRES:
|
||||||
|
case MDB_BACKEND_SQLITE:
|
||||||
fprintf(outfile, "CREATE");
|
fprintf(outfile, "CREATE");
|
||||||
if (idx->flags & MDB_IDX_UNIQUE)
|
if (idx->flags & MDB_IDX_UNIQUE)
|
||||||
fprintf (outfile, " UNIQUE");
|
fprintf (outfile, " UNIQUE");
|
||||||
@ -968,6 +1018,11 @@ generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *dbnamespace,
|
|||||||
fputs("\n", outfile);
|
fputs("\n", outfile);
|
||||||
} /* for */
|
} /* for */
|
||||||
|
|
||||||
|
if (export_options & MDB_SHEXP_INDEXES) {
|
||||||
|
// sqlite does not support ALTER TABLE PRIMARY KEY, so we need to place it directly into CREATE TABLE
|
||||||
|
mdb_print_pk_if_sqlite(outfile, table);
|
||||||
|
}
|
||||||
|
|
||||||
fputs(")", outfile);
|
fputs(")", outfile);
|
||||||
if (mdb->default_backend->per_table_comment_statement && export_options & MDB_SHEXP_COMMENTS) {
|
if (mdb->default_backend->per_table_comment_statement && export_options & MDB_SHEXP_COMMENTS) {
|
||||||
prop_value = mdb_table_get_prop(table, "Description");
|
prop_value = mdb_table_get_prop(table, "Description");
|
||||||
|
Loading…
Reference in New Issue
Block a user