@@ -859,22 +859,16 @@ parse_pair(const char buffer[], char key[], char value[])
859
859
* password is for, if one has been explicitly specified.
860
860
* Set malloc'd string to the global variable 'password'.
861
861
*/
862
- static void
862
+ static char *
863
863
prompt_for_password (const char * username )
864
864
{
865
- if (password )
866
- {
867
- free (password );
868
- password = NULL ;
869
- }
870
-
871
865
if (username == NULL )
872
- password = simple_prompt ("Password: " , 100 , false);
866
+ return simple_prompt ("Password: " , 100 , false);
873
867
else
874
868
{
875
869
char message [256 ];
876
870
snprintf (message , lengthof (message ), "Password for user %s: " , username );
877
- password = simple_prompt (message , 100 , false);
871
+ return simple_prompt (message , 100 , false);
878
872
}
879
873
}
880
874
#endif
@@ -887,32 +881,53 @@ PQconnectionNeedsPassword(PGconn *conn)
887
881
}
888
882
#endif
889
883
890
- PGconn *
891
- pgut_connect (int elevel )
884
+ static PGconn *
885
+ do_connect (int elevel ,
886
+ const char * my_host ,
887
+ const char * my_port ,
888
+ const char * my_dbname ,
889
+ const char * my_username ,
890
+ const char * my_password ,
891
+ char * * new_password )
892
892
{
893
- PGconn * conn ;
893
+ char * passwd ;
894
894
895
- if (interrupted && !in_cleanup )
896
- elog (ERROR_INTERRUPTED , "interrupted" );
895
+ CHECK_FOR_INTERRUPTS ();
896
+
897
+ if (new_password )
898
+ * new_password = NULL ;
897
899
898
900
#ifndef PGUT_NO_PROMPT
899
901
if (prompt_password == YES )
900
- prompt_for_password (username );
902
+ passwd = prompt_for_password (my_username );
903
+ else
901
904
#endif
905
+ passwd = (char * ) my_password ;
902
906
903
907
/* Start the connection. Loop until we have a password if requested by backend. */
904
908
for (;;)
905
909
{
906
- conn = PQsetdbLogin (host , port , NULL , NULL , dbname , username , password );
910
+ PGconn * conn ;
911
+
912
+ conn = PQsetdbLogin (my_host , my_port , NULL , NULL , my_dbname , my_username , passwd );
907
913
908
914
if (PQstatus (conn ) == CONNECTION_OK )
915
+ {
916
+ if (new_password )
917
+ * new_password = passwd ;
918
+ else
919
+ free (passwd );
909
920
return conn ;
921
+ }
922
+
923
+ if (passwd != my_password )
924
+ free (passwd );
910
925
911
926
#ifndef PGUT_NO_PROMPT
912
927
if (conn && PQconnectionNeedsPassword (conn ) && prompt_password != NO )
913
928
{
914
929
PQfinish (conn );
915
- prompt_for_password (username );
930
+ passwd = prompt_for_password (username );
916
931
continue ;
917
932
}
918
933
#endif
@@ -923,6 +938,74 @@ pgut_connect(int elevel)
923
938
}
924
939
}
925
940
941
+ PGconn *
942
+ pgut_connect (int elevel )
943
+ {
944
+ PGconn * conn ;
945
+ char * new_password ;
946
+
947
+ conn = do_connect (elevel , host , port , dbname , username , password , & new_password );
948
+
949
+ /* update password if a new one is supplied */
950
+ if (password != new_password )
951
+ {
952
+ free (password );
953
+ password = new_password ;
954
+ }
955
+
956
+ return conn ;
957
+ }
958
+
959
+ PGconn *
960
+ pgut_connectdb (const char * conninfo , int elevel )
961
+ {
962
+ PGconn * conn ;
963
+ const char * my_host = NULL ;
964
+ const char * my_port = NULL ;
965
+ const char * my_dbname = NULL ;
966
+ const char * my_username = NULL ;
967
+ const char * my_password = NULL ;
968
+ PQconninfoOption * options ;
969
+ char * message = NULL ;
970
+
971
+ options = PQconninfoParse (conninfo , & message );
972
+ if (message != NULL )
973
+ {
974
+ elog (elevel , "%s" , message );
975
+ PQfreemem (message );
976
+ return NULL ;
977
+ }
978
+ else if (options )
979
+ {
980
+ PQconninfoOption * option ;
981
+
982
+ for (option = options ; option -> keyword != NULL ; option ++ )
983
+ {
984
+ if (!option -> val || !option -> val [0 ])
985
+ continue ;
986
+ if (strcmp (option -> keyword , "host" ) == 0 )
987
+ my_host = option -> val ;
988
+ else if (strcmp (option -> keyword , "port" ) == 0 )
989
+ my_port = option -> val ;
990
+ else if (strcmp (option -> keyword , "dbname" ) == 0 )
991
+ my_dbname = option -> val ;
992
+ else if (strcmp (option -> keyword , "user" ) == 0 )
993
+ my_username = option -> val ;
994
+ else if (strcmp (option -> keyword , "password" ) == 0 )
995
+ my_password = option -> val ;
996
+ else
997
+ elog (WARNING , "unsupported connection option: %s = %s" ,
998
+ option -> keyword , option -> val );
999
+ }
1000
+ }
1001
+
1002
+ conn = do_connect (elevel , my_host , my_port , my_dbname , my_username , my_password , NULL );
1003
+
1004
+ PQconninfoFree (options );
1005
+
1006
+ return conn ;
1007
+ }
1008
+
926
1009
void
927
1010
pgut_disconnect (PGconn * conn )
928
1011
{
@@ -965,8 +1048,7 @@ pgut_execute(PGconn* conn, const char *query, int nParams, const char **params,
965
1048
{
966
1049
PGresult * res ;
967
1050
968
- if (interrupted && !in_cleanup )
969
- elog (ERROR_INTERRUPTED , "interrupted" );
1051
+ CHECK_FOR_INTERRUPTS ();
970
1052
971
1053
/* write query to elog if debug */
972
1054
if (debug )
@@ -1009,19 +1091,25 @@ pgut_execute(PGconn* conn, const char *query, int nParams, const char **params,
1009
1091
return res ;
1010
1092
}
1011
1093
1012
- void
1094
+ ExecStatusType
1013
1095
pgut_command (PGconn * conn , const char * query , int nParams , const char * * params , int elevel )
1014
1096
{
1015
- PQclear (pgut_execute (conn , query , nParams , params , elevel ));
1097
+ PGresult * res ;
1098
+ ExecStatusType code ;
1099
+
1100
+ res = pgut_execute (conn , query , nParams , params , elevel );
1101
+ code = PQresultStatus (res );
1102
+ PQclear (res );
1103
+
1104
+ return code ;
1016
1105
}
1017
1106
1018
1107
bool
1019
1108
pgut_send (PGconn * conn , const char * query , int nParams , const char * * params , int elevel )
1020
1109
{
1021
1110
int res ;
1022
1111
1023
- if (interrupted && !in_cleanup )
1024
- elog (ERROR_INTERRUPTED , "interrupted" );
1112
+ CHECK_FOR_INTERRUPTS ();
1025
1113
1026
1114
/* write query to elog if debug */
1027
1115
if (debug )
@@ -1135,6 +1223,16 @@ command(const char *query, int nParams, const char **params)
1135
1223
PQclear (execute (query , nParams , params ));
1136
1224
}
1137
1225
1226
+ /*
1227
+ * CHECK_FOR_INTERRUPTS - Ctrl+C pressed?
1228
+ */
1229
+ void
1230
+ CHECK_FOR_INTERRUPTS (void )
1231
+ {
1232
+ if (interrupted && !in_cleanup )
1233
+ elog (ERROR_INTERRUPTED , "interrupted" );
1234
+ }
1235
+
1138
1236
/*
1139
1237
* elog - log to stderr and exit if ERROR or FATAL
1140
1238
*/
@@ -1682,9 +1780,8 @@ wait_for_sockets(int nfds, fd_set *fds, struct timeval *timeout)
1682
1780
i = select (nfds , fds , NULL , NULL , timeout );
1683
1781
if (i < 0 )
1684
1782
{
1685
- if (interrupted )
1686
- elog (ERROR_INTERRUPTED , "interrupted" );
1687
- else if (errno != EINTR )
1783
+ CHECK_FOR_INTERRUPTS ();
1784
+ if (errno != EINTR )
1688
1785
elog (ERROR_SYSTEM , "select failed: %s" , strerror (errno ));
1689
1786
}
1690
1787
else
0 commit comments