forked from enova/pgl_ddl_deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgl_ddl_deploy.c
45 lines (39 loc) · 1.2 KB
/
pgl_ddl_deploy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "postgres.h"
#include "fmgr.h"
#include "catalog/pg_type.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "parser/parser.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(sql_command_tags);
/*
* Return a text array of the command tags in SQL command
*/
Datum
sql_command_tags(PG_FUNCTION_ARGS)
{
text *sql_t = PG_GETARG_TEXT_P(0);
char *sql;
List *parsetree_list;
ListCell *parsetree_item;
const char *commandTag;
ArrayBuildState *astate = NULL;
/*
* Get the SQL parsetree
*/
sql = text_to_cstring(sql_t);
parsetree_list = pg_parse_query(sql);
/*
* Iterate through each parsetree_item to get CommandTag
*/
foreach(parsetree_item, parsetree_list)
{
Node *parsetree = (Node *) lfirst(parsetree_item);
commandTag = CreateCommandTag(parsetree);
astate = accumArrayResult(astate, CStringGetTextDatum(commandTag),
false, TEXTOID, CurrentMemoryContext);
}
if (astate == NULL)
elog(ERROR, "Invalid sql command");
PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
}