LCOV - code coverage report
Current view: top level - src/backend/catalog - pg_cast.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 35 36 97.2 %
Date: 2020-06-01 08:06:25 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pg_cast.c
       4             :  *    routines to support manipulation of the pg_cast relation
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/catalog/pg_cast.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/htup_details.h"
      18             : #include "access/table.h"
      19             : #include "catalog/catalog.h"
      20             : #include "catalog/dependency.h"
      21             : #include "catalog/indexing.h"
      22             : #include "catalog/objectaccess.h"
      23             : #include "catalog/pg_cast.h"
      24             : #include "catalog/pg_proc.h"
      25             : #include "catalog/pg_type.h"
      26             : #include "utils/builtins.h"
      27             : #include "utils/rel.h"
      28             : #include "utils/syscache.h"
      29             : 
      30             : /*
      31             :  * ----------------------------------------------------------------
      32             :  *      CastCreate
      33             :  *
      34             :  * Forms and inserts catalog tuples for a new cast being created.
      35             :  * Caller must have already checked privileges, and done consistency
      36             :  * checks on the given datatypes and cast function (if applicable).
      37             :  *
      38             :  * 'behavior' indicates the types of the dependencies that the new
      39             :  * cast will have on its input and output types and the cast function.
      40             :  * ----------------------------------------------------------------
      41             :  */
      42             : ObjectAddress
      43         194 : CastCreate(Oid sourcetypeid, Oid targettypeid, Oid funcid, char castcontext,
      44             :            char castmethod, DependencyType behavior)
      45             : {
      46             :     Relation    relation;
      47             :     HeapTuple   tuple;
      48             :     Oid         castid;
      49             :     Datum       values[Natts_pg_cast];
      50             :     bool        nulls[Natts_pg_cast];
      51             :     ObjectAddress myself,
      52             :                 referenced;
      53             : 
      54         194 :     relation = table_open(CastRelationId, RowExclusiveLock);
      55             : 
      56             :     /*
      57             :      * Check for duplicate.  This is just to give a friendly error message,
      58             :      * the unique index would catch it anyway (so no need to sweat about race
      59             :      * conditions).
      60             :      */
      61         194 :     tuple = SearchSysCache2(CASTSOURCETARGET,
      62             :                             ObjectIdGetDatum(sourcetypeid),
      63             :                             ObjectIdGetDatum(targettypeid));
      64         194 :     if (HeapTupleIsValid(tuple))
      65           0 :         ereport(ERROR,
      66             :                 (errcode(ERRCODE_DUPLICATE_OBJECT),
      67             :                  errmsg("cast from type %s to type %s already exists",
      68             :                         format_type_be(sourcetypeid),
      69             :                         format_type_be(targettypeid))));
      70             : 
      71             :     /* ready to go */
      72         194 :     castid = GetNewOidWithIndex(relation, CastOidIndexId, Anum_pg_cast_oid);
      73         194 :     values[Anum_pg_cast_oid - 1] = ObjectIdGetDatum(castid);
      74         194 :     values[Anum_pg_cast_castsource - 1] = ObjectIdGetDatum(sourcetypeid);
      75         194 :     values[Anum_pg_cast_casttarget - 1] = ObjectIdGetDatum(targettypeid);
      76         194 :     values[Anum_pg_cast_castfunc - 1] = ObjectIdGetDatum(funcid);
      77         194 :     values[Anum_pg_cast_castcontext - 1] = CharGetDatum(castcontext);
      78         194 :     values[Anum_pg_cast_castmethod - 1] = CharGetDatum(castmethod);
      79             : 
      80         194 :     MemSet(nulls, false, sizeof(nulls));
      81             : 
      82         194 :     tuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
      83             : 
      84         194 :     CatalogTupleInsert(relation, tuple);
      85             : 
      86             :     /* make dependency entries */
      87         194 :     myself.classId = CastRelationId;
      88         194 :     myself.objectId = castid;
      89         194 :     myself.objectSubId = 0;
      90             : 
      91             :     /* dependency on source type */
      92         194 :     referenced.classId = TypeRelationId;
      93         194 :     referenced.objectId = sourcetypeid;
      94         194 :     referenced.objectSubId = 0;
      95         194 :     recordDependencyOn(&myself, &referenced, behavior);
      96             : 
      97             :     /* dependency on target type */
      98         194 :     referenced.classId = TypeRelationId;
      99         194 :     referenced.objectId = targettypeid;
     100         194 :     referenced.objectSubId = 0;
     101         194 :     recordDependencyOn(&myself, &referenced, behavior);
     102             : 
     103             :     /* dependency on function */
     104         194 :     if (OidIsValid(funcid))
     105             :     {
     106          78 :         referenced.classId = ProcedureRelationId;
     107          78 :         referenced.objectId = funcid;
     108          78 :         referenced.objectSubId = 0;
     109          78 :         recordDependencyOn(&myself, &referenced, behavior);
     110             :     }
     111             : 
     112             :     /* dependency on extension */
     113         194 :     recordDependencyOnCurrentExtension(&myself, false);
     114             : 
     115             :     /* Post creation hook for new cast */
     116         194 :     InvokeObjectPostCreateHook(CastRelationId, castid, 0);
     117             : 
     118         194 :     heap_freetuple(tuple);
     119             : 
     120         194 :     table_close(relation, RowExclusiveLock);
     121             : 
     122         194 :     return myself;
     123             : }

Generated by: LCOV version 1.13