Skip to content

Commit 8392b94

Browse files
committed
version 1.0.8.
1 parent a7a42b7 commit 8392b94

File tree

4 files changed

+130
-29
lines changed

4 files changed

+130
-29
lines changed

bin/pg_reorg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @brief Client Modules
99
*/
1010

11-
const char *PROGRAM_VERSION = "1.0.7";
11+
const char *PROGRAM_VERSION = "1.0.8";
1212
const char *PROGRAM_URL = "http://reorg.projects.postgresql.org/";
1313
const char *PROGRAM_EMAIL = "reorg-general@lists.pgfoundry.org";
1414

bin/pgut/pgut.c

Lines changed: 123 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -859,22 +859,16 @@ parse_pair(const char buffer[], char key[], char value[])
859859
* password is for, if one has been explicitly specified.
860860
* Set malloc'd string to the global variable 'password'.
861861
*/
862-
static void
862+
static char *
863863
prompt_for_password(const char *username)
864864
{
865-
if (password)
866-
{
867-
free(password);
868-
password = NULL;
869-
}
870-
871865
if (username == NULL)
872-
password = simple_prompt("Password: ", 100, false);
866+
return simple_prompt("Password: ", 100, false);
873867
else
874868
{
875869
char message[256];
876870
snprintf(message, lengthof(message), "Password for user %s: ", username);
877-
password = simple_prompt(message, 100, false);
871+
return simple_prompt(message, 100, false);
878872
}
879873
}
880874
#endif
@@ -887,32 +881,53 @@ PQconnectionNeedsPassword(PGconn *conn)
887881
}
888882
#endif
889883

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)
892892
{
893-
PGconn *conn;
893+
char *passwd;
894894

895-
if (interrupted && !in_cleanup)
896-
elog(ERROR_INTERRUPTED, "interrupted");
895+
CHECK_FOR_INTERRUPTS();
896+
897+
if (new_password)
898+
*new_password = NULL;
897899

898900
#ifndef PGUT_NO_PROMPT
899901
if (prompt_password == YES)
900-
prompt_for_password(username);
902+
passwd = prompt_for_password(my_username);
903+
else
901904
#endif
905+
passwd = (char *) my_password;
902906

903907
/* Start the connection. Loop until we have a password if requested by backend. */
904908
for (;;)
905909
{
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);
907913

908914
if (PQstatus(conn) == CONNECTION_OK)
915+
{
916+
if (new_password)
917+
*new_password = passwd;
918+
else
919+
free(passwd);
909920
return conn;
921+
}
922+
923+
if (passwd != my_password)
924+
free(passwd);
910925

911926
#ifndef PGUT_NO_PROMPT
912927
if (conn && PQconnectionNeedsPassword(conn) && prompt_password != NO)
913928
{
914929
PQfinish(conn);
915-
prompt_for_password(username);
930+
passwd = prompt_for_password(username);
916931
continue;
917932
}
918933
#endif
@@ -923,6 +938,74 @@ pgut_connect(int elevel)
923938
}
924939
}
925940

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+
9261009
void
9271010
pgut_disconnect(PGconn *conn)
9281011
{
@@ -965,8 +1048,7 @@ pgut_execute(PGconn* conn, const char *query, int nParams, const char **params,
9651048
{
9661049
PGresult *res;
9671050

968-
if (interrupted && !in_cleanup)
969-
elog(ERROR_INTERRUPTED, "interrupted");
1051+
CHECK_FOR_INTERRUPTS();
9701052

9711053
/* write query to elog if debug */
9721054
if (debug)
@@ -1009,19 +1091,25 @@ pgut_execute(PGconn* conn, const char *query, int nParams, const char **params,
10091091
return res;
10101092
}
10111093

1012-
void
1094+
ExecStatusType
10131095
pgut_command(PGconn* conn, const char *query, int nParams, const char **params, int elevel)
10141096
{
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;
10161105
}
10171106

10181107
bool
10191108
pgut_send(PGconn* conn, const char *query, int nParams, const char **params, int elevel)
10201109
{
10211110
int res;
10221111

1023-
if (interrupted && !in_cleanup)
1024-
elog(ERROR_INTERRUPTED, "interrupted");
1112+
CHECK_FOR_INTERRUPTS();
10251113

10261114
/* write query to elog if debug */
10271115
if (debug)
@@ -1135,6 +1223,16 @@ command(const char *query, int nParams, const char **params)
11351223
PQclear(execute(query, nParams, params));
11361224
}
11371225

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+
11381236
/*
11391237
* elog - log to stderr and exit if ERROR or FATAL
11401238
*/
@@ -1682,9 +1780,8 @@ wait_for_sockets(int nfds, fd_set *fds, struct timeval *timeout)
16821780
i = select(nfds, fds, NULL, NULL, timeout);
16831781
if (i < 0)
16841782
{
1685-
if (interrupted)
1686-
elog(ERROR_INTERRUPTED, "interrupted");
1687-
else if (errno != EINTR)
1783+
CHECK_FOR_INTERRUPTS();
1784+
if (errno != EINTR)
16881785
elog(ERROR_SYSTEM, "select failed: %s", strerror(errno));
16891786
}
16901787
else

bin/pgut/pgut.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ extern void pgut_atexit_pop(pgut_atexit_callback callback, void *userdata);
114114
* Database connections
115115
*/
116116
extern PGconn *pgut_connect(int elevel);
117+
extern PGconn *pgut_connectdb(const char *conninfo, int elevel);
117118
extern void pgut_disconnect(PGconn *conn);
118119
extern PGresult *pgut_execute(PGconn* conn, const char *query, int nParams, const char **params, int elevel);
119-
extern void pgut_command(PGconn* conn, const char *query, int nParams, const char **params, int elevel);
120+
extern ExecStatusType pgut_command(PGconn* conn, const char *query, int nParams, const char **params, int elevel);
120121
extern bool pgut_send(PGconn* conn, const char *query, int nParams, const char **params, int elevel);
121122
extern int pgut_wait(int num, PGconn *connections[], struct timeval *timeout);
122123

@@ -169,6 +170,9 @@ extern void
169170
elog(int elevel, const char *fmt, ...)
170171
__attribute__((format(printf, 2, 3)));
171172

173+
#undef CHECK_FOR_INTERRUPTS
174+
extern void CHECK_FOR_INTERRUPTS(void);
175+
172176
/*
173177
* Assert
174178
*/

lib/reorg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void RenameRelationInternal(Oid myrelid, const char *newrelname, Oid name
7575
Datum
7676
reorg_version(PG_FUNCTION_ARGS)
7777
{
78-
return CStringGetTextDatum("pg_reorg 1.0.7");
78+
return CStringGetTextDatum("pg_reorg 1.0.8");
7979
}
8080

8181
/**

0 commit comments

Comments
 (0)