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 1133 of file psqlscan.l.

1136{
1138 int lexresult;
1139
1140 /* Must be scanning already */
1141 Assert(state->scanbufhandle != NULL);
1142
1143 /* Set current output target */
1144 state->output_buf = query_buf;
1145
1146 /* Set input source */
1147 if (state->buffer_stack != NULL)
1148 yy_switch_to_buffer(state->buffer_stack->buf, state->scanner);
1149 else
1150 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1151
1152 /* And lex. */
1153 lexresult = yylex(NULL, state->scanner);
1154
1155 /* Notify psql_scan_get_location() that a yylex call has been made. */
1156 if (state->cur_line_no == 0)
1157 state->cur_line_no = 1;
1158
1159 /*
1160 * Check termination state and return appropriate result info.
1161 */
1162 switch (lexresult)
1163 {
1164 case LEXRES_EOL: /* end of input */
1165 switch (state->start_state)
1166 {
1167 case INITIAL:
1168 case xqs: /* we treat this like INITIAL */
1169 if (state->paren_depth > 0)
1170 {
1173 }
1174 else if (state->begin_depth > 0)
1175 {
1178 }
1179 else if (query_buf->len > 0)
1180 {
1181 result = PSCAN_EOL;
1183 }
1184 else
1185 {
1186 /* never bother to send an empty buffer */
1189 }
1190 break;
1191 case xb:
1194 break;
1195 case xc:
1198 break;
1199 case xd:
1202 break;
1203 case xh:
1206 break;
1207 case xe:
1210 break;
1211 case xq:
1214 break;
1215 case xdolq:
1218 break;
1219 case xui:
1222 break;
1223 case xus:
1226 break;
1227 default:
1228 /* can't get here */
1229 fprintf(stderr, "invalid YY_START\n");
1230 exit(1);
1231 }
1232 break;
1233 case LEXRES_SEMI: /* semicolon */
1236 break;
1237 case LEXRES_BACKSLASH: /* backslash */
1240 break;
1241 default:
1242 /* can't get here */
1243 fprintf(stderr, "invalid yylex result\n");
1244 exit(1);
1245 }
1246
1247 return result;
1248}
#define Assert(condition)
Definition c.h:943
uint32 result
#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, result, and yylex().

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

◆ psql_scan_create()

PsqlScanState psql_scan_create ( const PsqlScanCallbacks callbacks)
extern

Definition at line 1013 of file psqlscan.l.

1014{
1016
1018
1019 state->callbacks = callbacks;
1020
1021 yylex_init(&state->scanner);
1022
1023 yyset_extra(state, state->scanner);
1024
1026
1027 return state;
1028}
#define pg_malloc0_object(type)
Definition fe_memutils.h:51
void psql_scan_reset(PsqlScanState state)
Definition psqlscan.l:1287

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 1034 of file psqlscan.l.

1035{
1037
1039
1040 yylex_destroy(state->scanner);
1041
1042 free(state);
1043}
void psql_scan_finish(PsqlScanState state)
Definition psqlscan.l:1260
#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 1260 of file psqlscan.l.

1261{
1262 /* Drop any incomplete variable expansions. */
1263 while (state->buffer_stack != NULL)
1265
1266 /* Done with the outer scan buffer, too */
1267 if (state->scanbufhandle)
1268 yy_delete_buffer(state->scanbufhandle, state->scanner);
1269 state->scanbufhandle = NULL;
1270 if (state->scanbuf)
1271 free(state->scanbuf);
1272 state->scanbuf = NULL;
1273}
void psqlscan_pop_buffer_stack(PsqlScanState state)
Definition psqlscan.l:1425

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 1347 of file psqlscan.l.

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

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 1328 of file psqlscan.l.

1329{
1330 return state->start_state != INITIAL &&
1331 state->start_state != xqs;
1332}

References fb().

Referenced by MainLoop().

◆ psql_scan_reselect_sql_lexer()

void psql_scan_reselect_sql_lexer ( PsqlScanState  state)
extern

Definition at line 1315 of file psqlscan.l.

1316{
1317 state->start_state = INITIAL;
1318}

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 1287 of file psqlscan.l.

1288{
1289 state->start_state = INITIAL;
1290 state->paren_depth = 0;
1291 state->xcdepth = 0; /* not really necessary */
1292 if (state->dolqstart)
1293 free(state->dolqstart);
1294 state->dolqstart = NULL;
1295 state->identifier_count = 0;
1296 state->begin_depth = 0;
1297}

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 1053 of file psqlscan.l.

1054{
1055 state->cb_passthrough = passthrough;
1056}

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 1071 of file psqlscan.l.

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

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

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