aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2002-11-02 18:41:22 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2002-11-02 18:41:22 +0000
commit5123139210a6767e99f3e420038d64926beb7d78 (patch)
treee4eadcde9f1d7d76c9f9de43f3ef39e99101f7a1 /src/backend
parentf6e0130b5bee398b68ed64144cd0a467e8b8713a (diff)
downloadpostgresql-5123139210a6767e99f3e420038d64926beb7d78.tar.gz
postgresql-5123139210a6767e99f3e420038d64926beb7d78.zip
Remove encoding lookups from grammar stage, push them back to places
where it's safe to do database access. Along the way, fix core dump for 'DEFAULT' parameters to CREATE DATABASE. initdb forced due to change in pg_proc entry.
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/catalog/namespace.c31
-rw-r--r--src/backend/catalog/pg_conversion.c31
-rw-r--r--src/backend/commands/dbcommands.c32
-rw-r--r--src/backend/parser/gram.y116
-rw-r--r--src/backend/utils/mb/mbutils.c43
5 files changed, 124 insertions, 129 deletions
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index bd2eaf52cd8..4d9ff6c0a4f 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -13,7 +13,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.37 2002/11/02 02:33:03 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.38 2002/11/02 18:41:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1186,7 +1186,9 @@ makeRangeVarFromNameList(List *names)
/*
* NameListToString
* Utility routine to convert a qualified-name list into a string.
- * Used primarily to form error messages.
+ *
+ * This is used primarily to form error messages, and so we do not quote
+ * the list elements, for the sake of legibility.
*/
char *
NameListToString(List *names)
@@ -1207,6 +1209,31 @@ NameListToString(List *names)
}
/*
+ * NameListToQuotedString
+ * Utility routine to convert a qualified-name list into a string.
+ *
+ * Same as above except that names will be double-quoted where necessary,
+ * so the string could be re-parsed (eg, by textToQualifiedNameList).
+ */
+char *
+NameListToQuotedString(List *names)
+{
+ StringInfoData string;
+ List *l;
+
+ initStringInfo(&string);
+
+ foreach(l, names)
+ {
+ if (l != names)
+ appendStringInfoChar(&string, '.');
+ appendStringInfo(&string, "%s", quote_identifier(strVal(lfirst(l))));
+ }
+
+ return string.data;
+}
+
+/*
* isTempNamespace - is the given namespace my temporary-table namespace?
*/
bool
diff --git a/src/backend/catalog/pg_conversion.c b/src/backend/catalog/pg_conversion.c
index c724d7e15ab..7731a32bffc 100644
--- a/src/backend/catalog/pg_conversion.c
+++ b/src/backend/catalog/pg_conversion.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/catalog/pg_conversion.c,v 1.7 2002/11/02 02:33:03 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/catalog/pg_conversion.c,v 1.8 2002/11/02 18:41:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -273,38 +273,44 @@ FindConversion(const char *conname, Oid connamespace)
* CONVERT <left paren> <character value expression>
* USING <form-of-use conversion name> <right paren>
*
- * TEXT convert3(TEXT string, OID conversion_oid);
+ * TEXT convert_using(TEXT string, TEXT conversion_name)
*/
Datum
-pg_convert3(PG_FUNCTION_ARGS)
+pg_convert_using(PG_FUNCTION_ARGS)
{
text *string = PG_GETARG_TEXT_P(0);
- Oid convoid = PG_GETARG_OID(1);
+ text *conv_name = PG_GETARG_TEXT_P(1);
+ text *retval;
+ List *parsed_name;
+ Oid convoid;
HeapTuple tuple;
Form_pg_conversion body;
- text *retval;
unsigned char *str;
unsigned char *result;
int len;
- if (!OidIsValid(convoid))
- elog(ERROR, "Conversion does not exist");
-
- /* make sure that source string is null terminated */
+ /* Convert input string to null-terminated form */
len = VARSIZE(string) - VARHDRSZ;
str = palloc(len + 1);
memcpy(str, VARDATA(string), len);
*(str + len) = '\0';
+ /* Look up the conversion name */
+ parsed_name = textToQualifiedNameList(conv_name, "convert_using");
+ convoid = FindConversionByName(parsed_name);
+ if (!OidIsValid(convoid))
+ elog(ERROR, "conversion %s not found", NameListToString(parsed_name));
+
tuple = SearchSysCache(CONOID,
ObjectIdGetDatum(convoid),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "Conversion %u search from syscache failed", convoid);
+ body = (Form_pg_conversion) GETSTRUCT(tuple);
+ /* Temporary result area should be more than big enough */
result = palloc(len * 4 + 1);
- body = (Form_pg_conversion) GETSTRUCT(tuple);
OidFunctionCall5(body->conproc,
Int32GetDatum(body->conforencoding),
Int32GetDatum(body->contoencoding),
@@ -315,7 +321,7 @@ pg_convert3(PG_FUNCTION_ARGS)
ReleaseSysCache(tuple);
/*
- * build text data type structre. we cannot use textin() here, since
+ * build text result structure. we cannot use textin() here, since
* textin assumes that input string encoding is same as database
* encoding.
*/
@@ -327,8 +333,5 @@ pg_convert3(PG_FUNCTION_ARGS)
pfree(result);
pfree(str);
- /* free memory if allocated by the toaster */
- PG_FREE_IF_COPY(string, 0);
-
PG_RETURN_TEXT_P(retval);
}
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 327aac29593..291770f98cc 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.106 2002/10/21 22:06:19 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.107 2002/11/02 18:41:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -122,14 +122,34 @@ createdb(const CreatedbStmt *stmt)
defel->defname);
}
- if (downer)
+ if (downer && downer->arg)
dbowner = strVal(downer->arg);
- if (dpath)
+ if (dpath && dpath->arg)
dbpath = strVal(dpath->arg);
- if (dtemplate)
+ if (dtemplate && dtemplate->arg)
dbtemplate = strVal(dtemplate->arg);
- if (dencoding)
- encoding = intVal(dencoding->arg);
+ if (dencoding && dencoding->arg)
+ {
+ const char *encoding_name;
+
+ if (IsA(dencoding->arg, Integer))
+ {
+ encoding = intVal(dencoding->arg);
+ encoding_name = pg_encoding_to_char(encoding);
+ if (strcmp(encoding_name, "") == 0 ||
+ pg_valid_server_encoding(encoding_name) < 0)
+ elog(ERROR, "%d is not a valid encoding code", encoding);
+ }
+ else if (IsA(dencoding->arg, String))
+ {
+ encoding_name = strVal(dencoding->arg);
+ if (pg_valid_server_encoding(encoding_name) < 0)
+ elog(ERROR, "%s is not a valid encoding name", encoding_name);
+ encoding = pg_char_to_encoding(encoding_name);
+ }
+ else
+ elog(ERROR, "CREATE DATABASE: bogus encoding parameter");
+ }
/* obtain sysid of proposed owner */
if (dbowner)
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 32dd7b3480d..e1a179c7c13 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -11,7 +11,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.372 2002/11/01 22:52:33 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.373 2002/11/02 18:41:21 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
@@ -53,7 +53,6 @@
#include "access/htup.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
-#include "catalog/pg_conversion.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/params.h"
@@ -63,7 +62,6 @@
#include "utils/numeric.h"
#include "utils/datetime.h"
#include "utils/date.h"
-#include "mb/pg_wchar.h"
extern List *parsetree; /* final parse result is delivered here */
@@ -217,8 +215,8 @@ static void doNegateFloat(Value *v);
group_clause TriggerFuncArgs select_limit
opt_select_limit opclass_item_list trans_options
TableFuncElementList
- convert_args prep_type_clause prep_type_list
- execute_param_clause execute_param_list
+ prep_type_clause prep_type_list
+ execute_param_clause
%type <range> into_clause OptTempTableName
@@ -234,7 +232,7 @@ static void doNegateFloat(Value *v);
%type <jtype> join_type
%type <list> extract_list overlay_list position_list
-%type <list> substr_list trim_list convert_list
+%type <list> substr_list trim_list
%type <ival> opt_interval
%type <node> overlay_placing substr_from substr_for
@@ -265,7 +263,7 @@ static void doNegateFloat(Value *v);
%type <node> def_arg columnElem where_clause insert_column_item
a_expr b_expr c_expr r_expr AexprConst
in_expr having_clause func_table
-%type <list> row row_descriptor row_list in_expr_nodes type_list
+%type <list> row row_descriptor type_list
%type <node> case_expr case_arg when_clause case_default
%type <list> when_clause_list
%type <ival> sub_type
@@ -325,8 +323,8 @@ static void doNegateFloat(Value *v);
AGGREGATE ALL ALTER ANALYSE ANALYZE AND ANY AS ASC
ASSERTION ASSIGNMENT AT AUTHORIZATION
- BACKWARD BEFORE BEGIN_TRANS BETWEEN BIGINT BINARY BIT BOTH
- BOOLEAN BY
+ BACKWARD BEFORE BEGIN_TRANS BETWEEN BIGINT BINARY BIT
+ BOOLEAN BOTH BY
CACHE CALLED CASCADE CASE CAST CHAIN CHAR_P
CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
@@ -355,6 +353,7 @@ static void doNegateFloat(Value *v);
INTERVAL INTO INVOKER IS ISNULL ISOLATION
JOIN
+
KEY
LANCOMPILER LANGUAGE LEADING LEFT LEVEL LIKE LIMIT
@@ -371,8 +370,7 @@ static void doNegateFloat(Value *v);
ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNER
PARTIAL PASSWORD PATH_P PENDANT PLACING POSITION
- PRECISION PREPARE PRIMARY PRIOR PRIVILEGES PROCEDURE
- PROCEDURAL
+ PRECISION PREPARE PRIMARY PRIOR PRIVILEGES PROCEDURAL PROCEDURE
READ REAL RECHECK REFERENCES REINDEX RELATIVE RENAME REPLACE
RESET RESTRICT RETURNS REVOKE RIGHT ROLLBACK ROW
@@ -3618,27 +3616,15 @@ createdb_opt_item:
}
| ENCODING opt_equal Sconst
{
- int encoding;
-
- if (pg_valid_server_encoding($3) < 0)
- elog(ERROR, "%s is not a valid encoding name", $3);
- encoding = pg_char_to_encoding($3);
-
- $$ = makeDefElem("encoding", (Node *)makeInteger(encoding));
+ $$ = makeDefElem("encoding", (Node *)makeString($3));
}
| ENCODING opt_equal Iconst
{
- const char *encoding_name;
-
- encoding_name = pg_encoding_to_char($3);
- if (!strcmp(encoding_name,"") ||
- pg_valid_server_encoding(encoding_name) < 0)
- elog(ERROR, "%d is not a valid encoding code", $3);
$$ = makeDefElem("encoding", (Node *)makeInteger($3));
}
| ENCODING opt_equal DEFAULT
{
- $$ = makeDefElem("encoding", (Node *)makeInteger(-1));
+ $$ = makeDefElem("encoding", NULL);
}
| OWNER opt_equal name
{
@@ -3932,14 +3918,10 @@ ExecuteStmt: EXECUTE name execute_param_clause into_clause
}
;
-execute_param_clause: '(' execute_param_list ')' { $$ = $2; }
+execute_param_clause: '(' expr_list ')' { $$ = $2; }
| /* EMPTY */ { $$ = NIL; }
;
-execute_param_list: a_expr { $$ = makeList1($1); }
- | execute_param_list ',' a_expr { $$ = lappend($1, $3); }
- ;
-
/*****************************************************************************
*
* QUERY:
@@ -5470,14 +5452,9 @@ row: ROW '(' row_descriptor ')' { $$ = $3; }
| ROW '(' a_expr ')' { $$ = makeList1($3); }
| ROW '(' ')' { $$ = NULL; }
| '(' row_descriptor ')' { $$ = $2; }
-;
-
-row_descriptor:
- row_list ',' a_expr { $$ = lappend($1, $3); }
;
-row_list: a_expr { $$ = makeList1($1); }
- | row_list ',' a_expr { $$ = lappend($1, $3); }
+row_descriptor: expr_list ',' a_expr { $$ = lappend($1, $3); }
;
sub_type: ANY { $$ = ANY_SUBLINK; }
@@ -6366,7 +6343,21 @@ c_expr: columnref { $$ = (Node *) $1; }
n->agg_distinct = FALSE;
$$ = (Node *)n;
}
- | CONVERT '(' convert_list ')'
+ | CONVERT '(' a_expr USING any_name ')'
+ {
+ FuncCall *n = makeNode(FuncCall);
+ A_Const *c = makeNode(A_Const);
+
+ c->val.type = T_String;
+ c->val.val.str = NameListToQuotedString($5);
+
+ n->funcname = SystemFuncName("convert_using");
+ n->args = makeList2($3, c);
+ n->agg_star = FALSE;
+ n->agg_distinct = FALSE;
+ $$ = (Node *)n;
+ }
+ | CONVERT '(' expr_list ')'
{
FuncCall *n = makeNode(FuncCall);
n->funcname = SystemFuncName("convert");
@@ -6422,7 +6413,6 @@ opt_indirection:
expr_list: a_expr { $$ = makeList1($1); }
| expr_list ',' a_expr { $$ = lappend($1, $3); }
- | expr_list USING a_expr { $$ = lappend($1, $3); }
;
extract_list:
@@ -6540,60 +6530,13 @@ trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
| expr_list { $$ = $1; }
;
-/* CONVERT() arguments. We accept followings:
- * SQL99 syntax
- * o CONVERT(TEXT string USING conversion_name)
- *
- * Function calls
- * o CONVERT(TEXT string, NAME src_encoding_name, NAME dest_encoding_name)
- * o CONVERT(TEXT string, NAME encoding_name)
- */
-convert_list:
- a_expr USING any_name
- {
- Oid oid = FindConversionByName($3);
- Const *convoid = makeNode(Const);
-
- if (!OidIsValid(oid))
- {
- elog(ERROR, "Conversion \"%s\" does not exist",
- NameListToString($3));
- }
-
- convoid->consttype = OIDOID;
- convoid->constlen = sizeof(Oid);
- convoid->constvalue = oid;
- convoid->constisnull = FALSE;
- convoid->constbyval = TRUE;
- convoid->constisset = FALSE;
- convoid->constiscast = FALSE;
- $$ = makeList2($1, convoid);
- }
- | convert_args
- {
- $$ = $1;
- }
- | /*EMPTY*/
- { $$ = NIL; }
- ;
-
-convert_args: a_expr { $$ = makeList1($1); }
- | convert_args ',' a_expr { $$ = lappend($1, $3); }
- ;
-
-
in_expr: select_with_parens
{
SubLink *n = makeNode(SubLink);
n->subselect = $1;
$$ = (Node *)n;
}
- | '(' in_expr_nodes ')' { $$ = (Node *)$2; }
- ;
-
-in_expr_nodes:
- a_expr { $$ = makeList1($1); }
- | in_expr_nodes ',' a_expr { $$ = lappend($1, $3); }
+ | '(' expr_list ')' { $$ = (Node *)$2; }
;
/* Case clause
@@ -7215,6 +7158,7 @@ col_name_keyword:
| CHAR_P
| CHARACTER
| COALESCE
+ | CONVERT
| DEC
| DECIMAL
| EXISTS
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c
index dfcad8e18ea..6179fa9efd5 100644
--- a/src/backend/utils/mb/mbutils.c
+++ b/src/backend/utils/mb/mbutils.c
@@ -3,9 +3,11 @@
* client encoding and server internal encoding.
* (currently mule internal code (mic) is used)
* Tatsuo Ishii
- * $Id: mbutils.c,v 1.35 2002/09/04 20:31:31 momjian Exp $
+ *
+ * $Header: /cvsroot/pgsql/src/backend/utils/mb/mbutils.c,v 1.36 2002/11/02 18:41:22 tgl Exp $
*/
#include "postgres.h"
+
#include "access/xact.h"
#include "miscadmin.h"
#include "mb/pg_wchar.h"
@@ -23,19 +25,18 @@ static pg_enc2name *ClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static pg_enc2name *DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
/*
- * Caches for conversion function info. Note that Fcinfo.flinfo is
- * allocated in TopMemoryContext so that it survives outside
+ * Caches for conversion function info. Note that these values are
+ * allocated in TopMemoryContext so that they survive across
* transactions. See SetClientEncoding() for more details.
*/
-static FmgrInfo *ToServerConvPorc = NULL;
-static FmgrInfo *ToClientConvPorc = NULL;
+static FmgrInfo *ToServerConvProc = NULL;
+static FmgrInfo *ToClientConvProc = NULL;
/* Internal functions */
-static unsigned char *
- perform_default_encoding_conversion(unsigned char *src, int len, bool is_client_to_server);
+static unsigned char *perform_default_encoding_conversion(unsigned char *src,
+ int len, bool is_client_to_server);
+static int cliplen(const unsigned char *str, int len, int limit);
-static int
- cliplen(const unsigned char *str, int len, int limit);
/*
* Set the client encoding and save fmgrinfo for the converion
@@ -95,21 +96,21 @@ SetClientEncoding(int encoding, bool doit)
{
ClientEncoding = &pg_enc2name_tbl[encoding];
- if (ToServerConvPorc != NULL)
+ if (ToServerConvProc != NULL)
{
- if (ToServerConvPorc->fn_extra)
- pfree(ToServerConvPorc->fn_extra);
- pfree(ToServerConvPorc);
+ if (ToServerConvProc->fn_extra)
+ pfree(ToServerConvProc->fn_extra);
+ pfree(ToServerConvProc);
}
- ToServerConvPorc = to_server;
+ ToServerConvProc = to_server;
- if (ToClientConvPorc != NULL)
+ if (ToClientConvProc != NULL)
{
- if (ToClientConvPorc->fn_extra)
- pfree(ToClientConvPorc->fn_extra);
- pfree(ToClientConvPorc);
+ if (ToClientConvProc->fn_extra)
+ pfree(ToClientConvProc->fn_extra);
+ pfree(ToClientConvProc);
}
- ToClientConvPorc = to_client;
+ ToClientConvProc = to_client;
}
return 0;
}
@@ -323,13 +324,13 @@ perform_default_encoding_conversion(unsigned char *src, int len, bool is_client_
{
src_encoding = ClientEncoding->encoding;
dest_encoding = DatabaseEncoding->encoding;
- flinfo = ToServerConvPorc;
+ flinfo = ToServerConvProc;
}
else
{
src_encoding = DatabaseEncoding->encoding;
dest_encoding = ClientEncoding->encoding;
- flinfo = ToClientConvPorc;
+ flinfo = ToClientConvProc;
}
if (flinfo == NULL)