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 13devel Lines: 49 165 29.7 %
Date: 2019-11-15 23:07:02 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-2019, 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 "tcop/deparse_utility.h"
      15             : #include "tcop/utility.h"
      16             : #include "utils/builtins.h"
      17             : 
      18          42 : PG_MODULE_MAGIC;
      19             : 
      20          42 : PG_FUNCTION_INFO_V1(get_command_type);
      21          42 : PG_FUNCTION_INFO_V1(get_command_tag);
      22           8 : PG_FUNCTION_INFO_V1(get_altertable_subcmdtypes);
      23             : 
      24             : /*
      25             :  * Return the textual representation of the struct type used to represent a
      26             :  * command in struct CollectedCommand format.
      27             :  */
      28             : Datum
      29         248 : get_command_type(PG_FUNCTION_ARGS)
      30             : {
      31         248 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      32             :     const char *type;
      33             : 
      34         248 :     switch (cmd->type)
      35             :     {
      36             :         case SCT_Simple:
      37         218 :             type = "simple";
      38         218 :             break;
      39             :         case SCT_AlterTable:
      40          12 :             type = "alter table";
      41          12 :             break;
      42             :         case SCT_Grant:
      43          10 :             type = "grant";
      44          10 :             break;
      45             :         case SCT_AlterOpFamily:
      46           2 :             type = "alter operator family";
      47           2 :             break;
      48             :         case SCT_AlterDefaultPrivileges:
      49           2 :             type = "alter default privileges";
      50           2 :             break;
      51             :         case SCT_CreateOpClass:
      52           2 :             type = "create operator class";
      53           2 :             break;
      54             :         case SCT_AlterTSConfig:
      55           2 :             type = "alter text search configuration";
      56           2 :             break;
      57             :         default:
      58           0 :             type = "unknown command type";
      59           0 :             break;
      60             :     }
      61             : 
      62         248 :     PG_RETURN_TEXT_P(cstring_to_text(type));
      63             : }
      64             : 
      65             : /*
      66             :  * Return the command tag corresponding to a parse node contained in a
      67             :  * CollectedCommand struct.
      68             :  */
      69             : Datum
      70         248 : get_command_tag(PG_FUNCTION_ARGS)
      71             : {
      72         248 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      73             : 
      74         248 :     if (!cmd->parsetree)
      75          10 :         PG_RETURN_NULL();
      76             : 
      77         238 :     PG_RETURN_TEXT_P(cstring_to_text(CreateCommandTag(cmd->parsetree)));
      78             : }
      79             : 
      80             : /*
      81             :  * Return a text array representation of the subcommands of an ALTER TABLE
      82             :  * command.
      83             :  */
      84             : Datum
      85          12 : get_altertable_subcmdtypes(PG_FUNCTION_ARGS)
      86             : {
      87          12 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      88          12 :     ArrayBuildState *astate = NULL;
      89             :     ListCell   *cell;
      90             : 
      91          12 :     if (cmd->type != SCT_AlterTable)
      92           0 :         elog(ERROR, "command is not ALTER TABLE");
      93             : 
      94          30 :     foreach(cell, cmd->d.alterTable.subcmds)
      95             :     {
      96          18 :         CollectedATSubcmd *sub = lfirst(cell);
      97          18 :         AlterTableCmd *subcmd = castNode(AlterTableCmd, sub->parsetree);
      98             :         const char *strtype;
      99             : 
     100          18 :         switch (subcmd->subtype)
     101             :         {
     102             :             case AT_AddColumn:
     103           0 :                 strtype = "ADD COLUMN";
     104           0 :                 break;
     105             :             case AT_AddColumnRecurse:
     106           2 :                 strtype = "ADD COLUMN (and recurse)";
     107           2 :                 break;
     108             :             case AT_AddColumnToView:
     109           0 :                 strtype = "ADD COLUMN TO VIEW";
     110           0 :                 break;
     111             :             case AT_ColumnDefault:
     112           0 :                 strtype = "ALTER COLUMN SET DEFAULT";
     113           0 :                 break;
     114             :             case AT_DropNotNull:
     115           0 :                 strtype = "DROP NOT NULL";
     116           0 :                 break;
     117             :             case AT_SetNotNull:
     118           6 :                 strtype = "SET NOT NULL";
     119           6 :                 break;
     120             :             case AT_CheckNotNull:
     121           0 :                 strtype = "CHECK NOT NULL";
     122           0 :                 break;
     123             :             case AT_SetStatistics:
     124           0 :                 strtype = "SET STATS";
     125           0 :                 break;
     126             :             case AT_SetOptions:
     127           0 :                 strtype = "SET OPTIONS";
     128           0 :                 break;
     129             :             case AT_ResetOptions:
     130           0 :                 strtype = "RESET OPTIONS";
     131           0 :                 break;
     132             :             case AT_SetStorage:
     133           0 :                 strtype = "SET STORAGE";
     134           0 :                 break;
     135             :             case AT_DropColumn:
     136           0 :                 strtype = "DROP COLUMN";
     137           0 :                 break;
     138             :             case AT_DropColumnRecurse:
     139           0 :                 strtype = "DROP COLUMN (and recurse)";
     140           0 :                 break;
     141             :             case AT_AddIndex:
     142           2 :                 strtype = "ADD INDEX";
     143           2 :                 break;
     144             :             case AT_ReAddIndex:
     145           0 :                 strtype = "(re) ADD INDEX";
     146           0 :                 break;
     147             :             case AT_AddConstraint:
     148           0 :                 strtype = "ADD CONSTRAINT";
     149           0 :                 break;
     150             :             case AT_AddConstraintRecurse:
     151           6 :                 strtype = "ADD CONSTRAINT (and recurse)";
     152           6 :                 break;
     153             :             case AT_ReAddConstraint:
     154           0 :                 strtype = "(re) ADD CONSTRAINT";
     155           0 :                 break;
     156             :             case AT_AlterConstraint:
     157           0 :                 strtype = "ALTER CONSTRAINT";
     158           0 :                 break;
     159             :             case AT_ValidateConstraint:
     160           0 :                 strtype = "VALIDATE CONSTRAINT";
     161           0 :                 break;
     162             :             case AT_ValidateConstraintRecurse:
     163           0 :                 strtype = "VALIDATE CONSTRAINT (and recurse)";
     164           0 :                 break;
     165             :             case AT_ProcessedConstraint:
     166           0 :                 strtype = "ADD (processed) CONSTRAINT";
     167           0 :                 break;
     168             :             case AT_AddIndexConstraint:
     169           0 :                 strtype = "ADD CONSTRAINT (using index)";
     170           0 :                 break;
     171             :             case AT_DropConstraint:
     172           0 :                 strtype = "DROP CONSTRAINT";
     173           0 :                 break;
     174             :             case AT_DropConstraintRecurse:
     175           0 :                 strtype = "DROP CONSTRAINT (and recurse)";
     176           0 :                 break;
     177             :             case AT_ReAddComment:
     178           0 :                 strtype = "(re) ADD COMMENT";
     179           0 :                 break;
     180             :             case AT_AlterColumnType:
     181           0 :                 strtype = "ALTER COLUMN SET TYPE";
     182           0 :                 break;
     183             :             case AT_AlterColumnGenericOptions:
     184           0 :                 strtype = "ALTER COLUMN SET OPTIONS";
     185           0 :                 break;
     186             :             case AT_ChangeOwner:
     187           0 :                 strtype = "CHANGE OWNER";
     188           0 :                 break;
     189             :             case AT_ClusterOn:
     190           0 :                 strtype = "CLUSTER";
     191           0 :                 break;
     192             :             case AT_DropCluster:
     193           0 :                 strtype = "DROP CLUSTER";
     194           0 :                 break;
     195             :             case AT_SetLogged:
     196           0 :                 strtype = "SET LOGGED";
     197           0 :                 break;
     198             :             case AT_SetUnLogged:
     199           0 :                 strtype = "SET UNLOGGED";
     200           0 :                 break;
     201             :             case AT_DropOids:
     202           0 :                 strtype = "DROP OIDS";
     203           0 :                 break;
     204             :             case AT_SetTableSpace:
     205           0 :                 strtype = "SET TABLESPACE";
     206           0 :                 break;
     207             :             case AT_SetRelOptions:
     208           0 :                 strtype = "SET RELOPTIONS";
     209           0 :                 break;
     210             :             case AT_ResetRelOptions:
     211           0 :                 strtype = "RESET RELOPTIONS";
     212           0 :                 break;
     213             :             case AT_ReplaceRelOptions:
     214           2 :                 strtype = "REPLACE RELOPTIONS";
     215           2 :                 break;
     216             :             case AT_EnableTrig:
     217           0 :                 strtype = "ENABLE TRIGGER";
     218           0 :                 break;
     219             :             case AT_EnableAlwaysTrig:
     220           0 :                 strtype = "ENABLE TRIGGER (always)";
     221           0 :                 break;
     222             :             case AT_EnableReplicaTrig:
     223           0 :                 strtype = "ENABLE TRIGGER (replica)";
     224           0 :                 break;
     225             :             case AT_DisableTrig:
     226           0 :                 strtype = "DISABLE TRIGGER";
     227           0 :                 break;
     228             :             case AT_EnableTrigAll:
     229           0 :                 strtype = "ENABLE TRIGGER (all)";
     230           0 :                 break;
     231             :             case AT_DisableTrigAll:
     232           0 :                 strtype = "DISABLE TRIGGER (all)";
     233           0 :                 break;
     234             :             case AT_EnableTrigUser:
     235           0 :                 strtype = "ENABLE TRIGGER (user)";
     236           0 :                 break;
     237             :             case AT_DisableTrigUser:
     238           0 :                 strtype = "DISABLE TRIGGER (user)";
     239           0 :                 break;
     240             :             case AT_EnableRule:
     241           0 :                 strtype = "ENABLE RULE";
     242           0 :                 break;
     243             :             case AT_EnableAlwaysRule:
     244           0 :                 strtype = "ENABLE RULE (always)";
     245           0 :                 break;
     246             :             case AT_EnableReplicaRule:
     247           0 :                 strtype = "ENABLE RULE (replica)";
     248           0 :                 break;
     249             :             case AT_DisableRule:
     250           0 :                 strtype = "DISABLE RULE";
     251           0 :                 break;
     252             :             case AT_AddInherit:
     253           0 :                 strtype = "ADD INHERIT";
     254           0 :                 break;
     255             :             case AT_DropInherit:
     256           0 :                 strtype = "DROP INHERIT";
     257           0 :                 break;
     258             :             case AT_AddOf:
     259           0 :                 strtype = "OF";
     260           0 :                 break;
     261             :             case AT_DropOf:
     262           0 :                 strtype = "NOT OF";
     263           0 :                 break;
     264             :             case AT_ReplicaIdentity:
     265           0 :                 strtype = "REPLICA IDENTITY";
     266           0 :                 break;
     267             :             case AT_EnableRowSecurity:
     268           0 :                 strtype = "ENABLE ROW SECURITY";
     269           0 :                 break;
     270             :             case AT_DisableRowSecurity:
     271           0 :                 strtype = "DISABLE ROW SECURITY";
     272           0 :                 break;
     273             :             case AT_ForceRowSecurity:
     274           0 :                 strtype = "FORCE ROW SECURITY";
     275           0 :                 break;
     276             :             case AT_NoForceRowSecurity:
     277           0 :                 strtype = "NO FORCE ROW SECURITY";
     278           0 :                 break;
     279             :             case AT_GenericOptions:
     280           0 :                 strtype = "SET OPTIONS";
     281           0 :                 break;
     282             :             default:
     283           0 :                 strtype = "unrecognized";
     284           0 :                 break;
     285             :         }
     286             : 
     287          18 :         astate =
     288          18 :             accumArrayResult(astate, CStringGetTextDatum(strtype),
     289             :                              false, TEXTOID, CurrentMemoryContext);
     290             :     }
     291             : 
     292          12 :     if (astate == NULL)
     293           0 :         elog(ERROR, "empty alter table subcommand list");
     294             : 
     295          12 :     PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
     296             : }

Generated by: LCOV version 1.13