本文整理汇总了C++中PQsetdbLogin函数的典型用法代码示例。如果您正苦于以下问题:C++ PQsetdbLogin函数的具体用法?C++ PQsetdbLogin怎么用?C++ PQsetdbLogin使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PQsetdbLogin函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: InitDelayThread
bool DatabasePostgre::Initialize(const char *infoString)
{
if(!Database::Initialize(infoString))
return false;
tranThread = NULL;
InitDelayThread();
Tokens tokens = StrSplit(infoString, ";");
Tokens::iterator iter;
std::string host, port_or_socket_dir, user, password, database;
iter = tokens.begin();
if(iter != tokens.end())
host = *iter++;
if(iter != tokens.end())
port_or_socket_dir = *iter++;
if(iter != tokens.end())
user = *iter++;
if(iter != tokens.end())
password = *iter++;
if(iter != tokens.end())
database = *iter++;
if (host == ".")
mPGconn = PQsetdbLogin(NULL, port_or_socket_dir == "." ? NULL : port_or_socket_dir.c_str(), NULL, NULL, database.c_str(), user.c_str(), password.c_str());
else
mPGconn = PQsetdbLogin(host.c_str(), port_or_socket_dir.c_str(), NULL, NULL, database.c_str(), user.c_str(), password.c_str());
/* check to see that the backend connection was successfully made */
if (PQstatus(mPGconn) != CONNECTION_OK)
{
sLog.outError( "Could not connect to Postgre database at %s: %s",
host.c_str(), PQerrorMessage(mPGconn));
PQfinish(mPGconn);
mPGconn = NULL;
return false;
}
else
{
sLog.outDetail( "Connected to Postgre database at %s",
host.c_str());
sLog.outString( "PostgreSQL server ver: %d",PQserverVersion(mPGconn));
return true;
}
}
示例2: STAFF_ASSERT
void PostgresProvider::Init(const xml::Element& rConfig)
{
// initialize connection
const xml::Element& rConnection = rConfig.GetChildElementByName("connection");
m_pImpl->m_sHost = rConnection.GetChildElementByName("host").GetTextValue();
m_pImpl->m_sPort = rConnection.GetChildElementByName("port").GetTextValue();
m_pImpl->m_sDataBase = rConnection.GetChildElementByName("db").GetTextValue();
m_pImpl->m_sLogin = rConnection.GetChildElementByName("login").GetTextValue();
m_pImpl->m_sPassword = rConnection.GetChildElementByName("password").GetTextValue();
STAFF_ASSERT(!m_pImpl->m_pConn, "Already connected");
m_pImpl->m_pConn = PQsetdbLogin(m_pImpl->m_sHost.c_str(), m_pImpl->m_sPort.c_str(), "", "",
m_pImpl->m_sDataBase.c_str(), m_pImpl->m_sLogin.c_str(),
m_pImpl->m_sPassword.c_str());
STAFF_ASSERT(m_pImpl->m_pConn, "Failed to set db login");
if (PQstatus(m_pImpl->m_pConn) != CONNECTION_OK)
{
std::string sError = std::string("Failed to login: ") + PQerrorMessage(m_pImpl->m_pConn);
PQfinish(m_pImpl->m_pConn);
m_pImpl->m_pConn = NULL;
STAFF_THROW_ASSERT(sError);
}
int nResult = PQsetClientEncoding(m_pImpl->m_pConn, "UTF8");
STAFF_ASSERT(nResult == 0, std::string("error setting encoding: ") + PQerrorMessage(m_pImpl->m_pConn));
}
示例3: sql_conn
/* establish connection with database. */
PGconn *
sql_conn(struct options * my_opts)
{
PGconn *conn;
/* login */
conn = PQsetdbLogin(my_opts->hostname,
my_opts->port,
NULL, /* options */
NULL, /* tty */
my_opts->dbname,
my_opts->username,
my_opts->password);
/* deal with errors */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "%s: connection to database '%s' failed.\n", "oid2name", my_opts->dbname);
fprintf(stderr, "%s", PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
/* return the conn if good */
return conn;
}
示例4: PQsetdbLogin
/**
* Connect to Postgres with the current settings through libpq.
*
* @author Martin Turon
*
* @return Error code from Postgres after executing command
*
* @version 2004/8/8 mturon Initial version
*
*/
PGconn *xdb_connect()
{
char *pgoptions, *pgtty;
PGconn *conn;
/*
* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants
*/
pgoptions = NULL; /* special options to start up the backend
* server */
pgtty = NULL; /* debugging tty for the backend server */
/* make a connection to the database */
conn = PQsetdbLogin(g_server, g_port, pgoptions, pgtty,
g_dbname, g_user, g_passwd);
/*
* check to see that the backend connection was successfully made
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "error: Connection to database '%s' failed.\n",
g_dbname);
fprintf(stderr, "%s", PQerrorMessage(conn));
conn = xdb_exit(conn);
}
return conn;
}
示例5: pgconn_create
PGConnection *
pgconn_create(const char *host, const char *db, const char *user, const char *password)
{
PGConnection *conn;
if (db == NULL)
THROW("SQLException", "Database may not be (null)");
conn = emalloc(sizeof(PGConnection));
conn->stream = PQsetdbLogin(host,"5432",NULL,NULL,db,user,password);
if (PQstatus(conn->stream) != CONNECTION_OK)
THROW("SQLException", "%s", PQerrorMessage(conn->stream));
if(pgTypes == NULL){
PGResultSet *rset = pgconn_query(conn, "SELECT oid,typname FROM pg_type");
pgTypes = ihtab_createDefault();
while(pgrset_next(rset)){
ihtab_put(pgTypes, pgrset_getInt(rset, 0), pgrset_getString(rset, 1));
}
}
return conn;
}
示例6: db_init_pg_conn
void db_init_pg_conn(const char *conf_file) {
if(config_read_file(&config, conf_file) != CONFIG_TRUE) {
fprintf(stderr, "%s:%d %s\n", config_error_file(&config), config_error_line(&config), config_error_text(&config));
config_destroy(&config);
exit(EXIT_FAILURE);
}
conn = PQsetdbLogin(
read_db_setting("db_host"),
read_db_setting("db_port"),
NULL, NULL,
read_db_setting("db_name"),
read_db_setting("db_login"),
read_db_setting("db_password"));
const char *store_dir_tmp = read_db_setting("store_dir");
store_dir = (char*)malloc(strlen(store_dir_tmp)+1);
strcpy(store_dir, store_dir_tmp);
if(PQstatus(conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
config_destroy(&config);
exit(EXIT_FAILURE);
} else {
fprintf(stderr, "database OK\n");
}
if(pthread_mutex_init(&db_lock, NULL) < 0) {
fprintf(stderr, "pthread_mutex_init failed\n");
exit(EXIT_FAILURE);
}
}
示例7: switch_database
static int
switch_database(struct dbpath *dbpath)
{
PGconn *newdbconn;
if (dbpath_is_root(*dbpath))
return 1;
if (strcmp(dbpath->database, PQdb(dbconn)) == 0)
return 1;
newdbconn = PQsetdbLogin(PQhost(dbconn),
PQport(dbconn),
PQoptions(dbconn),
PQtty(dbconn),
dbpath->database,
PQuser(dbconn),
PQpass(dbconn));
if (PQstatus(newdbconn) != CONNECTION_OK)
{
debug("new connection failed");
PQfinish(newdbconn);
return 0;
}
PQfinish(dbconn);
dbconn = newdbconn;
return 1;
}
示例8: quorra_db_object_initConnection
gboolean quorra_db_object_initConnection(QuorraDbObject * obj,
gchar * pghost,
gchar * pgport,
gchar * dbName,
gchar * login,
gchar * pwd)
{
QuorraDbObjectPrivate * priv;
if (dbName == NULL)
{
g_warning("Connection to database failed: database isn't set!");
return FALSE;
}
priv = QUORRA_DBOBJ_GET_PRIVATE (obj);
if (priv)
{
priv->connection = PQsetdbLogin(pghost,pgport,NULL,NULL,dbName,login,pwd);
/* Check to see that the backend connection was successfully made */
if (PQstatus(priv->connection) != CONNECTION_OK)
{
g_warning("Connection to database failed: %s", PQerrorMessage(priv->connection));
PQfinish(priv->connection);
return FALSE;
}
return TRUE;
}
return FALSE;
}
示例9: pgsqlConnect
static HB_ERRCODE pgsqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
PGconn * pConn;
ConnStatusType status;
const char * pszHost;
pszHost = hb_arrayGetCPtr( pItem, 2 );
if( pszHost && ( strncmp( pszHost, "postgresql://", 13 ) == 0 || strchr( pszHost, '=' ) ) )
pConn = PQconnectdb( pszHost );
else
pConn = PQsetdbLogin( pszHost, hb_arrayGetCPtr( pItem, 6 ), hb_arrayGetCPtr( pItem, 7 ), hb_arrayGetCPtr( pItem, 8 ), hb_arrayGetCPtr( pItem, 5 ), hb_arrayGetCPtr( pItem, 3 ), hb_arrayGetCPtr( pItem, 4 ) );
if( ! pConn ) /* Low memory, etc */
{
/* TODO: error */
return HB_FAILURE;
}
status = PQstatus( pConn );
if( status != CONNECTION_OK )
{
/* TODO: error */
PQfinish( pConn );
return HB_FAILURE;
}
pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) );
( ( SDDCONN * ) pConnection->pSDDConn )->pConn = pConn;
return HB_SUCCESS;
}
示例10: PQreset
void PostgreSQLConnection::connect()
{
bool reconnecting = false;
if (_pgConn != nullptr) //reconnection attempt
{
if (!_ConnectionLost())
return;
else
reconnecting = true;
}
//remove any state from previous session
this->clear();
Poco::Logger& logger = _dbEngine->getLogger();
for(;;)
{
if (reconnecting)
PQreset(_pgConn);
else
{
if (_host == ".")
_pgConn = PQsetdbLogin(nullptr, _port == "." ? nullptr : _port.c_str(), nullptr, nullptr, _database.c_str(), _user.c_str(), _password.c_str());
else
_pgConn = PQsetdbLogin(_host.c_str(), _port.c_str(), nullptr, nullptr, _database.c_str(), _user.c_str(), _password.c_str());
}
//check to see that the backend connection was successfully made
if (_ConnectionLost())
{
const char* actionToDo = "connect";
if (reconnecting)
actionToDo = "reconnect";
static const long sleepTime = 1000;
logger.warning(Poco::format("Could not %s to Postgre database at %s: %s, retrying in %d seconds",
string(actionToDo),_host,lastErrorDescr(),static_cast<int>(sleepTime/1000)));
Poco::Thread::sleep(sleepTime);
continue;
}
break;
}
string actionDone = (reconnecting)?string("Reconnected"):string("Connected");
poco_information(logger,Poco::format("%s to Postgre database %s:%s/%s server ver: %d",actionDone,_host,_port,_database,PQserverVersion(_pgConn)));
}
示例11: main
int
main (int argc, char **argv)
{
int elapsed_time;
int status = STATE_UNKNOWN;
/* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants */
pgoptions = NULL; /* special options to start up the backend server */
pgtty = NULL; /* debugging tty for the backend server */
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
if (process_arguments (argc, argv) == ERROR)
usage4 (_("Could not parse arguments"));
/* Set signal handling and alarm */
if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
usage4 (_("Cannot catch SIGALRM"));
}
alarm (timeout_interval);
/* make a connection to the database */
time (&start_time);
conn =
PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
time (&end_time);
elapsed_time = (int) (end_time - start_time);
/* check to see that the backend connection was successfully made */
if (PQstatus (conn) == CONNECTION_BAD) {
printf (_("CRITICAL - no connection to '%s' (%s).\n"),
dbName, PQerrorMessage (conn));
PQfinish (conn);
return STATE_CRITICAL;
}
else if (elapsed_time > tcrit) {
status = STATE_CRITICAL;
}
else if (elapsed_time > twarn) {
status = STATE_WARNING;
}
else {
status = STATE_OK;
}
PQfinish (conn);
printf (_(" %s - database %s (%d sec.)|%s\n"),
state_text(status), dbName, elapsed_time,
fperfdata("time", elapsed_time, "s",
(int)twarn, twarn, (int)tcrit, tcrit, TRUE, 0, FALSE,0));
return status;
}
示例12: db_connect
/*
* Connect to the database.
*/
static isc_result_t db_connect (struct dbinfo *dbi)
{
dbi->conn = PQsetdbLogin (dbi->host, NULL, NULL, NULL, dbi->database, dbi->user, dbi->passwd);
if (PQstatus (dbi->conn) == CONNECTION_OK)
return (ISC_R_SUCCESS);
else
return (ISC_R_FAILURE);
}
示例13: db_postgres_new_connection
/*!
* \brief Create a new connection
*
* Create a new connection structure in private memory, open the PostgreSQL
* connection and set reference count to 1
* \param id database id
* \return postgres connection structure, 0 on error
*/
struct pg_con* db_postgres_new_connection(struct db_id* id)
{
struct pg_con* ptr;
char *ports;
LM_DBG("db_id = %p\n", id);
if (!id) {
LM_ERR("invalid db_id parameter value\n");
return 0;
}
ptr = (struct pg_con*)pkg_malloc(sizeof(struct pg_con));
if (!ptr) {
LM_ERR("failed trying to allocated %lu bytes for connection structure."
"\n", (unsigned long)sizeof(struct pg_con));
return 0;
}
LM_DBG("%p=pkg_malloc(%lu)\n", ptr, (unsigned long)sizeof(struct pg_con));
memset(ptr, 0, sizeof(struct pg_con));
ptr->ref = 1;
if (id->port) {
ports = int2str(id->port, 0);
LM_DBG("opening connection: postgres://xxxx:[email protected]%s:%d/%s\n", ZSW(id->host),
id->port, ZSW(id->database));
} else {
ports = NULL;
LM_DBG("opening connection: postgres://xxxx:[email protected]%s/%s\n", ZSW(id->host),
ZSW(id->database));
}
ptr->con = PQsetdbLogin(id->host, ports, NULL, NULL, id->database, id->username, id->password);
LM_DBG("PQsetdbLogin(%p)\n", ptr->con);
if( (ptr->con == 0) || (PQstatus(ptr->con) != CONNECTION_OK) )
{
LM_ERR("%s\n", PQerrorMessage(ptr->con));
PQfinish(ptr->con);
goto err;
}
ptr->connected = 1;
ptr->timestamp = time(0);
ptr->id = id;
return ptr;
err:
if (ptr) {
LM_ERR("cleaning up %p=pkg_free()\n", ptr);
pkg_free(ptr);
}
return 0;
}
示例14: _log
void *be_pg_init()
{
struct pg_backend *conf;
char *host, *user, *pass, *dbname, *p, *port;
char *userquery;
_log(LOG_DEBUG, "}}}} POSTGRES");
host = p_stab("host");
p = p_stab("port");
user = p_stab("user");
pass = p_stab("pass");
dbname = p_stab("dbname");
host = (host) ? host : strdup("localhost");
port = (p) ? p : strdup("5432");
userquery = p_stab("userquery");
if (!userquery) {
_fatal("Mandatory option 'userquery' is missing");
return (NULL);
}
if ((conf = (struct pg_backend *)malloc(sizeof(struct pg_backend))) == NULL)
return (NULL);
conf->conn = NULL;
conf->host = host;
conf->port = port;
conf->user = user;
conf->pass = pass;
conf->dbname = dbname;
conf->userquery = userquery;
conf->superquery = p_stab("superquery");
conf->aclquery = p_stab("aclquery");
_log( LOG_DEBUG, "HERE: %s", conf->superquery );
_log( LOG_DEBUG, "HERE: %s", conf->aclquery );
char *connect_string = NULL;
conf->conn = PQsetdbLogin(conf->host, conf->port, NULL, NULL, conf->dbname, conf->user, conf->pass );
if (PQstatus(conf->conn) == CONNECTION_BAD) {
free(conf);
free(connect_string);
_fatal("We were unable to connect to the database");
return (NULL);
}
free(connect_string);
return ((void *)conf);
}
示例15: main
int main()
{
Conn_pointer=PQsetdbLogin(pghost, pgport, pgoptions, pgtty, dbName, username, password);
if (PQstatus(Conn_pointer) == CONNECTION_BAD)
{
printf("cannot connect to the database!\n");
return -1;
}
printf ("Connect to database seccess\n");
return 0;
}