PostgreSQL Source Code git master
Loading...
Searching...
No Matches
psqlscan.h File Reference
#include "pqexpbuffer.h"
Include dependency graph for psqlscan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PsqlScanCallbacks
 

Typedefs

typedef struct PsqlScanStateDataPsqlScanState
 
typedef enum _promptStatus promptStatus_t
 
typedef struct PsqlScanCallbacks PsqlScanCallbacks
 

Enumerations

enum  PsqlScanResult { PSCAN_SEMICOLON , PSCAN_BACKSLASH , PSCAN_INCOMPLETE , PSCAN_EOL }
 
enum  _promptStatus {
  PROMPT_READY , PROMPT_CONTINUE , PROMPT_COMMENT , PROMPT_SINGLEQUOTE ,
  PROMPT_DOUBLEQUOTE , PROMPT_DOLLARQUOTE , PROMPT_PAREN , PROMPT_COPY
}
 
enum  PsqlScanQuoteType { PQUOTE_PLAIN , PQUOTE_SQL_LITERAL , PQUOTE_SQL_IDENT , PQUOTE_SHELL_ARG }
 

Functions

PsqlScanState psql_scan_create (const PsqlScanCallbacks *callbacks)
 
void psql_scan_destroy (PsqlScanState state)
 
void psql_scan_set_passthrough (PsqlScanState state, void *passthrough)
 
