forked from pganalyze/libpg_query
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpg_query.h
More file actions
235 lines (190 loc) · 7.76 KB
/
pg_query.h
File metadata and controls
235 lines (190 loc) · 7.76 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef PG_QUERY_H
#define PG_QUERY_H
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include "postgres_deparse.h"
typedef struct {
char* message; // exception message
char* funcname; // source function of exception (e.g. SearchSysCache)
char* filename; // source of exception (e.g. parse.l)
int lineno; // source of exception (e.g. 104)
int cursorpos; // char in query at which exception occurred
char* context; // additional context (optional, can be NULL)
} PgQueryError;
typedef struct {
int length;
bool *items;
PgQueryError* error;
} PgQueryIsUtilityResult;
typedef struct {
size_t len;
char* data;
} PgQueryProtobuf;
typedef struct {
PgQueryProtobuf pbuf;
char* stderr_buffer;
PgQueryError* error;
} PgQueryScanResult;
typedef struct {
char* parse_tree;
char* stderr_buffer;
PgQueryError* error;
} PgQueryParseResult;
typedef struct {
PgQueryProtobuf parse_tree;
char* stderr_buffer;
PgQueryError* error;
} PgQueryProtobufParseResult;
typedef struct {
int stmt_location;
int stmt_len;
} PgQuerySplitStmt;
typedef struct {
PgQuerySplitStmt **stmts;
int n_stmts;
char* stderr_buffer;
PgQueryError* error;
} PgQuerySplitResult;
typedef struct {
char* query;
PgQueryError* error;
} PgQueryDeparseResult;
typedef struct {
PostgresDeparseComment **comments;
size_t comment_count;
PgQueryError* error;
} PgQueryDeparseCommentsResult;
typedef struct {
char* plpgsql_funcs;
PgQueryError* error;
} PgQueryPlpgsqlParseResult;
typedef struct {
uint64_t fingerprint;
char* fingerprint_str;
char* stderr_buffer;
PgQueryError* error;
} PgQueryFingerprintResult;
typedef struct {
char* normalized_query;
PgQueryError* error;
} PgQueryNormalizeResult;
typedef struct {
PgQueryProtobuf summary;
char* stderr_buffer;
PgQueryError* error;
} PgQuerySummaryParseResult;
// Postgres parser options (parse mode and GUCs that affect parsing)
typedef enum
{
PG_QUERY_PARSE_DEFAULT = 0,
PG_QUERY_PARSE_TYPE_NAME,
PG_QUERY_PARSE_PLPGSQL_EXPR,
PG_QUERY_PARSE_PLPGSQL_ASSIGN1,
PG_QUERY_PARSE_PLPGSQL_ASSIGN2,
PG_QUERY_PARSE_PLPGSQL_ASSIGN3
} PgQueryParseMode;
// We technically only need 3 bits to store parse mode, but
// having 4 bits avoids API breaks if another one gets added.
#define PG_QUERY_PARSE_MODE_BITS 4
#define PG_QUERY_PARSE_MODE_BITMASK ((1 << PG_QUERY_PARSE_MODE_BITS) - 1)
#define PG_QUERY_DISABLE_BACKSLASH_QUOTE 16 // backslash_quote = off (default is safe_encoding, which is effectively on)
#define PG_QUERY_DISABLE_STANDARD_CONFORMING_STRINGS 32 // standard_conforming_strings = off (default is on)
#define PG_QUERY_DISABLE_ESCAPE_STRING_WARNING 64 // escape_string_warning = off (default is on)
#ifdef __cplusplus
extern "C" {
#endif
PgQueryNormalizeResult pg_query_normalize(const char* input);
PgQueryNormalizeResult pg_query_normalize_utility(const char* input);
PgQueryScanResult pg_query_scan(const char* input);
PgQueryParseResult pg_query_parse(const char* input);
PgQueryParseResult pg_query_parse_opts(const char* input, int parser_options);
PgQueryProtobufParseResult pg_query_parse_protobuf(const char* input);
PgQueryProtobufParseResult pg_query_parse_protobuf_opts(const char* input, int parser_options);
PgQueryPlpgsqlParseResult pg_query_parse_plpgsql(const char* input);
PgQueryFingerprintResult pg_query_fingerprint(const char* input);
PgQueryFingerprintResult pg_query_fingerprint_opts(const char* input, int parser_options);
// Use pg_query_split_with_scanner when you need to split statements that may
// contain parse errors, otherwise pg_query_split_with_parser is recommended
// for improved accuracy due the parser adding additional token handling.
//
// Note that we try to support special cases like comments, strings containing
// ";" on both, as well as oddities like "CREATE RULE .. (SELECT 1; SELECT 2);"
// which is treated as as single statement.
PgQuerySplitResult pg_query_split_with_scanner(const char *input);
PgQuerySplitResult pg_query_split_with_parser(const char *input);
PgQueryDeparseResult pg_query_deparse_protobuf(PgQueryProtobuf parse_tree);
PgQueryDeparseResult pg_query_deparse_protobuf_opts(PgQueryProtobuf parse_tree, struct PostgresDeparseOpts opts);
PgQueryDeparseCommentsResult pg_query_deparse_comments_for_query(const char *query);
PgQueryIsUtilityResult pg_query_is_utility_stmt(const char *query);
PgQuerySummaryParseResult pg_query_summary(const char* input, int parser_options, int truncate_limit);
void pg_query_free_normalize_result(PgQueryNormalizeResult result);
void pg_query_free_scan_result(PgQueryScanResult result);
void pg_query_free_parse_result(PgQueryParseResult result);
void pg_query_free_split_result(PgQuerySplitResult result);
void pg_query_free_deparse_result(PgQueryDeparseResult result);
void pg_query_free_deparse_comments_result(PgQueryDeparseCommentsResult result);
void pg_query_free_protobuf_parse_result(PgQueryProtobufParseResult result);
void pg_query_free_plpgsql_parse_result(PgQueryPlpgsqlParseResult result);
void pg_query_free_fingerprint_result(PgQueryFingerprintResult result);
void pg_query_free_is_utility_result(PgQueryIsUtilityResult result);
void pg_query_free_summary_parse_result(PgQuerySummaryParseResult result);
// Optional, cleans up the top-level memory context (automatically done for threads that exit)
void pg_query_exit(void);
// Postgres version information
#define PG_MAJORVERSION "17"
#define PG_VERSION "17.7"
#define PG_VERSION_NUM 170007
// Raw parse tree access (bypasses protobuf serialization)
// Note: The returned tree uses PostgreSQL's memory context. The tree is only
// valid until pg_query_free_raw_parse_result is called or pg_query_exit is called.
// Forward declaration of PostgreSQL List type (defined in nodes/pg_list.h)
struct List;
// Forward declaration of PostgreSQL MemoryContextData type
struct MemoryContextData;
typedef struct {
struct List *tree; // PostgreSQL parse tree (List of RawStmt nodes)
char* stderr_buffer;
PgQueryError* error;
struct MemoryContextData* context; // Internal: Memory context for the tree (do not modify)
} PgQueryRawParseResult;
PgQueryRawParseResult pg_query_parse_raw(const char* input);
PgQueryRawParseResult pg_query_parse_raw_opts(const char* input, int parser_options);
void pg_query_free_raw_parse_result(PgQueryRawParseResult result);
// Raw deparse (bypasses protobuf serialization)
// Takes a raw parse result and converts it back to SQL
PgQueryDeparseResult pg_query_deparse_raw(PgQueryRawParseResult parse_result);
PgQueryDeparseResult pg_query_deparse_raw_opts(PgQueryRawParseResult parse_result, struct PostgresDeparseOpts opts);
// Node building helpers for Rust (bypasses protobuf)
// These allow Rust to construct parse trees directly
void *pg_query_deparse_enter_context(void);
void pg_query_deparse_exit_context(void *ctx);
void *pg_query_alloc_node(size_t size, int tag);
char *pg_query_pstrdup(const char *str);
void *pg_query_list_make1(void *datum);
void *pg_query_list_append(void *list, void *datum);
PgQueryDeparseResult pg_query_deparse_nodes(void *stmts);
// Raw scan (bypasses protobuf serialization)
// Returns tokens directly without protobuf encoding
typedef struct {
int start;
int end;
int token; // Token type (matches Token enum in protobuf)
int keyword_kind; // KeywordKind enum value
} PgQueryRawScanToken;
typedef struct {
PgQueryRawScanToken *tokens;
size_t n_tokens;
char* stderr_buffer;
PgQueryError* error;
} PgQueryRawScanResult;
PgQueryRawScanResult pg_query_scan_raw(const char* input);
void pg_query_free_raw_scan_result(PgQueryRawScanResult result);
// Raw fingerprint (works with raw parse result, bypasses re-parsing)
PgQueryFingerprintResult pg_query_fingerprint_raw(PgQueryRawParseResult parse_result);
// Deprecated APIs below
void pg_query_init(void); // Deprecated as of 9.5-1.4.1, this is now run automatically as needed
#ifdef __cplusplus
}
#endif
#endif