2 Bacula® - The Network Backup Solution
4 Copyright (C) 2002-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * This file contains all the SQL commands that are either issued by the
30 * Director or which are database backend specific.
32 * Kern Sibbald, July MMII
35 * Note, PostgreSQL imposes some constraints on using DISTINCT and GROUP BY
36 * for example, the following is illegal in PostgreSQL:
37 * SELECT DISTINCT JobId FROM temp ORDER BY StartTime ASC;
38 * because all the ORDER BY expressions must appear in the SELECT list!
44 const char *cleanup_created_job =
45 "UPDATE Job SET JobStatus='f', StartTime=SchedTime, EndTime=SchedTime "
46 "WHERE JobStatus = 'C'";
47 const char *cleanup_running_job =
48 "UPDATE Job SET JobStatus='f', EndTime=StartTime WHERE JobStatus = 'R'";
50 /* For sql_update.c db_update_stats */
51 const char *fill_jobhisto =
52 "INSERT INTO JobHisto ("
53 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
54 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
55 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
56 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
57 "PurgedFiles, HasBase, Reviewed, Comment ) "
59 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
60 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
61 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
62 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
63 "PurgedFiles, HasBase, Reviewed, Comment "
65 "WHERE JobStatus IN ('T','W','f','A','E') "
67 "(SELECT JobHisto.JobId "
68 "FROM JobHisto WHERE JobHisto.Jobid=Job.JobId) "
72 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
74 /* For ua_dotcmds.c */
75 const char *client_backups =
76 "SELECT DISTINCT Job.JobId,Client.Name as Client,Level,StartTime,"
77 "JobFiles,JobBytes,VolumeName,MediaType,FileSet,Media.Enabled as Enabled"
78 " FROM Client,Job,JobMedia,Media,FileSet"
79 " WHERE Client.Name='%s'"
81 " AND Client.ClientId=Job.ClientId"
82 " AND JobStatus IN ('T','W') AND Type='B'"
83 " AND JobMedia.JobId=Job.JobId AND JobMedia.MediaId=Media.MediaId"
84 " AND Job.FileSetId=FileSet.FileSetId"
85 " ORDER BY Job.StartTime";
88 /* ====== ua_prune.c */
90 const char *del_File = "DELETE FROM File WHERE JobId=%s";
91 const char *upd_Purged = "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s";
92 const char *cnt_DelCand = "SELECT count(*) FROM DelCandidates";
93 const char *del_Job = "DELETE FROM Job WHERE JobId=%s";
94 const char *del_JobMedia = "DELETE FROM JobMedia WHERE JobId=%s";
95 const char *cnt_JobMedia = "SELECT count(*) FROM JobMedia WHERE MediaId=%s";
97 const char *sel_JobMedia =
98 "SELECT DISTINCT JobMedia.JobId FROM JobMedia,Job "
99 "WHERE MediaId=%s AND Job.JobId=JobMedia.JobId "
100 "AND Job.JobTDate<%s";
102 /* Count Select JobIds for File deletion */
103 const char *count_select_job =
104 "SELECT count(*) from Job "
110 /* Select JobIds for File deletion. */
111 const char *select_job =
112 "SELECT DISTINCT JobId from Job "
117 /* Delete temp tables and indexes */
118 const char *drop_deltabs[] = {
119 "DROP TABLE DelCandidates",
122 const char *create_delindex = "CREATE INDEX DelInx1 ON DelCandidates (JobId)";
124 /* ======= ua_restore.c */
125 const char *uar_count_files =
126 "SELECT JobFiles FROM Job WHERE JobId=%s";
128 /* List last 20 Jobs */
129 const char *uar_list_jobs =
130 "SELECT JobId,Client.Name as Client,StartTime,Level as "
131 "JobLevel,JobFiles,JobBytes "
132 "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus IN ('T','W') "
133 "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
135 const char *uar_print_jobs =
136 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
137 " FROM Job JOIN JobMedia USING (JobId) JOIN Media USING (MediaId) "
138 " WHERE JobId IN (%s) "
139 " ORDER BY StartTime ASC";
142 * Find all files for a particular JobId and insert them into
143 * the tree during a restore.
145 const char *uar_sel_files =
146 "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
147 "FROM File,Filename,Path "
148 "WHERE File.JobId IN (%s) AND Filename.FilenameId=File.FilenameId "
149 "AND Path.PathId=File.PathId";
151 const char *uar_del_temp = "DROP TABLE temp";
152 const char *uar_del_temp1 = "DROP TABLE temp1";
154 const char *uar_last_full =
155 "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
156 "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
157 "AND Job.ClientId=%s "
158 "AND Job.StartTime < '%s' "
159 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
160 "AND JobMedia.JobId=Job.JobId "
161 "AND Media.Enabled=1 "
162 "AND JobMedia.MediaId=Media.MediaId "
163 "AND Job.FileSetId=FileSet.FileSetId "
164 "AND FileSet.FileSet='%s' "
166 "ORDER BY Job.JobTDate DESC LIMIT 1";
168 const char *uar_full =
169 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
170 "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
171 "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
172 "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
173 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
174 "AND Media.Enabled=1 "
175 "AND JobMedia.JobId=Job.JobId "
176 "AND JobMedia.MediaId=Media.MediaId";
178 const char *uar_dif =
179 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
180 "Job.Level,Job.JobFiles,Job.JobBytes,"
181 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
182 "Job.VolSessionId,Job.VolSessionTime "
183 "FROM Job,JobMedia,Media,FileSet "
184 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
185 "AND Job.ClientId=%s "
186 "AND JobMedia.JobId=Job.JobId "
187 "AND Media.Enabled=1 "
188 "AND JobMedia.MediaId=Media.MediaId "
189 "AND Job.Level='D' AND JobStatus IN ('T','W') AND Type='B' "
190 "AND Job.FileSetId=FileSet.FileSetId "
191 "AND FileSet.FileSet='%s' "
193 "ORDER BY Job.JobTDate DESC LIMIT 1";
195 const char *uar_inc =
196 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
197 "Job.Level,Job.JobFiles,Job.JobBytes,"
198 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
199 "Job.VolSessionId,Job.VolSessionTime "
200 "FROM Job,JobMedia,Media,FileSet "
201 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
202 "AND Job.ClientId=%s "
203 "AND Media.Enabled=1 "
204 "AND JobMedia.JobId=Job.JobId "
205 "AND JobMedia.MediaId=Media.MediaId "
206 "AND Job.Level='I' AND JobStatus IN ('T','W') AND Type='B' "
207 "AND Job.FileSetId=FileSet.FileSetId "
208 "AND FileSet.FileSet='%s' "
211 const char *uar_list_temp =
212 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
214 " ORDER BY StartTime ASC";
217 const char *uar_sel_jobid_temp =
218 "SELECT DISTINCT JobId,StartTime FROM temp ORDER BY StartTime ASC";
220 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
222 const char *uar_sel_all_temp = "SELECT * FROM temp";
226 /* Select FileSet names for this Client */
227 const char *uar_sel_fileset =
228 "SELECT DISTINCT FileSet.FileSet FROM Job,"
229 "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
230 "AND Job.ClientId=%s AND Client.ClientId=%s "
231 "ORDER BY FileSet.FileSet";
233 /* Select all different FileSet for this client
234 * This query doesn't guarantee that the Id is the latest
235 * version of the FileSet. Can be used with other queries that
236 * use Ids to select the FileSet name. (like in accurate)
238 const char *uar_sel_filesetid =
239 "SELECT MAX(FileSetId) "
240 "FROM FileSet JOIN Job USING (FileSetId) "
241 "WHERE Job.ClientId=%s "
244 /* Find MediaType used by this Job */
245 const char *uar_mediatype =
246 "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
247 "AND JobMedia.MediaId=Media.MediaId";
250 * Find JobId, FileIndex for a given path/file and date
251 * for use when inserting individual files into the tree.
253 const char *uar_jobid_fileindex =
254 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
255 "WHERE Job.JobId=File.JobId "
256 "AND Job.StartTime<='%s' "
257 "AND Path.Path='%s' "
258 "AND Filename.Name='%s' "
259 "AND Client.Name='%s' "
260 "AND Job.ClientId=Client.ClientId "
261 "AND Path.PathId=File.PathId "
262 "AND Filename.FilenameId=File.FilenameId "
263 "AND JobStatus IN ('T','W') AND Type='B' "
264 "ORDER BY Job.StartTime DESC LIMIT 1";
266 const char *uar_jobids_fileindex =
267 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
268 "WHERE Job.JobId IN (%s) "
269 "AND Job.JobId=File.JobId "
270 "AND Job.StartTime<='%s' "
271 "AND Path.Path='%s' "
272 "AND Filename.Name='%s' "
273 "AND Client.Name='%s' "
274 "AND Job.ClientId=Client.ClientId "
275 "AND Path.PathId=File.PathId "
276 "AND Filename.FilenameId=File.FilenameId "
277 "ORDER BY Job.StartTime DESC LIMIT 1";
279 /* Query to get list of files from table -- presuably built by an external program */
280 const char *uar_jobid_fileindex_from_table =
281 "SELECT JobId,FileIndex from %s";
283 /* Get the list of the last recent version with a given jobid list
284 * This is a tricky part because with SQL the result of
286 * SELECT MAX(A), B, C, D FROM... GROUP BY (B,C)
288 * doesn't give the good result (for D).
290 * With PostgreSQL, we can use DISTINCT ON(), but with Mysql or Sqlite,
291 * we need an extra join using JobTDate.
293 const char *select_recent_version_with_basejob[5] = {
295 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
296 "File.FilenameId AS FilenameId, LStat, MD5 "
298 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
300 "SELECT JobTDate, PathId, FilenameId " /* Get all normal files */
301 "FROM File JOIN Job USING (JobId) " /* from selected backup */
302 "WHERE File.JobId IN (%s) "
304 "SELECT JobTDate, PathId, FilenameId " /* Get all files from */
305 "FROM BaseFiles " /* BaseJob */
306 "JOIN File USING (FileId) "
307 "JOIN Job ON (BaseJobId = Job.JobId) "
308 "WHERE BaseFiles.JobId IN (%s) " /* Use Max(JobTDate) to find */
309 ") AS tmp GROUP BY PathId, FilenameId " /* the latest file version */
311 "WHERE (Job.JobId IN ( " /* Security, we force JobId to be valid */
312 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
313 "OR Job.JobId IN (%s)) "
314 "AND T1.JobTDate = Job.JobTDate " /* Join on JobTDate to get the orginal */
315 "AND Job.JobId = File.JobId " /* Job/File record */
316 "AND T1.PathId = File.PathId "
317 "AND T1.FilenameId = File.FilenameId",
319 /* Postgresql */ /* The DISTINCT ON () permits to avoid extra join */
320 "SELECT DISTINCT ON (FilenameId, PathId) StartTime, JobId, FileId, "
321 "FileIndex, PathId, FilenameId, LStat, MD5 "
323 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5 "
324 "FROM File WHERE JobId IN (%s) "
326 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
327 "File.FileIndex, LStat, MD5 "
328 "FROM BaseFiles JOIN File USING (FileId) "
329 "WHERE BaseFiles.JobId IN (%s) "
330 ") AS T JOIN Job USING (JobId) "
331 "ORDER BY FilenameId, PathId, StartTime DESC ",
333 /* SQLite */ /* See Mysql section for doc */
334 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
335 "File.FilenameId AS FilenameId, LStat, MD5 "
337 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
339 "SELECT JobTDate, PathId, FilenameId "
340 "FROM File JOIN Job USING (JobId) "
341 "WHERE File.JobId IN (%s) "
343 "SELECT JobTDate, PathId, FilenameId "
345 "JOIN File USING (FileId) "
346 "JOIN Job ON (BaseJobId = Job.JobId) "
347 "WHERE BaseFiles.JobId IN (%s) "
348 ") AS tmp GROUP BY PathId, FilenameId "
350 "WHERE (Job.JobId IN ( "
351 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
352 "OR Job.JobId IN (%s)) "
353 "AND T1.JobTDate = Job.JobTDate "
354 "AND Job.JobId = File.JobId "
355 "AND T1.PathId = File.PathId "
356 "AND T1.FilenameId = File.FilenameId",
358 /* SQLite3 */ /* See Mysql section for doc */
359 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
360 "File.FilenameId AS FilenameId, LStat, MD5 "
362 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
364 "SELECT JobTDate, PathId, FilenameId "
365 "FROM File JOIN Job USING (JobId) "
366 "WHERE File.JobId IN (%s) "
368 "SELECT JobTDate, PathId, FilenameId "
370 "JOIN File USING (FileId) "
371 "JOIN Job ON (BaseJobId = Job.JobId) "
372 "WHERE BaseFiles.JobId IN (%s) "
373 ") AS tmp GROUP BY PathId, FilenameId "
375 "WHERE (Job.JobId IN ( "
376 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
377 "OR Job.JobId IN (%s)) "
378 "AND T1.JobTDate = Job.JobTDate "
379 "AND Job.JobId = File.JobId "
380 "AND T1.PathId = File.PathId "
381 "AND T1.FilenameId = File.FilenameId",
383 /* Ingres */ /* See Mysql section for doc */
384 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
385 "File.FilenameId AS FilenameId, LStat, MD5 "
387 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
389 "SELECT JobTDate, PathId, FilenameId "
390 "FROM File JOIN Job USING (JobId) "
391 "WHERE File.JobId IN (%s) "
393 "SELECT JobTDate, PathId, FilenameId "
395 "JOIN File USING (FileId) "
396 "JOIN Job ON (BaseJobId = Job.JobId) "
397 "WHERE BaseFiles.JobId IN (%s) "
398 ") AS tmp GROUP BY PathId, FilenameId "
400 "WHERE (Job.JobId IN ( "
401 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
402 "OR Job.JobId IN (%s)) "
403 "AND T1.JobTDate = Job.JobTDate "
404 "AND Job.JobId = File.JobId "
405 "AND T1.PathId = File.PathId "
406 "AND T1.FilenameId = File.FilenameId"
409 /* Get the list of the last recent version with a given BaseJob jobid list */
410 const char *select_recent_version[5] = {
412 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
413 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
414 "f1.LStat AS LStat, f1.MD5 AS MD5 "
415 "FROM ( " /* Choose the last version for each Path/Filename */
416 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
417 "FROM File JOIN Job USING (JobId) "
418 "WHERE File.JobId IN (%s) "
419 "GROUP BY PathId, FilenameId "
420 ") AS t1, Job AS j1, File AS f1 "
421 "WHERE t1.JobTDate = j1.JobTDate "
422 "AND j1.JobId IN (%s) "
423 "AND t1.FilenameId = f1.FilenameId "
424 "AND t1.PathId = f1.PathId "
425 "AND j1.JobId = f1.JobId",
428 "SELECT DISTINCT ON (FilenameId, PathId) StartTime, JobId, FileId, "
429 "FileIndex, PathId, FilenameId, LStat, MD5 "
430 "FROM File JOIN Job USING (JobId) "
431 "WHERE JobId IN (%s) "
432 "ORDER BY FilenameId, PathId, StartTime DESC ",
435 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
436 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
437 "f1.LStat AS LStat, f1.MD5 AS MD5 "
439 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
440 "FROM File JOIN Job USING (JobId) "
441 "WHERE File.JobId IN (%s) "
442 "GROUP BY PathId, FilenameId "
443 ") AS t1, Job AS j1, File AS f1 "
444 "WHERE t1.JobTDate = j1.JobTDate "
445 "AND j1.JobId IN (%s) "
446 "AND t1.FilenameId = f1.FilenameId "
447 "AND t1.PathId = f1.PathId "
448 "AND j1.JobId = f1.JobId",
451 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
452 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
453 "f1.LStat AS LStat, f1.MD5 AS MD5 "
455 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
456 "FROM File JOIN Job USING (JobId) "
457 "WHERE File.JobId IN (%s) "
458 "GROUP BY PathId, FilenameId "
459 ") AS t1, Job AS j1, File AS f1 "
460 "WHERE t1.JobTDate = j1.JobTDate "
461 "AND j1.JobId IN (%s) "
462 "AND t1.FilenameId = f1.FilenameId "
463 "AND t1.PathId = f1.PathId "
464 "AND j1.JobId = f1.JobId",
467 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
468 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
469 "f1.LStat AS LStat, f1.MD5 AS MD5 "
470 "FROM ( " /* Choose the last version for each Path/Filename */
471 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
472 "FROM File JOIN Job USING (JobId) "
473 "WHERE File.JobId IN (%s) "
474 "GROUP BY PathId, FilenameId "
475 ") AS t1, Job AS j1, File AS f1 "
476 "WHERE t1.JobTDate = j1.JobTDate "
477 "AND j1.JobId IN (%s) "
478 "AND t1.FilenameId = f1.FilenameId "
479 "AND t1.PathId = f1.PathId "
480 "AND j1.JobId = f1.JobId"
483 /* We don't create this table as TEMPORARY because MySQL MyISAM
484 * 5.0 and 5.1 are unable to run further queries in this mode
486 const char *create_temp_accurate_jobids_default =
487 "CREATE TABLE btemp3%s AS "
488 "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
489 "FROM Job JOIN FileSet USING (FileSetId) "
490 "WHERE ClientId = %s "
491 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
492 "AND StartTime<'%s' "
493 "AND FileSet.FileSet=(SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
494 "ORDER BY Job.JobTDate DESC LIMIT 1";
496 const char *create_temp_accurate_jobids[5] = {
498 create_temp_accurate_jobids_default,
500 create_temp_accurate_jobids_default,
502 create_temp_accurate_jobids_default,
504 create_temp_accurate_jobids_default,
506 "DECLARE GLOBAL TEMPORARY TABLE btemp3%s AS "
507 "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
508 "FROM Job JOIN FileSet USING (FileSetId) "
509 "WHERE ClientId = %s "
510 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
511 "AND StartTime<'%s' "
512 "AND FileSet.FileSet=(SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
513 "ORDER BY Job.JobTDate DESC FETCH FIRST 1 ROW ONLY "
514 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
517 const char *create_temp_basefile[5] = {
519 "CREATE TEMPORARY TABLE basefile%lld ("
520 "Path BLOB NOT NULL,"
521 "Name BLOB NOT NULL)",
523 "CREATE TEMPORARY TABLE basefile%lld ("
527 "CREATE TEMPORARY TABLE basefile%lld ("
531 "CREATE TEMPORARY TABLE basefile%lld ("
535 "DECLARE GLOBAL TEMPORARY TABLE basefile%lld ("
536 "Path VARBYTE(32000) NOT NULL,"
537 "Name VARBYTE(32000) NOT NULL) "
538 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
541 const char *create_temp_new_basefile[5] = {
543 "CREATE TEMPORARY TABLE new_basefile%lld AS "
544 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
545 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
547 "FROM ( %s ) AS Temp "
548 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
549 "JOIN Path ON (Path.PathId = Temp.PathId) "
550 "WHERE Temp.FileIndex > 0",
552 "CREATE TEMPORARY TABLE new_basefile%lld AS "
553 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
554 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
556 "FROM ( %s ) AS Temp "
557 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
558 "JOIN Path ON (Path.PathId = Temp.PathId) "
559 "WHERE Temp.FileIndex > 0",
561 "CREATE TEMPORARY TABLE new_basefile%lld AS "
562 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
563 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
565 "FROM ( %s ) AS Temp "
566 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
567 "JOIN Path ON (Path.PathId = Temp.PathId) "
568 "WHERE Temp.FileIndex > 0",
570 "CREATE TEMPORARY TABLE new_basefile%lld AS "
571 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
572 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
574 "FROM ( %s ) AS Temp "
575 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
576 "JOIN Path ON (Path.PathId = Temp.PathId) "
577 "WHERE Temp.FileIndex > 0",
579 "DECLARE GLOBAL TEMPORARY TABLE new_basefile%lld AS "
580 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
581 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
583 "FROM ( %s ) AS Temp "
584 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
585 "JOIN Path ON (Path.PathId = Temp.PathId) "
586 "WHERE Temp.FileIndex > 0 "
587 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
590 /* ====== ua_prune.c */
592 /* List of SQL commands to create temp table and indicies */
593 const char *create_deltabs[5] = {
595 "CREATE TEMPORARY TABLE DelCandidates ("
596 "JobId INTEGER UNSIGNED NOT NULL, "
597 "PurgedFiles TINYINT, "
598 "FileSetId INTEGER UNSIGNED, "
599 "JobFiles INTEGER UNSIGNED, "
600 "JobStatus BINARY(1))",
602 "CREATE TEMPORARY TABLE DelCandidates ("
603 "JobId INTEGER NOT NULL, "
604 "PurgedFiles SMALLINT, "
605 "FileSetId INTEGER, "
607 "JobStatus char(1))",
609 "CREATE TEMPORARY TABLE DelCandidates ("
610 "JobId INTEGER UNSIGNED NOT NULL, "
611 "PurgedFiles TINYINT, "
612 "FileSetId INTEGER UNSIGNED, "
613 "JobFiles INTEGER UNSIGNED, "
616 "CREATE TEMPORARY TABLE DelCandidates ("
617 "JobId INTEGER UNSIGNED NOT NULL, "
618 "PurgedFiles TINYINT, "
619 "FileSetId INTEGER UNSIGNED, "
620 "JobFiles INTEGER UNSIGNED, "
623 "DECLARE GLOBAL TEMPORARY TABLE DelCandidates ("
624 "JobId INTEGER NOT NULL, "
625 "PurgedFiles SMALLINT, "
626 "FileSetId INTEGER, "
628 "JobStatus CHAR(1)) "
629 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
632 /* ======= ua_purge.c */
634 /* Select the first available Copy Job that must be upgraded to a Backup job when the original backup job is expired. */
636 const char *uap_upgrade_copies_oldest_job_default =
637 "CREATE TEMPORARY TABLE cpy_tmp AS "
638 "SELECT MIN(JobId) AS JobId FROM Job " /* Choose the oldest job */
639 "WHERE Type='%c' " /* JT_JOB_COPY */
640 "AND ( PriorJobId IN (%s) " /* JobId selection */
645 "WHERE JobId IN (%s) " /* JobId selection */
649 "GROUP BY PriorJobId "; /* one result per copy */
651 const char *uap_upgrade_copies_oldest_job[5] = {
653 uap_upgrade_copies_oldest_job_default,
655 uap_upgrade_copies_oldest_job_default,
657 uap_upgrade_copies_oldest_job_default,
659 uap_upgrade_copies_oldest_job_default,
661 "DECLARE GLOBAL TEMPORARY TABLE cpy_tmp AS "
662 "SELECT MIN(JobId) AS JobId FROM Job " /* Choose the oldest job */
663 "WHERE Type='%c' " /* JT_JOB_COPY */
664 "AND ( PriorJobId IN (%s) " /* JobId selection */
669 "WHERE JobId IN (%s) " /* JobId selection */
673 "GROUP BY PriorJobId " /* one result per copy */
674 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
677 /* ======= ua_restore.c */
679 /* List Jobs where a particular file is saved */
680 const char *uar_file[5] = {
682 "SELECT Job.JobId as JobId,"
683 "CONCAT(Path.Path,Filename.Name) as Name, "
684 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
685 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
686 "AND Client.ClientId=Job.ClientId "
687 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
688 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
689 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
691 "SELECT Job.JobId as JobId,"
692 "Path.Path||Filename.Name as Name, "
693 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
694 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
695 "AND Client.ClientId=Job.ClientId "
696 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
697 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
698 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
700 "SELECT Job.JobId as JobId,"
701 "Path.Path||Filename.Name as Name, "
702 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
703 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
704 "AND Client.ClientId=Job.ClientId "
705 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
706 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
707 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
709 "SELECT Job.JobId as JobId,"
710 "Path.Path||Filename.Name as Name, "
711 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
712 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
713 "AND Client.ClientId=Job.ClientId "
714 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
715 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
716 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
718 "SELECT Job.JobId as JobId,"
719 "Path.Path||Filename.Name as Name, "
720 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
721 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
722 "AND Client.ClientId=Job.ClientId "
723 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
724 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
725 "AND Filename.Name='%s' ORDER BY StartTime DESC FETCH FIRST 20 ROWS ONLY"
728 const char *uar_create_temp[5] = {
730 "CREATE TEMPORARY TABLE temp ("
731 "JobId INTEGER UNSIGNED NOT NULL,"
732 "JobTDate BIGINT UNSIGNED,"
733 "ClientId INTEGER UNSIGNED,"
735 "JobFiles INTEGER UNSIGNED,"
736 "JobBytes BIGINT UNSIGNED,"
739 "StartFile INTEGER UNSIGNED,"
740 "VolSessionId INTEGER UNSIGNED,"
741 "VolSessionTime INTEGER UNSIGNED)",
743 "CREATE TEMPORARY TABLE temp ("
744 "JobId INTEGER NOT NULL,"
753 "VolSessionId INTEGER,"
754 "VolSessionTime INTEGER)",
756 "CREATE TEMPORARY TABLE temp ("
757 "JobId INTEGER UNSIGNED NOT NULL,"
758 "JobTDate BIGINT UNSIGNED,"
759 "ClientId INTEGER UNSIGNED,"
761 "JobFiles INTEGER UNSIGNED,"
762 "JobBytes BIGINT UNSIGNED,"
765 "StartFile INTEGER UNSIGNED,"
766 "VolSessionId INTEGER UNSIGNED,"
767 "VolSessionTime INTEGER UNSIGNED)",
769 "CREATE TEMPORARY TABLE temp ("
770 "JobId INTEGER UNSIGNED NOT NULL,"
771 "JobTDate BIGINT UNSIGNED,"
772 "ClientId INTEGER UNSIGNED,"
774 "JobFiles INTEGER UNSIGNED,"
775 "JobBytes BIGINT UNSIGNED,"
778 "StartFile INTEGER UNSIGNED,"
779 "VolSessionId INTEGER UNSIGNED,"
780 "VolSessionTime INTEGER UNSIGNED)",
782 "DECLARE GLOBAL TEMPORARY TABLE temp ("
783 "JobId INTEGER NOT NULL,"
789 "StartTime TIMESTAMP WITHOUT TIME ZONE,"
790 "VolumeName VARBYTE(128),"
792 "VolSessionId INTEGER,"
793 "VolSessionTime INTEGER) "
794 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
797 const char *uar_create_temp1[5] = {
799 "CREATE TEMPORARY TABLE temp1 ("
800 "JobId INTEGER UNSIGNED NOT NULL,"
801 "JobTDate BIGINT UNSIGNED)",
803 "CREATE TEMPORARY TABLE temp1 ("
804 "JobId INTEGER NOT NULL,"
807 "CREATE TEMPORARY TABLE temp1 ("
808 "JobId INTEGER UNSIGNED NOT NULL,"
809 "JobTDate BIGINT UNSIGNED)",
811 "CREATE TEMPORARY TABLE temp1 ("
812 "JobId INTEGER UNSIGNED NOT NULL,"
813 "JobTDate BIGINT UNSIGNED)",
815 "DECLARE GLOBAL TEMPORARY TABLE temp1 ("
816 "JobId INTEGER NOT NULL,"
818 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
821 /* Query to get all files in a directory -- no recursing
822 * Note, for PostgreSQL since it respects the "Single Value
823 * rule", the results of the SELECT will be unoptimized.
824 * I.e. the same file will be restored multiple times, once
825 * for each time it was backed up.
828 const char *uar_jobid_fileindex_from_dir[5] = {
830 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
831 "WHERE Job.JobId IN (%s) "
832 "AND Job.JobId=File.JobId "
833 "AND Path.Path='%s' "
834 "AND Client.Name='%s' "
835 "AND Job.ClientId=Client.ClientId "
836 "AND Path.PathId=File.Pathid "
837 "AND Filename.FilenameId=File.FilenameId "
838 "GROUP BY File.FileIndex ",
840 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
841 "WHERE Job.JobId IN (%s) "
842 "AND Job.JobId=File.JobId "
843 "AND Path.Path='%s' "
844 "AND Client.Name='%s' "
845 "AND Job.ClientId=Client.ClientId "
846 "AND Path.PathId=File.Pathid "
847 "AND Filename.FilenameId=File.FilenameId",
849 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
850 "WHERE Job.JobId IN (%s) "
851 "AND Job.JobId=File.JobId "
852 "AND Path.Path='%s' "
853 "AND Client.Name='%s' "
854 "AND Job.ClientId=Client.ClientId "
855 "AND Path.PathId=File.Pathid "
856 "AND Filename.FilenameId=File.FilenameId "
857 "GROUP BY File.FileIndex ",
859 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
860 "WHERE Job.JobId IN (%s) "
861 "AND Job.JobId=File.JobId "
862 "AND Path.Path='%s' "
863 "AND Client.Name='%s' "
864 "AND Job.ClientId=Client.ClientId "
865 "AND Path.PathId=File.Pathid "
866 "AND Filename.FilenameId=File.FilenameId "
867 "GROUP BY File.FileIndex ",
869 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
870 "WHERE Job.JobId IN (%s) "
871 "AND Job.JobId=File.JobId "
872 "AND Path.Path='%s' "
873 "AND Client.Name='%s' "
874 "AND Job.ClientId=Client.ClientId "
875 "AND Path.PathId=File.Pathid "
876 "AND Filename.FilenameId=File.FilenameId"
879 const char *sql_media_order_most_recently_written[5] = {
881 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
883 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
885 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
887 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
889 "ORDER BY IFNULL(LastWritten, '1970-01-01 00:00:00') DESC,MediaId"
892 const char *sql_get_max_connections[5] = {
894 "SHOW VARIABLES LIKE 'max_connections'",
896 "SHOW max_connections",
905 /* Row number of the max_connections setting */
906 const uint32_t sql_get_max_connections_index[5] = {