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 13beta1 Lines: 60 230 26.1 %
Date: 2020-06-01 00:06:26 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-2020, 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         258 : get_command_type(PG_FUNCTION_ARGS)
      30             : {
      31         258 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      32             :     const char *type;
      33             : 
      34         258 :     switch (cmd->type)
      35             :     {
      36         226 :         case SCT_Simple:
      37         226 :             type = "simple";
      38         226 :             break;
      39          12 :         case SCT_AlterTable:
      40          12 :             type = "alter table";
      41          12 :             break;
      42          12 :         case SCT_Grant:
      43          12 :             type = "grant";
      44          12 :             break;
      45           2 :         case SCT_AlterOpFamily:
      46           2 :             type = "alter operator family";
      47           2 :             break;
      48           2 :         case SCT_AlterDefaultPrivileges:
      49           2 :             type = "alter default privileges";
      50           2 :             break;
      51           2 :         case SCT_CreateOpClass:
      52           2 :             type = "create operator class";
      53           2 :             break;
      54           2 :         case SCT_AlterTSConfig:
      55           2 :             type = "alter text search configuration";
      56           2 :             break;
      57           0 :         default:
      58           0 :             type = "unknown command type";
      59           0 :             break;
      60             :     }
      61             : 
      62         258 :     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         258 : get_command_tag(PG_FUNCTION_ARGS)
      71             : {
      72         258 :     CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
      73             : 
      74         258 :     if (!cmd->parsetree)
      75          12 :         PG_RETURN_NULL();
      76             : 
      77         246 :     PG_RETURN_TEXT_P(cstring_to_text(CreateCommandName(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           0 :             case AT_AddColumn:
     103           0 :                 strtype = "ADD COLUMN";
     104           0 :                 break;
     105           2 :             case AT_AddColumnRecurse:
     106           2 :                 strtype = "ADD COLUMN (and recurse)";
     107           2 :                 break;
     108           0 :             case AT_AddColumnToView:
     109           0 :                 strtype = "ADD COLUMN TO VIEW";
     110           0 :                 break;
     111           0 :             case AT_ColumnDefault:
     112           0 :                 strtype = "ALTER COLUMN SET DEFAULT";
     113           0 :                 break;
     114           0 :             case AT_DropNotNull:
     115           0 :                 strtype = "DROP NOT NULL";
     116           0 :                 break;
     117           6 :             case AT_SetNotNull:
     118           6 :                 strtype = "SET NOT NULL";
     119           6 :                 break;
     120           0 :             case AT_CheckNotNull:
     121           0 :                 strtype = "CHECK NOT NULL";
     122           0 :                 break;
     123           0 :             case AT_SetStatistics:
     124           0 :                 strtype = "SET STATS";
     125           0 :                 break;
     126           0 :             case AT_SetOptions:
     127           0 :                 strtype = "SET OPTIONS";
     128           0 :                 break;
     129           0 :             case AT_ResetOptions:
     130           0 :                 strtype = "RESET OPTIONS";
     131           0 :                 break;
     132           0 :             case AT_SetStorage:
     133           0 :                 strtype = "SET STORAGE";
     134           0 :                 break;
     135           0 :             case AT_DropColumn:
     136           0 :                 strtype = "DROP COLUMN";
     137           0 :                 break;
     138           0 :             case AT_DropColumnRecurse:
     139           0 :                 strtype = "DROP COLUMN (and recurse)";
     140           0 :                 break;
     141           2 :             case AT_AddIndex:
     142           2 :                 strtype = "ADD INDEX";
     143           2 :                 break;
     144           0 :             case AT_ReAddIndex:
     145           0 :                 strtype = "(re) ADD INDEX";
     146           0 :                 break;
     147           0 :             case AT_AddConstraint:
     148           0 :                 strtype = "ADD CONSTRAINT";
     149           0 :                 break;
     150           6 :             case AT_AddConstraintRecurse:
     151           6 :                 strtype = "ADD CONSTRAINT (and recurse)";
     152           6 :                 break;
     153           0 :             case AT_ReAddConstraint:
     154           0 :                 strtype = "(re) ADD CONSTRAINT";
     155           0 :                 break;
     156           0 :             case AT_AlterConstraint:
     157           0 :                 strtype = "ALTER CONSTRAINT";
     158           0 :                 break;
     159           0 :             case AT_ValidateConstraint:
     160           0 :                 strtype = "VALIDATE CONSTRAINT";
     161           0 :                 break;
     162           0 :             case AT_ValidateConstraintRecurse:
     163           0 :                 strtype = "VALIDATE CONSTRAINT (and recurse)";
     164           0 :                 break;
     165           0 :             case AT_AddIndexConstraint:
     166           0 :                 strtype = "ADD CONSTRAINT (using index)";
     167           0 :                 break;
     168           0 :             case AT_DropConstraint:
     169           0 :                 strtype = "DROP CONSTRAINT";
     170           0 :                 break;
     171           0 :             case AT_DropConstraintRecurse:
     172           0 :                 strtype = "DROP CONSTRAINT (and recurse)";
     173           0 :                 break;
     174           0 :             case AT_ReAddComment:
     175           0 :                 strtype = "(re) ADD COMMENT";
     176           0 :                 break;
     177           0 :             case AT_AlterColumnType:
     178           0 :                 strtype = "ALTER COLUMN SET TYPE";
     179           0 :                 break;
     180           0 :             case AT_AlterColumnGenericOptions:
     181           0 :                 strtype = "ALTER COLUMN SET OPTIONS";
     182           0 :                 break;
     183           0 :             case AT_ChangeOwner:
     184           0 :                 strtype = "CHANGE OWNER";
     185           0 :                 break;
     186           0 :             case AT_ClusterOn:
     187           0 :                 strtype = "CLUSTER";
     188           0 :                 break;
     189           0 :             case AT_DropCluster:
     190           0 :                 strtype = "DROP CLUSTER";
     191           0 :                 break;
     192           0 :             case AT_SetLogged:
     193           0 :                 strtype = "SET LOGGED";
     194           0 :                 break;
     195           0 :             case AT_SetUnLogged:
     196           0 :                 strtype = "SET UNLOGGED";
     197           0 :                 break;
     198           0 :             case AT_DropOids:
     199           0 :                 strtype = "DROP OIDS";
     200           0 :                 break;
     201           0 :             case AT_SetTableSpace:
     202           0 :                 strtype = "SET TABLESPACE";
     203           0 :                 break;
     204           0 :             case AT_SetRelOptions:
     205           0 :                 strtype = "SET RELOPTIONS";
     206           0 :                 break;
     207           0 :             case AT_ResetRelOptions:
     208           0 :                 strtype = "RESET RELOPTIONS";
     209           0 :                 break;
     210           2 :             case AT_ReplaceRelOptions:
     211           2 :                 strtype = "REPLACE RELOPTIONS";
     212           2 :                 break;
     213           0 :             case AT_EnableTrig:
     214           0 :                 strtype = "ENABLE TRIGGER";
     215           0 :                 break;
     216           0 :             case AT_EnableAlwaysTrig:
     217           0 :                 strtype = "ENABLE TRIGGER (always)";
     218           0 :                 break;
     219           0 :             case AT_EnableReplicaTrig:
     220           0 :                 strtype = "ENABLE TRIGGER (replica)";
     221           0 :                 break;
     222           0 :             case AT_DisableTrig:
     223           0 :                 strtype = "DISABLE TRIGGER";
     224           0 :                 break;
     225           0 :             case AT_EnableTrigAll:
     226           0 :                 strtype = "ENABLE TRIGGER (all)";
     227           0 :                 break;
     228           0 :             case AT_DisableTrigAll:
     229           0 :                 strtype = "DISABLE TRIGGER (all)";
     230           0 :                 break;
     231           0 :             case AT_EnableTrigUser:
     232           0 :                 strtype = "ENABLE TRIGGER (user)";
     233           0 :                 break;
     234           0 :             case AT_DisableTrigUser:
     235           0 :                 strtype = "DISABLE TRIGGER (user)";
     236           0 :                 break;
     237           0 :             case AT_EnableRule:
     238           0 :                 strtype = "ENABLE RULE";
     239           0 :                 break;
     240           0 :             case AT_EnableAlwaysRule:
     241           0 :                 strtype = "ENABLE RULE (always)";
     242           0 :                 break;
     243           0 :             case AT_EnableReplicaRule:
     244           0 :                 strtype = "ENABLE RULE (replica)";
     245           0 :                 break;
     246           0 :             case AT_DisableRule:
     247           0 :                 strtype = "DISABLE RULE";
     248           0 :                 break;
     249           0 :             case AT_AddInherit:
     250           0 :                 strtype = "ADD INHERIT";
     251           0 :                 break;
     252           0 :             case AT_DropInherit:
     253           0 :                 strtype = "DROP INHERIT";
     254           0 :                 break;
     255           0 :             case AT_AddOf:
     256           0 :                 strtype = "OF";
     257           0 :                 break;
     258           0 :             case AT_DropOf:
     259           0 :                 strtype = "NOT OF";
     260           0 :                 break;
     261           0 :             case AT_ReplicaIdentity:
     262           0 :                 strtype = "REPLICA IDENTITY";
     263           0 :                 break;
     264           0 :             case AT_EnableRowSecurity:
     265           0 :                 strtype = "ENABLE ROW SECURITY";
     266           0 :                 break;
     267           0 :             case AT_DisableRowSecurity:
     268           0 :                 strtype = "DISABLE ROW SECURITY";
     269           0 :                 break;
     270           0 :             case AT_ForceRowSecurity:
     271           0 :                 strtype = "FORCE ROW SECURITY";
     272           0 :                 break;
     273           0 :             case AT_NoForceRowSecurity:
     274           0 :                 strtype = "NO FORCE ROW SECURITY";
     275           0 :                 break;
     276           0 :             case AT_GenericOptions:
     277           0 :                 strtype = "SET OPTIONS";
     278           0 :                 break;
     279           0 :             default:
     280           0 :                 strtype = "unrecognized";
     281           0 :                 break;
     282             :         }
     283             : 
     284             :         astate =
     285          18 :             accumArrayResult(astate, CStringGetTextDatum(strtype),
     286             :                              false, TEXTOID, CurrentMemoryContext);
     287             :     }
     288             : 
     289          12 :     if (astate == NULL)
     290           0 :         elog(ERROR, "empty alter table subcommand list");
     291             : 
     292          12 :     PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
     293             : }

Generated by: LCOV version 1.13