LCOV - code coverage report
Current view: top level - src/backend/commands - conversioncmds.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16beta1 Lines: 20 29 69.0 %
Date: 2023-05-30 18:12:27 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * conversioncmds.c
       4             :  *    conversion creation command support code
       5             :  *
       6             :  * Portions Copyright (c) 1996-2023, 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/htup_details.h"
      18             : #include "catalog/dependency.h"
      19             : #include "catalog/indexing.h"
      20             : #include "catalog/pg_conversion.h"
      21             : #include "catalog/pg_namespace.h"
      22             : #include "catalog/pg_proc.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/acl.h"
      30             : #include "utils/builtins.h"
      31             : #include "utils/lsyscache.h"
      32             : #include "utils/rel.h"
      33             : #include "utils/syscache.h"
      34             : 
      35             : /*
      36             :  * CREATE CONVERSION
      37             :  */
      38             : ObjectAddress
      39          64 : CreateConversionCommand(CreateConversionStmt *stmt)
      40             : {
      41             :     Oid         namespaceId;
      42             :     char       *conversion_name;
      43             :     AclResult   aclresult;
      44             :     int         from_encoding;
      45             :     int         to_encoding;
      46             :     Oid         funcoid;
      47          64 :     const char *from_encoding_name = stmt->for_encoding_name;
      48          64 :     const char *to_encoding_name = stmt->to_encoding_name;
      49          64 :     List       *func_name = stmt->func_name;
      50             :     static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID, BOOLOID};
      51             :     char        result[1];
      52             :     Datum       funcresult;
      53             : 
      54             :     /* Convert list of names to a name and namespace */
      55          64 :     namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
      56             :                                                     &conversion_name);
      57             : 
      58             :     /* Check we have creation rights in target namespace */
      59          64 :     aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(), ACL_CREATE);
      60          64 :     if (aclresult != ACLCHECK_OK)
      61           0 :         aclcheck_error(aclresult, OBJECT_SCHEMA,
      62           0 :                        get_namespace_name(namespaceId));
      63             : 
      64             :     /* Check the encoding names */
      65          64 :     from_encoding = pg_char_to_encoding(from_encoding_name);
      66          64 :     if (from_encoding < 0)
      67           0 :         ereport(ERROR,
      68             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
      69             :                  errmsg("source encoding \"%s\" does not exist",
      70             :                         from_encoding_name)));
      71             : 
      72          64 :     to_encoding = pg_char_to_encoding(to_encoding_name);
      73          64 :     if (to_encoding < 0)
      74           0 :         ereport(ERROR,
      75             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
      76             :                  errmsg("destination encoding \"%s\" does not exist",
      77             :                         to_encoding_name)));
      78             : 
      79             :     /*
      80             :      * We consider conversions to or from SQL_ASCII to be meaningless.  (If
      81             :      * you wish to change this, note that pg_do_encoding_conversion() and its
      82             :      * sister functions have hard-wired fast paths for any conversion in which
      83             :      * the source or target encoding is SQL_ASCII, so that an encoding
      84             :      * conversion function declared for such a case will never be used.)
      85             :      */
      86          64 :     if (from_encoding == PG_SQL_ASCII || to_encoding == PG_SQL_ASCII)
      87           0 :         ereport(ERROR,
      88             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
      89             :                  errmsg("encoding conversion to or from \"SQL_ASCII\" is not supported")));
      90             : 
      91             :     /*
      92             :      * Check the existence of the conversion function. Function name could be
      93             :      * a qualified name.
      94             :      */
      95          64 :     funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
      96             :                              funcargs, false);
      97             : 
      98             :     /* Check it returns int4, else it's probably the wrong function */
      99          64 :     if (get_func_rettype(funcoid) != INT4OID)
     100           0 :         ereport(ERROR,
     101             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     102             :                  errmsg("encoding conversion function %s must return type %s",
     103             :                         NameListToString(func_name), "integer")));
     104             : 
     105             :     /* Check we have EXECUTE rights for the function */
     106          64 :     aclresult = object_aclcheck(ProcedureRelationId, funcoid, GetUserId(), ACL_EXECUTE);
     107          64 :     if (aclresult != ACLCHECK_OK)
     108           0 :         aclcheck_error(aclresult, OBJECT_FUNCTION,
     109           0 :                        NameListToString(func_name));
     110             : 
     111             :     /*
     112             :      * Check that the conversion function is suitable for the requested source
     113             :      * and target encodings. We do that by calling the function with an empty
     114             :      * string; the conversion function should throw an error if it can't
     115             :      * perform the requested conversion.
     116             :      */
     117          64 :     funcresult = OidFunctionCall6(funcoid,
     118             :                                   Int32GetDatum(from_encoding),
     119             :                                   Int32GetDatum(to_encoding),
     120             :                                   CStringGetDatum(""),
     121             :                                   CStringGetDatum(result),
     122             :                                   Int32GetDatum(0),
     123             :                                   BoolGetDatum(false));
     124             : 
     125             :     /*
     126             :      * The function should return 0 for empty input. Might as well check that,
     127             :      * too.
     128             :      */
     129          64 :     if (DatumGetInt32(funcresult) != 0)
     130           0 :         ereport(ERROR,
     131             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     132             :                  errmsg("encoding conversion function %s returned incorrect result for empty input",
     133             :                         NameListToString(func_name))));
     134             : 
     135             :     /*
     136             :      * All seem ok, go ahead (possible failure would be a duplicate conversion
     137             :      * name)
     138             :      */
     139          64 :     return ConversionCreate(conversion_name, namespaceId, GetUserId(),
     140          64 :                             from_encoding, to_encoding, funcoid, stmt->def);
     141             : }

Generated by: LCOV version 1.14