Skip to content

Commit 5db3bf7

Browse files
committed
Clean up from commit 1495eff
Fix some comments, and remove the hacky way of quoting database names in favor of appendStringLiteralConn.
1 parent 64fba9c commit 5db3bf7

File tree

1 file changed

+24
-83
lines changed

1 file changed

+24
-83
lines changed

src/bin/pg_dump/pg_restore.c

Lines changed: 24 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ static int get_dbnames_list_to_restore(PGconn *conn,
7171
SimpleStringList db_exclude_patterns);
7272
static int get_dbname_oid_list_from_mfile(const char *dumpdirpath,
7373
SimpleOidStringList *dbname_oid_list);
74-
static size_t quote_literal_internal(char *dst, const char *src, size_t len);
75-
static char *quote_literal_cstr(const char *rawstr);
7674

7775
int
7876
main(int argc, char **argv)
@@ -947,29 +945,26 @@ get_dbnames_list_to_restore(PGconn *conn,
947945
db_cell; db_cell = db_cell->next)
948946
{
949947
bool skip_db_restore = false;
948+
PQExpBuffer db_lit = createPQExpBuffer();
949+
950+
appendStringLiteralConn(db_lit, db_cell->str, conn);
950951

951952
for (SimpleStringListCell *pat_cell = db_exclude_patterns.head; pat_cell; pat_cell = pat_cell->next)
952953
{
953954
/*
954-
* the construct pattern matching query: SELECT 1 WHERE XXX
955-
* OPERATOR(pg_catalog.~) '^(PATTERN)$' COLLATE pg_catalog.default
956-
*
957-
* XXX represents the string literal database name derived from
958-
* the dbname_oid_list, which is initially extracted from the
959-
* map.dat file located in the backup directory. that's why we
960-
* need quote_literal_cstr.
961-
*
962-
* If no db connection, then consider PATTERN as NAME.
955+
* If there is an exact match then we don't need to try a pattern
956+
* match
963957
*/
964958
if (pg_strcasecmp(db_cell->str, pat_cell->val) == 0)
965959
skip_db_restore = true;
960+
/* Otherwise, try a pattern match if there is a connection */
966961
else if (conn)
967962
{
968963
int dotcnt;
969964

970965
appendPQExpBufferStr(query, "SELECT 1 ");
971966
processSQLNamePattern(conn, query, pat_cell->val, false,
972-
false, NULL, quote_literal_cstr(db_cell->str),
967+
false, NULL, db_lit->data,
973968
NULL, NULL, NULL, &dotcnt);
974969

975970
if (dotcnt > 0)
@@ -996,7 +991,10 @@ get_dbnames_list_to_restore(PGconn *conn,
996991
break;
997992
}
998993

999-
/* Increment count if database needs to be restored. */
994+
/*
995+
* Mark db to be skipped or increment the counter of dbs to be
996+
* restored
997+
*/
1000998
if (skip_db_restore)
1001999
{
10021000
pg_log_info("excluding database \"%s\"", db_cell->str);
@@ -1110,18 +1108,15 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
11101108

11111109
num_total_db = get_dbname_oid_list_from_mfile(dumpdirpath, &dbname_oid_list);
11121110

1113-
/*
1114-
* If map.dat has no entry, return from here after processing global.dat
1115-
* file.
1116-
*/
1111+
/* If map.dat has no entry, return after processing global.dat */
11171112
if (dbname_oid_list.head == NULL)
11181113
return process_global_sql_commands(conn, dumpdirpath, opts->filename);
11191114

11201115
pg_log_info("found total %d database names in map.dat file", num_total_db);
11211116

11221117
if (!conn)
11231118
{
1124-
pg_log_info("trying to connect database \"postgres\" to dump into out file");
1119+
pg_log_info("trying to connect database \"postgres\"");
11251120

11261121
conn = ConnectDatabase("postgres", NULL, opts->cparams.pghost,
11271122
opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
@@ -1130,7 +1125,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
11301125
/* Try with template1. */
11311126
if (!conn)
11321127
{
1133-
pg_log_info("trying to connect database \"template1\" as failed to connect to database \"postgres\" to dump into out file");
1128+
pg_log_info("trying to connect database \"template1\"");
11341129

11351130
conn = ConnectDatabase("template1", NULL, opts->cparams.pghost,
11361131
opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
@@ -1139,7 +1134,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
11391134
}
11401135

11411136
/*
1142-
* processing pg_restore --exclude-database=PATTERN/NAME if no connection.
1137+
* filter the db list according to the exclude patterns
11431138
*/
11441139
num_db_restore = get_dbnames_list_to_restore(conn, &dbname_oid_list,
11451140
db_exclude_patterns);
@@ -1158,7 +1153,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
11581153
return n_errors_total;
11591154
}
11601155

1161-
pg_log_info("needs to restore %d databases out of %d databases", num_db_restore, num_total_db);
1156+
pg_log_info("need to restore %d databases out of %d databases", num_db_restore, num_total_db);
11621157

11631158
/*
11641159
* Till now, we made a list of databases, those needs to be restored after
@@ -1179,7 +1174,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
11791174

11801175
/*
11811176
* We need to reset override_dbname so that objects can be restored
1182-
* into already created database. (used with -d/--dbname option)
1177+
* into an already created database. (used with -d/--dbname option)
11831178
*/
11841179
if (opts->cparams.override_dbname)
11851180
{
@@ -1241,7 +1236,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
12411236
opts->dumpSchema = dumpSchema;
12421237
opts->dumpStatistics = dumpStatistics;
12431238

1244-
/* Restore single database. */
1239+
/* Restore the single database. */
12451240
n_errors = restore_one_database(subdirpath, opts, numWorkers, true, count);
12461241

12471242
/* Print a summary of ignored errors during single database restore. */
@@ -1266,12 +1261,12 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
12661261
/*
12671262
* process_global_sql_commands
12681263
*
1269-
* This will open global.dat file and will execute all global sql commands one
1270-
* by one statement.
1271-
* Semicolon is considered as statement terminator. If outfile is passed, then
1272-
* this will copy all sql commands into outfile rather then executing them.
1264+
* Open global.dat and execute or copy the sql commands one by one.
1265+
*
1266+
* If outfile is not NULL, copy all sql commands into outfile rather than
1267+
* executing them.
12731268
*
1274-
* returns the number of errors while processing global.dat
1269+
* Returns the number of errors while processing global.dat
12751270
*/
12761271
static int
12771272
process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *outfile)
@@ -1346,7 +1341,7 @@ process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *o
13461341
/*
13471342
* copy_or_print_global_file
13481343
*
1349-
* This will copy global.dat file into the output file. If "-" is used as outfile,
1344+
* Copy global.dat into the output file. If "-" is used as outfile,
13501345
* then print commands to stdout.
13511346
*/
13521347
static void
@@ -1381,57 +1376,3 @@ copy_or_print_global_file(const char *outfile, FILE *pfile)
13811376
if (strcmp(outfile, "-") != 0)
13821377
fclose(OPF);
13831378
}
1384-
1385-
/*
1386-
* quote_literal_internal
1387-
*/
1388-
static size_t
1389-
quote_literal_internal(char *dst, const char *src, size_t len)
1390-
{
1391-
const char *s;
1392-
char *savedst = dst;
1393-
1394-
for (s = src; s < src + len; s++)
1395-
{
1396-
if (*s == '\\')
1397-
{
1398-
*dst++ = ESCAPE_STRING_SYNTAX;
1399-
break;
1400-
}
1401-
}
1402-
1403-
*dst++ = '\'';
1404-
while (len-- > 0)
1405-
{
1406-
if (SQL_STR_DOUBLE(*src, true))
1407-
*dst++ = *src;
1408-
*dst++ = *src++;
1409-
}
1410-
*dst++ = '\'';
1411-
1412-
return dst - savedst;
1413-
}
1414-
1415-
/*
1416-
* quote_literal_cstr
1417-
*
1418-
* returns a properly quoted literal
1419-
* copied from src/backend/utils/adt/quote.c
1420-
*/
1421-
static char *
1422-
quote_literal_cstr(const char *rawstr)
1423-
{
1424-
char *result;
1425-
int len;
1426-
int newlen;
1427-
1428-
len = strlen(rawstr);
1429-
1430-
/* We make a worst-case result area; wasting a little space is OK */
1431-
result = pg_malloc(len * 2 + 3 + 1);
1432-
1433-
newlen = quote_literal_internal(result, rawstr, len);
1434-
result[newlen] = '\0';
1435-
1436-
return result;
1437-
}

0 commit comments

Comments
 (0)