1 /*-------------------------------------------------------------------------
2  *
3  * conversioncmds.c
4  *	  conversion creation command support code
5  *
6  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/backend/commands/conversioncmds.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/heapam.h"
18 #include "access/htup_details.h"
19 #include "catalog/dependency.h"
20 #include "catalog/indexing.h"
21 #include "catalog/pg_conversion.h"
22 #include "catalog/pg_conversion_fn.h"
23 #include "catalog/pg_type.h"
24 #include "commands/alter.h"
25 #include "commands/conversioncmds.h"
26 #include "mb/pg_wchar.h"
27 #include "miscadmin.h"
28 #include "parser/parse_func.h"
29 #include "utils/builtins.h"
30 #include "utils/lsyscache.h"
31 #include "utils/rel.h"
32 #include "utils/syscache.h"
33 
34 /*
35  * CREATE CONVERSION
36  */
37 ObjectAddress
CreateConversionCommand(CreateConversionStmt * stmt)38 CreateConversionCommand(CreateConversionStmt *stmt)
39 {
40 	Oid			namespaceId;
41 	char	   *conversion_name;
42 	AclResult	aclresult;
43 	int			from_encoding;
44 	int			to_encoding;
45 	Oid			funcoid;
46 	const char *from_encoding_name = stmt->for_encoding_name;
47 	const char *to_encoding_name = stmt->to_encoding_name;
48 	List	   *func_name = stmt->func_name;
49 	static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
50 	char		result[1];
51 
52 	/* Convert list of names to a name and namespace */
53 	namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
54 													&conversion_name);
55 
56 	/* Check we have creation rights in target namespace */
57 	aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
58 	if (aclresult != ACLCHECK_OK)
59 		aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
60 					   get_namespace_name(namespaceId));
61 
62 	/* Check the encoding names */
63 	from_encoding = pg_char_to_encoding(from_encoding_name);
64 	if (from_encoding < 0)
65 		ereport(ERROR,
66 				(errcode(ERRCODE_UNDEFINED_OBJECT),
67 				 errmsg("source encoding \"%s\" does not exist",
68 						from_encoding_name)));
69 
70 	to_encoding = pg_char_to_encoding(to_encoding_name);
71 	if (to_encoding < 0)
72 		ereport(ERROR,
73 				(errcode(ERRCODE_UNDEFINED_OBJECT),
74 				 errmsg("destination encoding \"%s\" does not exist",
75 						to_encoding_name)));
76 
77 	/*
78 	 * Check the existence of the conversion function. Function name could be
79 	 * a qualified name.
80 	 */
81 	funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
82 							 funcargs, false);
83 
84 	/* Check it returns VOID, else it's probably the wrong function */
85 	if (get_func_rettype(funcoid) != VOIDOID)
86 		ereport(ERROR,
87 				(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
88 				 errmsg("encoding conversion function %s must return type %s",
89 						NameListToString(func_name), "void")));
90 
91 	/* Check we have EXECUTE rights for the function */
92 	aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
93 	if (aclresult != ACLCHECK_OK)
94 		aclcheck_error(aclresult, ACL_KIND_PROC,
95 					   NameListToString(func_name));
96 
97 	/*
98 	 * Check that the conversion function is suitable for the requested source
99 	 * and target encodings. We do that by calling the function with an empty
100 	 * string; the conversion function should throw an error if it can't
101 	 * perform the requested conversion.
102 	 */
103 	OidFunctionCall5(funcoid,
104 					 Int32GetDatum(from_encoding),
105 					 Int32GetDatum(to_encoding),
106 					 CStringGetDatum(""),
107 					 CStringGetDatum(result),
108 					 Int32GetDatum(0));
109 
110 	/*
111 	 * All seem ok, go ahead (possible failure would be a duplicate conversion
112 	 * name)
113 	 */
114 	return ConversionCreate(conversion_name, namespaceId, GetUserId(),
115 							from_encoding, to_encoding, funcoid, stmt->def);
116 }
117