PostgreSQL Source Code  git master
conversioncmds.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/pg_conversion.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"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for conversioncmds.c:

Go to the source code of this file.

Functions

ObjectAddress CreateConversionCommand (CreateConversionStmt *stmt)
 

Function Documentation

◆ CreateConversionCommand()

ObjectAddress CreateConversionCommand ( CreateConversionStmt stmt)

Definition at line 36 of file conversioncmds.c.

References ACL_CREATE, ACL_EXECUTE, aclcheck_error(), ACLCHECK_OK, CreateConversionStmt::conversion_name, ConversionCreate(), CStringGetDatum, CreateConversionStmt::def, ereport, errcode(), errmsg(), ERROR, CreateConversionStmt::for_encoding_name, CreateConversionStmt::func_name, get_func_rettype(), get_namespace_name(), GetUserId(), Int32GetDatum, LookupFuncName(), NameListToString(), OBJECT_FUNCTION, OBJECT_SCHEMA, OidFunctionCall5, pg_char_to_encoding(), pg_namespace_aclcheck(), pg_proc_aclcheck(), QualifiedNameGetCreationNamespace(), and CreateConversionStmt::to_encoding_name.

Referenced by ProcessUtilitySlow().

37 {
38  Oid namespaceId;
39  char *conversion_name;
40  AclResult aclresult;
41  int from_encoding;
42  int to_encoding;
43  Oid funcoid;
44  const char *from_encoding_name = stmt->for_encoding_name;
45  const char *to_encoding_name = stmt->to_encoding_name;
46  List *func_name = stmt->func_name;
47  static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
48  char result[1];
49 
50  /* Convert list of names to a name and namespace */
52  &conversion_name);
53 
54  /* Check we have creation rights in target namespace */
55  aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
56  if (aclresult != ACLCHECK_OK)
57  aclcheck_error(aclresult, OBJECT_SCHEMA,
58  get_namespace_name(namespaceId));
59 
60  /* Check the encoding names */
61  from_encoding = pg_char_to_encoding(from_encoding_name);
62  if (from_encoding < 0)
63  ereport(ERROR,
64  (errcode(ERRCODE_UNDEFINED_OBJECT),
65  errmsg("source encoding \"%s\" does not exist",
66  from_encoding_name)));
67 
68  to_encoding = pg_char_to_encoding(to_encoding_name);
69  if (to_encoding < 0)
70  ereport(ERROR,
71  (errcode(ERRCODE_UNDEFINED_OBJECT),
72  errmsg("destination encoding \"%s\" does not exist",
73  to_encoding_name)));
74 
75  /*
76  * Check the existence of the conversion function. Function name could be
77  * a qualified name.
78  */
79  funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
80  funcargs, false);
81 
82  /* Check it returns VOID, else it's probably the wrong function */
83  if (get_func_rettype(funcoid) != VOIDOID)
84  ereport(ERROR,
85  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
86  errmsg("encoding conversion function %s must return type %s",
87  NameListToString(func_name), "void")));
88 
89  /* Check we have EXECUTE rights for the function */
90  aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
91  if (aclresult != ACLCHECK_OK)
93  NameListToString(func_name));
94 
95  /*
96  * Check that the conversion function is suitable for the requested source
97  * and target encodings. We do that by calling the function with an empty
98  * string; the conversion function should throw an error if it can't
99  * perform the requested conversion.
100  */
101  OidFunctionCall5(funcoid,
102  Int32GetDatum(from_encoding),
103  Int32GetDatum(to_encoding),
104  CStringGetDatum(""),
105  CStringGetDatum(result),
106  Int32GetDatum(0));
107 
108  /*
109  * All seem ok, go ahead (possible failure would be a duplicate conversion
110  * name)
111  */
112  return ConversionCreate(conversion_name, namespaceId, GetUserId(),
113  from_encoding, to_encoding, funcoid, stmt->def);
114 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
Oid GetUserId(void)
Definition: miscinit.c:379
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2975
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4689
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1444
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:84
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3051
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
char * NameListToString(List *names)
Definition: namespace.c:3082
ObjectAddress ConversionCreate(const char *conname, Oid connamespace, Oid conowner, int32 conforencoding, int32 contoencoding, Oid conproc, bool def)
Definition: pg_conversion.c:40
AclResult
Definition: acl.h:178
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
Definition: parse_func.c:2019
#define Int32GetDatum(X)
Definition: postgres.h:464
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_EXECUTE
Definition: parsenodes.h:81
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4651
Definition: pg_list.h:45
#define OidFunctionCall5(functionId, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:636