Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1. table-partition option and 2. foreign key constraint query after table create query #100

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CMakeCache.txt
CMakeFiles/
Makefile
cmake_install.cmake
install_manifest.txt
mini/CMakeFiles/
mini/Makefile
mini/cmake_install.cmake
mini/libmini.so
pgquarrel
.vscode/
pg.ini
todo.md
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ typedef struct QuarrelGeneralOptions
bool statistics;
bool subscription;
bool table;
bool tablepartition;
bool textsearch;
bool transform;
bool trigger;
Expand Down
8 changes: 7 additions & 1 deletion src/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ getIndexes(PGconn *c, int *n)

logNoise("index: server version: %d", PQserverVersion(c));

query = psprintf("SELECT c.oid, n.nspname, c.relname, t.spcname AS tablespacename, pg_get_indexdef(c.oid) AS indexdef, array_to_string(c.reloptions, ', ') AS reloptions, obj_description(c.oid, 'pg_class') AS description FROM pg_class c INNER JOIN pg_namespace n ON (c.relnamespace = n.oid) INNER JOIN pg_index i ON (i.indexrelid = c.oid) LEFT JOIN pg_tablespace t ON (c.reltablespace = t.oid) WHERE relkind = 'i' AND nspname !~ '^pg_' AND nspname <> 'information_schema' %s%s AND NOT indisprimary ORDER BY nspname, relname", include_schema_str, exclude_schema_str);
if (PQserverVersion(c) >= 100000 && !options.tablepartition){
// THIS QUERY IS OPTIMIZED ONLY FOR DISCOVERING INDEXES WHICH DOES NOT BELONG PARTITIONS
query = psprintf("SELECT c.oid, n.nspname, c.relname, t.spcname AS tablespacename, pg_get_indexdef(c.oid) AS indexdef, array_to_string(c.reloptions, ', ') AS reloptions, obj_description(c.oid, 'pg_class') AS description FROM pg_class c INNER JOIN pg_namespace n ON (c.relnamespace = n.oid) INNER JOIN pg_index i ON (i.indexrelid = c.oid) LEFT JOIN pg_tablespace t ON (c.reltablespace = t.oid) LEFT JOIN pg_class pt ON NOT c.relispartition AND i.indrelid=pt.oid WHERE c.relkind = 'i' AND NOT c.relispartition AND NOT pt.relispartition AND nspname !~ '^pg_' AND nspname <> 'information_schema' %s%s AND NOT indisprimary ORDER BY nspname, c.relname", include_schema_str, exclude_schema_str);
}
else {
query = psprintf("SELECT c.oid, n.nspname, c.relname, t.spcname AS tablespacename, pg_get_indexdef(c.oid) AS indexdef, array_to_string(c.reloptions, ', ') AS reloptions, obj_description(c.oid, 'pg_class') AS description FROM pg_class c INNER JOIN pg_namespace n ON (c.relnamespace = n.oid) INNER JOIN pg_index i ON (i.indexrelid = c.oid) LEFT JOIN pg_tablespace t ON (c.reltablespace = t.oid) WHERE relkind = 'i' AND nspname !~ '^pg_' AND nspname <> 'information_schema' %s%s AND NOT indisprimary ORDER BY nspname, relname", include_schema_str, exclude_schema_str);
}

res = PQexec(c, query);

