Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit895e36b

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

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

‎src/bin/psql/common.c

Lines changed: 5 additions & 1 deletion
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(constchar*varname,boolescape,boolas_ident)
127+
psql_get_variable(constchar*varname,boolescape,boolas_ident,
128+
void*passthrough)
125129
{
126130
char*result;
127131
constchar*value;

‎src/bin/psql/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
externboolopenQueryOutputFile(constchar*fname,FILE**fout,bool*is_pipe);
1717
externboolsetQFout(constchar*fname);
1818

19-
externchar*psql_get_variable(constchar*varname,boolescape,boolas_ident);
19+
externchar*psql_get_variable(constchar*varname,boolescape,boolas_ident,
20+
void*passthrough);
2021

2122
externvoidpsql_error(constchar*fmt,...)pg_attribute_printf(1,2);
2223

‎src/bin/psql/psqlscanslash.l

Lines changed: 2 additions & 1 deletion
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

Lines changed: 17 additions & 2 deletions
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

Lines changed: 4 additions & 1 deletion
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) (constchar*varname,boolescape,boolas_ident);
56+
char*(*get_variable) (constchar*varname,boolescape,
57+
boolas_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
externPsqlScanStatepsql_scan_create(constPsqlScanCallbacks*callbacks);
6869
externvoidpsql_scan_destroy(PsqlScanStatestate);
6970

71+
externvoidpsql_scan_set_passthrough(PsqlScanStatestate,void*passthrough);
72+
7073
externvoidpsql_scan_setup(PsqlScanStatestate,
7174
constchar*line,intline_len,
7275
intencoding,boolstd_strings);

‎src/include/fe_utils/psqlscan_int.h

Lines changed: 3 additions & 1 deletion
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
constPsqlScanCallbacks*callbacks;
122+
void*cb_passthrough;
121123
}PsqlScanStateData;
122124

123125

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp