PostgreSQL Source Code: src/bin/psql/common.h File Reference (original) (raw)

#include <setjmp.h>
#include <signal.h>
#include "[fe_utils/print.h](fe%5F%5Futils%5F2print%5F8h%5Fsource.html)"
#include "[fe_utils/psqlscan.h](psqlscan%5F8h%5Fsource.html)"
#include "[libpq-fe.h](libpq-fe%5F8h%5Fsource.html)"

Go to the source code of this file.

Functions
bool openQueryOutputFile (const char *fname, FILE **fout, bool *is_pipe)
bool setQFout (const char *fname)
char * psql_get_variable (const char *varname, PsqlScanQuoteType quote, void *passthrough)
void NoticeProcessor (void *arg, const char *message)
void psql_setup_cancel_handler (void)
void SetShellResultVariables (int wait_result)
PGresult * PSQLexec (const char *query)
int PSQLexecWatch (const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
bool SendQuery (const char *query)
bool is_superuser (void)
bool standard_strings (void)
const char * session_username (void)
void expand_tilde (char **filename)
void clean_extended_state (void)
bool recognized_connection_string (const char *connstr)

clean_extended_state()

void clean_extended_state ( void )

Definition at line 2598 of file common.c.

2599{

2600 int i;

2601

2603 {

2606 break;

2609 break;

2617 break;

2625 break;

2626 }

2627

2630}

@ PSQL_SEND_PIPELINE_SYNC

@ PSQL_SEND_FLUSH_REQUEST

@ PSQL_SEND_START_PIPELINE_MODE

@ PSQL_SEND_EXTENDED_QUERY_PARAMS

@ PSQL_SEND_EXTENDED_PARSE

@ PSQL_SEND_END_PIPELINE_MODE

@ PSQL_SEND_EXTENDED_CLOSE

@ PSQL_SEND_EXTENDED_QUERY_PREPARED

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, free, i, pset, PSQL_SEND_END_PIPELINE_MODE, PSQL_SEND_EXTENDED_CLOSE, PSQL_SEND_EXTENDED_PARSE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, PSQL_SEND_FLUSH, PSQL_SEND_FLUSH_REQUEST, PSQL_SEND_GET_RESULTS, PSQL_SEND_PIPELINE_SYNC, PSQL_SEND_QUERY, PSQL_SEND_START_PIPELINE_MODE, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command_bind(), exec_command_bind_named(), exec_command_close(), exec_command_g(), exec_command_gexec(), exec_command_gset(), exec_command_parse(), exec_command_sendpipeline(), exec_command_watch(), and SendQuery().

expand_tilde()

void expand_tilde ( char ** filename )

Definition at line 2514 of file common.c.

2515{

2517 return;

2518

2519

2520

2521

2522

2523

2524#ifndef WIN32

2525

2526

2528 {

2529 char *fn;

2530 char oldp,

2531 *p;

2532 struct passwd *pw;

2534

2536 *home = '\0';

2537

2538 p = fn + 1;

2539 while (*p != '/' && *p != '\0')

2540 p++;

2541

2542 oldp = *p;

2543 *p = '\0';

2544

2545 if (*(fn + 1) == '\0')

2547 else if ((pw = getpwnam(fn + 1)) != NULL)

2548 strlcpy(home, pw->pw_dir, sizeof(home));

2549

2550 *p = oldp;

2551 if (strlen(home) != 0)

2552 {

2553 char *newfn;

2554

2555 newfn = psprintf("%s%s", home, p);

2558 }

2559 }

2560#endif

2561}

bool get_home_path(char *ret_path)

size_t strlcpy(char *dst, const char *src, size_t siz)

char * psprintf(const char *fmt,...)

static void * fn(void *arg)

References filename, fn(), free, get_home_path(), MAXPGPATH, psprintf(), and strlcpy().

Referenced by exec_command_edit(), exec_command_g(), exec_command_include(), exec_command_lo(), exec_command_out(), exec_command_s(), exec_command_write(), initializeInput(), parse_slash_copy(), and process_psqlrc().

is_superuser()

bool is_superuser ( void )

Definition at line 2453 of file common.c.

2454{

2455 const char *val;

2456

2458 return false;

2459

2461

2462 if (val && strcmp(val, "on") == 0)

2463 return true;

2464

2465 return false;

2466}

const char * PQparameterStatus(const PGconn *conn, const char *paramName)

References _psqlSettings::db, PQparameterStatus(), pset, and val.

NoticeProcessor()

void NoticeProcessor ( void * arg,
const char * message
)

openQueryOutputFile()

bool openQueryOutputFile ( const char * fname,
FILE ** fout,
bool * is_pipe
)

Definition at line 56 of file common.c.

57{

58 if (!fname || fname[0] == '\0')

59 {

61 *is_pipe = false;

62 }

63 else if (*fname == '|')

64 {

65 fflush(NULL);

66 *fout = popen(fname + 1, "w");

67 *is_pipe = true;

68 }

69 else

70 {

71 *fout = fopen(fname, "w");

72 *is_pipe = false;

73 }

74

75 if (*fout == NULL)

76 {

78 return false;

79 }

80

81 return true;

82}

#define pg_log_error(...)

References pg_log_error, and generate_unaccent_rules::stdout.

Referenced by setQFout(), and SetupGOutput().

psql_get_variable()

char * psql_get_variable ( const char * varname,
PsqlScanQuoteType quote,
void * passthrough
)

Definition at line 188 of file common.c.

190{

191 char *result = NULL;

192 const char *value;

193

194

196 return NULL;

197

200 return NULL;

201

202 switch (quote)

203 {

206 break;

209 {

210

211

212

213

214 char *escaped_value;

215

217 {

218 pg_log_error("cannot escape without active connection");

219 return NULL;

220 }

221

223 escaped_value =

225 else

226 escaped_value =

228

229 if (escaped_value == NULL)

230 {

232

234 return NULL;

235 }

236

237

238

239

240

241

242 result = pg_strdup(escaped_value);

244 break;

245 }

247 {

248

249

250

251

252

253

255

258 {

259 pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",

262 return NULL;

263 }

264 result = buf.data;

265 break;

266 }

267

268

269 }

270

271 return result;

272}

bool conditional_active(ConditionalStack cstack)

char * PQerrorMessage(const PGconn *conn)

void PQfreemem(void *ptr)

char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)