Expand Down
14 changes: 14 additions & 0 deletions src/quarrel.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ help(void)
(opts.general.subscription) ? "true" : "false");
printf(" --table=BOOL table (default: %s)\n",
(opts.general.table) ? "true" : "false");
printf(" --table-partition=BOOL table partition (default: %s)\n",
(opts.general.tablepartition) ? "true" : "false");
printf(" --text-search=BOOL text search (default: %s)\n",
(opts.general.textsearch) ? "true" : "false");
printf(" --transform=BOOL transform (default: %s)\n",
Expand Down Expand Up @@ -365,6 +367,7 @@ loadConfig(const char *cf, QuarrelOptions *options)
options->general.statistics = false; /* general - statistics */
options->general.subscription = false; /* general - subscription */
options->general.table = true; /* general - table */
options->general.tablepartition = true; /* general - table-partition */
options->general.textsearch = false; /* general - text-search */
options->general.transform = false; /* general - transform */
options->general.trigger = true; /* general - trigger */
Expand Down Expand Up @@ -592,6 +595,10 @@ loadConfig(const char *cf, QuarrelOptions *options)
options->general.table = parseBoolean("table", mini_file_get_value(config,
"general", "table"));

if (mini_file_get_value(config, "general", "table-partition") != NULL)
options->general.tablepartition = parseBoolean("table-partition", mini_file_get_value(config,
"general", "table-partition"));

if (mini_file_get_value(config, "general", "text-search") != NULL)
options->general.textsearch = parseBoolean("text-search",
mini_file_get_value(config,
Expand Down Expand Up @@ -4671,6 +4678,7 @@ int main(int argc, char *argv[])
{"temp-directory", required_argument, NULL, 37},
{"include-schema", required_argument, NULL, 45},
{"exclude-schema", required_argument, NULL, 46},
{"table-partition", required_argument, NULL, 47},
{NULL, 0, NULL, 0}
};

Expand Down Expand Up @@ -4862,6 +4870,10 @@ int main(int argc, char *argv[])
gopts.table = parseBoolean("table", optarg);
gopts_given.table = true;
break;
case 47:
gopts.tablepartition = parseBoolean("table-partition", optarg);
gopts_given.tablepartition = true;
break;
case 32:
gopts.textsearch = parseBoolean("text-search", optarg);
gopts_given.textsearch = true;
Expand Down Expand Up @@ -5007,6 +5019,8 @@ int main(int argc, char *argv[])
options.subscription = gopts.subscription;
if (gopts_given.table)
options.table = gopts.table;
if (gopts_given.tablepartition)
options.tablepartition = gopts.tablepartition;
if (gopts_given.textsearch)
options.textsearch = gopts.textsearch;
if (gopts_given.transform)
Expand Down
10 changes: 5 additions & 5 deletions src/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ getTables(PGconn *c, int *n, char k)
{
if (PGQ_IS_REGULAR_OR_PARTITIONED_TABLE(k))
{
query = psprintf("SELECT c.oid, n.nspname, c.relname, c.relkind, t.spcname AS tablespacename, c.relpersistence, array_to_string(c.reloptions, ', ') AS reloptions, obj_description(c.oid, 'pg_class') AS description, pg_get_userbyid(c.relowner) AS relowner, relacl, relreplident, reloftype, o.nspname AS typnspname, y.typname, c.relispartition, pg_get_partkeydef(c.oid) AS partitionkeydef, pg_get_expr(c.relpartbound, c.oid) AS partitionbound, c.relhassubclass FROM pg_class c INNER JOIN pg_namespace n ON (c.relnamespace = n.oid) LEFT JOIN pg_tablespace t ON (c.reltablespace = t.oid) LEFT JOIN (pg_type y INNER JOIN pg_namespace o ON (y.typnamespace = o.oid)) ON (c.reloftype = y.oid) WHERE relkind IN ('r', 'p') AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' %s%s AND NOT EXISTS(SELECT 1 FROM pg_depend d WHERE t.oid = d.objid AND d.deptype = 'e') ORDER BY n.nspname, relname", include_schema_str, exclude_schema_str);
query = psprintf("SELECT c.oid, n.nspname, c.relname, c.relkind, t.spcname AS tablespacename, c.relpersistence, array_to_string(c.reloptions, ', ') AS reloptions, obj_description(c.oid, 'pg_class') AS description, pg_get_userbyid(c.relowner) AS relowner, relacl, relreplident, reloftype, o.nspname AS typnspname, y.typname, c.relispartition, pg_get_partkeydef(c.oid) AS partitionkeydef, pg_get_expr(c.relpartbound, c.oid) AS partitionbound, c.relhassubclass FROM pg_class c INNER JOIN pg_namespace n ON (c.relnamespace = n.oid) LEFT JOIN pg_tablespace t ON (c.reltablespace = t.oid) LEFT JOIN (pg_type y INNER JOIN pg_namespace o ON (y.typnamespace = o.oid)) ON (c.reloftype = y.oid) WHERE relkind IN ('r', 'p') AND c.relispartition=%s AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' %s%s AND NOT EXISTS(SELECT 1 FROM pg_depend d WHERE t.oid = d.objid AND d.deptype = 'e') ORDER BY n.nspname, relname", options.tablepartition? "TRUE":"FALSE", include_schema_str, exclude_schema_str);
}
else if (PGQ_IS_FOREIGN_TABLE(k))
{
Expand Down Expand Up @@ -1345,10 +1345,10 @@ dumpCreateTable(FILE *output, FILE *output2, PQLTable *t)
/* print foreign key constraints */
for (i = 0; i < t->nfk; i++)
{
fprintf(output, "\n\n");
fprintf(output, "ALTER TABLE ONLY %s.%s\n", schema, tabname);
fprintf(output, "\tADD CONSTRAINT %s %s", t->fk[i].conname, t->fk[i].condef);
fprintf(output, ";");
fprintf(output2, "\n\n");
fprintf(output2, "ALTER TABLE ONLY %s.%s\n", schema, tabname);
fprintf(output2, "\tADD CONSTRAINT %s %s", t->fk[i].conname, t->fk[i].condef);
fprintf(output2, ";");
}

/* XXX Should it belong to sequence.c? */
Expand Down