PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
)

Definition at line 1121 of file psqlscan.l.

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

References Assert(), fprintf, PQExpBufferData::len, 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)

Definition at line 1001 of file psqlscan.l.

1002{
1004
1006
1007 state->callbacks = callbacks;
1008
1009 yylex_init(&state->scanner);
1010
1011 yyset_extra(state, state->scanner);
1012
1014
1015 return state;
1016}
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void psql_scan_reset(PsqlScanState state)
Definition: psqlscan.l:1275

References pg_malloc0(), and psql_scan_reset().

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

◆ psql_scan_destroy()

void psql_scan_destroy ( PsqlScanState  state)

Definition at line 1022 of file psqlscan.l.

1023{
1025
1027
1028 yylex_destroy(state->scanner);
1029
1030 free(state);
1031}
#define free(a)
Definition: header.h:65
void psql_scan_finish(PsqlScanState state)
Definition: psqlscan.l:1248

References 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)

Definition at line 1248 of file psqlscan.l.

1249{
1250 /* Drop any incomplete variable expansions. */
1251 while (state->buffer_stack != NULL)
1253
1254 /* Done with the outer scan buffer, too */
1255 if (state->scanbufhandle)
1256 yy_delete_buffer(state->scanbufhandle, state->scanner);
1257 state->scanbufhandle = NULL;
1258 if (state->scanbuf)
1259 free(state->scanbuf);
1260 state->scanbuf = NULL;
1261}
void psqlscan_pop_buffer_stack(PsqlScanState state)
Definition: psqlscan.l:1413

References 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 
)

Definition at line 1335 of file psqlscan.l.

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

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

◆ psql_scan_in_quote()

bool psql_scan_in_quote ( PsqlScanState  state)

Definition at line 1316 of file psqlscan.l.

1317{
1318 return state->start_state != INITIAL &&
1319 state->start_state != xqs;
1320}

Referenced by MainLoop().

◆ psql_scan_reselect_sql_lexer()

void psql_scan_reselect_sql_lexer ( PsqlScanState  state)

Definition at line 1303 of file psqlscan.l.

1304{
1305 state->start_state = INITIAL;
1306}

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)

Definition at line 1275 of file psqlscan.l.

1276{
1277 state->start_state = INITIAL;
1278 state->paren_depth = 0;
1279 state->xcdepth = 0; /* not really necessary */
1280 if (state->dolqstart)
1281 free(state->dolqstart);
1282 state->dolqstart = NULL;
1283 state->identifier_count = 0;
1284 state->begin_depth = 0;
1285}

References 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 
)

Definition at line 1041 of file psqlscan.l.

1042{
1043 state->cb_passthrough = passthrough;
1044}

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 
)

Definition at line 1059 of file psqlscan.l.

1062{
1063 /* Mustn't be scanning already */
1064 Assert(state->scanbufhandle == NULL);
1065 Assert(state->buffer_stack == NULL);
1066
1067 /* Do we need to hack the character set encoding? */
1068 state->encoding = encoding;
1069 state->safe_encoding = pg_valid_server_encoding_id(encoding);
1070
1071 /* Save standard-strings flag as well */
1072 state->std_strings = std_strings;
1073
1074 /* Set up flex input buffer with appropriate translation and padding */
1075 state->scanbufhandle = psqlscan_prepare_buffer(state, line, line_len,
1076 &state->scanbuf);
1077 state->scanline = line;
1078
1079 /* Set lookaside data in case we have to map unsafe encoding */
1080 state->curline = state->scanbuf;
1081 state->refline = state->scanline;
1082
1083 /* Initialize state for psql_scan_get_location() */
1084 state->cur_line_no = 0; /* yylex not called yet */
1085 state->cur_line_ptr = state->scanbuf;
1086}
int32 encoding
Definition: pg_database.h:41
#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:1476

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

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