PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
collationcmds.h File Reference
Include dependency graph for collationcmds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress DefineCollation (ParseState *pstate, List *names, List *parameters, bool if_not_exists)
 
void IsThereCollationInNamespace (const char *collname, Oid nspOid)
 

Function Documentation

ObjectAddress DefineCollation ( ParseState pstate,
List names,
List parameters,
bool  if_not_exists 
)

Definition at line 40 of file collationcmds.c.

References ACL_CREATE, ACL_KIND_NAMESPACE, aclcheck_error(), ACLCHECK_OK, castNode, check_encoding_locale_matches(), CollationCreate(), CollationRelationId, COLLOID, CommandCounterIncrement(), defGetQualifiedName(), defGetString(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, get_collation_oid(), get_namespace_name(), GetDatabaseEncoding(), GETSTRUCT, GetUserId(), HeapTupleIsValid, InvalidObjectAddress, lfirst, list_length(), DefElem::location, NameStr, NULL, ObjectAddressSet, ObjectIdGetDatum, OidIsValid, parser_errposition(), pg_namespace_aclcheck(), pg_newlocale_from_collation(), pg_strcasecmp(), pstrdup(), QualifiedNameGetCreationNamespace(), ReleaseSysCache(), and SearchSysCache1.

Referenced by ProcessUtilitySlow().

41 {
42  char *collName;
43  Oid collNamespace;
44  AclResult aclresult;
45  ListCell *pl;
46  DefElem *fromEl = NULL;
47  DefElem *localeEl = NULL;
48  DefElem *lccollateEl = NULL;
49  DefElem *lcctypeEl = NULL;
50  char *collcollate = NULL;
51  char *collctype = NULL;
52  Oid newoid;
53  ObjectAddress address;
54 
55  collNamespace = QualifiedNameGetCreationNamespace(names, &collName);
56 
57  aclresult = pg_namespace_aclcheck(collNamespace, GetUserId(), ACL_CREATE);
58  if (aclresult != ACLCHECK_OK)
60  get_namespace_name(collNamespace));
61 
62  foreach(pl, parameters)
63  {
64  DefElem *defel = castNode(DefElem, lfirst(pl));
65  DefElem **defelp;
66 
67  if (pg_strcasecmp(defel->defname, "from") == 0)
68  defelp = &fromEl;
69  else if (pg_strcasecmp(defel->defname, "locale") == 0)
70  defelp = &localeEl;
71  else if (pg_strcasecmp(defel->defname, "lc_collate") == 0)
72  defelp = &lccollateEl;
73  else if (pg_strcasecmp(defel->defname, "lc_ctype") == 0)
74  defelp = &lcctypeEl;
75  else
76  {
77  ereport(ERROR,
78  (errcode(ERRCODE_SYNTAX_ERROR),
79  errmsg("collation attribute \"%s\" not recognized",
80  defel->defname),
81  parser_errposition(pstate, defel->location)));
82  break;
83  }
84 
85  *defelp = defel;
86  }
87 
88  if ((localeEl && (lccollateEl || lcctypeEl))
89  || (fromEl && list_length(parameters) != 1))
90  ereport(ERROR,
91  (errcode(ERRCODE_SYNTAX_ERROR),
92  errmsg("conflicting or redundant options")));
93 
94  if (fromEl)
95  {
96  Oid collid;
97  HeapTuple tp;
98 
99  collid = get_collation_oid(defGetQualifiedName(fromEl), false);
101  if (!HeapTupleIsValid(tp))
102  elog(ERROR, "cache lookup failed for collation %u", collid);
103 
104  collcollate = pstrdup(NameStr(((Form_pg_collation) GETSTRUCT(tp))->collcollate));
105  collctype = pstrdup(NameStr(((Form_pg_collation) GETSTRUCT(tp))->collctype));
106 
107  ReleaseSysCache(tp);
108  }
109 
110  if (localeEl)
111  {
112  collcollate = defGetString(localeEl);
113  collctype = defGetString(localeEl);
114  }
115 
116  if (lccollateEl)
117  collcollate = defGetString(lccollateEl);
118 
119  if (lcctypeEl)
120  collctype = defGetString(lcctypeEl);
121 
122  if (!collcollate)
123  ereport(ERROR,
124  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
125  errmsg("parameter \"lc_collate\" must be specified")));
126 
127  if (!collctype)
128  ereport(ERROR,
129  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
130  errmsg("parameter \"lc_ctype\" must be specified")));
131 
132  check_encoding_locale_matches(GetDatabaseEncoding(), collcollate, collctype);
133 
134  newoid = CollationCreate(collName,
135  collNamespace,
136  GetUserId(),
138  collcollate,
139  collctype,
140  if_not_exists);
141 
142  if (!OidIsValid(newoid))
143  return InvalidObjectAddress;
144 
145  ObjectAddressSet(address, CollationRelationId, newoid);
146 
147  /* check that the locales can be loaded */
149  (void) pg_newlocale_from_collation(newoid);
150 
151  return address;
152 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
void check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
Definition: dbcommands.c:722
Oid GetUserId(void)
Definition: miscinit.c:283
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2790
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
unsigned int Oid
Definition: postgres_ext.h:31
Oid CollationCreate(const char *collname, Oid collnamespace, Oid collowner, int32 collencoding, const char *collcollate, const char *collctype, bool if_not_exists)
Definition: pg_collation.c:41
#define OidIsValid(objectId)
Definition: c.h:538
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4458
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:75
char * defGetString(DefElem *def)
Definition: define.c:49
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3377
int location
Definition: parsenodes.h:711
#define ereport(elevel, rest)
Definition: elog.h:122
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1259
AclResult
Definition: acl.h:170
void CommandCounterIncrement(void)
Definition: xact.c:922
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1081
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
#define CollationRelationId
Definition: pg_collation.h:30
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * defGetQualifiedName(DefElem *def)
Definition: define.c:223
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:47
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
char * defname
Definition: parsenodes.h:708
#define elog
Definition: elog.h:219
Oid get_collation_oid(List *name, bool missing_ok)
Definition: namespace.c:3324
void IsThereCollationInNamespace ( const char *  collname,
Oid  nspOid 
)

Definition at line 161 of file collationcmds.c.

References COLLNAMEENCNSP, CStringGetDatum, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, get_namespace_name(), GetDatabaseEncoding(), GetDatabaseEncodingName(), Int32GetDatum, ObjectIdGetDatum, and SearchSysCacheExists3.

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().

162 {
163  /* make sure the name doesn't already exist in new schema */
165  CStringGetDatum(collname),
167  ObjectIdGetDatum(nspOid)))
168  ereport(ERROR,
170  errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
171  collname, GetDatabaseEncodingName(),
172  get_namespace_name(nspOid))));
173 
174  /* mustn't match an any-encoding entry, either */
176  CStringGetDatum(collname),
177  Int32GetDatum(-1),
178  ObjectIdGetDatum(nspOid)))
179  ereport(ERROR,
181  errmsg("collation \"%s\" already exists in schema \"%s\"",
182  collname, get_namespace_name(nspOid))));
183 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:171
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1021
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31