@@ -71,8 +71,6 @@ static int get_dbnames_list_to_restore(PGconn *conn,
71
71
SimpleStringList db_exclude_patterns );
72
72
static int get_dbname_oid_list_from_mfile (const char * dumpdirpath ,
73
73
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 );
76
74
77
75
int
78
76
main (int argc , char * * argv )
@@ -947,29 +945,26 @@ get_dbnames_list_to_restore(PGconn *conn,
947
945
db_cell ; db_cell = db_cell -> next )
948
946
{
949
947
bool skip_db_restore = false;
948
+ PQExpBuffer db_lit = createPQExpBuffer ();
949
+
950
+ appendStringLiteralConn (db_lit , db_cell -> str , conn );
950
951
951
952
for (SimpleStringListCell * pat_cell = db_exclude_patterns .head ; pat_cell ; pat_cell = pat_cell -> next )
952
953
{
953
954
/*
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
963
957
*/
964
958
if (pg_strcasecmp (db_cell -> str , pat_cell -> val ) == 0 )
965
959
skip_db_restore = true;
960
+ /* Otherwise, try a pattern match if there is a connection */
966
961
else if (conn )
967
962
{
968
963
int dotcnt ;
969
964
970
965
appendPQExpBufferStr (query , "SELECT 1 " );
971
966
processSQLNamePattern (conn , query , pat_cell -> val , false,
972
- false, NULL , quote_literal_cstr ( db_cell -> str ) ,
967
+ false, NULL , db_lit -> data ,
973
968
NULL , NULL , NULL , & dotcnt );
974
969
975
970
if (dotcnt > 0 )
@@ -996,7 +991,10 @@ get_dbnames_list_to_restore(PGconn *conn,
996
991
break ;
997
992
}
998
993
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
+ */
1000
998
if (skip_db_restore )
1001
999
{
1002
1000
pg_log_info ("excluding database \"%s\"" , db_cell -> str );
@@ -1110,18 +1108,15 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
1110
1108
1111
1109
num_total_db = get_dbname_oid_list_from_mfile (dumpdirpath , & dbname_oid_list );
1112
1110
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 */
1117
1112
if (dbname_oid_list .head == NULL )
1118
1113
return process_global_sql_commands (conn , dumpdirpath , opts -> filename );
1119
1114
1120
1115
pg_log_info ("found total %d database names in map.dat file" , num_total_db );
1121
1116
1122
1117
if (!conn )
1123
1118
{
1124
- pg_log_info ("trying to connect database \"postgres\" to dump into out file " );
1119
+ pg_log_info ("trying to connect database \"postgres\"" );
1125
1120
1126
1121
conn = ConnectDatabase ("postgres" , NULL , opts -> cparams .pghost ,
1127
1122
opts -> cparams .pgport , opts -> cparams .username , TRI_DEFAULT ,
@@ -1130,7 +1125,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
1130
1125
/* Try with template1. */
1131
1126
if (!conn )
1132
1127
{
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\"" );
1134
1129
1135
1130
conn = ConnectDatabase ("template1" , NULL , opts -> cparams .pghost ,
1136
1131
opts -> cparams .pgport , opts -> cparams .username , TRI_DEFAULT ,
@@ -1139,7 +1134,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
1139
1134
}
1140
1135
1141
1136
/*
1142
- * processing pg_restore --exclude-database=PATTERN/NAME if no connection.
1137
+ * filter the db list according to the exclude patterns
1143
1138
*/
1144
1139
num_db_restore = get_dbnames_list_to_restore (conn , & dbname_oid_list ,
1145
1140
db_exclude_patterns );
@@ -1158,7 +1153,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
1158
1153
return n_errors_total ;
1159
1154
}
1160
1155
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 );
1162
1157
1163
1158
/*
1164
1159
* 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,
1179
1174
1180
1175
/*
1181
1176
* 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)
1183
1178
*/
1184
1179
if (opts -> cparams .override_dbname )
1185
1180
{
@@ -1241,7 +1236,7 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
1241
1236
opts -> dumpSchema = dumpSchema ;
1242
1237
opts -> dumpStatistics = dumpStatistics ;
1243
1238
1244
- /* Restore single database. */
1239
+ /* Restore the single database. */
1245
1240
n_errors = restore_one_database (subdirpath , opts , numWorkers , true, count );
1246
1241
1247
1242
/* Print a summary of ignored errors during single database restore. */
@@ -1266,12 +1261,12 @@ restore_all_databases(PGconn *conn, const char *dumpdirpath,
1266
1261
/*
1267
1262
* process_global_sql_commands
1268
1263
*
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.
1273
1268
*
1274
- * returns the number of errors while processing global.dat
1269
+ * Returns the number of errors while processing global.dat
1275
1270
*/
1276
1271
static int
1277
1272
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
1346
1341
/*
1347
1342
* copy_or_print_global_file
1348
1343
*
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,
1350
1345
* then print commands to stdout.
1351
1346
*/
1352
1347
static void
@@ -1381,57 +1376,3 @@ copy_or_print_global_file(const char *outfile, FILE *pfile)
1381
1376
if (strcmp (outfile , "-" ) != 0 )
1382
1377
fclose (OPF );
1383
1378
}
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