]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_cmds.c
Fix Win32 build with new .def files and DLL_IMP_EXP ...
[bacula/bacula] / bacula / src / cats / sql_cmds.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2002-2008 Free Software Foundation Europe e.V.
5
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 two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
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.
17
18    You should have received a copy of the GNU 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
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
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.
27 */
28 /*
29  *
30  *  This file contains all the SQL commands issued by the Director
31  *
32  *     Kern Sibbald, July MMII
33  *
34  *   Version $Id$
35  */
36 /*
37  * Note, PostgreSQL imposes some constraints on using DISTINCT and GROUP BY
38  *  for example, the following is illegal in PostgreSQL:
39  *  SELECT DISTINCT JobId FROM temp ORDER BY StartTime ASC;
40  *  because all the ORDER BY expressions must appear in the SELECT list!
41  */
42
43
44 #include "bacula.h"
45 #include "cats.h"
46
47 /* For ua_update.c */
48 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
49
50 /* For ua_dotcmds.c */
51 const char *client_backups =
52    "SELECT DISTINCT Job.JobId,Client.Name as Client,Level,StartTime,"
53    "JobFiles,JobBytes,VolumeName,MediaType,FileSet,Media.Enabled as Enabled"
54    " FROM Client,Job,JobMedia,Media,FileSet"
55    " WHERE Client.Name='%s'"
56    " AND FileSet='%s'"
57    " AND Client.ClientId=Job.ClientId"
58    " AND JobStatus='T' AND Type='B'" 
59    " AND JobMedia.JobId=Job.JobId AND JobMedia.MediaId=Media.MediaId"
60    " AND Job.FileSetId=FileSet.FileSetId"
61    " ORDER BY Job.StartTime";
62
63
64 /* ====== ua_prune.c */
65
66 const char *del_File     = "DELETE FROM File WHERE JobId=%s";
67 const char *upd_Purged   = "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s";
68 const char *cnt_DelCand  = "SELECT count(*) FROM DelCandidates";
69 const char *del_Job      = "DELETE FROM Job WHERE JobId=%s";
70 const char *del_JobMedia = "DELETE FROM JobMedia WHERE JobId=%s";
71 const char *cnt_JobMedia = "SELECT count(*) FROM JobMedia WHERE MediaId=%s";
72
73 const char *sel_JobMedia = 
74    "SELECT DISTINCT JobMedia.JobId FROM JobMedia,Job "
75    "WHERE MediaId=%s AND Job.JobId=JobMedia.JobId "
76    "AND Job.JobTDate<%s";
77
78 /* Count Select JobIds for File deletion */
79 const char *count_select_job = 
80    "SELECT count(*) from Job "
81    "WHERE JobTDate<%s "
82    "AND ClientId=%s "
83    "AND PurgedFiles=0";
84
85
86 /* Select JobIds for File deletion. */
87 const char *select_job =
88    "SELECT DISTINCT JobId from Job "
89    "WHERE JobTDate<%s "
90    "AND ClientId=%s "
91    "AND PurgedFiles=0";
92
93 /* Delete temp tables and indexes  */
94 const char *drop_deltabs[] = {
95    "DROP TABLE DelCandidates",
96    NULL};
97
98
99 /* List of SQL commands to create temp table and indicies  */
100 const char *create_deltabs[] = {
101    "CREATE TEMPORARY TABLE DelCandidates ("
102 #if defined(HAVE_MYSQL)
103       "JobId INTEGER UNSIGNED NOT NULL, "
104       "PurgedFiles TINYINT, "
105       "FileSetId INTEGER UNSIGNED, "
106       "JobFiles INTEGER UNSIGNED, "
107       "JobStatus BINARY(1))",
108 #elif defined(HAVE_POSTGRESQL)
109       "JobId INTEGER NOT NULL, "
110       "PurgedFiles SMALLINT, "
111       "FileSetId INTEGER, "
112       "JobFiles INTEGER, "
113       "JobStatus char(1))",
114 #else
115       "JobId INTEGER UNSIGNED NOT NULL, "
116       "PurgedFiles TINYINT, "
117       "FileSetId INTEGER UNSIGNED, "
118       "JobFiles INTEGER UNSIGNED, "
119       "JobStatus CHAR)",
120 #endif
121    "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
122    NULL};
123
124 /* Fill candidates table with all Jobs subject to being deleted.
125  *  This is used for pruning Jobs (first the files, then the Jobs).
126  */
127 const char *insert_delcand =
128    "INSERT INTO DelCandidates "
129    "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus FROM Job "
130    "WHERE Type='%c' "
131    "AND JobTDate<%s "
132    "AND ClientId=%s";
133
134 /*
135  * Select Jobs from the DelCandidates table that have a
136  * more recent backup -- i.e. are not the only backup.
137  * This is the list of Jobs to delete for a Backup Job.
138  * At the same time, we select "orphanned" jobs
139  * (i.e. no files, ...) for deletion.
140  */
141 #ifdef old_way
142 const char *select_backup_del =
143    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
144    "FROM Job,DelCandidates "
145    "WHERE (Job.JobTDate<%s AND ((DelCandidates.JobFiles=0) OR "
146    "(DelCandidates.JobStatus!='T'))) OR "
147    "(Job.JobTDate>%s "
148    "AND Job.ClientId=%s "
149    "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
150    "AND Job.FileSetId=DelCandidates.FileSetId)";
151
152 /* Select Jobs from the DelCandidates table that have a
153  * more recent InitCatalog -- i.e. are not the only InitCatalog
154  * This is the list of Jobs to delete for a Verify Job.
155  */
156 const char *select_verify_del =
157    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
158    "FROM Job,DelCandidates "
159    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
160    "(Job.JobTDate>%s "
161    "AND Job.ClientId=%s "
162    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
163    "AND Job.FileSetId=DelCandidates.FileSetId)";
164
165
166 /* Select Jobs from the DelCandidates table.
167  * This is the list of Jobs to delete for a Restore Job.
168  */
169 const char *select_restore_del =
170    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
171    "FROM Job,DelCandidates "
172    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
173    "(Job.JobTDate>%s "
174    "AND Job.ClientId=%s "
175    "AND Job.Type='R')";
176
177 /* Select Jobs from the DelCandidates table.
178  * This is the list of Jobs to delete for an Admin Job.
179  */
180 const char *select_admin_del =
181    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
182    "FROM Job,DelCandidates "
183    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
184    "(Job.JobTDate>%s "
185    "AND Job.ClientId=%s "
186    "AND Job.Type='D')";
187
188 /*
189  * Select Jobs from the DelCandidates table.
190  * This is the list of Jobs to delete for an Migrate Job.
191  */
192 const char *select_migrate_del =
193    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
194    "FROM Job,DelCandidates "
195    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
196    "(Job.JobTDate>%s "
197    "AND Job.ClientId=%s "
198    "AND Job.Type='g')";
199
200 #else
201 /* Faster way */
202 const char *select_backup_del =
203    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
204    "FROM Job,DelCandidates "
205    "WHERE (Job.JobId=DelCandidates.JobId AND ((DelCandidates.JobFiles=0) OR "
206    "(DelCandidates.JobStatus!='T'))) OR "
207    "(Job.JobTDate>%s "
208    "AND Job.ClientId=%s "
209    "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
210    "AND Job.FileSetId=DelCandidates.FileSetId)";
211
212 /* Select Jobs from the DelCandidates table that have a
213  * more recent InitCatalog -- i.e. are not the only InitCatalog
214  * This is the list of Jobs to delete for a Verify Job.
215  */
216 const char *select_verify_del =
217    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
218    "FROM Job,DelCandidates "
219    "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
220    "(Job.JobTDate>%s "
221    "AND Job.ClientId=%s "
222    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
223    "AND Job.FileSetId=DelCandidates.FileSetId)";
224
225
226 /* Select Jobs from the DelCandidates table.
227  * This is the list of Jobs to delete for a Restore Job.
228  */
229 const char *select_restore_del =
230    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
231    "FROM Job,DelCandidates "
232    "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
233    "(Job.JobTDate>%s "
234    "AND Job.ClientId=%s "
235    "AND Job.Type='R')";
236
237 /* Select Jobs from the DelCandidates table.
238  * This is the list of Jobs to delete for an Admin Job.
239  */
240 const char *select_admin_del =
241    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
242    "FROM Job,DelCandidates "
243    "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
244    "(Job.JobTDate>%s "
245    "AND Job.ClientId=%s "
246    "AND Job.Type='D')";
247
248 /*
249  * Select Jobs from the DelCandidates table.
250  * This is the list of Jobs to delete for an Migrate Job.
251  */
252 const char *select_migrate_del =
253    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
254    "FROM Job,DelCandidates "
255    "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
256    "(Job.JobTDate>%s "
257    "AND Job.ClientId=%s "
258    "AND Job.Type='g')";
259
260 #endif
261
262 /* ======= ua_restore.c */
263 const char *uar_count_files =
264    "SELECT JobFiles FROM Job WHERE JobId=%s";
265
266 /* List last 20 Jobs */
267 const char *uar_list_jobs =
268    "SELECT JobId,Client.Name as Client,StartTime,Level as "
269    "JobLevel,JobFiles,JobBytes "
270    "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus='T' "
271    "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
272
273 #ifdef HAVE_MYSQL
274 /*  MYSQL IS NOT STANDARD SQL !!!!! */
275 /* List Jobs where a particular file is saved */
276 const char *uar_file =
277    "SELECT Job.JobId as JobId,"
278    "CONCAT(Path.Path,Filename.Name) as Name, "
279    "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
280    "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
281    "AND Client.ClientId=Job.ClientId "
282    "AND Job.JobId=File.JobId "
283    "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
284    "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
285 #else
286 /* List Jobs where a particular file is saved */
287 const char *uar_file =
288    "SELECT Job.JobId as JobId,"
289    "Path.Path||Filename.Name as Name, "
290    "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
291    "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
292    "AND Client.ClientId=Job.ClientId "
293    "AND Job.JobId=File.JobId "
294    "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
295    "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
296 #endif
297
298
299 /*
300  * Find all files for a particular JobId and insert them into
301  *  the tree during a restore.
302  */
303 const char *uar_sel_files =
304    "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
305    "FROM File,Filename,Path "
306    "WHERE File.JobId=%s AND Filename.FilenameId=File.FilenameId "
307    "AND Path.PathId=File.PathId";
308
309 const char *uar_del_temp  = "DROP TABLE temp";
310 const char *uar_del_temp1 = "DROP TABLE temp1";
311
312 const char *uar_create_temp =
313    "CREATE TEMPORARY TABLE temp ("
314 #ifdef HAVE_POSTGRESQL
315    "JobId INTEGER NOT NULL,"
316    "JobTDate BIGINT,"
317    "ClientId INTEGER,"
318    "Level CHAR,"
319    "JobFiles INTEGER,"
320    "JobBytes BIGINT,"
321    "StartTime TEXT,"
322    "VolumeName TEXT,"
323    "StartFile INTEGER,"
324    "VolSessionId INTEGER,"
325    "VolSessionTime INTEGER)";
326 #else
327    "JobId INTEGER UNSIGNED NOT NULL,"
328    "JobTDate BIGINT UNSIGNED,"
329    "ClientId INTEGER UNSIGNED,"
330    "Level CHAR,"
331    "JobFiles INTEGER UNSIGNED,"
332    "JobBytes BIGINT UNSIGNED,"
333    "StartTime TEXT,"
334    "VolumeName TEXT,"
335    "StartFile INTEGER UNSIGNED,"
336    "VolSessionId INTEGER UNSIGNED,"
337    "VolSessionTime INTEGER UNSIGNED)";
338 #endif
339
340 const char *uar_create_temp1 =
341    "CREATE TEMPORARY TABLE temp1 ("
342 #ifdef HAVE_POSTGRESQL
343    "JobId INTEGER NOT NULL,"
344    "JobTDate BIGINT)";
345 #else
346    "JobId INTEGER UNSIGNED NOT NULL,"
347    "JobTDate BIGINT UNSIGNED)";
348 #endif
349
350 const char *uar_last_full =
351    "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
352    "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
353    "AND Job.ClientId=%s "
354    "AND Job.StartTime<'%s' "
355    "AND Level='F' AND JobStatus='T' AND Type='B' "
356    "AND JobMedia.JobId=Job.JobId "
357    "AND Media.Enabled=1 "
358    "AND JobMedia.MediaId=Media.MediaId "
359    "AND Job.FileSetId=FileSet.FileSetId "
360    "AND FileSet.FileSet='%s' "
361    "%s"
362    "ORDER BY Job.JobTDate DESC LIMIT 1";
363
364 const char *uar_full =
365    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
366    "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
367    "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
368    "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
369    "AND Level='F' AND JobStatus='T' AND Type='B' "
370    "AND Media.Enabled=1 "
371    "AND JobMedia.JobId=Job.JobId "
372    "AND JobMedia.MediaId=Media.MediaId";
373
374 const char *uar_dif =
375    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
376    "Job.Level,Job.JobFiles,Job.JobBytes,"
377    "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
378    "Job.VolSessionId,Job.VolSessionTime "
379    "FROM Job,JobMedia,Media,FileSet "
380    "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
381    "AND Job.ClientId=%s "
382    "AND JobMedia.JobId=Job.JobId "
383    "AND Media.Enabled=1 "
384    "AND JobMedia.MediaId=Media.MediaId "
385    "AND Job.Level='D' AND JobStatus='T' AND Type='B' "
386    "AND Job.FileSetId=FileSet.FileSetId "
387    "AND FileSet.FileSet='%s' "
388    "%s"
389    "ORDER BY Job.JobTDate DESC LIMIT 1";
390
391 const char *uar_inc =
392    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
393    "Job.Level,Job.JobFiles,Job.JobBytes,"
394    "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
395    "Job.VolSessionId,Job.VolSessionTime "
396    "FROM Job,JobMedia,Media,FileSet "
397    "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
398    "AND Job.ClientId=%s "
399    "AND Media.Enabled=1 "
400    "AND JobMedia.JobId=Job.JobId "
401    "AND JobMedia.MediaId=Media.MediaId "
402    "AND Job.Level='I' AND JobStatus='T' AND Type='B' "
403    "AND Job.FileSetId=FileSet.FileSetId "
404    "AND FileSet.FileSet='%s' "
405    "%s";
406
407 const char *uar_list_temp =
408    "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
409    " FROM temp"
410    " ORDER BY StartTime ASC";
411
412
413 const char *uar_sel_jobid_temp = 
414    "SELECT DISTINCT JobId,StartTime FROM temp ORDER BY StartTime ASC";
415
416 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
417
418 const char *uar_sel_all_temp = "SELECT * FROM temp";
419
420
421
422 /* Select FileSet names for this Client */
423 const char *uar_sel_fileset =
424    "SELECT DISTINCT FileSet.FileSet FROM Job,"
425    "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
426    "AND Job.ClientId=%s AND Client.ClientId=%s "
427    "ORDER BY FileSet.FileSet";
428
429 /* Find MediaType used by this Job */
430 const char *uar_mediatype =
431    "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
432    "AND JobMedia.MediaId=Media.MediaId";
433
434 /*
435  *  Find JobId, FileIndex for a given path/file and date
436  *  for use when inserting individual files into the tree.
437  */
438 const char *uar_jobid_fileindex =
439    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
440    "WHERE Job.JobId=File.JobId "
441    "AND Job.StartTime<='%s' "
442    "AND Path.Path='%s' "
443    "AND Filename.Name='%s' "
444    "AND Client.Name='%s' "
445    "AND Job.ClientId=Client.ClientId "
446    "AND Path.PathId=File.PathId "
447    "AND Filename.FilenameId=File.FilenameId "
448    "ORDER BY Job.StartTime DESC LIMIT 1";
449
450 const char *uar_jobids_fileindex =
451    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
452    "WHERE Job.JobId IN (%s) "
453    "AND Job.JobId=File.JobId "
454    "AND Job.StartTime<='%s' "
455    "AND Path.Path='%s' "
456    "AND Filename.Name='%s' "
457    "AND Client.Name='%s' "
458    "AND Job.ClientId=Client.ClientId "
459    "AND Path.PathId=File.PathId "
460    "AND Filename.FilenameId=File.FilenameId "
461    "ORDER BY Job.StartTime DESC LIMIT 1";
462
463 /* Query to get all files in a directory -- no recursing   
464  *  Note, for PostgreSQL since it respects the "Single Value
465  *  rule", the results of the SELECT will be unoptimized.
466  *  I.e. the same file will be restored multiple times, once
467  *  for each time it was backed up.
468  */
469
470 #ifdef HAVE_POSTGRESQL
471 const char *uar_jobid_fileindex_from_dir = 
472    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
473    "WHERE Job.JobId IN (%s) "
474    "AND Job.JobId=File.JobId "
475    "AND Path.Path='%s' "
476    "AND Client.Name='%s' "
477    "AND Job.ClientId=Client.ClientId "
478    "AND Path.PathId=File.Pathid "
479    "AND Filename.FilenameId=File.FilenameId"; 
480 #else
481 const char *uar_jobid_fileindex_from_dir = 
482    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
483    "WHERE Job.JobId IN (%s) "
484    "AND Job.JobId=File.JobId "
485    "AND Path.Path='%s' "
486    "AND Client.Name='%s' "
487    "AND Job.ClientId=Client.ClientId "
488    "AND Path.PathId=File.Pathid "
489    "AND Filename.FilenameId=File.FilenameId "
490    "GROUP BY File.FileIndex ";
491 #endif
492  
493 /* Query to get list of files from table -- presuably built by an external program */
494 const char *uar_jobid_fileindex_from_table = 
495    "SELECT JobId,FileIndex from %s";