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
|
|
|
|
*
|
2015-01-06 17:43:47 +01:00
|
|
|
* Portions Copyright (c) 1996-2015, 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"
|
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "access/heapam.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"
|
2008-03-27 04:57:34 +01:00
|
|
|
#include "catalog/pg_conversion_fn.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"
|
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
|
|
|
|
*/
|
2012-12-24 00:25:03 +01:00
|
|
|
Oid
|
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)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
|
|
|
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
|
|
|
|
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),
|
2009-06-11 16:49:15 +02:00
|
|
|
errmsg("encoding conversion function %s must return type \"void\"",
|
|
|
|
NameListToString(func_name))));
|
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)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_PROC,
|
|
|
|
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
|
|
|
}
|