Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 895e36b

Browse files
committed
Add a "void *" passthrough pointer for psqlscan.l's callback functions.
The immediate motivation for this is to provide clean infrastructure for the proposed \if...\endif patch for psql; but it seems like a good thing to have even if that patch doesn't get in. Previously the callback functions could only make use of application-global state, which is a pretty severe handicap. For the moment, the pointer is only passed through to the get_variable callback function. I considered also passing it to the write_error callback, but for now let's not. Neither psql nor pgbench has a use for that, and in the case of psql we'd have to invent a separate wrapper function because we would certainly not want to change the signature of psql_error(). Discussion: https://postgr.es/m/10108.1489418309@sss.pgh.pa.us
1 parent 1c7a66a commit 895e36b

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

src/bin/psql/common.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,13 @@ setQFout(const char *fname)
119119
* If "escape" is true, return the value suitably quoted and escaped,
120120
* as an identifier or string literal depending on "as_ident".
121121
* (Failure in escaping should lead to returning NULL.)
122+
*
123+
* "passthrough" is the pointer previously given to psql_scan_set_passthrough.
124+
* psql currently doesn't use this.
122125
*/
123126
char *
124-
psql_get_variable(const char *varname, bool escape, bool as_ident)
127+
psql_get_variable(const char *varname, bool escape, bool as_ident,
128+
void *passthrough)
125129
{
126130
char *result;
127131
const char *value;

src/bin/psql/common.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
extern bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe);
1717
extern bool setQFout(const char *fname);
1818

19-
extern char *psql_get_variable(const char *varname, bool escape, bool as_ident);
19+
extern char *psql_get_variable(const char *varname, bool escape, bool as_ident,
20+
void *passthrough);
2021

2122
extern void psql_error(const char *fmt,...) pg_attribute_printf(1, 2);
2223

src/bin/psql/psqlscanslash.l

+2-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ other .
243243
yyleng - 1);
244244
value = cur_state->callbacks->get_variable(varname,
245245
false,
246-
false);
246+
false,
247+
cur_state->cb_passthrough);
247248
free(varname);
248249

249250
/*

src/fe_utils/psqlscan.l

+17-2
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,8 @@ other .
700700
if (cur_state->callbacks->get_variable)
701701
value = cur_state->callbacks->get_variable(varname,
702702
false,
703-
false);
703+
false,
704+
cur_state->cb_passthrough);
704705
else
705706
value = NULL;
706707

@@ -922,6 +923,19 @@ psql_scan_destroy(PsqlScanState state)
922923
free(state);
923924
}
924925

926+
/*
927+
* Set the callback passthrough pointer for the lexer.
928+
*
929+
* This could have been integrated into psql_scan_create, but keeping it
930+
* separate allows the application to change the pointer later, which might
931+
* be useful.
932+
*/
933+
void
934+
psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
935+
{
936+
state->cb_passthrough = passthrough;
937+
}
938+
925939
/*
926940
* Set up to perform lexing of the given input line.
927941
*
@@ -1409,7 +1423,8 @@ psqlscan_escape_variable(PsqlScanState state, const char *txt, int len,
14091423
/* Variable lookup. */
14101424
varname = psqlscan_extract_substring(state, txt + 2, len - 3);
14111425
if (state->callbacks->get_variable)
1412-
value = state->callbacks->get_variable(varname, true, as_ident);
1426+
value = state->callbacks->get_variable(varname, true, as_ident,
1427+
state->cb_passthrough);
14131428
else
14141429
value = NULL;
14151430
free(varname);

src/include/fe_utils/psqlscan.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ typedef struct PsqlScanCallbacks
5353
{
5454
/* Fetch value of a variable, as a pfree'able string; NULL if unknown */
5555
/* This pointer can be NULL if no variable substitution is wanted */
56-
char *(*get_variable) (const char *varname, bool escape, bool as_ident);
56+
char *(*get_variable) (const char *varname, bool escape,
57+
bool as_ident, void *passthrough);
5758
/* Print an error message someplace appropriate */
5859
/* (very old gcc versions don't support attributes on function pointers) */
5960
#if defined(__GNUC__) && __GNUC__ < 4
@@ -67,6 +68,8 @@ typedef struct PsqlScanCallbacks
6768
extern PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks);
6869
extern void psql_scan_destroy(PsqlScanState state);
6970

71+
extern void psql_scan_set_passthrough(PsqlScanState state, void *passthrough);
72+
7073
extern void psql_scan_setup(PsqlScanState state,
7174
const char *line, int line_len,
7275
int encoding, bool std_strings);

src/include/fe_utils/psqlscan_int.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ typedef struct PsqlScanStateData
115115
char *dolqstart; /* current $foo$ quote start string */
116116

117117
/*
118-
* Callback functions provided by the program making use of the lexer.
118+
* Callback functions provided by the program making use of the lexer,
119+
* plus a void* callback passthrough argument.
119120
*/
120121
const PsqlScanCallbacks *callbacks;
122+
void *cb_passthrough;
121123
} PsqlScanStateData;
122124

123125

0 commit comments

Comments
 (0)