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

1218{
1220 int lexresult;
1221
1222 /* Must be scanning already */
1223 Assert(state->scanbufhandle != NULL);
1224
1225 /* Set current output target */
1226 state->output_buf = query_buf;
1227
1228 /* Set input source */
1229 if (state->buffer_stack != NULL)
1230 yy_switch_to_buffer(state->buffer_stack->buf, state->scanner);
1231 else
1232 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1233
1234 /* And lex. */
1235 lexresult = yylex(NULL, state->scanner);
1236
1237 /* Notify psql_scan_get_location() that a yylex call has been made. */
1238 if (state->cur_line_no == 0)
1239 state->cur_line_no = 1;
1240
1241 /*
1242 * Check termination state and return appropriate result info.
1243 */
1244 switch (lexresult)
1245 {
1246 case LEXRES_EOL: /* end of input */
1247 switch (state->start_state)
1248 {
1249 case INITIAL:
1250 case xqs: /* we treat this like INITIAL */
1251 if (state->paren_depth > 0)
1252 {
1255 }
1256 else if (state->begin_depth > 0)
1257 {
1260 }
1261 else if (query_buf->len > 0)
1262 {
1263 result = PSCAN_EOL;
1265 }
1266 else
1267 {
1268 /* never bother to send an empty buffer */
1271 }
1272 break;
1273 case xb:
1276 break;
1277 case xc:
1280 break;
1281 case xd:
1284 break;
1285 case xh:
1288 break;
1289 case xe:
1292 break;
1293 case xq:
1296 break;
1297 case xdolq:
1300 break;
1301 case xui:
1304 break;
1305 case xus:
1308 break;
1309 default:
1310 /* can't get here */
1311 fprintf(stderr, "invalid YY_START\n");
1312 exit(1);
1313 }
1314 break;
1315 case LEXRES_SEMI: /* semicolon */
1318 break;
1319 case LEXRES_BACKSLASH: /* backslash */
1322 break;
1323 default:
1324 /* can't get here */
1325 fprintf(stderr, "invalid yylex result\n");
1326 exit(1);
1327 }
1328
1329 return result;
1330}
#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 1095 of file psqlscan.l.

1096{
1098
1100
1101 state->callbacks = callbacks;
1102
1103 yylex_init(&state->scanner);
1104
1105 yyset_extra(state, state->scanner);
1106
1108
1109 return state;
1110}
#define pg_malloc0_object(type)
Definition fe_memutils.h:61
void psql_scan_reset(PsqlScanState state)
Definition psqlscan.l:1369

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

1117{
1119
1121
1122 yylex_destroy(state->scanner);
1123
1124 free(state);
1125}
void psql_scan_finish(PsqlScanState state)
Definition psqlscan.l:1342
#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 1342 of file psqlscan.l.

1343{
1344 /* Drop any incomplete variable expansions. */
1345 while (state->buffer_stack != NULL)
1347
1348 /* Done with the outer scan buffer, too */
1349 if (state->scanbufhandle)
1350 yy_delete_buffer(state->scanbufhandle, state->scanner);
1351 state->scanbufhandle = NULL;
1352 if (state->scanbuf)
1353 free(state->scanbuf);
1354 state->scanbuf = NULL;
1355}
void psqlscan_pop_buffer_stack(PsqlScanState state)
Definition psqlscan.l:1507

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

1431{
1432 const char *line_end;
1433
1434 /*
1435 * We rely on flex's having stored a NUL after the current token in
1436 * scanbuf. Therefore we must specially handle the state before yylex()
1437 * has been called, when obviously that won't have happened yet.
1438 */
1439 if (state->cur_line_no == 0)
1440 {
1441 *lineno = 1;
1442 *offset = 0;
1443 return;
1444 }
1445
1446 /*
1447 * Advance cur_line_no/cur_line_ptr past whatever has been lexed so far.
1448 * Doing this prevents repeated calls from being O(N^2) for long inputs.
1449 */
1450 while ((line_end = strchr(state->cur_line_ptr, '\n')) != NULL)
1451 {
1452 state->cur_line_no++;
1453 state->cur_line_ptr = line_end + 1;
1454 }
1455 state->cur_line_ptr += strlen(state->cur_line_ptr);
1456
1457 /* Report current location. */
1458 *lineno = state->cur_line_no;
1459 *offset = state->cur_line_ptr - state->scanbuf;
1460}

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

1411{
1412 return state->start_state != INITIAL &&
1413 state->start_state != xqs;
1414}

References fb().

Referenced by MainLoop().

◆ psql_scan_reselect_sql_lexer()

void psql_scan_reselect_sql_lexer ( PsqlScanState  state)
extern

Definition at line 1397 of file psqlscan.l.

1398{
1399 state->start_state = INITIAL;
1400}

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

1370{
1371 state->start_state = INITIAL;
1372 state->paren_depth = 0;
1373 state->xcdepth = 0; /* not really necessary */
1374 if (state->dolqstart)
1375 free(state->dolqstart);
1376 state->dolqstart = NULL;
1377 state->begin_depth = 0;
1378 state->init_idents_count = 0;
1379}

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

1136{
1137 state->cb_passthrough = passthrough;
1138}

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

1156{
1157 /* Mustn't be scanning already */
1158 Assert(state->scanbufhandle == NULL);
1159 Assert(state->buffer_stack == NULL);
1160
1161 /* Do we need to hack the character set encoding? */
1162 state->encoding = encoding;
1163 state->safe_encoding = pg_valid_server_encoding_id(encoding);
1164
1165 /* Save standard-strings flag as well */
1166 state->std_strings = std_strings;
1167
1168 /* Set up flex input buffer with appropriate translation and padding */
1169 state->scanbufhandle = psqlscan_prepare_buffer(state, line, line_len,
1170 &state->scanbuf);
1171 state->scanline = line;
1172
1173 /* Set lookaside data in case we have to map unsafe encoding */
1174 state->curline = state->scanbuf;
1175 state->refline = state->scanline;
1176
1177 /* Initialize state for psql_scan_get_location() */
1178 state->cur_line_no = 0; /* yylex not called yet */
1179 state->cur_line_ptr = state->scanbuf;
1180}
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:1570

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

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