Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2334723

Browse files
committed
Tweak the API for per-datatype typmodin functions so that they are passed
an array of strings rather than an array of integers, and allow any simpleconstant or identifier to be used in typmods; for examplecreate table foo (f1 widget(42,'23skidoo',point));Of course the typmodin function has still got to pack this info into anon-negative int32 for storage, but it's still a useful improvement inflexibility, especially considering that you can do nearly anything if youare willing to keep the info in a side table. We can get away with thischange since we have not yet released a version providing user-definabletypmods. Per discussion.
1 parent839fcc9 commit2334723

File tree

16 files changed

+113
-62
lines changed

16 files changed

+113
-62
lines changed

‎doc/src/sgml/ref/create_type.sgml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_type.sgml,v 1.71 2007/05/12 00:54:59 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_type.sgml,v 1.72 2007/06/15 20:56:49 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -195,11 +195,11 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
195195
are needed if the type supports modifiers, that is optional constraints
196196
attached to a type declaration, such as <literal>char(5)</> or
197197
<literal>numeric(30,2)</>. <productname>PostgreSQL</productname> allows
198-
user-defined types to take one or moreinteger constantsas modifiers;
199-
however, this information must be capable of being packed into a single
200-
non-negative integer value for storage in the system catalogs. The
198+
user-defined types to take one or moresimple constantsor identifiers as
199+
modifiers;however, this information must be capable of being packed into a
200+
singlenon-negative integer value for storage in the system catalogs. The
201201
<replaceable class="parameter">type_modifier_input_function</replaceable>
202-
is passed the declared modifier(s) in the form ofan <type>integer</>
202+
is passed the declared modifier(s) in the form ofa <type>cstring</>
203203
array. It must check the values for validity (throwing an error if they
204204
are wrong), and if they are correct, return a single non-negative
205205
<type>integer</> value that will be stored as the column <quote>typmod</>.

‎src/backend/commands/typecmds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.104 2007/05/12 00:54:59 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.105 2007/06/15 20:56:49 tgl Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -1270,9 +1270,9 @@ findTypeTypmodinFunction(List *procname)
12701270
OidprocOid;
12711271

12721272
/*
1273-
* typmodin functions always take oneint4[] argument and return int4.
1273+
* typmodin functions always take onecstring[] argument and return int4.
12741274
*/
1275-
argList[0]=INT4ARRAYOID;
1275+
argList[0]=CSTRINGARRAYOID;
12761276

12771277
procOid=LookupFuncName(procname,1,argList, true);
12781278
if (!OidIsValid(procOid))

‎src/backend/parser/gram.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.593 2007/06/11 22:22:41 tgl Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.594 2007/06/15 20:56:49 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHORDATEMAJOR EVENT
@@ -6695,7 +6695,7 @@ ConstTypename:
66956695
* by the standard, including qualified names. We also allow type modifiers.
66966696
* To avoid parsing conflicts against function invocations, the modifiers
66976697
* have to be shown as expr_list here, but parse analysis will only accept
6698-
*integerconstants for them.
6698+
* constants for them.
66996699
*/
67006700
GenericType:
67016701
type_function_name opt_type_modifiers

‎src/backend/parser/parse_type.c

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_type.c,v 1.90 2007/05/11 17:57:12 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_type.c,v 1.91 2007/06/15 20:56:50 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -289,28 +289,55 @@ typenameTypeMod(ParseState *pstate, const TypeName *typename,
289289
parser_errposition(pstate,typename->location)));
290290

291291
/*
292-
* Convert the list of(rawgrammaroutput) expressions toan integer
293-
*array.Currently, weonlyallow simpleinteger constants,though
294-
* possibly this could be extended.
292+
* Convert the list of raw-grammar-output expressions toa cstring array.
293+
* Currently, we allow simplenumeric constants,string literals, and
294+
*identifiers;possibly this list could be extended.
295295
*/
296296
datums= (Datum*)palloc(list_length(typename->typmods)*sizeof(Datum));
297297
n=0;
298298
foreach(l,typename->typmods)
299299
{
300-
A_Const*ac= (A_Const*)lfirst(l);
300+
Node*tm= (Node*)lfirst(l);
301+
char*cstr=NULL;
301302

302-
if (!IsA(ac,A_Const)||
303-
!IsA(&ac->val,Integer))
303+
if (IsA(tm,A_Const))
304+
{
305+
A_Const*ac= (A_Const*)tm;
306+
307+
/*
308+
* The grammar hands back some integers with ::int4 attached,
309+
* so allow a cast decoration if it's an Integer value, but
310+
* not otherwise.
311+
*/
312+
if (IsA(&ac->val,Integer))
313+
{
314+
cstr= (char*)palloc(32);
315+
snprintf(cstr,32,"%ld", (long)ac->val.val.ival);
316+
}
317+
elseif (ac->typename==NULL)/* no casts allowed */
318+
{
319+
/* otherwise we can just use the str field directly. */
320+
cstr=ac->val.val.str;
321+
}
322+
}
323+
elseif (IsA(tm,ColumnRef))
324+
{
325+
ColumnRef*cr= (ColumnRef*)tm;
326+
327+
if (list_length(cr->fields)==1)
328+
cstr=strVal(linitial(cr->fields));
329+
}
330+
if (!cstr)
304331
ereport(ERROR,
305332
(errcode(ERRCODE_SYNTAX_ERROR),
306-
errmsg("type modifiers must beinteger constants"),
333+
errmsg("type modifiers must besimple constants or identifiers"),
307334
parser_errposition(pstate,typename->location)));
308-
datums[n++]=Int32GetDatum(ac->val.val.ival);
335+
datums[n++]=CStringGetDatum(cstr);
309336
}
310337

