Moves validation to preprocess for createdb

pull/7240/head
gindibay 2023-11-13 15:49:19 +03:00
parent 712fd8ebf3
commit a9977e8840
2 changed files with 54 additions and 34 deletions

View File

@ -21,6 +21,7 @@
#include "catalog/pg_database_d.h"
#include "catalog/pg_tablespace.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
@ -270,6 +271,55 @@ PreprocessAlterDatabaseSetStmt(Node *node, const char *queryString,
}
/*
* This function validates the options provided for the CREATE DATABASE command.
* It iterates over each option in the stmt->options list and checks if it's supported.
* If an unsupported option is found, or if a supported option has an invalid value,
* it raises an error.
*
* Parameters:
* stmt: A CreatedbStmt struct representing a CREATE DATABASE command.
* The options field is a list of DefElem structs, each representing an option.
*
* Currently, this function checks for the following:
* - The "oid" option is not supported.
* - The "template" option is only supported with the value "template1".
* - The "strategy" option is only supported with the value "wal_log".
*
* If any of these checks fail, the function calls ereport to raise an error.
*/
static void
EnsureSupportedCreateDatabaseCommand(CreatedbStmt *stmt)
{
DefElem *option = NULL;
foreach_ptr(option, stmt->options)
{
if (strcmp(option->defname, "oid") == 0)
{
ereport(ERROR,
errmsg("CREATE DATABASE option \"%s\" is not supported",
option->defname));
}
char *optionValue = defGetString(option);
if (strcmp(option->defname, "template") == 0 && strcmp(optionValue,
"template1") != 0)
{
ereport(ERROR, errmsg("Only template1 is supported as template "
"parameter for CREATE DATABASE"));
}
if (strcmp(option->defname, "strategy") == 0 && strcmp(optionValue, "wal_log") !=
0)
{
ereport(ERROR, errmsg("Only wal_log is supported as strategy "
"parameter for CREATE DATABASE"));
}
}
}
/*
* PostprocessAlterDatabaseStmt is executed before the statement is applied to the local
* postgres instance.
@ -288,8 +338,9 @@ PreprocessCreateDatabaseStmt(Node *node, const char *queryString,
EnsureCoordinator();
/*Validate the statement */
DeparseTreeNode(node);
/*validate the statement*/
CreatedbStmt *stmt = castNode(CreatedbStmt, node);
EnsureSupportedCreateDatabaseCommand(stmt);
return NIL;
}

View File

@ -251,37 +251,6 @@ DeparseAlterDatabaseSetStmt(Node *node)
}
/*
* Validates for if option is template, lc_type, locale or lc_collate, propagation will
* not be supported since template and strategy options are not stored in the catalog
* and lc_type, locale and lc_collate options depends on template parameter.
*/
static void
ValidateCreateDatabaseOptions(DefElem *option)
{
if (strcmp(option->defname, "oid") == 0)
{
ereport(ERROR,
errmsg("CREATE DATABASE option \"%s\" is not supported",
option->defname));
}
char *optionValue = defGetString(option);
if (strcmp(option->defname, "template") == 0 && strcmp(optionValue, "template1") != 0)
{
ereport(ERROR, errmsg("Only template1 is supported as template "
"parameter for CREATE DATABASE"));
}
if (strcmp(option->defname, "strategy") == 0 && strcmp(optionValue, "wal_log") != 0)
{
ereport(ERROR, errmsg("Only wal_log is supported as strategy "
"parameter for CREATE DATABASE"));
}
}
static void
AppendCreateDatabaseStmt(StringInfo buf, CreatedbStmt *stmt)
{
@ -293,7 +262,7 @@ AppendCreateDatabaseStmt(StringInfo buf, CreatedbStmt *stmt)
foreach_ptr(option, stmt->options)
{
ValidateCreateDatabaseOptions(option);
/*ValidateCreateDatabaseOptions(option); */
DefElemOptionToStatement(buf, option, create_database_option_formats,
lengthof(create_database_option_formats));