PGconn * db_connect(struct cks_config *config) { PGconn *conn = NULL; char *pghost = NULL, *pgport = NULL, *pgoptions = NULL, *pgtty = NULL; char *dbName = NULL; pghost = (char *)config->dbsrvr_ip; pgport = (char *)config->dbsrvr_port; pgoptions = NULL; pgtty = NULL; dbName = (char *)config->dbsrvr_db; conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); if(PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr,_("db.c: Failed to connect to postgres")); fprintf(stderr,_("db.c: Connection to database '%s' failed.\n"), dbName); fprintf(stderr,"db.c: %s", PQerrorMessage(conn)); db_disconnect(conn); return NULL; } return conn; }
static PGresult *do_sql_pg(char *query) { PGconn *conn; PGresult *res; conn=PQsetdb( rcfg_lookup(conf.cf, "databases.sql.host"), rcfg_lookup(conf.cf, "databases.sql.port"), rcfg_lookup(conf.cf, "databases.sql.options"), rcfg_lookup(conf.cf, "databases.sql.tty"), rcfg_lookup(conf.cf, "databases.sql.db")); if(PQstatus(conn)==CONNECTION_BAD) { /* Keen logging thing goes here */ _ndebug(2, (PQerrorMessage(conn))); return(NULL); } _ndebug(2, ("Doing query:\n%s\n", query)); res=PQexec(conn, query); if(PQresultStatus(res)!=PGRES_TUPLES_OK) { /* Keen logging thing goes here */ _ndebug(2, (PQerrorMessage(conn))); return(NULL); } PQfinish(conn); return(res); }
void sql_connect_db( char *dbname, int *dbhandle, int *status ) { PGconn *conn; char db[81]; char *s; /* trim right spaces on the buffer */ memmove(db,dbname,80); db[80]=0; s = db+79; while ((s >= db) && (*s == ' ')) s--; if (s >= db) *(s+1)=0; conn = PQsetdb("","","","",db); *status = PQstatus(conn); *dbhandle = (int)conn; }
int main(int argc, char **argv) { char *in_filename, *out_filename, *out_filename2; char *database; Oid lobjOid; PGconn *conn; PGresult *res; if (argc != 5) { fprintf(stderr, "Usage: %s database_name in_filename out_filename out_filename2\n", argv[0]); exit(1); } database = argv[1]; in_filename = argv[2]; out_filename = argv[3]; out_filename2 = argv[4]; /* * set up the connection */ conn = PQsetdb(NULL, NULL, NULL, NULL, database); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } res = PQexec(conn, "begin"); PQclear(res); printf("importing file \"%s\" ...\n", in_filename); /* lobjOid = importFile(conn, in_filename); */ lobjOid = lo_import(conn, in_filename); if (lobjOid == 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); else { printf("\tas large object %u.\n", lobjOid); printf("picking out bytes 4294967000-4294968000 of the large object\n"); pickout(conn, lobjOid, 4294967000U, 1000); printf("overwriting bytes 4294967000-4294968000 of the large object with X's\n"); overwrite(conn, lobjOid, 4294967000U, 1000); printf("exporting large object to file \"%s\" ...\n", out_filename); /* exportFile(conn, lobjOid, out_filename); */ if (lo_export(conn, lobjOid, out_filename) < 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); printf("truncating to 3294968000 bytes\n"); my_truncate(conn, lobjOid, 3294968000U); printf("exporting truncated large object to file \"%s\" ...\n", out_filename2); if (lo_export(conn, lobjOid, out_filename2) < 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); } res = PQexec(conn, "end"); PQclear(res); PQfinish(conn); return 0; }
int main(int argc, char **argv) { char *porigin, *zonefile; dns_fixedname_t forigin, fname; dns_name_t *origin, *name; dns_db_t *db = NULL; dns_dbiterator_t *dbiter; dns_dbnode_t *node; dns_rdatasetiter_t *rdsiter; dns_rdataset_t rdataset; dns_rdata_t rdata = DNS_RDATA_INIT; isc_mem_t *mctx = NULL; isc_entropy_t *ectx = NULL; isc_buffer_t b; isc_result_t result; PGresult *res; if (argc != 5) { printf("usage: %s origin file dbname dbtable\n", argv[0]); printf("Note that dbname must be an existing database.\n"); exit(1); } porigin = argv[1]; zonefile = argv[2]; dbname = argv[3]; dbtable = argv[4]; dns_result_register(); mctx = NULL; result = isc_mem_create(0, 0, &mctx); check_result(result, "isc_mem_create"); result = isc_entropy_create(mctx, &ectx); result_check (result, "isc_entropy_create"); result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE); check_result (result, "isc_hash_create"); isc_buffer_init(&b, porigin, strlen(porigin)); isc_buffer_add(&b, strlen(porigin)); dns_fixedname_init(&forigin); origin = dns_fixedname_name(&forigin); result = dns_name_fromtext(origin, &b, dns_rootname, ISC_FALSE, NULL); check_result(result, "dns_name_fromtext"); db = NULL; result = dns_db_create(mctx, "rbt", origin, dns_dbtype_zone, dns_rdataclass_in, 0, NULL, &db); check_result(result, "dns_db_create"); result = dns_db_load(db, zonefile); if (result == DNS_R_SEENINCLUDE) result = ISC_R_SUCCESS; check_result(result, "dns_db_load"); printf("Connecting to '%s'\n", dbname); conn = PQsetdb(NULL, NULL, NULL, NULL, dbname); if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to database '%s' failed: %s\n", dbname, PQerrorMessage(conn)); closeandexit(1); } snprintf(str, sizeof(str), "DROP TABLE %s", dbtable); printf("%s\n", str); res = PQexec(conn, str); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) fprintf(stderr, "DROP TABLE command failed: %s\n", PQresultErrorMessage(res)); PQclear(res); snprintf(str, sizeof(str), "BEGIN"); printf("%s\n", str); res = PQexec(conn, str); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "BEGIN command failed: %s\n", PQresultErrorMessage(res)); PQclear(res); closeandexit(1); } PQclear(res); snprintf(str, sizeof(str), "CREATE TABLE %s " "(NAME TEXT, TTL INTEGER, RDTYPE TEXT, RDATA TEXT)", dbtable); printf("%s\n", str); res = PQexec(conn, str); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "CREATE TABLE command failed: %s\n", PQresultErrorMessage(res)); PQclear(res); closeandexit(1); } PQclear(res); dbiter = NULL; result = dns_db_createiterator(db, ISC_FALSE, &dbiter); check_result(result, "dns_db_createiterator()"); result = dns_dbiterator_first(dbiter); check_result(result, "dns_dbiterator_first"); dns_fixedname_init(&fname); name = dns_fixedname_name(&fname); dns_rdataset_init(&rdataset); dns_rdata_init(&rdata); while (result == ISC_R_SUCCESS) { node = NULL; result = dns_dbiterator_current(dbiter, &node, name); if (result == ISC_R_NOMORE) break; check_result(result, "dns_dbiterator_current"); rdsiter = NULL; result = dns_db_allrdatasets(db, node, NULL, 0, &rdsiter); check_result(result, "dns_db_allrdatasets"); result = dns_rdatasetiter_first(rdsiter); while (result == ISC_R_SUCCESS) { dns_rdatasetiter_current(rdsiter, &rdataset); result = dns_rdataset_first(&rdataset); check_result(result, "dns_rdataset_first"); while (result == ISC_R_SUCCESS) { dns_rdataset_current(&rdataset, &rdata); addrdata(name, rdataset.ttl, &rdata); dns_rdata_reset(&rdata); result = dns_rdataset_next(&rdataset); } dns_rdataset_disassociate(&rdataset); result = dns_rdatasetiter_next(rdsiter); } dns_rdatasetiter_destroy(&rdsiter); dns_db_detachnode(db, &node); result = dns_dbiterator_next(dbiter); } snprintf(str, sizeof(str), "COMMIT TRANSACTION"); printf("%s\n", str); res = PQexec(conn, str); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "COMMIT command failed: %s\n", PQresultErrorMessage(res)); PQclear(res); closeandexit(1); } PQclear(res); dns_dbiterator_destroy(&dbiter); dns_db_detach(&db); isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); closeandexit(0); exit(0); }
int main(int argc, char **argv) { char *pghost, *pgport, *pgoptions, *pgtty; char *dbName1, *dbName2; char *tblName; int nFields; int i, j; PGconn *conn1, *conn2; /* * PGresult *res1, *res2; */ PGresult *res1; if (argc != 4) { fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]); fprintf(stderr, " compares two tables in two databases\n"); exit(1); } tblName = argv[1]; dbName1 = argv[2]; dbName2 = argv[3]; /* * 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 */ pghost = NULL; /* host name of the backend */ pgport = NULL; /* port of the backend */ pgoptions = NULL; /* special options to start up the backend * server */ pgtty = NULL; /* debugging tty for the backend */ /* make a connection to the database */ conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1); check_prepare_conn(conn1, dbName1); conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2); check_prepare_conn(conn2, dbName2); /* start a transaction block */ res1 = PQexec(conn1, "BEGIN"); if (PQresultStatus(res1) != PGRES_COMMAND_OK) { fprintf(stderr, "BEGIN command failed\n"); PQclear(res1); exit_nicely(conn1, conn2); } /* * make sure to PQclear() a PGresult whenever it is no longer needed to * avoid memory leaks */ PQclear(res1); /* * fetch instances from the pg_database, the system catalog of databases */ res1 = PQexec(conn1, "DECLARE myportal CURSOR FOR select * from pg_database"); if (PQresultStatus(res1) != PGRES_COMMAND_OK) { fprintf(stderr, "DECLARE CURSOR command failed\n"); PQclear(res1); exit_nicely(conn1, conn2); } PQclear(res1); res1 = PQexec(conn1, "FETCH ALL in myportal"); if (PQresultStatus(res1) != PGRES_TUPLES_OK) { fprintf(stderr, "FETCH ALL command didn't return tuples properly\n"); PQclear(res1); exit_nicely(conn1, conn2); } /* first, print out the attribute names */ nFields = PQnfields(res1); for (i = 0; i < nFields; i++) printf("%-15s", PQfname(res1, i)); printf("\n\n"); /* next, print out the instances */ for (i = 0; i < PQntuples(res1); i++) { for (j = 0; j < nFields; j++) printf("%-15s", PQgetvalue(res1, i, j)); printf("\n"); } PQclear(res1); /* close the portal */ res1 = PQexec(conn1, "CLOSE myportal"); PQclear(res1); /* end the transaction */ res1 = PQexec(conn1, "END"); PQclear(res1); /* close the connections to the database and cleanup */ PQfinish(conn1); PQfinish(conn2); /* fclose(debug); */ return 0; }
int main(int argc, char **argv) { char *in_filename, *out_filename; char *database; Oid lobjOid; PGconn *conn; PGresult *res; if (argc != 4) { fprintf(stderr, "Usage: %s database_name in_filename out_filename\n", argv[0]); exit(1); } database = argv[1]; in_filename = argv[2]; out_filename = argv[3]; /* * set up the connection */ conn = PQsetdb(NULL, NULL, NULL, NULL, database); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } /* Set always-secure search path, so malicous users can't take control. */ res = PQexec(conn, "SELECT pg_catalog.set_config('search_path', '', false)"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "SET failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, "begin"); PQclear(res); printf("importing file \"%s\" ...\n", in_filename); /* lobjOid = importFile(conn, in_filename); */ lobjOid = lo_import(conn, in_filename); if (lobjOid == 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); else { printf("\tas large object %u.\n", lobjOid); printf("picking out bytes 1000-2000 of the large object\n"); pickout(conn, lobjOid, 1000, 1000); printf("overwriting bytes 1000-2000 of the large object with X's\n"); overwrite(conn, lobjOid, 1000, 1000); printf("exporting large object to file \"%s\" ...\n", out_filename); /* exportFile(conn, lobjOid, out_filename); */ if (lo_export(conn, lobjOid, out_filename) < 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); } res = PQexec(conn, "end"); PQclear(res); PQfinish(conn); return 0; }
int Pg_connect(ClientData cData, Tcl_Interp *interp, int argc, CONST84 char *argv[]) { const char *pghost = NULL; const char *pgtty = NULL; const char *pgport = NULL; const char *pgoptions = NULL; const char *dbName; int i; PGconn *conn; if (argc == 1) { Tcl_AppendResult(interp, "pg_connect: database name missing\n", 0); Tcl_AppendResult(interp, "pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]\n", 0); Tcl_AppendResult(interp, "pg_connect -conninfo conninfoString", 0); return TCL_ERROR; } if (!strcmp("-conninfo", argv[1])) { /* * Establish a connection using the new PQconnectdb() interface */ if (argc != 3) { Tcl_AppendResult(interp, "pg_connect: syntax error\n", 0); Tcl_AppendResult(interp, "pg_connect -conninfo conninfoString", 0); return TCL_ERROR; } conn = PQconnectdb(argv[2]); } else { /* * Establish a connection using the old PQsetdb() interface */ if (argc > 2) { /* parse for pg environment settings */ i = 2; while (i + 1 < argc) { if (strcmp(argv[i], "-host") == 0) { pghost = argv[i + 1]; i += 2; } else if (strcmp(argv[i], "-port") == 0) { pgport = argv[i + 1]; i += 2; } else if (strcmp(argv[i], "-tty") == 0) { pgtty = argv[i + 1]; i += 2; } else if (strcmp(argv[i], "-options") == 0) { pgoptions = argv[i + 1]; i += 2; } else { Tcl_AppendResult(interp, "Bad option to pg_connect: ", argv[i], 0); Tcl_AppendResult(interp, "\npg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]", 0); return TCL_ERROR; } } /* while */ if ((i % 2 != 0) || i != argc) { Tcl_AppendResult(interp, "wrong # of arguments to pg_connect: ", argv[i], 0); Tcl_AppendResult(interp, "\npg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]", 0); return TCL_ERROR; } } dbName = argv[1]; conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); } if (PQstatus(conn) == CONNECTION_OK) { PgSetConnectionId(interp, conn); return TCL_OK; } else { Tcl_AppendResult(interp, "Connection to database failed\n", PQerrorMessage(conn), 0); PQfinish(conn); return TCL_ERROR; } }
void main(int argc, char **argv) { int c; int errflag = 0; char *progname; char *debug_file; char *dbname; char *command; int exit_status = 0; char errbuf[ERROR_MSG_LENGTH]; char *username, usernamebuf[NAMEDATALEN + 1]; char *pghost = NULL; char *pgtty = NULL; char *pgoptions = NULL; char *pgport = NULL; int pgtracep = 0; /* * Processing command line arguments. * * h : sets the hostname. * p : sets the coom. port * t : sets the tty. * o : sets the other options. (see doc/libpq) * d : enable debugging mode. * q : run in quiet mode * Q : run in VERY quiet mode (no output except on errors) * c : monitor will run one POSTQUEL command and exit * * s : step mode (pauses after each command) * S : don't use semi colon as \g * * T : terse mode - no formatting * N : no attribute names - only columns of data * (these two options are useful in conjunction with the "-c" option * in scripts.) */ progname = *argv; Debugging = false; Verbose = true; Silent = false; /* prepend PGOPTION, if any */ argsetup(&argc, &argv); while ((c = getopt(argc, argv, "a:h:f:p:t:d:qsSTNQc:")) != EOF) { switch (c) { case 'a': fe_setauthsvc(optarg, errbuf); break; case 'h' : pghost = optarg; break; case 'f' : RunOneFile = optarg; break; case 'p' : pgport = optarg; break; case 't' : pgtty = optarg; break; case 'T' : TerseOutput = true; break; case 'N' : PrintAttNames = false; break; case 'd' : /* * When debugging is turned on, the debugging messages * will be sent to the specified debug file, which * can be a tty .. */ Debugging = true; debug_file = optarg; debug_port = fopen(debug_file,"w+"); if (debug_port == NULL) { fprintf(stderr,"Unable to open debug file %s \n", debug_file); exit(1); } pgtracep = 1; break; case 'q' : Verbose = false; break; case 's' : SingleStepMode = true; SemicolonIsGo = true; break; case 'S' : SemicolonIsGo = false; break; case 'Q' : Verbose = false; Silent = true; break; case 'c' : Verbose = false; Silent = true; RunOneCommand = true; command = optarg; break; case '?' : default : errflag++; break; } } if (errflag ) { fprintf(stderr, "usage: %s [options...] [dbname]\n", progname); fprintf(stderr, "\t-a authsvc\tset authentication service\n"); fprintf(stderr, "\t-c command\t\texecute one command\n"); fprintf(stderr, "\t-d debugfile\t\tdebugging output file\n"); fprintf(stderr, "\t-h host\t\t\tserver host name\n"); fprintf(stderr, "\t-f file\t\t\trun query from file\n"); fprintf(stderr, "\t-p port\t\t\tserver port number\n"); fprintf(stderr, "\t-q\t\t\tquiet output\n"); fprintf(stderr, "\t-t logfile\t\terror-logging tty\n"); fprintf(stderr, "\t-N\t\t\toutput without attribute names\n"); fprintf(stderr, "\t-Q\t\t\tREALLY quiet output\n"); fprintf(stderr, "\t-T\t\t\tterse output\n"); exit(2); } /* Determine our username (according to the authentication system, if * there is one). */ if ((username = fe_getauthname(errbuf)) == (char *) NULL) { fprintf(stderr, "%s: could not find a valid user name\n", progname); exit(2); } memset(usernamebuf, 0, sizeof(usernamebuf)); (void) strncpy(usernamebuf, username, NAMEDATALEN); username = usernamebuf; /* find database */ if (!(dbname = argv[optind]) && !(dbname = getenv("DATABASE")) && !(dbname = username)) { fprintf(stderr, "%s: no database name specified\n", progname); exit (2); } conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbname); if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr,"Connection to database '%s' failed.\n", dbname); fprintf(stderr,"%s",PQerrorMessage(conn)); exit(1); } if (pgtracep) PQtrace(conn,debug_port); /* print out welcome message and start up */ welcome(); init_tmon(); /* parse input */ if (RunOneCommand) { exit_status = handle_execution(command); } else if (RunOneFile) { bool oldVerbose; FILE *ifp; if ((ifp = fopen(RunOneFile, "r")) == NULL) { fprintf(stderr, "Cannot open %s\n", RunOneFile); } if (SingleStepMode) { oldVerbose = Verbose; Verbose = false; } do_input(ifp); fclose(ifp); if (SingleStepMode) Verbose = oldVerbose; } else { do_input(stdin); } handle_exit(exit_status); }
CONDITION TBL_OpenDB(const char *databaseName, TBL_HANDLE ** handle) { TBL_CONTEXT* tc; char *sn; char server_name[50]; char *tdb; char *ttb; PGconn *conn; PGresult *res; char* tableName = "none"; (*handle) = (void *) NULL; #ifdef CTN_USE_THREADS THR_ObtainMutex(FAC_TBL); #endif tc = G_ContextHead; while (tc != (TBL_CONTEXT *) NULL) { if ((strcmp(tc->databaseName, databaseName) == 0) && (strcmp(tc->tableName, tableName) == 0)) { tc->refCount++; (*handle) = (void *) tc; G_OpenFlag++; #ifdef CTN_USE_THREADS THR_ReleaseMutex(FAC_TBL); #endif return TBL_NORMAL; } tc = tc->next; } conn = PQsetdb(NULL, /* host */ NULL, /* port */ NULL, /* backend options */ NULL, /* debugging tty for backend */ databaseName); if (PQstatus(conn) == CONNECTION_BAD) { #ifdef CTN_USE_THREADS THR_ReleaseMutex(FAC_TBL); #endif return COND_PushCondition(TBL_ERROR(TBL_OPENFAILED), tableName); } /* We have to assume at this point that everything will be ok... */ if ((tc = (TBL_CONTEXT *) malloc(sizeof(TBL_CONTEXT))) == (TBL_CONTEXT *) NULL) { #ifdef CTN_USE_THREADS THR_ReleaseMutex(FAC_TBL); #endif return COND_PushCondition(TBL_ERROR(TBL_NOMEMORY), "TBL_Open"); } if ((tdb = (char *) malloc(strlen(databaseName) + 1)) == (char *) NULL) { free(tc); #ifdef CTN_USE_THREADS THR_ReleaseMutex(FAC_TBL); #endif return COND_PushCondition(TBL_ERROR(TBL_NOMEMORY), "TBL_Open"); } if ((ttb = (char *) malloc(strlen(tableName) + 1)) == (char *) NULL) { free(tc); free(tdb); #ifdef CTN_USE_THREADS THR_ReleaseMutex(FAC_TBL); #endif return COND_PushCondition(TBL_ERROR(TBL_NOMEMORY), "TBL_Open"); } strcpy(tdb, databaseName); strcpy(ttb, tableName); tc->databaseName = tdb; tc->tableName = ttb; tc->refCount = 1; tc->dbSpecific = conn; tc->next = G_ContextHead; G_ContextHead = tc; (*handle) = (void *) G_ContextHead; G_OpenFlag++; #ifdef CTN_USE_THREADS THR_ReleaseMutex(FAC_TBL); #endif return TBL_NORMAL; }