Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Add to_regtypemod() Function #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions doc/src/sgml/func.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -24872,7 +24872,7 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);

<tbody>
<row>
<entry role="func_table_entry"><para role="func_signature">
<entry id="format_type" xreflabel="format_type" role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>format_type</primary>
</indexterm>
Expand Down Expand Up @@ -25570,21 +25570,61 @@ SELECT collation for ('foo' COLLATE "de_DE");
</row>

<row>
<entry role="func_table_entry"><para role="func_signature">
<entry id="to_regtype" xreflabel="to_regtype" role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>to_regtype</primary>
</indexterm>
<function>to_regtype</function> ( <type>text</type> )
<returnvalue>regtype</returnvalue>
</para>
<para>
Translates a textual type name to its OID. A similar result is
obtained by casting the string to type <type>regtype</type> (see
<xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is
not found.
Parses a string of text, extracts a potential type name from it, and
translates that name into an OID. A similar result is obtained by
casting the string to type <type>regtype</type> (see
<xref linkend="datatype-oid"/>). Failure to extract a valid potential
type name results in an error. For example:
<programlisting>
SELECT to_regtype('interval nonesuch');
ERROR: syntax error at or near "nonesuch"
LINE 1: select to_regtype('interval nonesuch');
^
CONTEXT: invalid type name "interval nonesuch"
</programlisting>
However, if the extracted name is not known to the system, this function
will return <literal>NULL</literal>. For example:
<programlisting>
SELECT to_regtype('party');
to_regtype
------------

</programlisting>
</para></entry>
</row>

<row>
<entry id="to_regtypemod" role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>to_regtypemod</primary>
</indexterm>
<function>to_regtypemod</function> ( <type>text</type> )
<returnvalue>integer</returnvalue>
</para>
<para>
Parses a string of text, extracts a potential type name from it, and
translates its type modifier, if any. Failure to extract a valid
potential type name results in an error; however, if the extracted name
is not known to the system, this function will return
<literal>NULL</literal>. Complements <xref linkend="to_regtype"/>, and
can be passed to <xref linkend="format_type" />. For example:
<programlisting>
SELECT format_type(to_regtype('varchar(32)'), to_regtypemod('varchar(32)'));
format_type
-----------------------
character varying(32)
</programlisting>
</para></entry>
</row>

</tbody>
</tgroup>
</table>
Expand Down
2 changes: 1 addition & 1 deletion src/backend/catalog/genbki.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ sub lookup_oids
{
warn sprintf
"unresolved OID reference \"%s\" in %s.dat field %s line %s\n",
$lookupname, $catname, $attname, $bki_values->{line_number};
$lookupname || '', $catname || '', $attname || '', $bki_values->{line_number} || '';
$num_errors++;
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/backend/utils/adt/regproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,35 @@ to_regtype(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(result);
}


/*
* to_regtypemod() complements to_regtype, returning the typmod for the type,
* if any.
*
* If the type name is not found, we return NULL.
*
* Internally it relies on the Postgres core parseTypeString() function defined
* in src/backend/parser/parse_type.c.
*/
Datum
to_regtypemod(PG_FUNCTION_ARGS)
{
char *typ_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
Oid typid;
int32 typmod;
ErrorSaveContext escontext = {T_ErrorSaveContext};

/*
* Parse type-name argument to obtain the encoded typmod. Return NULL
* on failure.
*/
if (!parseTypeString(typ_name, &typid, &typmod, (Node *) &escontext)) {
PG_RETURN_NULL();
}

PG_RETURN_INT32(typmod);
}

/*
* regtypeout - converts type OID to "typ_name"
*/
Expand Down
3 changes: 3 additions & 0 deletions src/include/catalog/pg_proc.dat
Original file line number Diff line number Diff line change
Expand Up @@ -7155,6 +7155,9 @@
{ oid => '3493', descr => 'convert type name to regtype',
proname => 'to_regtype', provolatile => 's', prorettype => 'regtype',
proargtypes => 'text', prosrc => 'to_regtype' },
{ oid => '8401', descr => 'convert type name to type mod',
proname => 'to_regtypemod', provolatile => 's', prorettype => 'int4',
proargtypes => 'text', prosrc => 'to_regtypemod' },
{ oid => '1079', descr => 'convert text to regclass',
proname => 'regclass', provolatile => 's', prorettype => 'regclass',
proargtypes => 'text', prosrc => 'text_regclass' },
Expand Down
66 changes: 66 additions & 0 deletions src/test/regress/expected/regproc.out
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,69 @@ SELECT * FROM pg_input_error_info('way.too.many.names', 'regtype');
ERROR: improper qualified name (too many dotted names): way.too.many.names
SELECT * FROM pg_input_error_info('no_such_catalog.schema.name', 'regtype');
ERROR: cross-database references are not implemented: no_such_catalog.schema.name
-- Test to_regtypemod
SELECT to_regtypemod('text');
to_regtypemod
---------------
-1
(1 row)

SELECT to_regtypemod('timestamp(4)');
to_regtypemod
---------------
4
(1 row)

SELECT to_regtypemod('interval(0)');
to_regtypemod
---------------
2147418112
(1 row)

SELECT to_regtypemod('interval second(0)');
to_regtypemod
---------------
268435456
(1 row)

SELECT to_regtypemod('timestamptz');
to_regtypemod
---------------
-1
(1 row)

SELECT to_regtypemod('timestamptz(6)');
to_regtypemod
---------------
6
(1 row)

SELECT to_regtypemod('varchar');
to_regtypemod
---------------
-1
(1 row)

SELECT to_regtypemod('varchar(128)');
to_regtypemod
---------------
132
(1 row)

SELECT to_regtypemod(NULL); -- returns null on null input
to_regtypemod
---------------

(1 row)

SELECT to_regtypemod('year(4)'); -- error trapped, returns null
to_regtypemod
---------------

(1 row)

SELECT to_regtypemod('interval nonesuch'); -- grammar error raised, not trapped
ERROR: syntax error at or near "nonesuch"
LINE 1: SELECT to_regtypemod('interval nonesuch');
^
CONTEXT: invalid type name "interval nonesuch"
14 changes: 14 additions & 0 deletions src/test/regress/sql/regproc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ SELECT * FROM pg_input_error_info('incorrect type name syntax', 'regtype');
SELECT * FROM pg_input_error_info('numeric(1,2,3)', 'regtype'); -- bogus typmod
SELECT * FROM pg_input_error_info('way.too.many.names', 'regtype');
SELECT * FROM pg_input_error_info('no_such_catalog.schema.name', 'regtype');

-- Test to_regtypemod
SELECT to_regtypemod('text');
SELECT to_regtypemod('timestamp(4)');
SELECT to_regtypemod('interval(0)');
SELECT to_regtypemod('interval second(0)');
SELECT to_regtypemod('timestamptz');
SELECT to_regtypemod('timestamptz(6)');
SELECT to_regtypemod('varchar');
SELECT to_regtypemod('varchar(128)');

SELECT to_regtypemod(NULL); -- returns null on null input
SELECT to_regtypemod('year(4)'); -- error trapped, returns null
SELECT to_regtypemod('interval nonesuch'); -- grammar error raised, not trapped