char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)

char * pg_strdup(const char *in)

void initPQExpBuffer(PQExpBuffer str)

bool appendShellStringNoError(PQExpBuffer buf, const char *str)

const char * GetVariable(VariableSpace space, const char *name)

References appendShellStringNoError(), buf, conditional_active(), _psqlSettings::db, error(), free, GetVariable(), initPQExpBuffer(), pg_log_error, pg_log_info, pg_strdup(), PQerrorMessage(), PQescapeIdentifier(), PQescapeLiteral(), PQfreemem(), PQUOTE_PLAIN, PQUOTE_SHELL_ARG, PQUOTE_SQL_IDENT, PQUOTE_SQL_LITERAL, pset, value, and _psqlSettings::vars.

psql_setup_cancel_handler()

void psql_setup_cancel_handler ( void )

PSQLexec()

PGresult * PSQLexec ( const char * query )

Definition at line 655 of file common.c.

656{

658

660 {

661 pg_log_error("You are currently not connected to a database.");

662 return NULL;

663 }

664

666 {

667 printf(_("/******** QUERY *********/\n"

668 "%s\n"

669 "/************************/\n\n"), query);

672 {

674 _("/******** QUERY *********/\n"

675 "%s\n"

676 "/************************/\n\n"), query);

678 }

679

681 return NULL;

682 }

683

685

687

689

691 {

693 res = NULL;

694 }

695

696 return res;

697}

static void ClearOrSaveResult(PGresult *result)

static bool AcceptResult(const PGresult *result, bool show_error)

