LCOV - code coverage report
Current view: top level - src/test/modules/test_ddl_deparse - test_ddl_deparse.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 144 272 52.9 %
Date: 2022-12-05 12:10:47 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*----------------------------------------------------------------------
       2             :  * test_ddl_deparse.c
       3             :  *      Support functions for the test_ddl_deparse module
       4             :  *
       5             :  * Copyright (c) 2014-2022, PostgreSQL Global Development Group
       6             :  *
       7             :  * IDENTIFICATION
       8             :  *    src/test/modules/test_ddl_deparse/test_ddl_deparse.c
       9             :  *----------------------------------------------------------------------
      10             :  */
      11             : #include "postgres.h"
      12             : 
      13             : #include "catalog/pg_type.h"
      14             : #include "funcapi.h"
      15             : #include "nodes/execnodes.h"
      16             : #include "tcop/deparse_utility.h"
      17             : #include "tcop/utility.h"
      18             : #include "utils/builtins.h"
      19             : 
      20          42 : PG_MODULE_MAGIC;
      21             : 
      22          42 : PG_FUNCTION_INFO_V1(get_command_type);
      23          42 : PG_FUNCTION_INFO_V1(get_command_tag);
      24           8 : PG_FUNCTION_INFO_V1(get_altertable_subcmdinfo);
      25             : 
      26             : /*
      27             :  * Return the textual representation of the struct type used to represent a
      28             :  * command in struct CollectedCommand format.
      29             :  */
      30             : Datum
      31         356 : get_command_type(PG_FUNCTION_ARGS)
      32             : {
      33         356 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      34             :     const char *type;
      35             : 
      36         356 :     switch (cmd->type)
      37             :     {
      38         266 :         case SCT_Simple:
      39         266 :             type = "simple";
      40         266 :             break;
      41          64 :         case SCT_AlterTable:
      42          64 :             type = "alter table";
      43          64 :             break;
      44          18 :         case SCT_Grant:
      45          18 :             type = "grant";
      46          18 :             break;
      47           2 :         case SCT_AlterOpFamily:
      48           2 :             type = "alter operator family";
      49           2 :             break;
      50           2 :         case SCT_AlterDefaultPrivileges:
      51           2 :             type = "alter default privileges";
      52           2 :             break;
      53           2 :         case SCT_CreateOpClass:
      54           2 :             type = "create operator class";
      55           2 :             break;
      56           2 :         case SCT_AlterTSConfig:
      57           2 :             type = "alter text search configuration";
      58           2 :             break;
      59           0 :         default:
      60           0 :             type = "unknown command type";
      61           0 :             break;
      62             :     }
      63             : 
      64         356 :     PG_RETURN_TEXT_P(cstring_to_text(type));
      65             : }
      66             : 
      67             : /*
      68             :  * Return the command tag corresponding to a parse node contained in a
      69             :  * CollectedCommand struct.
      70             :  */
      71             : Datum
      72         356 : get_command_tag(PG_FUNCTION_ARGS)
      73             : {
      74         356 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      75             : 
      76         356 :     if (!cmd->parsetree)
      77          18 :         PG_RETURN_NULL();
      78             : 
      79         338 :     PG_RETURN_TEXT_P(cstring_to_text(CreateCommandName(cmd->parsetree)));
      80             : }
      81             : 
      82             : /*
      83             :  * Return a text array representation of the subcommands of an ALTER TABLE
      84             :  * command.
      85             :  */
      86             : Datum
      87          64 : get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
      88             : {
      89          64 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      90             :     ListCell   *cell;
      91          64 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
      92             : 
      93          64 :     if (cmd->type != SCT_AlterTable)
      94           0 :         elog(ERROR, "command is not ALTER TABLE");
      95             : 
      96          64 :     InitMaterializedSRF(fcinfo, 0);
      97             : 
      98          64 :     if (cmd->d.alterTable.subcmds == NIL)
      99           0 :         elog(ERROR, "empty alter table subcommand list");
     100             : 
     101         166 :     foreach(cell, cmd->d.alterTable.subcmds)
     102             :     {
     103         102 :         CollectedATSubcmd *sub = lfirst(cell);
     104         102 :         AlterTableCmd *subcmd = castNode(AlterTableCmd, sub->parsetree);
     105         102 :         const char *strtype = "unrecognized";
     106             :         Datum       values[2];
     107             :         bool        nulls[2];
     108             : 
     109         102 :         memset(values, 0, sizeof(values));
     110         102 :         memset(nulls, 0, sizeof(nulls));
     111             : 
     112         102 :         switch (subcmd->subtype)
     113             :         {
     114           0 :             case AT_AddColumn:
     115           0 :                 strtype = "ADD COLUMN";
     116           0 :                 break;
     117           2 :             case AT_AddColumnRecurse:
     118           2 :                 strtype = "ADD COLUMN (and recurse)";
     119           2 :                 break;
     120           0 :             case AT_AddColumnToView:
     121           0 :                 strtype = "ADD COLUMN TO VIEW";
     122           0 :                 break;
     123           6 :             case AT_ColumnDefault:
     124           6 :                 strtype = "ALTER COLUMN SET DEFAULT";
     125           6 :                 break;
     126           2 :             case AT_CookedColumnDefault:
     127           2 :                 strtype = "ALTER COLUMN SET DEFAULT (precooked)";
     128           2 :                 break;
     129           6 :             case AT_DropNotNull:
     130           6 :                 strtype = "DROP NOT NULL";
     131           6 :                 break;
     132          18 :             case AT_SetNotNull:
     133          18 :                 strtype = "SET NOT NULL";
     134          18 :                 break;
     135           2 :             case AT_DropExpression:
     136           2 :                 strtype = "DROP EXPRESSION";
     137           2 :                 break;
     138           0 :             case AT_CheckNotNull:
     139           0 :                 strtype = "CHECK NOT NULL";
     140           0 :                 break;
     141           6 :             case AT_SetStatistics:
     142           6 :                 strtype = "SET STATS";
     143           6 :                 break;
     144           0 :             case AT_SetOptions:
     145           0 :                 strtype = "SET OPTIONS";
     146           0 :                 break;
     147           0 :             case AT_ResetOptions:
     148           0 :                 strtype = "RESET OPTIONS";
     149           0 :                 break;
     150           6 :             case AT_SetStorage:
     151           6 :                 strtype = "SET STORAGE";
     152           6 :                 break;
     153           2 :             case AT_SetCompression:
     154           2 :                 strtype = "SET COMPRESSION";
     155           2 :                 break;
     156           0 :             case AT_DropColumn:
     157           0 :                 strtype = "DROP COLUMN";
     158           0 :                 break;
     159           0 :             case AT_DropColumnRecurse:
     160           0 :                 strtype = "DROP COLUMN (and recurse)";
     161           0 :                 break;
     162           2 :             case AT_AddIndex:
     163           2 :                 strtype = "ADD INDEX";
     164           2 :                 break;
     165           0 :             case AT_ReAddIndex:
     166           0 :                 strtype = "(re) ADD INDEX";
     167           0 :                 break;
     168           2 :             case AT_AddConstraint:
     169           2 :                 strtype = "ADD CONSTRAINT";
     170           2 :                 break;
     171           6 :             case AT_AddConstraintRecurse:
     172           6 :                 strtype = "ADD CONSTRAINT (and recurse)";
     173           6 :                 break;
     174           0 :             case AT_ReAddConstraint:
     175           0 :                 strtype = "(re) ADD CONSTRAINT";
     176           0 :                 break;
     177           2 :             case AT_ReAddDomainConstraint:
     178           2 :                 strtype = "(re) ADD DOMAIN CONSTRAINT";
     179           2 :                 break;
     180           0 :             case AT_AlterConstraint:
     181           0 :                 strtype = "ALTER CONSTRAINT";
     182           0 :                 break;
     183           0 :             case AT_ValidateConstraint:
     184           0 :                 strtype = "VALIDATE CONSTRAINT";
     185           0 :                 break;
     186           0 :             case AT_ValidateConstraintRecurse:
     187           0 :                 strtype = "VALIDATE CONSTRAINT (and recurse)";
     188           0 :                 break;
     189           0 :             case AT_AddIndexConstraint:
     190           0 :                 strtype = "ADD CONSTRAINT (using index)";
     191           0 :                 break;
     192           0 :             case AT_DropConstraint:
     193           0 :                 strtype = "DROP CONSTRAINT";
     194           0 :                 break;
     195           0 :             case AT_DropConstraintRecurse:
     196           0 :                 strtype = "DROP CONSTRAINT (and recurse)";
     197           0 :                 break;
     198           0 :             case AT_ReAddComment:
     199           0 :                 strtype = "(re) ADD COMMENT";
     200           0 :                 break;
     201           8 :             case AT_AlterColumnType:
     202           8 :                 strtype = "ALTER COLUMN SET TYPE";
     203           8 :                 break;
     204           0 :             case AT_AlterColumnGenericOptions:
     205           0 :                 strtype = "ALTER COLUMN SET OPTIONS";
     206           0 :                 break;
     207           0 :             case AT_ChangeOwner:
     208           0 :                 strtype = "CHANGE OWNER";
     209           0 :                 break;
     210           2 :             case AT_ClusterOn:
     211           2 :                 strtype = "CLUSTER";
     212           2 :                 break;
     213           0 :             case AT_DropCluster:
     214           0 :                 strtype = "DROP CLUSTER";
     215           0 :                 break;
     216           2 :             case AT_SetLogged:
     217           2 :                 strtype = "SET LOGGED";
     218           2 :                 break;
     219           2 :             case AT_SetUnLogged:
     220           2 :                 strtype = "SET UNLOGGED";
     221           2 :                 break;
     222           0 :             case AT_DropOids:
     223           0 :                 strtype = "DROP OIDS";
     224           0 :                 break;
     225           0 :             case AT_SetAccessMethod:
     226           0 :                 strtype = "SET ACCESS METHOD";
     227           0 :                 break;
     228           0 :             case AT_SetTableSpace:
     229           0 :                 strtype = "SET TABLESPACE";
     230           0 :                 break;
     231           2 :             case AT_SetRelOptions:
     232           2 :                 strtype = "SET RELOPTIONS";
     233           2 :                 break;
     234           2 :             case AT_ResetRelOptions:
     235           2 :                 strtype = "RESET RELOPTIONS";
     236           2 :                 break;
     237           2 :             case AT_ReplaceRelOptions:
     238           2 :                 strtype = "REPLACE RELOPTIONS";
     239           2 :                 break;
     240           0 :             case AT_EnableTrig:
     241           0 :                 strtype = "ENABLE TRIGGER";
     242           0 :                 break;
     243           0 :             case AT_EnableAlwaysTrig:
     244           0 :                 strtype = "ENABLE TRIGGER (always)";
     245           0 :                 break;
     246           0 :             case AT_EnableReplicaTrig:
     247           0 :                 strtype = "ENABLE TRIGGER (replica)";
     248           0 :                 break;
     249           0 :             case AT_DisableTrig:
     250           0 :                 strtype = "DISABLE TRIGGER";
     251           0 :                 break;
     252           0 :             case AT_EnableTrigAll:
     253           0 :                 strtype = "ENABLE TRIGGER (all)";
     254           0 :                 break;
     255           0 :             case AT_DisableTrigAll:
     256           0 :                 strtype = "DISABLE TRIGGER (all)";
     257           0 :                 break;
     258           0 :             case AT_EnableTrigUser:
     259           0 :                 strtype = "ENABLE TRIGGER (user)";
     260           0 :                 break;
     261           0 :             case AT_DisableTrigUser:
     262           0 :                 strtype = "DISABLE TRIGGER (user)";
     263           0 :                 break;
     264           0 :             case AT_EnableRule:
     265           0 :                 strtype = "ENABLE RULE";
     266           0 :                 break;
     267           0 :             case AT_EnableAlwaysRule:
     268           0 :                 strtype = "ENABLE RULE (always)";
     269           0 :                 break;
     270           0 :             case AT_EnableReplicaRule:
     271           0 :                 strtype = "ENABLE RULE (replica)";
     272           0 :                 break;
     273           0 :             case AT_DisableRule:
     274           0 :                 strtype = "DISABLE RULE";
     275           0 :                 break;
     276           0 :             case AT_AddInherit:
     277           0 :                 strtype = "ADD INHERIT";
     278           0 :                 break;
     279           0 :             case AT_DropInherit:
     280           0 :                 strtype = "DROP INHERIT";
     281           0 :                 break;
     282           0 :             case AT_AddOf:
     283           0 :                 strtype = "OF";
     284           0 :                 break;
     285           0 :             case AT_DropOf:
     286           0 :                 strtype = "NOT OF";
     287           0 :                 break;
     288           0 :             case AT_ReplicaIdentity:
     289           0 :                 strtype = "REPLICA IDENTITY";
     290           0 :                 break;
     291           2 :             case AT_EnableRowSecurity:
     292           2 :                 strtype = "ENABLE ROW SECURITY";
     293           2 :                 break;
     294           2 :             case AT_DisableRowSecurity:
     295           2 :                 strtype = "DISABLE ROW SECURITY";
     296           2 :                 break;
     297           2 :             case AT_ForceRowSecurity:
     298           2 :                 strtype = "FORCE ROW SECURITY";
     299           2 :                 break;
     300           2 :             case AT_NoForceRowSecurity:
     301           2 :                 strtype = "NO FORCE ROW SECURITY";
     302           2 :                 break;
     303           0 :             case AT_GenericOptions:
     304           0 :                 strtype = "SET OPTIONS";
     305           0 :                 break;
     306           2 :             case AT_DetachPartition:
     307           2 :                 strtype = "DETACH PARTITION";
     308           2 :                 break;
     309           2 :             case AT_AttachPartition:
     310           2 :                 strtype = "ATTACH PARTITION";
     311           2 :                 break;
     312           0 :             case AT_DetachPartitionFinalize:
     313           0 :                 strtype = "DETACH PARTITION ... FINALIZE";
     314           0 :                 break;
     315           2 :             case AT_AddIdentity:
     316           2 :                 strtype = "ADD IDENTITY";
     317           2 :                 break;
     318           2 :             case AT_SetIdentity:
     319           2 :                 strtype = "SET IDENTITY";
     320           2 :                 break;
     321           2 :             case AT_DropIdentity:
     322           2 :                 strtype = "DROP IDENTITY";
     323           2 :                 break;
     324           2 :             case AT_ReAddStatistics:
     325           2 :                 strtype = "(re) ADD STATS";
     326           2 :                 break;
     327             :         }
     328             : 
     329         102 :         values[0] = CStringGetTextDatum(strtype);
     330         102 :         if (OidIsValid(sub->address.objectId))
     331             :         {
     332             :             char       *objdesc;
     333          84 :             objdesc = getObjectDescription((const ObjectAddress *) &sub->address, false);
     334          84 :             values[1] = CStringGetTextDatum(objdesc);
     335             :         }
     336             :         else
     337          18 :             nulls[1] = true;
     338             : 
     339         102 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
     340             :     }
     341             : 
     342          64 :     return (Datum) 0;
     343             : }

Generated by: LCOV version 1.14