Skip to content

Commit

Permalink
core: sql_* add leading space to sql construct
Browse files Browse the repository at this point in the history
- fix ERROR: trailing junk after numeric literal at or near "123A"
  in sql_list.cc
- Add IF EXISTS in DROP TABLE statement
- unifomize the query in sql_upgrade.cc
- control all sql function for missing space

Signed-off-by: Bruno Friedmann <bruno.friedmann@bareos.com>
  • Loading branch information
bruno-at-bareos committed Jan 11, 2024
1 parent 115b3eb commit 762673d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
12 changes: 6 additions & 6 deletions core/src/cats/bvfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2009-2010 Free Software Foundation Europe e.V.
Copyright (C) 2016-2016 Planets Communications B.V.
Copyright (C) 2016-2023 Bareos GmbH & Co. KG
Copyright (C) 2016-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -674,7 +674,7 @@ bool Bvfs::DropRestoreList(char* output_table)
{
PoolMem query(PM_MESSAGE);
if (CheckTemp(output_table)) {
Mmsg(query, "DROP TABLE %s", output_table);
Mmsg(query, "DROP TABLE IF EXISTS %s", output_table);
db->SqlQuery(query.c_str());
return true;
}
Expand Down Expand Up @@ -704,10 +704,10 @@ bool Bvfs::compute_restore_list(char* fileid,
DbLocker _{db};

/* Cleanup old tables first */
Mmsg(query, "DROP TABLE btemp%s", output_table);
Mmsg(query, "DROP TABLE IF EXISTS btemp%s", output_table);
db->SqlQuery(query.c_str());

Mmsg(query, "DROP TABLE %s", output_table);
Mmsg(query, "DROP TABLE IF EXISTS %s", output_table);
db->SqlQuery(query.c_str());

Mmsg(query, "CREATE TABLE btemp%s AS ", output_table);
Expand All @@ -717,7 +717,7 @@ bool Bvfs::compute_restore_list(char* fileid,
Mmsg(tmp,
"SELECT Job.JobId, JobTDate, FileIndex, File.Name, "
"PathId, FileId "
"FROM File JOIN Job USING (JobId) WHERE FileId IN (%s)",
"FROM File JOIN Job USING (JobId) WHERE FileId IN (%s) ",
fileid);
PmStrcat(query, tmp.c_str());
}
Expand Down Expand Up @@ -834,7 +834,7 @@ bool Bvfs::compute_restore_list(char* fileid,
retval = true;

bail_out:
Mmsg(query, "DROP TABLE btemp%s", output_table);
Mmsg(query, "DROP TABLE IF EXISTS btemp%s", output_table);
db->SqlQuery(query.c_str());
return retval;
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/cats/sql_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -825,7 +825,7 @@ bool BareosDb::WriteBatchFileRecords(JobControlRecord* jcr)


bail_out:
SqlQuery("DROP TABLE batch");
SqlQuery("DROP TABLE IF EXISTS batch");
jcr->batch_started = false;
changes = 0;

Expand Down Expand Up @@ -1025,10 +1025,10 @@ bool BareosDb::CreateBaseFileAttributesRecord(JobControlRecord* jcr,
void BareosDb::CleanupBaseFile(JobControlRecord* jcr)
{
PoolMem buf(PM_MESSAGE);
Mmsg(buf, "DROP TABLE new_basefile%lld", (uint64_t)jcr->JobId);
Mmsg(buf, "DROP TABLE IF EXISTS new_basefile%lld", (uint64_t)jcr->JobId);
SqlQuery(buf.c_str());

Mmsg(buf, "DROP TABLE basefile%lld", (uint64_t)jcr->JobId);
Mmsg(buf, "DROP TABLE IF EXISTS basefile%lld", (uint64_t)jcr->JobId);
SqlQuery(buf.c_str());
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/cats/sql_get.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -1377,7 +1377,7 @@ bool BareosDb::AccurateGetJobids(JobControlRecord* jcr,
retval = true;

bail_out:
Mmsg(query, "DROP TABLE btemp3%s", jobid);
Mmsg(query, "DROP TABLE IF EXISTS btemp3%s", jobid);
SqlQuery(query.c_str());
return retval;
}
Expand Down
40 changes: 20 additions & 20 deletions core/src/cats/sql_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -179,7 +179,7 @@ void BareosDb::ListClientRecords(JobControlRecord* jcr,
} else {
Mmsg(cmd,
"SELECT ClientId,Name,FileRetention,JobRetention "
"FROM Client %s ORDER BY ClientId",
"FROM Client %s ORDER BY ClientId ",
clientfilter.c_str());
}

Expand Down Expand Up @@ -271,27 +271,27 @@ void BareosDb::ListJobmediaRecords(JobControlRecord* jcr,
"FirstIndex,LastIndex,StartFile,JobMedia.EndFile,StartBlock,"
"JobMedia.EndBlock "
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
"AND JobMedia.JobId=%s",
"AND JobMedia.JobId=%s ",
edit_int64(JobId, ed1));
} else {
Mmsg(cmd,
"SELECT JobMediaId,JobId,Media.MediaId,Media.VolumeName,"
"FirstIndex,LastIndex,StartFile,JobMedia.EndFile,StartBlock,"
"JobMedia.EndBlock "
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId");
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId ");
}

} else {
if (JobId > 0) { /* do by JobId */
Mmsg(cmd,
"SELECT JobId,Media.VolumeName,FirstIndex,LastIndex "
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
"AND JobMedia.JobId=%s",
"AND JobMedia.JobId=%s ",
edit_int64(JobId, ed1));
} else {
Mmsg(cmd,
"SELECT JobId,Media.VolumeName,FirstIndex,LastIndex "
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId");
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId ");
}
}
if (!QUERY_DB(jcr, cmd)) { return; }
Expand All @@ -318,13 +318,13 @@ void BareosDb::ListVolumesOfJobid(JobControlRecord* jcr,
Mmsg(cmd,
"SELECT JobMediaId,JobId,Media.MediaId,Media.VolumeName "
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
"AND JobMedia.JobId=%s",
"AND JobMedia.JobId=%s ",
edit_int64(JobId, ed1));
} else {
Mmsg(cmd,
"SELECT DISTINCT Media.VolumeName "
"FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId "
"AND JobMedia.JobId=%s",
"AND JobMedia.JobId=%s ",
edit_int64(JobId, ed1));
}
if (!QUERY_DB(jcr, cmd)) { return; }
Expand Down Expand Up @@ -357,7 +357,7 @@ void BareosDb::ListCopiesRecords(JobControlRecord* jcr,
"FROM Job "
"JOIN JobMedia USING (JobId) "
"JOIN Media USING (MediaId) "
"WHERE Job.Type = '%c' %s ORDER BY Job.PriorJobId DESC %s",
"WHERE Job.Type = '%c' %s ORDER BY Job.PriorJobId DESC %s ",
(char)JT_JOB_COPY, str_jobids.c_str(), range);

if (!QUERY_DB(jcr, cmd)) { return; }
Expand Down Expand Up @@ -399,7 +399,7 @@ void BareosDb::ListLogRecords(JobControlRecord* jcr,
"LEFT JOIN Client USING (ClientId) "
"WHERE Job.Type != 'C' "
"%s"
"ORDER BY Log.LogId DESC %s",
"ORDER BY Log.LogId DESC %s ",
client_filter.c_str(), range);
} else {
Mmsg(cmd,
Expand All @@ -411,8 +411,8 @@ void BareosDb::ListLogRecords(JobControlRecord* jcr,
"LEFT JOIN Client USING (ClientId) "
"WHERE Job.Type != 'C' "
"%s"
"ORDER BY Log.LogId DESC %s"
") AS sub ORDER BY LogId ASC",
"ORDER BY Log.LogId DESC %s "
") AS sub ORDER BY LogId ASC ",
client_filter.c_str(), range);
}

Expand Down Expand Up @@ -520,7 +520,7 @@ void BareosDb::ListJobRecords(JobControlRecord* jcr,
PoolMem temp(PM_MESSAGE), selection(PM_MESSAGE), criteria(PM_MESSAGE);

if (jr->JobId > 0) {
temp.bsprintf("AND Job.JobId=%s", edit_int64(jr->JobId, ed1));
temp.bsprintf("AND Job.JobId=%s ", edit_int64(jr->JobId, ed1));
PmStrcat(selection, temp.c_str());
}

Expand Down Expand Up @@ -654,7 +654,7 @@ void BareosDb::ListFilesForJob(JobControlRecord* jcr,
"ON (BaseFiles.FileId = File.FileId) "
"WHERE BaseFiles.JobId = %s"
") AS F, Path "
"WHERE Path.PathId=F.PathId",
"WHERE Path.PathId=F.PathId ",
edit_int64(jobid, ed1), ed1);

sendit->ArrayStart("filenames");
Expand All @@ -678,7 +678,7 @@ void BareosDb::ListBaseFilesForJob(JobControlRecord* jcr,
"FROM BaseFiles, File, Path "
"WHERE BaseFiles.JobId=%s AND BaseFiles.BaseJobId = File.JobId "
"AND BaseFiles.FileId = File.FileId "
"AND Path.PathId=File.PathId",
"AND Path.PathId=File.PathId ",
edit_int64(jobid, ed1));

sendit->ArrayStart("files");
Expand All @@ -704,7 +704,7 @@ void BareosDb::ListFilesets(JobControlRecord* jcr,
"CreateTime, FileSetText "
"FROM Job, FileSet "
"WHERE Job.FileSetId = FileSet.FileSetId "
"AND Job.Name='%s'%s",
"AND Job.Name='%s' %s",
esc, range);
} else if (jr->Job[0] != 0) {
EscapeString(jcr, esc, jr->Job, strlen(jr->Job));
Expand All @@ -713,27 +713,27 @@ void BareosDb::ListFilesets(JobControlRecord* jcr,
"CreateTime, FileSetText "
"FROM Job, FileSet "
"WHERE Job.FileSetId = FileSet.FileSetId "
"AND Job.Name='%s'%s",
"AND Job.Name='%s' %s",
esc, range);
} else if (jr->JobId != 0) {
Mmsg(cmd,
"SELECT DISTINCT FileSet.FileSetId AS FileSetId, FileSet, MD5, "
"CreateTime, FileSetText "
"FROM Job, FileSet "
"WHERE Job.FileSetId = FileSet.FileSetId "
"AND Job.JobId='%s'%s",
"AND Job.JobId='%s' %s",
edit_int64(jr->JobId, esc), range);
} else if (jr->FileSetId != 0) {
Mmsg(cmd,
"SELECT FileSetId, FileSet, MD5, CreateTime, FileSetText "
"FROM FileSet "
"WHERE FileSetId=%s",
"WHERE FileSetId=%s ",
edit_int64(jr->FileSetId, esc));
} else { /* all records */
Mmsg(cmd,
"SELECT DISTINCT FileSet.FileSetId AS FileSetId, FileSet, MD5, "
"CreateTime, FileSetText "
"FROM FileSet ORDER BY FileSetId ASC%s",
"FROM FileSet ORDER BY FileSetId ASC %s",
range);
}

Expand Down
20 changes: 10 additions & 10 deletions core/src/cats/sql_update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -303,8 +303,8 @@ bool BareosDb::UpdateMediaRecord(JobControlRecord* jcr, MediaDbRecord* mr)
ttime = mr->FirstWritten;
bstrutime(dt, sizeof(dt), ttime);
Mmsg(cmd,
"UPDATE Media SET FirstWritten='%s'"
" WHERE VolumeName='%s'",
"UPDATE Media SET FirstWritten='%s' "
"WHERE VolumeName='%s'",
dt, esc_medianame);
UPDATE_DB(jcr, cmd);
Dmsg1(400, "Firstwritten=%d\n", mr->FirstWritten);
Expand Down Expand Up @@ -341,8 +341,8 @@ bool BareosDb::UpdateMediaRecord(JobControlRecord* jcr, MediaDbRecord* mr)
"MaxVolJobs=%d,MaxVolFiles=%d,Enabled=%d,LocationId=%s,"
"ScratchPoolId=%s,RecyclePoolId=%s,RecycleCount=%d,Recycle=%d,"
"ActionOnPurge=%d,"
"MinBlocksize=%u,MaxBlocksize=%u"
" WHERE VolumeName='%s'",
"MinBlocksize=%u,MaxBlocksize=%u "
"WHERE VolumeName='%s'",
mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
mr->VolMounts, mr->VolErrors, mr->VolWrites,
edit_uint64(mr->MaxVolBytes, ed2), esc_status, mr->Slot, mr->InChanger,
Expand Down Expand Up @@ -383,8 +383,8 @@ bool BareosDb::UpdateMediaDefaults(JobControlRecord* jcr, MediaDbRecord* mr)
"UPDATE Media SET "
"ActionOnPurge=%d,Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
"MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,RecyclePoolId=%s,"
"MinBlocksize=%d,MaxBlocksize=%d"
" WHERE VolumeName='%s'",
"MinBlocksize=%d,MaxBlocksize=%d "
"WHERE VolumeName='%s'",
mr->ActionOnPurge, mr->Recycle, edit_uint64(mr->VolRetention, ed1),
edit_uint64(mr->VolUseDuration, ed2), mr->MaxVolJobs, mr->MaxVolFiles,
edit_uint64(mr->MaxVolBytes, ed3), edit_uint64(mr->RecyclePoolId, ed4),
Expand All @@ -394,8 +394,8 @@ bool BareosDb::UpdateMediaDefaults(JobControlRecord* jcr, MediaDbRecord* mr)
"UPDATE Media SET "
"ActionOnPurge=%d,Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
"MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,RecyclePoolId=%s,"
"MinBlocksize=%d,MaxBlocksize=%d"
" WHERE PoolId=%s",
"MinBlocksize=%d,MaxBlocksize=%d "
"WHERE PoolId=%s",
mr->ActionOnPurge, mr->Recycle, edit_uint64(mr->VolRetention, ed1),
edit_uint64(mr->VolUseDuration, ed2), mr->MaxVolJobs, mr->MaxVolFiles,
edit_uint64(mr->MaxVolBytes, ed3), edit_int64(mr->RecyclePoolId, ed4),
Expand Down Expand Up @@ -571,6 +571,6 @@ void BareosDb::UpgradeCopies(const char* jobids)
JT_COPY);
SqlQuery(query.c_str());

SqlQuery("DROP TABLE cpy_tmp");
SqlQuery("DROP TABLE IF EXISTS cpy_tmp");
}
#endif /* HAVE_POSTGRESQL */

0 comments on commit 762673d

Please sign in to comment.