311-
/* hardwired knowledge aboutint4's representation details here */
312-
arrtypmod=construct_array(datums,n,INT4OID,
313-
sizeof(int4), true,'i');
338+
/* hardwired knowledge aboutcstring's representation details here */
339+
arrtypmod=construct_array(datums,n,CSTRINGOID,
340+
-2, false,'c');
314341

315342
result=DatumGetInt32(OidFunctionCall1(typmodin,
316343
PointerGetDatum(arrtypmod)));

‎src/backend/utils/adt/arrayutils.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.23 2007/01/05 22:19:40 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.24 2007/06/15 20:56:50 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -17,6 +17,7 @@
1717

1818
#include"catalog/pg_type.h"
1919
#include"utils/array.h"
20+
#include"utils/builtins.h"
2021
#include"utils/memutils.h"
2122

2223

@@ -191,16 +192,21 @@ mda_next_tuple(int n, int *curr, const int *span)
191192
}
192193

193194
/*
194-
* ArrayGetTypmods: verify that argument is a 1-D integer array,
195-
* return its length and a pointer to the first contained integer.
195+
* ArrayGetIntegerTypmods: verify that argument is a 1-D cstring array,
196+
* and get the contents converted to integers. Returns a palloc'd array
197+
* and places the length at *n.
196198
*/
197199
int32*
198-
ArrayGetTypmods(ArrayType*arr,int*n)
200+
ArrayGetIntegerTypmods(ArrayType*arr,int*n)
199201
{
200-
if (ARR_ELEMTYPE(arr)!=INT4OID)
202+
int32*result;
203+
Datum*elem_values;
204+
inti;
205+
206+
if (ARR_ELEMTYPE(arr)!=CSTRINGOID)
201207
ereport(ERROR,
202208
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
203-
errmsg("typmod array must be typeinteger[]")));
209+
errmsg("typmod array must be typecstring[]")));
204210

205211
if (ARR_NDIM(arr)!=1)
206212
ereport(ERROR,
@@ -212,7 +218,18 @@ ArrayGetTypmods(ArrayType *arr, int *n)
212218
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
213219
errmsg("typmod array must not contain nulls")));
214220

215-
*n=ArrayGetNItems(ARR_NDIM(arr),ARR_DIMS(arr));
221+
/* hardwired knowledge about cstring's representation details here */
222+
deconstruct_array(arr,CSTRINGOID,
223+
-2, false,'c',
224+
&elem_values,NULL,n);
225+
226+
result= (int32*)palloc(*n*sizeof(int32));
227+
228+
for (i=0;i<*n;i++)
229+
result[i]=pg_atoi(DatumGetCString(elem_values[i]),
230+
sizeof(int32),'\0');
231+
232+
pfree(elem_values);
216233

217-
return(int32*)ARR_DATA_PTR(arr);
234+
returnresult;
218235
}

‎src/backend/utils/adt/date.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.132 2007/06/05 21:31:06 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.133 2007/06/15 20:56:50 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -53,7 +53,7 @@ anytime_typmodin(bool istz, ArrayType *ta)
5353
int32*tl;
5454
intn;
5555

56-
tl=ArrayGetTypmods(ta,&n);
56+
tl=ArrayGetIntegerTypmods(ta,&n);
5757

5858
/*
5959
* we're not too tense about good error message here because grammar

‎src/backend/utils/adt/numeric.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Copyright (c) 1998-2007, PostgreSQL Global Development Group
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.104 2007/06/09 15:52:30 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.105 2007/06/15 20:56:50 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -547,7 +547,7 @@ numerictypmodin(PG_FUNCTION_ARGS)
547547
intn;
548548
int32typmod;
549549

550-
tl=ArrayGetTypmods(ta,&n);
550+
tl=ArrayGetIntegerTypmods(ta,&n);
551551

552552
if (n==2)
553553
{

‎src/backend/utils/adt/timestamp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.177 2007/06/05 21:31:06 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.178 2007/06/15 20:56:50 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -64,7 +64,7 @@ anytimestamp_typmodin(bool istz, ArrayType *ta)
6464
int32*tl;
6565
intn;
6666

67-
tl=ArrayGetTypmods(ta,&n);
67+
tl=ArrayGetIntegerTypmods(ta,&n);
6868

6969
/*
7070
* we're not too tense about good error message here because grammar
@@ -719,7 +719,7 @@ intervaltypmodin(PG_FUNCTION_ARGS)
719719
intn;
720720
int32typmod;
721721

722-
tl=ArrayGetTypmods(ta,&n);
722+
tl=ArrayGetIntegerTypmods(ta,&n);
723723

724724
/*
725725
* tl[0] - opt_interval

‎src/backend/utils/adt/varbit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.53 2007/02/27 23:48:09 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.54 2007/06/15 20:56:51 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -32,7 +32,7 @@ anybit_typmodin(ArrayType *ta, const char *typename)
3232
int32*tl;
3333
intn;
3434

35-
tl=ArrayGetTypmods(ta,&n);
35+
tl=ArrayGetIntegerTypmods(ta,&n);
3636

3737
/*
3838
* we're not too tense about good error message here because grammar

‎src/backend/utils/adt/varchar.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.123 2007/04/06 04:21:43 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.124 2007/06/15 20:56:51 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -31,7 +31,7 @@ anychar_typmodin(ArrayType *ta, const char *typename)
3131
int32*tl;
3232
intn;
3333

34-
tl=ArrayGetTypmods(ta,&n);
34+
tl=ArrayGetIntegerTypmods(ta,&n);
3535

3636
/*
3737
* we're not too tense about good error message here because grammar

‎src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.411 2007/06/06 23:00:40 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.412 2007/06/15 20:56:51 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO200706061
56+
#defineCATALOG_VERSION_NO200706151
5757

5858
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp