2002-07-11 09:39:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2002-08-22 02:01:51 +02:00
|
|
|
* conversioncmds.c
|
2002-07-11 09:39:28 +02:00
|
|
|
* conversion creation command support code
|
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
2002-07-11 09:39:28 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/commands/conversioncmds.c
|
2002-07-11 09:39:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2005-07-07 22:40:02 +02:00
|
|
|
#include "catalog/dependency.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "catalog/indexing.h"
|
2006-07-11 18:35:33 +02:00
|
|
|
#include "catalog/pg_conversion.h"
|
2002-07-11 09:39:28 +02:00
|
|
|
#include "catalog/pg_type.h"
|
2010-11-26 23:27:23 +01:00
|
|
|
#include "commands/alter.h"
|
2002-07-11 09:39:28 +02:00
|
|
|
#include "commands/conversioncmds.h"
|
2006-07-11 18:35:33 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
2002-07-11 09:39:28 +02:00
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "parser/parse_func.h"
|
2020-03-10 10:22:52 +01:00
|
|
|
#include "utils/acl.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "utils/builtins.h"
|
2002-07-11 09:39:28 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2008-06-19 02:46:06 +02:00
|
|
|
#include "utils/rel.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "utils/syscache.h"
|
2002-07-11 09:39:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CREATE CONVERSION
|
|
|
|
*/
|
Change many routines to return ObjectAddress rather than OID
The changed routines are mostly those that can be directly called by
ProcessUtilitySlow; the intention is to make the affected object
information more precise, in support for future event trigger changes.
Originally it was envisioned that the OID of the affected object would
be enough, and in most cases that is correct, but upon actually
implementing the event trigger changes it turned out that ObjectAddress
is more widely useful.
Additionally, some command execution routines grew an output argument
that's an object address which provides further info about the executed
command. To wit:
* for ALTER DOMAIN / ADD CONSTRAINT, it corresponds to the address of
the new constraint
* for ALTER OBJECT / SET SCHEMA, it corresponds to the address of the
schema that originally contained the object.
* for ALTER EXTENSION {ADD, DROP} OBJECT, it corresponds to the address
of the object added to or dropped from the extension.
There's no user-visible change in this commit, and no functional change
either.
Discussion: 20150218213255.GC6717@tamriel.snowman.net
Reviewed-By: Stephen Frost, Andres Freund
2015-03-03 18:10:50 +01:00
|
|
|
ObjectAddress
|
2002-07-11 09:39:28 +02:00
|
|
|
CreateConversionCommand(CreateConversionStmt *stmt)
|
|
|
|
{
|
|
|
|
Oid namespaceId;
|
2002-09-04 22:31:48 +02:00
|
|
|
char *conversion_name;
|
2002-07-11 09:39:28 +02:00
|
|
|
AclResult aclresult;
|
2003-07-20 23:56:35 +02:00
|
|
|
int from_encoding;
|
2002-07-11 09:39:28 +02:00
|
|
|
int to_encoding;
|
|
|
|
Oid funcoid;
|
2003-07-20 23:56:35 +02:00
|
|
|
const char *from_encoding_name = stmt->for_encoding_name;
|
2002-07-11 09:39:28 +02:00
|
|
|
const char *to_encoding_name = stmt->to_encoding_name;
|
2002-09-04 22:31:48 +02:00
|
|
|
List *func_name = stmt->func_name;
|
2014-01-18 22:04:11 +01:00
|
|
|
static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
|
2009-02-27 17:35:26 +01:00
|
|
|
char result[1];
|
2002-07-11 09:39:28 +02:00
|
|
|
|
|
|
|
/* Convert list of names to a name and namespace */
|
2002-11-02 03:33:03 +01:00
|
|
|
namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
|
|
|
|
&conversion_name);
|
2002-07-11 09:39:28 +02:00
|
|
|
|
|
|
|
/* Check we have creation rights in target namespace */
|
|
|
|
aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2017-12-02 15:26:34 +01:00
|
|
|
aclcheck_error(aclresult, OBJECT_SCHEMA,
|
2003-08-01 02:15:26 +02:00
|
|
|
get_namespace_name(namespaceId));
|
2002-07-11 09:39:28 +02:00
|
|
|
|
|
|
|
/* Check the encoding names */
|
2003-07-20 23:56:35 +02:00
|
|
|
from_encoding = pg_char_to_encoding(from_encoding_name);
|
|
|
|
if (from_encoding < 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("source encoding \"%s\" does not exist",
|
|
|
|
from_encoding_name)));
|
2002-07-11 09:39:28 +02:00
|
|
|
|
|
|
|
to_encoding = pg_char_to_encoding(to_encoding_name);
|
|
|
|
if (to_encoding < 0)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("destination encoding \"%s\" does not exist",
|
|
|
|
to_encoding_name)));
|
2002-07-11 09:39:28 +02:00
|
|
|
|
2019-07-05 20:17:27 +02:00
|
|
|
/*
|
|
|
|
* We consider conversions to or from SQL_ASCII to be meaningless. (If
|
|
|
|
* you wish to change this, note that pg_do_encoding_conversion() and its
|
|
|
|
* sister functions have hard-wired fast paths for any conversion in which
|
|
|
|
* the source or target encoding is SQL_ASCII, so that an encoding
|
|
|
|
* conversion function declared for such a case will never be used.)
|
|
|
|
*/
|
|
|
|
if (from_encoding == PG_SQL_ASCII || to_encoding == PG_SQL_ASCII)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
|
|
|
errmsg("encoding conversion to or from \"SQL_ASCII\" is not supported")));
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check the existence of the conversion function. Function name could be
|
|
|
|
* a qualified name.
|
2002-07-11 09:39:28 +02:00
|
|
|
*/
|
2003-07-04 04:51:34 +02:00
|
|
|
funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
|
|
|
|
funcargs, false);
|
2002-07-11 09:39:28 +02:00
|
|
|
|
2008-11-14 18:40:56 +01:00
|
|
|
/* Check it returns VOID, else it's probably the wrong function */
|
|
|
|
if (get_func_rettype(funcoid) != VOIDOID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2016-06-10 00:02:36 +02:00
|
|
|
errmsg("encoding conversion function %s must return type %s",
|
|
|
|
NameListToString(func_name), "void")));
|
2008-11-14 18:40:56 +01:00
|
|
|
|
2002-11-02 03:33:03 +01:00
|
|
|
/* Check we have EXECUTE rights for the function */
|
2002-07-11 09:39:28 +02:00
|
|
|
aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2017-12-02 15:26:34 +01:00
|
|
|
aclcheck_error(aclresult, OBJECT_FUNCTION,
|
2003-08-01 02:15:26 +02:00
|
|
|
NameListToString(func_name));
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2009-02-27 17:35:26 +01:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Check that the conversion function is suitable for the requested source
|
|
|
|
* and target encodings. We do that by calling the function with an empty
|
|
|
|
* string; the conversion function should throw an error if it can't
|
|
|
|
* perform the requested conversion.
|
2009-02-27 17:35:26 +01:00
|
|
|
*/
|
|
|
|
OidFunctionCall5(funcoid,
|
|
|
|
Int32GetDatum(from_encoding),
|
|
|
|
Int32GetDatum(to_encoding),
|
|
|
|
CStringGetDatum(""),
|
|
|
|
CStringGetDatum(result),
|
|
|
|
Int32GetDatum(0));
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* All seem ok, go ahead (possible failure would be a duplicate conversion
|
|
|
|
* name)
|
2002-09-04 22:31:48 +02:00
|
|
|
*/
|
2012-12-24 00:25:03 +01:00
|
|
|
return ConversionCreate(conversion_name, namespaceId, GetUserId(),
|
|
|
|
from_encoding, to_encoding, funcoid, stmt->def);
|
2002-07-11 09:39:28 +02:00
|
|
|
}
|