1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-27 23:21:58 +03:00

WITH CHECK OPTION support for auto-updatable VIEWs

For simple views which are automatically updatable, this patch allows
the user to specify what level of checking should be done on records
being inserted or updated.  For 'LOCAL CHECK', new tuples are validated
against the conditionals of the view they are being inserted into, while
for 'CASCADED CHECK' the new tuples are validated against the
conditionals for all views involved (from the top down).

This option is part of the SQL specification.

Dean Rasheed, reviewed by Pavel Stehule
This commit is contained in:
Stephen Frost
2013-07-18 17:10:16 -04:00
parent 6f9e39bc99
commit 4cbe3ac3e8
33 changed files with 1245 additions and 107 deletions

View File

@ -27,6 +27,7 @@
#include "parser/parse_relation.h"
#include "rewrite/rewriteDefine.h"
#include "rewrite/rewriteManip.h"
#include "rewrite/rewriteHandler.h"
#include "rewrite/rewriteSupport.h"
#include "utils/acl.h"
#include "utils/builtins.h"
@ -37,6 +38,24 @@
static void checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc);
/*---------------------------------------------------------------------
* Validator for "check_option" reloption on views. The allowed values
* are "local" and "cascaded".
*/
void
validateWithCheckOption(char *value)
{
if (value == NULL ||
(pg_strcasecmp(value, "local") != 0 &&
pg_strcasecmp(value, "cascaded") != 0))
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid value for \"check_option\" option"),
errdetail("Valid values are \"local\", and \"cascaded\".")));
}
}
/*---------------------------------------------------------------------
* DefineVirtualRelation
*
@ -374,6 +393,9 @@ DefineView(ViewStmt *stmt, const char *queryString)
Query *viewParse;
Oid viewOid;
RangeVar *view;
ListCell *cell;
bool check_option;
bool security_barrier;
/*
* Run parse analysis to convert the raw parse tree to a Query. Note this
@ -410,6 +432,52 @@ DefineView(ViewStmt *stmt, const char *queryString)
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("views must not contain data-modifying statements in WITH")));
/*
* If the user specified the WITH CHECK OPTION, add it to the list of
* reloptions.
*/
if (stmt->withCheckOption == LOCAL_CHECK_OPTION)
stmt->options = lappend(stmt->options,
makeDefElem("check_option",
(Node *) makeString("local")));
else if (stmt->withCheckOption == CASCADED_CHECK_OPTION)
stmt->options = lappend(stmt->options,
makeDefElem("check_option",
(Node *) makeString("cascaded")));
/*
* Check that the view is auto-updatable if WITH CHECK OPTION was
* specified.
*/
check_option = false;
security_barrier = false;
foreach(cell, stmt->options)
{
DefElem *defel = (DefElem *) lfirst(cell);
if (pg_strcasecmp(defel->defname, "check_option") == 0)
check_option = true;
if (pg_strcasecmp(defel->defname, "security_barrier") == 0)
security_barrier = defGetBoolean(defel);
}
/*
* If the check option is specified, look to see if the view is
* actually auto-updatable or not.
*/
if (check_option)
{
const char *view_updatable_error =
view_query_is_auto_updatable(viewParse, security_barrier);
if (view_updatable_error)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("WITH CHECK OPTION is supported only on auto-updatable views"),
errhint("%s", view_updatable_error)));
}
/*
* If a list of column names was given, run through and insert these into
* the actual query tree. - thomas 2000-03-08