void ResetCancelConn(void)

void SetCancelConn(PGconn *conn)

#define fprintf(file, fmt, msg)

PGresult * PQexec(PGconn *conn, const char *query)

@ PSQL_ECHO_HIDDEN_NOEXEC

PSQL_ECHO_HIDDEN echo_hidden

References _, AcceptResult(), ClearOrSaveResult(), _psqlSettings::db, _psqlSettings::echo_hidden, fprintf, _psqlSettings::logfile, pg_log_error, PQexec(), printf, pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, ResetCancelConn(), SetCancelConn(), and generate_unaccent_rules::stdout.

Referenced by add_tablespace_footer(), addFooterToPublicationDesc(), describeAccessMethods(), describeAggregates(), describeConfigurationParameters(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describePublications(), describeRoleGrants(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_lo_import(), exec_command_password(), fail_lo_xact(), finish_lo_xact(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtendedStats(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listLargeObjects(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), main(), objectDescription(), permissionsList(), and start_lo_xact().

PSQLexecWatch()

int PSQLexecWatch ( const char * query,
const printQueryOpt * opt,
FILE * printQueryFout,
int min_rows
)

Definition at line 710 of file common.c.

711{

713 double elapsed_msec = 0;

714 int res;

715

717 {

718 pg_log_error("You are currently not connected to a database.");

719 return 0;

720 }

721

723

725

727

728

729 if (timing)

731

732 return res;

733}

static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)

static void PrintTiming(double elapsed_msec)

References _psqlSettings::db, ExecQueryAndProcessResults(), pg_log_error, PrintTiming(), pset, ResetCancelConn(), SetCancelConn(), and _psqlSettings::timing.

Referenced by do_watch().

recognized_connection_string()

bool recognized_connection_string ( const char * connstr )

SendQuery()

bool SendQuery ( const char * query )

Definition at line 1118 of file common.c.

1119{

1122 double elapsed_msec = 0;

1123 bool OK = false;

1124 int i;

1125 bool on_error_rollback_savepoint = false;

1126 bool svpt_gone = false;

1127

1129 {

1130 pg_log_error("You are currently not connected to a database.");

1131 goto sendquery_cleanup;

1132 }

1133

1135 {

1136 char buf[3];

1137

1138 fflush(stderr);

1139 printf(_("/**(Single step mode: verify command)******************************************/\n"

1140 "%s\n"

1141 "/**(press return to proceed or enter x and return to cancel)*******************/\n"),

1142 query);

1144 if (fgets(buf, sizeof(buf), stdin) != NULL)

1145 if (buf[0] == 'x')

1146 goto sendquery_cleanup;

1148 goto sendquery_cleanup;

1149 }

1151 {

1152 puts(query);

1154 }

1155

1157 {

1159 _("/******** QUERY *********/\n"

1160 "%s\n"

1161 "/************************/\n\n"), query);

1163 }

1164

1166

1168

1172 {

1174

1177 {

1180 goto sendquery_cleanup;

1181 }

1184 }

1185

1190 {

1192

1193 result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");

1195 {

1198 goto sendquery_cleanup;

1199 }

1201 on_error_rollback_savepoint = true;

1202 }

1203

1205 {

1206

1208 }

1209 else

1210 {

1211

1213 }

1214

1217

1218

1219 if (on_error_rollback_savepoint)

1220 {

1221 const char *svptcmd = NULL;

1222

1224

1225 switch (transaction_status)

1226 {

1228

1229 svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";

1230 break;

1231

1233

1234 break;

1235

1237

1238

1239

1240

1241

1242 if (!svpt_gone)

1243 svptcmd = "RELEASE pg_psql_temporary_savepoint";

1244 break;

1245

1248 default:

1249 OK = false;

1250

1252 pg_log_error("unexpected transaction status (%d)",

1253 transaction_status);

1254 break;

1255 }

1256

1257 if (svptcmd)

1258 {

1260

1263 {

1266 OK = false;

1267

1268 goto sendquery_cleanup;

1269 }

1271 }

1272 }

1273

1274

1275 if (timing)

1277

1278

1279

1282 {

1283

1288 }

1289

1291

1292

1293

1294sendquery_cleanup:

1295

1296

1298

1299

1301 {

1304 }

1305

1306

1308 {

1311 }

1312

1313

1315

1316

1318 {

1321 }

1322

1323

1325

1326

1328

1329

1332 {

1335 }

1336

1337 return OK;

1338}

static bool DescribeQuery(const char *query, double *elapsed_msec)

static void PrintNotifications(void)

void clean_extended_state(void)

static bool ConnectionUp(void)

static bool command_no_begin(const char *query)

void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)

PGTransactionStatusType PQtransactionStatus(const PGconn *conn)

int PQclientEncoding(const PGconn *conn)

ExecStatusType PQresultStatus(const PGresult *res)

void PQclear(PGresult *res)

volatile sig_atomic_t cancel_pressed

#define pg_encoding_to_char

@ PSQL_ERROR_ROLLBACK_OFF

PSQL_ERROR_ROLLBACK on_error_rollback

printQueryOpt * gsavepopt

bool SetVariable(VariableSpace space, const char *name, const char *value)

References _, _psqlSettings::autocommit, buf, cancel_pressed, clean_extended_state(), ClearOrSaveResult(), command_no_begin(), ConnectionUp(), _psqlSettings::crosstab_flag, _psqlSettings::ctv_args, _psqlSettings::cur_cmd_interactive, _psqlSettings::db, DescribeQuery(), _psqlSettings::echo, _psqlSettings::encoding, printTableOpt::encoding, ExecQueryAndProcessResults(), fprintf, free, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _psqlSettings::gset_prefix, i, lengthof, _psqlSettings::logfile, _psqlSettings::on_error_rollback, pg_encoding_to_char, pg_free(), pg_log_error, pg_log_info, PGRES_COMMAND_OK, _psqlSettings::popt, PQclear(), PQclientEncoding(), PQerrorMessage(), PQexec(), PQresultStatus(), PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQTRANS_UNKNOWN, PQtransactionStatus(), printf, PrintNotifications(), PrintTiming(), pset, PSQL_ECHO_ERRORS, PSQL_ECHO_QUERIES, PSQL_ERROR_ROLLBACK_OFF, PSQL_ERROR_ROLLBACK_ON, ResetCancelConn(), restorePsetInfo(), SetCancelConn(), SetVariable(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by do_copy(), ExecQueryTuples(), main(), and MainLoop().

session_username()

const char * session_username ( void )

setQFout()

bool setQFout ( const char * fname )

Definition at line 144 of file common.c.

145{

146 FILE *fout;

147 bool is_pipe;

148

149

151 return false;

152

153

155 {

158 else

160 }

161

164

165

168

169 return true;

170}

void SetShellResultVariables(int wait_result)

bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)

void restore_sigpipe_trap(void)

void set_sigpipe_trap_state(bool ignore)

References openQueryOutputFile(), pset, _psqlSettings::queryFout, _psqlSettings::queryFoutPipe, restore_sigpipe_trap(), set_sigpipe_trap_state(), SetShellResultVariables(), and generate_unaccent_rules::stdout.

Referenced by exec_command_out(), main(), and parse_psql_options().

SetShellResultVariables()

void SetShellResultVariables ( int wait_result )

Definition at line 516 of file common.c.

517{

518 char buf[32];

519

521 (wait_result == 0) ? "false" : "true");

524}

int wait_result_to_exit_code(int exit_status)

References buf, pset, SetVariable(), snprintf, _psqlSettings::vars, and wait_result_to_exit_code().

Referenced by CloseGOutput(), do_copy(), do_shell(), evaluate_backtick(), exec_command_write(), and setQFout().

standard_strings()

bool standard_strings ( void )

sigint_interrupt_enabled

volatile sig_atomic_t sigint_interrupt_enabled extern

sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp extern