void psql_scan_setup (PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
 
void psql_scan_finish (PsqlScanState state)
 
PsqlScanResult psql_scan (PsqlScanState state, PQExpBuffer query_buf, promptStatus_t *prompt)
 
void psql_scan_reset (PsqlScanState state)
 
void psql_scan_reselect_sql_lexer (PsqlScanState state)
 
bool psql_scan_in_quote (PsqlScanState state)
 
void psql_scan_get_location (PsqlScanState state, int *lineno, int *offset)
 

Typedef Documentation

◆ promptStatus_t

◆ PsqlScanCallbacks

◆ PsqlScanState

Definition at line 27 of file psqlscan.h.

Enumeration Type Documentation

◆ _promptStatus

Enumerator
PROMPT_READY 
PROMPT_CONTINUE 
PROMPT_COMMENT 
PROMPT_SINGLEQUOTE 
PROMPT_DOUBLEQUOTE 
PROMPT_DOLLARQUOTE 
PROMPT_PAREN 
PROMPT_COPY 

Definition at line 39 of file psqlscan.h.

40{
enum _promptStatus promptStatus_t
@ PROMPT_READY
Definition psqlscan.h:41
@ PROMPT_COPY
Definition psqlscan.h:48
@ PROMPT_PAREN
Definition psqlscan.h:47
@ PROMPT_COMMENT
Definition psqlscan.h:43
@ PROMPT_CONTINUE
Definition psqlscan.h:42
@ PROMPT_SINGLEQUOTE
Definition psqlscan.h:44
@ PROMPT_DOLLARQUOTE
Definition psqlscan.h:46
@ PROMPT_DOUBLEQUOTE
Definition psqlscan.h:45

◆ PsqlScanQuoteType

Enumerator
PQUOTE_PLAIN 
PQUOTE_SQL_LITERAL 
PQUOTE_SQL_IDENT 
PQUOTE_SHELL_ARG 

Definition at line 52 of file psqlscan.h.

53{
54 PQUOTE_PLAIN, /* just return the actual value */
55 PQUOTE_SQL_LITERAL, /* add quotes to make a valid SQL literal */
56 PQUOTE_SQL_IDENT, /* quote if needed to make a SQL identifier */
57 PQUOTE_SHELL_ARG, /* quote if needed to be safe in a shell cmd */
PsqlScanQuoteType
Definition psqlscan.h:53
@ PQUOTE_SQL_LITERAL
Definition psqlscan.h:55
@ PQUOTE_PLAIN
Definition psqlscan.h:54
@ PQUOTE_SHELL_ARG
Definition psqlscan.h:57
@ PQUOTE_SQL_IDENT
Definition psqlscan.h:56

◆ PsqlScanResult

Enumerator
PSCAN_SEMICOLON 
PSCAN_BACKSLASH 
PSCAN_INCOMPLETE 
PSCAN_EOL 

Definition at line 30 of file psqlscan.h.

31{
32 PSCAN_SEMICOLON, /* found command-ending semicolon */
33 PSCAN_BACKSLASH, /* found backslash command */
34 PSCAN_INCOMPLETE, /* end of line, SQL statement incomplete */
35 PSCAN_EOL, /* end of line, SQL possibly complete */
PsqlScanResult
Definition psqlscan.h:31
@ PSCAN_BACKSLASH
Definition psqlscan.h:33
@ PSCAN_EOL
Definition psqlscan.h:35
@ PSCAN_INCOMPLETE
Definition psqlscan.h:34
@ PSCAN_SEMICOLON
Definition psqlscan.h:32

Function Documentation

◆ psql_scan()

PsqlScanResult psql_scan ( PsqlScanState  state,
PQExpBuffer  query_buf,
promptStatus_t prompt 
)
extern

Definition at line 1127 of file psqlscan.l.

1130{
1131 PsqlScanResult result;
1132 int lexresult;
1133
1134 /* Must be scanning already */
1135 Assert(state->scanbufhandle != NULL);
1136
1137 /* Set current output target */
1138 state->output_buf = query_buf;
1139
1140 /* Set input source */
1141 if (state->buffer_stack != NULL)
1142 yy_switch_to_buffer(state->buffer_stack->buf, state->scanner);
1143 else
1144 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1145
1146 /* And lex. */
1147 lexresult = yylex(NULL, state->scanner);
1148
1149 /* Notify psql_scan_get_location() that a yylex call has been made. */
1150 if (state->cur_line_no == 0)
1151 state->cur_line_no = 1;
1152
1153 /*
1154 * Check termination state and return appropriate result info.
1155 */
1156 switch (lexresult)
1157 {
1158 case LEXRES_EOL: /* end of input */
1159 switch (state->start_state)
1160 {
1161 case INITIAL:
1162 case xqs: /* we treat this like INITIAL */
1163 if (state->paren_depth > 0)
1164 {
1165 result = PSCAN_INCOMPLETE;
1167 }
1168 else if (state->begin_depth > 0)
1169 {
1170 result = PSCAN_INCOMPLETE;
1172 }
1173 else if (query_buf->len > 0)
1174 {
1175 result = PSCAN_EOL;
1177 }
1178 else
1179 {
1180 /* never bother to send an empty buffer */
1181 result = PSCAN_INCOMPLETE;
1183 }
1184 break;
1185 case xb:
1186 result = PSCAN_INCOMPLETE;
1188 break;
1189 case xc:
1190 result = PSCAN_INCOMPLETE;
1192 break;
1193 case xd:
1194 result = PSCAN_INCOMPLETE;
1196 break;
1197 case xh:
1198 result = PSCAN_INCOMPLETE;
1200 break;
1201 case xe:
1202 result = PSCAN_INCOMPLETE;
1204 break;
1205 case xq:
1206 result = PSCAN_INCOMPLETE;
1208 break;
1209 case xdolq:
1210 result = PSCAN_INCOMPLETE;
1212 break;
1213 case xui:
1214 result = PSCAN_INCOMPLETE;
1216 break;
1217 case xus:
1218 result = PSCAN_INCOMPLETE;
1220 break;
1221 default:
1222 /* can't get here */
1223 fprintf(stderr, "invalid YY_START\n");
1224 exit(1);
1225 }
1226 break;
1227 case LEXRES_SEMI: /* semicolon */
1228 result = PSCAN_SEMICOLON;
1230 break;
1231 case LEXRES_BACKSLASH: /* backslash */
1232 result = PSCAN_BACKSLASH;
1234 break;
1235 default:
1236 /* can't get here */
1237 fprintf(stderr, "invalid yylex result\n");
1238 exit(1);
1239 }
1240
1241 return result;
1242}
#define Assert(condition)
Definition c.h:945
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
int yylex(void)
Definition pgc.l:465
static int fb(int x)
#define LEXRES_BACKSLASH
Definition psqlscan.l:59
#define LEXRES_SEMI
Definition psqlscan.l:58
#define LEXRES_EOL
Definition psqlscan.l:57

References Assert, fb(), fprintf, LEXRES_BACKSLASH, LEXRES_EOL, LEXRES_SEMI, PROMPT_COMMENT, PROMPT_CONTINUE, PROMPT_DOLLARQUOTE, PROMPT_DOUBLEQUOTE, PROMPT_PAREN, PROMPT_READY, PROMPT_SINGLEQUOTE, PSCAN_BACKSLASH, PSCAN_EOL, PSCAN_INCOMPLETE, PSCAN_SEMICOLON, and yylex().

Referenced by MainLoop(), ParseScript(), and test_psql_parse().

◆ psql_scan_create()

PsqlScanState psql_scan_create ( const PsqlScanCallbacks callbacks)
extern

Definition at line 1007 of file psqlscan.l.

1008{
1010
1012
1013 state->callbacks = callbacks;
1014
1015 yylex_init(&state->scanner);
1016
1017 yyset_extra(state, state->scanner);
1018
1020
1021 return state;
1022}
#define pg_malloc0_object(type)
Definition fe_memutils.h:51
void psql_scan_reset(PsqlScanState state)
Definition psqlscan.l:1281

References fb(), pg_malloc0_object, and psql_scan_reset().

Referenced by main(), MainLoop(), ParseScript(), and test_psql_parse().

◆ psql_scan_destroy()

void psql_scan_destroy ( PsqlScanState  state)
extern

Definition at line 1028 of file psqlscan.l.

1029{
1031
1033
1034 yylex_destroy(state->scanner);
1035
1036 free(state);
1037}
void psql_scan_finish(PsqlScanState state)
Definition psqlscan.l:1254
#define free(a)

References fb(), free, psql_scan_finish(), and psql_scan_reset().

Referenced by main(), MainLoop(), ParseScript(), and test_psql_parse().

◆ psql_scan_finish()

void psql_scan_finish ( PsqlScanState  state)
extern

Definition at line 1254 of file psqlscan.l.

1255{
1256 /* Drop any incomplete variable expansions. */
1257 while (state->buffer_stack != NULL)
1259
1260 /* Done with the outer scan buffer, too */
1261 if (state->scanbufhandle)
1262 yy_delete_buffer(state->scanbufhandle, state->scanner);
1263 state->scanbufhandle = NULL;
1264 if (state->scanbuf)
1265 free(state->scanbuf);
1266 state->scanbuf = NULL;
1267}
void psqlscan_pop_buffer_stack(PsqlScanState state)
Definition psqlscan.l:1419

References fb(), free, and psqlscan_pop_buffer_stack().

Referenced by MainLoop(), ParseScript(), and psql_scan_destroy().

◆ psql_scan_get_location()

void psql_scan_get_location ( PsqlScanState  state,
int lineno,
int offset 
)
extern

Definition at line 1341 of file psqlscan.l.

1343{
1344 const char *line_end;
1345
1346 /*
1347 * We rely on flex's having stored a NUL after the current token in
1348 * scanbuf. Therefore we must specially handle the state before yylex()
1349 * has been called, when obviously that won't have happened yet.
1350 */
1351 if (state->cur_line_no == 0)
1352 {
1353 *lineno = 1;
1354 *offset = 0;
1355 return;
1356 }
1357
1358 /*
1359 * Advance cur_line_no/cur_line_ptr past whatever has been lexed so far.
1360 * Doing this prevents repeated calls from being O(N^2) for long inputs.
1361 */
1362 while ((line_end = strchr(state->cur_line_ptr, '\n')) != NULL)
1363 {
1364 state->cur_line_no++;
1365 state->cur_line_ptr = line_end + 1;
1366 }
1367 state->cur_line_ptr += strlen(state->cur_line_ptr);
1368
1369 /* Report current location. */
1370 *lineno = state->cur_line_no;
1371 *offset = state->cur_line_ptr - state->scanbuf;
1372}

References fb().

Referenced by expr_lex_one_word(), expr_yyerror_more(), and ParseScript().

◆ psql_scan_in_quote()

bool psql_scan_in_quote ( PsqlScanState  state)
extern

Definition at line 1322 of file psqlscan.l.

1323{
1324 return state->start_state != INITIAL &&
1325 state->start_state != xqs;
1326}

References fb().

Referenced by MainLoop().

◆ psql_scan_reselect_sql_lexer()

void psql_scan_reselect_sql_lexer ( PsqlScanState  state)
extern

Definition at line 1309 of file psqlscan.l.

1310{
1311 state->start_state = INITIAL;
1312}

References fb().

Referenced by expr_lex_one_word(), expr_scanner_finish(), psql_scan_slash_command(), psql_scan_slash_command_end(), and psql_scan_slash_option().

◆ psql_scan_reset()

void psql_scan_reset ( PsqlScanState  state)
extern

Definition at line 1281 of file psqlscan.l.

1282{
1283 state->start_state = INITIAL;
1284 state->paren_depth = 0;
1285 state->xcdepth = 0; /* not really necessary */
1286 if (state->dolqstart)
1287 free(state->dolqstart);
1288 state->dolqstart = NULL;
1289 state->identifier_count = 0;
1290 state->begin_depth = 0;
1291}

References fb(), and free.

Referenced by exec_command_reset(), exec_command_watch(), MainLoop(), psql_scan_create(), and psql_scan_destroy().

◆ psql_scan_set_passthrough()

void psql_scan_set_passthrough ( PsqlScanState  state,
void passthrough 
)
extern

Definition at line 1047 of file psqlscan.l.

1048{
1049 state->cb_passthrough = passthrough;
1050}

References fb().

Referenced by main(), and MainLoop().

◆ psql_scan_setup()

void psql_scan_setup ( PsqlScanState  state,
const char line,
int  line_len,
int  encoding,
bool  std_strings 
)
extern

Definition at line 1065 of file psqlscan.l.

1068{
1069 /* Mustn't be scanning already */
1070 Assert(state->scanbufhandle == NULL);
1071 Assert(state->buffer_stack == NULL);
1072
1073 /* Do we need to hack the character set encoding? */
1074 state->encoding = encoding;
1075 state->safe_encoding = pg_valid_server_encoding_id(encoding);
1076
1077 /* Save standard-strings flag as well */
1078 state->std_strings = std_strings;
1079
1080 /* Set up flex input buffer with appropriate translation and padding */
1081 state->scanbufhandle = psqlscan_prepare_buffer(state, line, line_len,
1082 &state->scanbuf);
1083 state->scanline = line;
1084
1085 /* Set lookaside data in case we have to map unsafe encoding */
1086 state->curline = state->scanbuf;
1087 state->refline = state->scanline;
1088
1089 /* Initialize state for psql_scan_get_location() */
1090 state->cur_line_no = 0; /* yylex not called yet */
1091 state->cur_line_ptr = state->scanbuf;
1092}
static char * encoding
Definition initdb.c:139
#define pg_valid_server_encoding_id
Definition pg_wchar.h:632
YY_BUFFER_STATE psqlscan_prepare_buffer(PsqlScanState state, const char *txt, int len, char **txtcopy)
Definition psqlscan.l:1482

References Assert, encoding, fb(), pg_valid_server_encoding_id, and psqlscan_prepare_buffer().

Referenced by main(), MainLoop(), ParseScript(), and test_psql_parse().