postgresql/src/backend/commands/conversioncmds.c

168 lines
5.1 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* conversioncmds.c
* conversion creation command support code
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
2010-09-20 22:08:53 +02:00
* src/backend/commands/conversioncmds.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
2003-06-27 16:45:32 +02:00
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/dependency.h"
2003-06-27 16:45:32 +02:00
#include "catalog/indexing.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_conversion_fn.h"
#include "catalog/pg_type.h"
#include "commands/alter.h"
#include "commands/conversioncmds.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
2003-06-27 16:45:32 +02:00
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
2003-06-27 16:45:32 +02:00
#include "utils/syscache.h"
/*
* CREATE CONVERSION
*/
void
CreateConversionCommand(CreateConversionStmt *stmt)
{
Oid namespaceId;
2002-09-04 22:31:48 +02:00
char *conversion_name;
AclResult aclresult;
int from_encoding;
int to_encoding;
Oid funcoid;
const char *from_encoding_name = stmt->for_encoding_name;
const char *to_encoding_name = stmt->to_encoding_name;
2002-09-04 22:31:48 +02:00
List *func_name = stmt->func_name;
static Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
char result[1];
/* Convert list of names to a name and namespace */
namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
&conversion_name);
/* Check we have creation rights in target namespace */
aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
get_namespace_name(namespaceId));
/* Check the encoding names */
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)));
to_encoding = pg_char_to_encoding(to_encoding_name);
if (to_encoding < 0)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("destination encoding \"%s\" does not exist",
to_encoding_name)));
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.
*/
funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
funcargs, false);
/* Check it returns VOID, else it's probably the wrong function */
if (get_func_rettype(funcoid) != VOIDOID)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("encoding conversion function %s must return type \"void\"",
NameListToString(func_name))));
/* Check we have EXECUTE rights for the function */
aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_PROC,
NameListToString(func_name));
2002-09-04 22:31:48 +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.
*/
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
*/
ConversionCreate(conversion_name, namespaceId, GetUserId(),
from_encoding, to_encoding, funcoid, stmt->def);
}
2003-06-27 16:45:32 +02:00
/*
* Rename conversion
*/
void
RenameConversion(List *name, const char *newname)
{
Oid conversionOid;
Oid namespaceOid;
HeapTuple tup;
Relation rel;
AclResult aclresult;
rel = heap_open(ConversionRelationId, RowExclusiveLock);
2003-06-27 16:45:32 +02:00
conversionOid = get_conversion_oid(name, false);
2003-06-27 16:45:32 +02:00
tup = SearchSysCacheCopy1(CONVOID, ObjectIdGetDatum(conversionOid));
2003-06-27 16:45:32 +02:00
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
2003-06-27 16:45:32 +02:00
namespaceOid = ((Form_pg_conversion) GETSTRUCT(tup))->connamespace;
/* make sure the new name doesn't exist */
if (SearchSysCacheExists2(CONNAMENSP,
CStringGetDatum(newname),
ObjectIdGetDatum(namespaceOid)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
2005-10-15 04:49:52 +02:00
errmsg("conversion \"%s\" already exists in schema \"%s\"",
newname, get_namespace_name(namespaceOid))));
2003-06-27 16:45:32 +02:00
/* must be owner */
2005-10-15 04:49:52 +02:00
if (!pg_conversion_ownercheck(conversionOid, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
NameListToString(name));
2003-06-27 16:45:32 +02:00
/* must have CREATE privilege on namespace */
aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
get_namespace_name(namespaceOid));
2003-06-27 16:45:32 +02:00
/* rename */
namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
simple_heap_update(rel, &tup->t_self, tup);
CatalogUpdateIndexes(rel, tup);
heap_close(rel, NoLock);
heap_freetuple(tup);
}