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; }
LispObj * Lisp_PQoptions(LispBuiltin *builtin) /* pq-options connection */ { char *string; PGconn *conn; LispObj *connection; connection = ARGUMENT(0); if (!CHECKO(connection, PGconn_t)) LispDestroy("%s: cannot convert %s to PGconn*", STRFUN(builtin), STROBJ(connection)); conn = (PGconn*)(connection->data.opaque.data); string = PQoptions(conn); return (string ? STRING(string) : NIL); }
int main() { // próba po³±czenia PGconn *myconnection = PQconnectdb(""); // sprawdzamy status po³±czenia if(PQstatus(myconnection) == CONNECTION_OK) { printf("connection made\n"); // informacje o po³±czeniu printf("PGDBNAME = %s\n",PQdb(myconnection)); printf("PGUSER = %s\n",PQuser(myconnection)); printf("PGPASSWORD = %s\n",PQpass(myconnection)); printf("PGHOST = %s\n",PQhost(myconnection)); printf("PGPORT = %s\n",PQport(myconnection)); printf("OPTIONS = %s\n",PQoptions(myconnection)); } else printf("connection failed: %s\n", PQerrorMessage(myconnection)); // w razie utraty po³±czenia wywo³anie // PQreset(myconnection); // zamyka op³±czenie i nawi±zuje je raz jeszcze // z dotychczasowymi parametrami PQfinish(myconnection); return EXIT_SUCCESS; }
Con_Handle * RS_PostgreSQL_newConnection(Mgr_Handle * mgrHandle, s_object * con_params) { S_EVALUATOR RS_DBI_connection * con; RS_PostgreSQL_conParams *conParams; Con_Handle *conHandle; PGconn *my_connection; const char *user = NULL, *password = NULL, *host = NULL, *dbname = NULL, *port = NULL, *tty = NULL, *options = NULL; if (!is_validHandle(mgrHandle, MGR_HANDLE_TYPE)) { RS_DBI_errorMessage("invalid PostgreSQLManager", RS_DBI_ERROR); } user = CHR_EL(con_params, 0); password = CHR_EL(con_params, 1); host = CHR_EL(con_params, 2); dbname = CHR_EL(con_params, 3); port = CHR_EL(con_params, 4); tty = CHR_EL(con_params, 5); options = CHR_EL(con_params, 6); my_connection = PQsetdbLogin(host, port, options, tty, dbname, user, password); conParams = RS_postgresql_allocConParams(); /* save actual connection parameters */ conParams->user = RS_DBI_copyString(PQuser(my_connection)); conParams->password = RS_DBI_copyString(PQpass(my_connection)); { const char *tmphost = PQhost(my_connection); if (tmphost) { conParams->host = RS_DBI_copyString(tmphost); } else { conParams->host = RS_DBI_copyString(""); } } conParams->dbname = RS_DBI_copyString(PQdb(my_connection)); conParams->port = RS_DBI_copyString(PQport(my_connection)); conParams->tty = RS_DBI_copyString(PQtty(my_connection)); conParams->options = RS_DBI_copyString(PQoptions(my_connection)); if (PQstatus(my_connection) != CONNECTION_OK) { char buf[1000]; sprintf(buf, "could not connect %s@%s on dbname \"%s\"\n", PQuser(my_connection), host?host:"local", PQdb(my_connection)); PQfinish(my_connection); my_connection = NULL; RS_PostgreSQL_freeConParams(conParams); /*free BEFORE emitting err message that do not come back */ RS_DBI_errorMessage(buf, RS_DBI_ERROR); return R_NilValue; /* don't reach here as it goes back to R proc */ } PROTECT(conHandle = RS_DBI_allocConnection(mgrHandle, (Sint) 1)); /* The second argument (1) specifies the number of result sets allocated */ con = RS_DBI_getConnection(conHandle); if (my_connection && !con) { PQfinish(my_connection); my_connection = NULL; RS_PostgreSQL_freeConParams(conParams); conParams = (RS_PostgreSQL_conParams *) NULL; RS_DBI_errorMessage("could not alloc space for connection object", RS_DBI_ERROR); } if(con) { con->drvConnection = (void *) my_connection; con->conParams = (void *) conParams; } UNPROTECT(1); return conHandle; }
/* ML type : pgconn_ -> string */ EXTERNML value pq_options(value conn) { return copy_string(PQoptions(PGconn_val(conn))); }
int main() { // próba po³±czenia PGconn *myconnection = PQconnectdb("host=localhost port=5432 dbname=myDb user=mateuszek password=mateuszek"); //PGconn *myconnection = PQconnectdb(""); // sprawdzamy status po³±czenia if(PQstatus(myconnection) == CONNECTION_OK) { printf("connection made\n"); // informacje o po³±czeniu printf("PGDBNAME = %s\n",PQdb(myconnection)); printf("PGUSER = %s\n",PQuser(myconnection)); //printf("PGPASSWORD = %s\n",PQpass(myconnection)); printf("PGPASSWORD = ********\n"); printf("PGHOST = %s\n",PQhost(myconnection)); printf("PGPORT = %s\n",PQport(myconnection)); printf("OPTIONS = %s\n",PQoptions(myconnection)); } else{ printf("connection failed: %s\n", PQerrorMessage(myconnection)); // w razie utraty po³±czenia wywo³anie // PQreset(myconnection); // zamyka op³±czenie i nawi±zuje je raz jeszcze // z dotychczasowymi parametrami PQfinish(myconnection); return EXIT_SUCCESS; } int k=1; int choice; while (k==1){ printf("wpisz: \n"); printf("\t '0': aby zakonczyc\n"); printf("\t '1' : aby utworzyc tabele\n"); printf("\t '2' : aby usunac tabele\n"); printf("\t '3' : aby dodac rekord\n"); printf("\t '4' : aby edytowac rekord\n"); printf("\t '6' : aby wyswietlic wszystkie rekordy\n"); printf("\t '7' : wyszukaj pracownika po dacie urodzenia\n"); printf("\t '8' : wyszukaj pracownika po stanowisku i nazwisku\n"); scanf("\t%d",&choice); switch (choice){ case 1 : system("clear"); createTable(myconnection); break; case 2 : system("clear"); dropTable(myconnection); break; case 3 : system("clear"); addRecord(myconnection); break; case 4 : system("clear"); editRecord(myconnection); break; case 5 : system("clear"); deleteRecord(myconnection); break; case 6 : system("clear"); showAllRecords(myconnection); break; case 7 : system("clear"); searchByBirthDate(myconnection); break; case 8 : system("clear"); searchByPosAndSalary(myconnection); break; default: system("clear"); printf("Nieodpowiedni wybor ('%d').\n\n",choice); } } }
/************************************************************* *Returns the options used in the connection. *************************************************************/ char * dbi_options(DBI_conn *conn) { return(PQoptions(conn)); }