LCOV - differential code coverage report
Current view: top level - src/backend/commands - conversioncmds.c (source / functions) Coverage Total Hit LBC UIC GIC GNC CBC EUB ECB
Current: Differential Code Coverage HEAD vs 15 Lines: 69.0 % 29 20 6 3 17 2 1 9 13
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 1 1 1 1
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  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 GIC          32 : CreateConversionCommand(CreateConversionStmt *stmt)
      40                 : {
      41 ECB             :     Oid         namespaceId;
      42                 :     char       *conversion_name;
      43                 :     AclResult   aclresult;
      44                 :     int         from_encoding;
      45                 :     int         to_encoding;
      46                 :     Oid         funcoid;
      47 GIC          32 :     const char *from_encoding_name = stmt->for_encoding_name;
      48              32 :     const char *to_encoding_name = stmt->to_encoding_name;
      49 CBC          32 :     List       *func_name = stmt->func_name;
      50 ECB             :     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 GIC          32 :     namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
      56                 :                                                     &conversion_name);
      57 ECB             : 
      58                 :     /* Check we have creation rights in target namespace */
      59 GNC          32 :     aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(), ACL_CREATE);
      60 GIC          32 :     if (aclresult != ACLCHECK_OK)
      61 LBC           0 :         aclcheck_error(aclresult, OBJECT_SCHEMA,
      62               0 :                        get_namespace_name(namespaceId));
      63 EUB             : 
      64                 :     /* Check the encoding names */
      65 GIC          32 :     from_encoding = pg_char_to_encoding(from_encoding_name);
      66              32 :     if (from_encoding < 0)
      67 LBC           0 :         ereport(ERROR,
      68 ECB             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
      69 EUB             :                  errmsg("source encoding \"%s\" does not exist",
      70                 :                         from_encoding_name)));
      71                 : 
      72 GIC          32 :     to_encoding = pg_char_to_encoding(to_encoding_name);
      73              32 :     if (to_encoding < 0)
      74 LBC           0 :         ereport(ERROR,
      75 ECB             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
      76 EUB             :                  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 GIC          32 :     if (from_encoding == PG_SQL_ASCII || to_encoding == PG_SQL_ASCII)
      87 UIC           0 :         ereport(ERROR,
      88 ECB             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
      89 EUB             :                  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 GIC          32 :     funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
      96                 :                              funcargs, false);
      97 ECB             : 
      98                 :     /* Check it returns int4, else it's probably the wrong function */
      99 GIC          32 :     if (get_func_rettype(funcoid) != INT4OID)
     100 UIC           0 :         ereport(ERROR,
     101 ECB             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     102 EUB             :                  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 GNC          32 :     aclresult = object_aclcheck(ProcedureRelationId, funcoid, GetUserId(), ACL_EXECUTE);
     107 GIC          32 :     if (aclresult != ACLCHECK_OK)
     108 LBC           0 :         aclcheck_error(aclresult, OBJECT_FUNCTION,
     109               0 :                        NameListToString(func_name));
     110 EUB             : 
     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 GIC          32 :     funcresult = OidFunctionCall6(funcoid,
     118                 :                                   Int32GetDatum(from_encoding),
     119 ECB             :                                   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 GIC          32 :     if (DatumGetInt32(funcresult) != 0)
     130 UIC           0 :         ereport(ERROR,
     131 ECB             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     132 EUB             :                  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 GIC          32 :     return ConversionCreate(conversion_name, namespaceId, GetUserId(),
     140              32 :                             from_encoding, to_encoding, funcoid, stmt->def);
     141 ECB             : }
        

Generated by: LCOV version v1.16-55-g56c0a2a