本文整理汇总了C++中PQparameterStatus函数的典型用法代码示例。如果您正苦于以下问题:C++ PQparameterStatus函数的具体用法?C++ PQparameterStatus怎么用?C++ PQparameterStatus使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PQparameterStatus函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: _check_database_version
static void
_check_database_version(ArchiveHandle *AH)
{
const char *remoteversion_str;
int remoteversion;
remoteversion_str = PQparameterStatus(AH->connection, "server_version");
remoteversion = PQserverVersion(AH->connection);
if (remoteversion == 0 || !remoteversion_str)
exit_horribly(modulename, "could not get server_version from libpq\n");
AH->public__.remoteVersionStr = pg_strdup(remoteversion_str);
AH->public__.remoteVersion = remoteversion;
if (!AH->archiveRemoteVersion)
AH->archiveRemoteVersion = AH->public__.remoteVersionStr;
if (remoteversion != PG_VERSION_NUM
&& (remoteversion < AH->public__.minRemoteVersion ||
remoteversion > AH->public__.maxRemoteVersion))
{
write_msg(NULL, "server version: %s; %s version: %s\n",
remoteversion_str, progname, PG_VERSION);
exit_horribly(NULL, "aborting because of server version mismatch\n");
}
}
开发者ID:jarulraj,项目名称:postgres-cpp,代码行数:25,代码来源:pg_backup_db.c
示例2: conn_get_standard_conforming_strings
int
conn_get_standard_conforming_strings(PGconn *pgconn)
{
int equote;
const char *scs;
/*
* The presence of the 'standard_conforming_strings' parameter
* means that the server _accepts_ the E'' quote.
*
* If the parameter is off, the PQescapeByteaConn returns
* backslash escaped strings (e.g. '\001' -> "\\001"),
* so the E'' quotes are required to avoid warnings
* if 'escape_string_warning' is set.
*
* If the parameter is on, the PQescapeByteaConn returns
* not escaped strings (e.g. '\001' -> "\001"), relying on the
* fact that the '\' will pass untouched the string parser.
* In this case the E'' quotes are NOT to be used.
*/
scs = PQparameterStatus(pgconn, "standard_conforming_strings");
Dprintf("conn_connect: server standard_conforming_strings parameter: %s",
scs ? scs : "unavailable");
equote = (scs && (0 == strcmp("off", scs)));
Dprintf("conn_connect: server requires E'' quotes: %s",
equote ? "YES" : "NO");
return equote;
}
开发者ID:psycopg,项目名称:psycopg2,代码行数:29,代码来源:connection_int.c
示例3: pqt_getfmtinfo
void
pqt_getfmtinfo(const PGconn *conn, PGtypeFormatInfo *info)
{
const char *value;
memset(info, 0, sizeof(PGtypeFormatInfo));
if ((value = PQparameterStatus(conn, "DateStyle")))
pqt_strcpy(info->datestyle, sizeof(info->datestyle), value);
if ((value = PQparameterStatus(conn, "integer_datetimes")))
info->integer_datetimes = strcmp(value, "on")==0 ? TRUE : FALSE;
info->sversion = PQserverVersion(conn);
info->pversion = PQprotocolVersion(conn);
}
开发者ID:KingDuckZ,项目名称:dindexer,代码行数:16,代码来源:handler.c
示例4: session_username
/*
* Return the session user of the current connection.
*
* Note: this will correctly detect the session user only with a
* protocol-3.0 or newer backend; otherwise it will return the
* connection user.
*/
const char *
session_username(void)
{
const char *val;
if (!pset.db)
return NULL;
val = PQparameterStatus(pset.db, "session_authorization");
if (val)
return val;
else
return PQuser(pset.db);
}
开发者ID:GisKook,项目名称:Gis,代码行数:21,代码来源:common.c
示例5: is_superuser
/*
* Test if the current user is a database superuser.
*
* Note: this will correctly detect superuserness only with a protocol-3.0
* or newer backend; otherwise it will always say "false".
*/
bool
is_superuser(void)
{
const char *val;
if (!pset.db)
return false;
val = PQparameterStatus(pset.db, "is_superuser");
if (val && strcmp(val, "on") == 0)
return true;
return false;
}
开发者ID:GisKook,项目名称:Gis,代码行数:21,代码来源:common.c
示例6: standard_strings
/*
* Test if the current session uses standard string literals.
*
* Note: With a pre-protocol-3.0 connection this will always say "false",
* which should be the right answer.
*/
bool
standard_strings(void)
{
const char *val;
if (!pset.db)
return false;
val = PQparameterStatus(pset.db, "standard_conforming_strings");
if (val && strcmp(val, "on") == 0)
return true;
return false;
}
开发者ID:GisKook,项目名称:Gis,代码行数:21,代码来源:common.c
示例7: conn_is_datestyle_ok
/* Return 1 if the server datestyle allows us to work without problems,
0 if it needs to be set to something better, e.g. ISO. */
static int
conn_is_datestyle_ok(PGconn *pgconn)
{
const char *ds;
ds = PQparameterStatus(pgconn, "DateStyle");
Dprintf("conn_connect: DateStyle %s", ds);
/* pgbouncer does not pass on DateStyle */
if (ds == NULL)
return 0;
/* Return true if ds starts with "ISO"
* e.g. "ISO, DMY" is fine, "German" not. */
return (ds[0] == 'I' && ds[1] == 'S' && ds[2] == 'O');
}
开发者ID:psycopg,项目名称:psycopg2,代码行数:18,代码来源:connection_int.c
示例8: psyco_conn_get_parameter_status
static PyObject *
psyco_conn_get_parameter_status(connectionObject *self, PyObject *args)
{
const char *param = NULL;
const char *val = NULL;
EXC_IF_CONN_CLOSED(self);
if (!PyArg_ParseTuple(args, "s", ¶m)) return NULL;
val = PQparameterStatus(self->pgconn, param);
if (!val) {
Py_RETURN_NONE;
}
return conn_text_from_chars(self, val);
}
开发者ID:gencer,项目名称:psycopg2,代码行数:16,代码来源:connection_type.c
示例9: psyco_conn_get_parameter_status
static PyObject *
psyco_conn_get_parameter_status(connectionObject *self, PyObject *args)
{
const char *param = NULL;
const char *val = NULL;
EXC_IF_CONN_CLOSED(self);
if (!PyArg_ParseTuple(args, "s", ¶m)) return NULL;
val = PQparameterStatus(self->pgconn, param);
if (!val) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString(val);
}
开发者ID:amrik,项目名称:pyvertica,代码行数:17,代码来源:connection_type.c
示例10: connection_warnings
void
connection_warnings(bool in_startup)
{
if (!pset.quiet && !pset.notty)
{
int client_ver = parse_version(PG_VERSION);
if (pset.sversion != client_ver)
{
const char *server_version;
char server_ver_str[16];
/* Try to get full text form, might include "devel" etc */
server_version = PQparameterStatus(pset.db, "server_version");
if (!server_version)
{
snprintf(server_ver_str, sizeof(server_ver_str),
"%d.%d.%d",
pset.sversion / 10000,
(pset.sversion / 100) % 100,
pset.sversion % 100);
server_version = server_ver_str;
}
printf(_("%s (%s, server %s)\n"),
pset.progname, PG_VERSION, server_version);
}
/* For version match, only print psql banner on startup. */
else if (in_startup)
printf("%s (%s)\n", pset.progname, PG_VERSION);
if (pset.sversion / 100 != client_ver / 100)
printf(_("WARNING: %s version %d.%d, server version %d.%d.\n"
" Some psql features might not work.\n"),
pset.progname, client_ver / 10000, (client_ver / 100) % 100,
pset.sversion / 10000, (pset.sversion / 100) % 100);
#ifdef WIN32
checkWin32Codepage();
#endif
printSSLInfo();
}
}
开发者ID:50wu,项目名称:gpdb,代码行数:43,代码来源:command.c
示例11: pgsql_session_open
gboolean
pgsql_session_open (GSQLEPGSQLSession *spec_session,
gchar *username,
gchar *password,
gchar *database,
gchar *hostname,
gchar *port)
{
GSQL_TRACE_FUNC;
gchar *conninfo = g_strdup_printf ("host = '%s' port='%s' "\
"dbname = '%s' " \
"user = '%s' password = '%s' " \
"connect_timeout = '10'",
hostname, port, database, username,
password);
spec_session->pgconn = PQconnectdb ( conninfo );
if (! spec_session->pgconn ||
PQstatus(spec_session->pgconn) != CONNECTION_OK)
{
GSQL_DEBUG ("Connection failed");
g_free ( conninfo );
return FALSE;
}
spec_session->hash_conns = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_insert(spec_session->hash_conns, g_strdup(database),
spec_session->pgconn);
spec_session->server_version =
(gchar *) PQparameterStatus (spec_session->pgconn, "server_version");
spec_session->use = TRUE;
g_free ( conninfo );
if ( ! PQexec(spec_session->pgconn, "BEGIN;") ) {
GSQL_DEBUG ("Trans: Transaction Not Started!!!");
}
return TRUE;
}
开发者ID:antono,项目名称:gsql,代码行数:43,代码来源:pgsql.c
示例12: conn_read_encoding
/* Read the client encoding from the connection.
*
* Store the encoding in the pgconn->encoding field and the name of the
* matching python codec in codec. The buffers are allocated on the Python
* heap.
*
* Return 0 on success, else nonzero.
*/
RAISES_NEG static int
conn_read_encoding(connectionObject *self, PGconn *pgconn)
{
char *enc = NULL, *codec = NULL;
const char *tmp;
int rv = -1;
tmp = PQparameterStatus(pgconn, "client_encoding");
Dprintf("conn_connect: client encoding: %s", tmp ? tmp : "(none)");
if (!tmp) {
PyErr_SetString(OperationalError,
"server didn't return client encoding");
goto exit;
}
if (0 > clear_encoding_name(tmp, &enc)) {
goto exit;
}
/* Look for this encoding in Python codecs. */
if (0 > conn_encoding_to_codec(enc, &codec)) {
goto exit;
}
/* Good, success: store the encoding/codec in the connection. */
PyMem_Free(self->encoding);
self->encoding = enc;
enc = NULL;
PyMem_Free(self->codec);
self->codec = codec;
codec = NULL;
rv = 0;
exit:
PyMem_Free(enc);
PyMem_Free(codec);
return rv;
}
开发者ID:YellowDinar,项目名称:HashTags,代码行数:48,代码来源:connection_int.c
示例13: conn_read_encoding
/* Read the client encoding from the backend and store it in the connection.
*
* Return 0 on success, else -1.
*/
RAISES_NEG static int
conn_read_encoding(connectionObject *self, PGconn *pgconn)
{
const char *encoding;
int rv = -1;
encoding = PQparameterStatus(pgconn, "client_encoding");
Dprintf("conn_connect: client encoding: %s", encoding ? encoding : "(none)");
if (!encoding) {
PyErr_SetString(OperationalError,
"server didn't return client encoding");
goto exit;
}
if (0 > conn_store_encoding(self, encoding)) {
goto exit;
}
rv = 0;
exit:
return rv;
}
开发者ID:psycopg,项目名称:psycopg2,代码行数:27,代码来源:connection_int.c
示例14: StreamLog
/*
* Start the log streaming
*/
static void
StreamLog(void)
{
PGresult *res;
uint32 timeline;
XLogRecPtr startpos;
int minServerMajor,
maxServerMajor;
int serverMajor;
/*
* Connect in replication mode to the server
*/
conn = GetConnection();
if (!conn)
/* Error message already written in GetConnection() */
return;
/*
* Check server version. IDENTIFY_SYSTEM didn't return the current xlog
* position before 9.1, so we can't work with servers older than 9.1. And
* we don't support servers newer than the client.
*/
minServerMajor = 901;
maxServerMajor = PG_VERSION_NUM / 100;
serverMajor = PQserverVersion(conn) / 100;
if (serverMajor < minServerMajor || serverMajor > maxServerMajor)
{
fprintf(stderr, _("%s: unsupported server version %s\n"),
progname, PQparameterStatus(conn, "server_version"));
disconnect_and_exit(1);
}
/*
* Run IDENTIFY_SYSTEM so we can get the timeline and current xlog
* position.
*/
res = PQexec(conn, "IDENTIFY_SYSTEM");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn));
disconnect_and_exit(1);
}
if (PQntuples(res) != 1 || PQnfields(res) != 3)
{
fprintf(stderr,
_("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d fields\n"),
progname, PQntuples(res), PQnfields(res), 1, 3);
disconnect_and_exit(1);
}
timeline = atoi(PQgetvalue(res, 0, 1));
if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &startpos.xlogid, &startpos.xrecoff) != 2)
{
fprintf(stderr,
_("%s: could not parse transaction log location \"%s\"\n"),
progname, PQgetvalue(res, 0, 2));
disconnect_and_exit(1);
}
PQclear(res);
/*
* Figure out where to start streaming.
*/
startpos = FindStreamingStart(startpos, timeline);
/*
* Always start streaming at the beginning of a segment
*/
startpos.xrecoff -= startpos.xrecoff % XLOG_SEG_SIZE;
/*
* Start the replication
*/
if (verbose)
fprintf(stderr,
_("%s: starting log streaming at %X/%X (timeline %u)\n"),
progname, startpos.xlogid, startpos.xrecoff, timeline);
ReceiveXlogStream(conn, startpos, timeline, NULL, basedir,
stop_streaming, standby_message_timeout, false);
PQfinish(conn);
}
开发者ID:mikanradojevic,项目名称:sdkpub,代码行数:89,代码来源:pg_receivexlog.c
示例15: BaseBackup
static void
BaseBackup(void)
{
PGresult *res;
char *sysidentifier;
uint32 latesttli;
uint32 starttli;
char current_path[MAXPGPATH];
char escaped_label[MAXPGPATH];
int i;
char xlogstart[64];
char xlogend[64];
int minServerMajor,
maxServerMajor;
int serverMajor;
/*
* Connect in replication mode to the server
*/
conn = GetConnection();
if (!conn)
/* Error message already written in GetConnection() */
exit(1);
/*
* Check server version. BASE_BACKUP command was introduced in 9.1, so we
* can't work with servers older than 9.1.
*/
minServerMajor = 901;
maxServerMajor = PG_VERSION_NUM / 100;
serverMajor = PQserverVersion(conn) / 100;
if (serverMajor < minServerMajor || serverMajor > maxServerMajor)
{
const char *serverver = PQparameterStatus(conn, "server_version");
fprintf(stderr, _("%s: incompatible server version %s\n"),
progname, serverver ? serverver : "'unknown'");
disconnect_and_exit(1);
}
/*
* If WAL streaming was requested, also check that the server is new
* enough for that.
*/
if (streamwal && !CheckServerVersionForStreaming(conn))
{
/* Error message already written in CheckServerVersionForStreaming() */
disconnect_and_exit(1);
}
/*
* Build contents of recovery.conf if requested
*/
if (writerecoveryconf)
GenerateRecoveryConf(conn);
/*
* Run IDENTIFY_SYSTEM so we can get the timeline
*/
res = PQexec(conn, "IDENTIFY_SYSTEM");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn));
disconnect_and_exit(1);
}
if (PQntuples(res) != 1 || PQnfields(res) != 3)
{
fprintf(stderr,
_("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d fields\n"),
progname, PQntuples(res), PQnfields(res), 1, 3);
disconnect_and_exit(1);
}
sysidentifier = pg_strdup(PQgetvalue(res, 0, 0));
latesttli = atoi(PQgetvalue(res, 0, 1));
PQclear(res);
/*
* Start the actual backup
*/
PQescapeStringConn(conn, escaped_label, label, sizeof(escaped_label), &i);
snprintf(current_path, sizeof(current_path),
"BASE_BACKUP LABEL '%s' %s %s %s %s",
escaped_label,
showprogress ? "PROGRESS" : "",
includewal && !streamwal ? "WAL" : "",
fastcheckpoint ? "FAST" : "",
includewal ? "NOWAIT" : "");
if (PQsendQuery(conn, current_path) == 0)
{
fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
progname, "BASE_BACKUP", PQerrorMessage(conn));
disconnect_and_exit(1);
}
/*
* Get the starting xlog position
*/
res = PQgetResult(conn);
//.........这里部分代码省略.........
开发者ID:42penguins,项目名称:postgres,代码行数:101,代码来源:pg_basebackup.c
示例16: main
int main( int argc, char *argv[] )
{
int res;
PGconn *conn;
struct fuse_args args = FUSE_ARGS_INIT( argc, argv );
PgFuseOptions pgfuse;
PgFuseData userdata;
const char *value;
memset( &pgfuse, 0, sizeof( pgfuse ) );
pgfuse.multi_threaded = 1;
pgfuse.block_size = DEFAULT_BLOCK_SIZE;
if( fuse_opt_parse( &args, &pgfuse, pgfuse_opts, pgfuse_opt_proc ) == -1 ) {
if( pgfuse.print_help ) {
/* print our options */
print_usage( basename( argv[0] ) );
fflush( stdout );
/* print options of FUSE itself */
argv[1] = "-ho";
argv[2] = "mountpoint";
(void)dup2( STDOUT_FILENO, STDERR_FILENO ); /* force fuse help to stdout */
fuse_main( 2, argv, &pgfuse_oper, NULL);
exit( EXIT_SUCCESS );
}
if( pgfuse.print_version ) {
printf( "%s\n", PGFUSE_VERSION );
exit( EXIT_SUCCESS );
}
exit( EXIT_FAILURE );
}
if( pgfuse.conninfo == NULL ) {
fprintf( stderr, "Missing Postgresql connection data\n" );
fprintf( stderr, "See '%s -h' for usage\n", basename( argv[0] ) );
exit( EXIT_FAILURE );
}
/* just test if the connection can be established, do the
* real connection in the fuse init function!
*/
conn = PQconnectdb( pgfuse.conninfo );
if( PQstatus( conn ) != CONNECTION_OK ) {
fprintf( stderr, "Connection to database failed: %s",
PQerrorMessage( conn ) );
PQfinish( conn );
exit( EXIT_FAILURE );
}
/* test storage of timestamps (expecting uint64 as it is the
* standard for PostgreSQL 8.4 or newer). Otherwise bail out
* currently..
*/
value = PQparameterStatus( conn, "integer_datetimes" );
if( value == NULL ) {
fprintf( stderr, "PQ param integer_datetimes not available?\n"
"You use a too old version of PostgreSQL..can't continue.\n" );
PQfinish( conn );
exit( EXIT_FAILURE );
}
if( strcmp( value, "on" ) != 0 ) {
fprintf( stderr, "Expecting UINT64 for timestamps, not doubles. You may use an old version of PostgreSQL (<8.4)\n"
"or PostgreSQL has been compiled with the deprecated compile option '--disable-integer-datetimes'\n" );
PQfinish( conn );
exit( EXIT_FAILURE );
}
openlog( basename( argv[0] ), LOG_PID, LOG_USER );
/* Compare blocksize given as parameter and blocksize in database */
res = psql_get_block_size( conn, pgfuse.block_size );
if( res < 0 ) {
PQfinish( conn );
exit( EXIT_FAILURE );
}
if( res != pgfuse.block_size ) {
fprintf( stderr, "Blocksize parameter mismatch (is '%zu', in database we have '%zu') taking the later one!\n",
pgfuse.block_size, (size_t)res );
PQfinish( conn );
exit( EXIT_FAILURE );
}
PQfinish( conn );
/* check sanity of the mount point, remember it's permission and owner in
* case we want to inherit them or overrule them
*/
res = check_mountpoint( &pgfuse.mountpoint );
if( res < 0 ) {
/* something is fishy, bail out, check_mountpointed reported errors already */
exit( EXIT_FAILURE );
}
memset( &userdata, 0, sizeof( PgFuseData ) );
userdata.conninfo = pgfuse.conninfo;
userdata.mountpoint = pgfuse.mountpoint;
userdata.verbose = pgfuse.verbose;
userdata.read_only = pgfuse.read_only;
//.........这里部分代码省略.........
开发者ID:andreasbaumann,项目名称:pgfuse,代码行数:101,代码来源:pgfuse.c
示例17: client_encoding
std::string client_encoding() const
{
return PQparameterStatus(conn_, "client_encoding");
}
开发者ID:cquest,项目名称:mapnik,代码行数:4,代码来源:connection.hpp
示例18: main
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s QUERY", argv[0]);
}
char *query = argv[1];
PGconn *conn = PQconnectdb("");
if (PQstatus(conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
return 1;
}
const char *version = PQparameterStatus(conn, "crdb_version");
if (version == NULL) {
fprintf(stderr, "ERROR PQparameterStatus: crdb_version not reported: %s\n", PQgeterror());
return 1;
}
if (strncmp(version, "CockroachDB ", strlen("CockroachDB ")) != 0) {
fprintf(stderr, "crdb_version mismatch: '%s' doesn't start with 'CockroachDB '\n", version);
return 1;
}
/* Always call first on any conn that is to be used with libpqtypes */
PQinitTypes(conn);
PGparam *param = PQparamCreate(conn);
PGbool b = 1;
if (!PQputf(param, "%bool", b)) {
fprintf(stderr, "ERROR PQputf(bool): %s\n", PQgeterror());
return 1;
}
char bytes[] = "hello";
PGbytea bytea;
bytea.len = sizeof(bytes);
bytea.data = bytes;
if (!PQputf(param, "%bytea", &bytea)) {
fprintf(stderr, "ERROR PQputf(bytea): %s\n", PQgeterror());
return 1;
}
// '1401-01-19 BC'
PGdate date;
date.isbc = 1;
date.year = 1401;
date.mon = 0;
date.mday = 19;
if (!PQputf(param, "%date", &date)) {
fprintf(stderr, "ERROR PQputf(date): %s\n", PQgeterror());
return 1;
}
PGnumeric numeric1 = "42";
if (!PQputf(param, "%numeric", numeric1)) {
fprintf(stderr, "ERROR PQputf(numeric): %s\n", PQgeterror());
return 1;
}
PGnumeric numeric2 = "-1728718718271827121233.1212121212";
if (!PQputf(param, "%numeric", numeric2)) {
fprintf(stderr, "ERROR PQputf(numeric): %s\n", PQgeterror());
return 1;
}
PGfloat8 f8 = 123456.789;
if (!PQputf(param, "%float8", f8)) {
fprintf(stderr, "ERROR PQputf(float8): %s\n", PQgeterror());
return 1;
}
PGint8 i8 = INT_MAX;
if (!PQputf(param, "%int8", i8)) {
fprintf(stderr, "ERROR PQputf(int8): %s\n", PQgeterror());
return 1;
}
// "20 years 8 months 9 hours 10 mins 15 secs 123456 usecs"
PGinterval interval;
interval.years = 20;
interval.mons = 8;
interval.days = 0; // not used, set to 0
interval.hours = 9;
interval.mins = 10;
interval.secs = 15;
interval.usecs = 123456;
// TODO(tamird,nvanbenschoten): implement interval binary encoding/decoding.
if (0) {
if (!PQputf(param, "%interval", &interval)) {
fprintf(stderr, "ERROR PQputf(interval): %s\n", PQgeterror());
return 1;
}
}
PGtext text = "foobar";
if (!PQputf(param, "%text", text)) {
fprintf(stderr, "ERROR PQputf(text): %s\n", PQgeterror());
return 1;
}
//.........这里部分代码省略.........
开发者ID:tamird,项目名称:cockroach,代码行数:101,代码来源:test.c
示例19: GetConnection
//.........这里部分代码省略.........
}
if (dbport)
{
keywords[i] = "port";
values[i] = dbport;
i++;
}
while (true)
{
if (password)
free(password);
if (dbpassword)
{
/*
* We've saved a password when a previous connection succeeded,
* meaning this is the call for a second session to the same
* database, so just forcibly reuse that password.
*/
keywords[argcount - 1] = "password";
values[argcount - 1] = dbpassword;
dbgetpassword = -1; /* Don't try again if this fails */
}
else if (dbgetpassword == 1)
{
password = simple_prompt(_("Password: "), 100, false);
keywords[argcount - 1] = "password";
values[argcount - 1] = password;
}
tmpconn = PQconnectdbParams(keywords, values, true);
/*
* If there is too little memory even to allocate the PGconn object
* and PQconnectdbParams returns NULL, we call exit(1) directly.
*/
if (!tmpconn)
{
fprintf(stderr, _("%s: could not connect to server\n"),
progname);
exit(1);
}
if (PQstatus(tmpconn) == CONNECTION_BAD &&
PQconnectionNeedsPassword(tmpconn) &&
dbgetpassword != -1)
{
dbgetpassword = 1; /* ask for password next time */
PQfinish(tmpconn);
continue;
}
if (PQstatus(tmpconn) != CONNECTION_OK)
{
fprintf(stderr, _("%s: could not connect to server: %s\n"),
progname, PQerrorMessage(tmpconn));
PQfinish(tmpconn);
free(values);
free(keywords);
return NULL;
}
/* Connection ok! */
free(values);
free(keywords);
/*
* Ensure we have the same value of integer timestamps as the server
* we are connecting to.
*/
tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
if (!tmpparam)
{
fprintf(stderr,
_("%s: could not determine server setting for integer_datetimes\n"),
progname);
PQfinish(tmpconn);
exit(1);
}
#ifdef HAVE_INT64_TIMESTAMP
if (strcmp(tmpparam, "on") != 0)
#else
if (strcmp(tmpparam, "off") != 0)
#endif
{
fprintf(stderr,
_("%s: integer_datetimes compile flag does not match server\n"),
progname);
PQfinish(tmpconn);
exit(1);
}
/* Store the password for next run */
if (password)
dbpassword = password;
return tmpconn;
}
}
开发者ID:bwright,项目名称:postgres,代码行数:101,代码来源:streamutil.c
示例20: main
int main( int argc, char *argv[] )
{
PGconn *conn;
const char *value;
bool integer_datetimes;
PGresult *res;
uint64_t tmp;
uint64_t param1;
const char *values[1] = { (const char *)¶m1 };
int lengths[1] = { sizeof( param1 ) };
int binary[1] = { 1 };
struct timespec now;
if( argc != 2 ) {
fprintf( stderr, "usage: testpgsql <Pg conn info>\n" );
return 1;
}
conn = PQconnectdb( argv[1] );
if( PQstatus( conn ) != CONNECTION_OK ) {
fprintf( stderr, "Connection to database failed: %s",
PQerrorMessage( conn ) );
PQfinish( conn );
return 1;
}
value = PQparameterStatus( conn, "integer_datetimes" );
if( value == NULL ) {
fprintf( stderr, "PQ param integer_datetimes empty?\n" );
PQfinish( conn );
return 1;
}
integer_datetimes = strcmp( value, "on" ) == 0 ? true : false;
printf( "integer_datetimes: %s\n", integer_datetimes ? "true" : "false" );
now = get_now( );
tmp = ( (uint64_t)now.tv_sec - POSTGRES_EPOCH_DATE ) * 1000000;
tmp += now.tv_nsec / 1000;
param1 = htobe64( tmp );
res = PQexecParams( conn, "SELECT now(),$1::timestamp",
1, NULL, values, lengths, binary, 1 );
if( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
fprintf( stderr, "select error: %s\n", PQerrorMessage( conn ) );
PQclear( res );
PQfinish( conn );
return 1;
}
if( PQntuples( res ) == 0 ) {
PQclear( res );
PQfinish( conn );
return 1;
}
if( PQntuples( res ) > 1 ) {
fprintf( stderr, "Expecting exactly one tuple as result." );
PQclear( res );
PQfinish( conn );
return 1;
}
/* Since PostgreSQL 8.4 int64 representation should be the default
* unless changed at compilation time
*/
if( integer_datetimes ) {
char *data_db;
char *data_select;
struct timespec time_db;
struct timespec time_select;
uint64_t t_db;
uint64_t t_select;
data_db = PQgetvalue( res, 0, 0 );
t_db = be64toh( *( (uint64_t *)data_db ) );
time_db.tv_sec = POSTGRES_EPOCH_DATE + t_db / 1000000;
time_db.tv_nsec = ( t_db % 1000000 ) * 1000;
data_select = PQgetvalue( res, 0, 1 );
t_select = be64toh( *( (uint64_t *)data_select ) );
time_select.tv_sec = POSTGRES_EPOCH_DATE + t_select / 1000000;
time_select.tv_nsec = ( t_select % 1000000 ) * 1000;
now = get_now( );
printf( "now passed as param: %lu.%lu, now from database: %lu.%lu, now computed: %lu.%lu\n",
time_select.tv_sec, time_select.tv_nsec, time_db.tv_sec, time_db.tv_nsec, now.tv_sec, now.tv_nsec );
} else {
/* doubles have no standard network representation! */
fprintf( stderr, "Not supporting dates as doubles!\n" );
PQclear( res );
PQfinish( conn );
return 1;
}
//.........这里部分代码省略.........
开发者ID:armos-pg,项目名称:pgfuse,代码行数:101,代码来源:testpgsql.c
注:本文中的PQparameterStatus函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论