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

Commit4e1d2a1

Browse files
committed
Decouple psqlscan.l from surrounding program.
Remove assorted external references from psqlscan.l in preparation formaking it usable by other frontend programs. This mostly involvesgetting rid of direct calls to psql_error() and GetVariable() in favorof introducing a callback-functions struct to encapsulate variablefetching and error printing. In addition, pass the current encodingand standard-strings status as additional parameters to psql_scan_setupinstead of looking directly at "pset" or calling additional functions.I did not bother to change some references to psql_error that are infunctions that will soon migrate to a psql-specific backslash-commandlexer. Other than that, this version of psqlscan.l is capable ofcompiling standalone. It still depends on assorted src/common functionsas well as some encoding-related libpq functions, but we expect thatall programs using it will be happy with those dependencies.Kyotaro Horiguchi, somewhat editorialized on by me
1 parent08a6d36 commit4e1d2a1

File tree

7 files changed

+154
-64
lines changed

7 files changed

+154
-64
lines changed

‎src/bin/psql/common.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,65 @@ setQFout(const char *fname)
107107
}
108108

109109

110+
/*
111+
* Variable-fetching callback for flex lexer
112+
*
113+
* If the specified variable exists, return its value as a string (malloc'd
114+
* and expected to be freed by the caller); else return NULL.
115+
*
116+
* If "escape" is true, return the value suitably quoted and escaped,
117+
* as an identifier or string literal depending on "as_ident".
118+
* (Failure in escaping should lead to returning NULL.)
119+
*/
120+
char*
121+
psql_get_variable(constchar*varname,boolescape,boolas_ident)
122+
{
123+
char*result;
124+
constchar*value;
125+
126+
value=GetVariable(pset.vars,varname);
127+
if (!value)
128+
returnNULL;
129+
130+
if (escape)
131+
{
132+
char*escaped_value;
133+
134+
if (!pset.db)
135+
{
136+
psql_error("can't escape without active connection\n");
137+
returnNULL;
138+
}
139+
140+
if (as_ident)
141+
escaped_value=
142+
PQescapeIdentifier(pset.db,value,strlen(value));
143+
else
144+
escaped_value=
145+
PQescapeLiteral(pset.db,value,strlen(value));
146+
147+
if (escaped_value==NULL)
148+
{
149+
constchar*error=PQerrorMessage(pset.db);
150+
151+
psql_error("%s",error);
152+
returnNULL;
153+
}
154+
155+
/*
156+
* Rather than complicate the lexer's API with a notion of which
157+
* free() routine to use, just pay the price of an extra strdup().
158+
*/
159+
result=pg_strdup(escaped_value);
160+
PQfreemem(escaped_value);
161+
}
162+
else
163+
result=pg_strdup(value);
164+
165+
returnresult;
166+
}
167+
168+
110169
/*
111170
* Error reporting for scripts. Errors should look like
112171
* psql:filename:lineno: message

‎src/bin/psql/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
externboolopenQueryOutputFile(constchar*fname,FILE**fout,bool*is_pipe);
1919
externboolsetQFout(constchar*fname);
2020

21+
externchar*psql_get_variable(constchar*varname,boolescape,boolas_ident);
22+
2123
externvoidpsql_error(constchar*fmt,...)pg_attribute_printf(1,2);
2224

2325
externvoidNoticeProcessor(void*arg,constchar*message);

‎src/bin/psql/mainloop.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include"postgres_fe.h"
99
#include"mainloop.h"
1010

11-
1211
#include"command.h"
1312
#include"common.h"
1413
#include"input.h"
@@ -17,6 +16,13 @@
1716
#include"mb/pg_wchar.h"
1817

1918

19+
/* callback functions for our flex lexer */
20+
constPsqlScanCallbackspsqlscan_callbacks= {
21+
psql_get_variable,
22+
psql_error
23+
};
24+
25+
2026
/*
2127
* Main processing loop for reading lines of input
2228
*and sending them to the backend.
@@ -61,7 +67,7 @@ MainLoop(FILE *source)
6167
pset.stmt_lineno=1;
6268

6369
/* Create working state */
64-
scan_state=psql_scan_create();
70+
scan_state=psql_scan_create(&psqlscan_callbacks);
6571

6672
query_buf=createPQExpBuffer();
6773
previous_buf=createPQExpBuffer();
@@ -233,7 +239,8 @@ MainLoop(FILE *source)
233239
/*
234240
* Parse line, looking for command separators.
235241
*/
236-
psql_scan_setup(scan_state,line,strlen(line));
242+
psql_scan_setup(scan_state,line,strlen(line),
243+
pset.encoding,standard_strings());
237244
success= true;
238245
line_saved_in_history= false;
239246

@@ -373,7 +380,8 @@ MainLoop(FILE *source)
373380
resetPQExpBuffer(query_buf);
374381
/* reset parsing state since we are rescanning whole line */
375382
psql_scan_reset(scan_state);
376-
psql_scan_setup(scan_state,line,strlen(line));
383+
psql_scan_setup(scan_state,line,strlen(line),
384+
pset.encoding,standard_strings());
377385
line_saved_in_history= false;
378386
prompt_status=PROMPT_READY;
379387
}

‎src/bin/psql/mainloop.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#ifndefMAINLOOP_H
99
#defineMAINLOOP_H
1010

11+
#include"psqlscan.h"
12+
13+
externconstPsqlScanCallbackspsqlscan_callbacks;
14+
1115
externintMainLoop(FILE*source);
1216

1317
#endif/* MAINLOOP_H */

‎src/bin/psql/psqlscan.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ enum slash_option_type
3636
OT_NO_EVAL/* no expansion of backticks or variables */
3737
};
3838

39+
/* Callback functions to be used by the lexer */
40+
typedefstructPsqlScanCallbacks
41+
{
42+
/* Fetch value of a variable, as a pfree'able string; NULL if unknown */
43+
/* This pointer can be NULL if no variable substitution is wanted */
44+
char*(*get_variable) (constchar*varname,boolescape,boolas_ident);
45+
/* Print an error message someplace appropriate */
46+
void(*write_error) (constchar*fmt,...)pg_attribute_printf(1,2);
47+
}PsqlScanCallbacks;
48+
3949

40-
externPsqlScanStatepsql_scan_create(void);
50+
externPsqlScanStatepsql_scan_create(constPsqlScanCallbacks*callbacks);
4151
externvoidpsql_scan_destroy(PsqlScanStatestate);
4252

4353
externvoidpsql_scan_setup(PsqlScanStatestate,
44-
constchar*line,intline_len);
54+
constchar*line,intline_len,
55+
intencoding,boolstd_strings);
4556
externvoidpsql_scan_finish(PsqlScanStatestate);
4657

4758
externPsqlScanResultpsql_scan(PsqlScanStatestate,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp