]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/sql_cmds.c
c1bf44d086f7b157074742d76ad8f2d97f00a4a4
[bacula/bacula] / bacula / src / dird / sql_cmds.c
1 /*
2  *
3  *  This file contains all the SQL commands issued by the Director
4  *
5  *     Kern Sibbald, July MMII
6  *
7  *   Version $Id$
8  */
9
10 /*
11    Copyright (C) 2002 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 /* ====== ua_prune.c */
34
35 char *cnt_File     = "SELECT count(*) FROM File WHERE JobId=%u";
36 char *del_File     = "DELETE FROM File WHERE JobId=%u";
37 char *upd_Purged   = "UPDATE Job Set PurgedFiles=1 WHERE JobId=%u";
38 char *cnt_DelCand  = "SELECT count(*) FROM DelCandidates";
39 char *del_Job      = "DELETE FROM Job WHERE JobId=%u";
40 char *del_JobMedia = "DELETE FROM JobMedia WHERE JobId=%u"; 
41 char *cnt_JobMedia = "SELECT count(*) FROM JobMedia WHERE MediaId=%u";
42 char *sel_JobMedia = "SELECT JobId FROM JobMedia WHERE MediaId=%u";
43
44 /* Select JobIds for File deletion. */
45 char *select_job =
46    "SELECT JobId from Job "    
47    "WHERE JobTDate < %s "
48    "AND ClientId=%u "
49    "AND PurgedFiles=0";
50
51 /* Delete temp tables and indexes  */
52 char *drop_deltabs[] = {
53    "DROP TABLE DelCandidates",
54    "DROP INDEX DelInx1",
55    NULL};
56
57 /* List of SQL commands to create temp table and indicies  */
58 char *create_deltabs[] = {
59    "CREATE TABLE DelCandidates ("
60       "JobId INTEGER UNSIGNED NOT NULL, "
61       "PurgedFiles TINYINT, "
62       "FileSetId INTEGER UNSIGNED)",
63    "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
64    NULL};
65
66 /* Fill candidates table with all Files subject to being deleted.
67  *  This is used for pruning Jobs (first the files, then the Jobs).
68  */
69 char *insert_delcand = 
70    "INSERT INTO DelCandidates "
71    "SELECT JobId, PurgedFiles, FileSetId FROM Job "
72    "WHERE JobTDate < %s " 
73    "AND ClientId=%u";
74
75 /* Select files from the DelCandidates table that have a
76  * more recent backup -- i.e. are not the only backup.
77  * This is the list of files to delete for a Backup Job.
78  */
79 char *select_backup_del =
80    "SELECT DelCandidates.JobId "
81    "FROM Job,DelCandidates "
82    "WHERE Job.JobTDate >= %s "
83    "AND Job.ClientId=%u "
84    "AND Job.JobType='B' "
85    "AND Job.Level='F' "
86    "AND Job.JobStatus='T' "
87    "AND Job.FileSetId=DelCandidates.FileSetId";
88
89 /* Select files from the DelCandidates table that have a
90  * more recent InitCatalog -- i.e. are not the only InitCatalog
91  * This is the list of files to delete for a Verify Job.
92  */
93 char *select_verify_del =
94    "SELECT DelCandidates.JobId "
95    "FROM Job,DelCandidates "
96    "WHERE Job.JobTDate >= %s "
97    "AND Job.ClientId=%u "
98    "AND Job.JobType='V' "
99    "AND Job.Level='V' "
100    "AND Job.JobStatus='T' "
101    "AND Job.FileSetId=DelCandidates.FileSetId";
102
103
104 /* Select files from the DelCandidates table.
105  * This is the list of files to delete for a Restore Job.
106  */
107 char *select_restore_del =
108    "SELECT DelCandidates.JobId "
109    "FROM Job,DelCandidates "
110    "WHERE Job.JobTDate >= %s "
111    "AND Job.ClientId=%u "   
112    "AND Job.JobType='R'";
113
114
115
116 /* ======= ua_restore.c */
117
118 /* List last 20 Jobs */
119 char *uar_list_jobs = 
120    "SELECT JobId,Client.Name as Client,StartTime,Type as "
121    "JobType,JobFiles,JobBytes "
122    "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus='T' "
123    "AND Type='B' LIMIT 20";
124
125 #ifdef HAVE_MYSQL
126 /*  MYSQL IS NOT STANDARD SQL !!!!! */
127 /* List Jobs where a particular file is saved */
128 char *uar_file = 
129    "SELECT Job.JobId as JobId, Client.Name as Client, "
130    "CONCAT(Path.Path,Filename.Name) as Name, "
131    "StartTime,Type as JobType,JobFiles,JobBytes "
132    "FROM Client,Job,File,Filename,Path WHERE Client.ClientId=Job.ClientId "
133    "AND JobStatus='T' AND Job.JobId=File.JobId "
134    "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
135    "AND Filename.Name='%s' LIMIT 20";
136 #else
137 /* List Jobs where a particular file is saved */
138 char *uar_file = 
139    "SELECT Job.JobId as JobId, Client.Name as Client, "
140    "Path.Path||Filename.Name as Name, "
141    "StartTime,Type as JobType,JobFiles,JobBytes "
142    "FROM Client,Job,File,Filename,Path WHERE Client.ClientId=Job.ClientId "
143    "AND JobStatus='T' AND Job.JobId=File.JobId "
144    "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
145    "AND Filename.Name='%s' LIMIT 20";
146 #endif
147
148
149 char *uar_sel_files = 
150    "SELECT Path.Path,Filename.Name,FileIndex,JobId "
151    "FROM File,Filename,Path "
152    "WHERE File.JobId=%d AND Filename.FilenameId=File.FilenameId "
153    "AND Path.PathId=File.PathId";
154
155 char *uar_del_temp  = "DROP TABLE temp";
156 char *uar_del_temp1 = "DROP TABLE temp1";
157
158 char *uar_create_temp = 
159    "CREATE TABLE temp (JobId INTEGER UNSIGNED NOT NULL,"
160    "JobTDate BIGINT UNSIGNED,"
161    "ClientId INTEGER UNSIGNED,"
162    "Level CHAR,"
163    "JobFiles INTEGER UNSIGNED,"
164    "StartTime TEXT,"
165    "VolumeName TEXT,"
166    "StartFile INTEGER UNSIGNED,"
167    "VolSessionId INTEGER UNSIGNED,"
168    "VolSessionTime INTEGER UNSIGNED)";
169
170 char *uar_create_temp1 = 
171    "CREATE TABLE temp1 (JobId INTEGER UNSIGNED NOT NULL,"
172    "JobTDate BIGINT UNSIGNED)";
173
174 char *uar_last_full =
175    "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
176    "FROM Client,Job,JobMedia,Media WHERE Client.ClientId=%u "
177    "AND Job.ClientId=%u "
178    "AND Level='F' AND JobStatus='T' "
179    "AND JobMedia.JobId=Job.JobId "
180    "AND JobMedia.MediaId=Media.MediaId "
181    "AND Job.FileSetId=%u "
182    "ORDER BY Job.JobTDate DESC LIMIT 1";
183
184 char *uar_full = 
185    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"          
186    " Job.ClientId,Job.Level,Job.JobFiles,"
187    " StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
188    "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
189    "AND Level='F' AND JobStatus='T' "
190    "AND JobMedia.JobId=Job.JobId "
191    "AND JobMedia.MediaId=Media.MediaId";
192
193 char *uar_inc =
194    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
195    "Job.Level,Job.JobFiles,Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
196    "Job.VolSessionId,Job.VolSessionTime "
197    "FROM Job,JobMedia,Media "
198    "WHERE Job.JobTDate>%s AND Job.ClientId=%u "
199    "AND JobMedia.JobId=Job.JobId "
200    "AND JobMedia.MediaId=Media.MediaId "
201    "AND Job.Level='I' AND JobStatus='T' "
202    "AND Job.FileSetId=%u "
203    "GROUP BY Job.JobId";
204
205 char *uar_list_temp = 
206    "SELECT JobId,Level,JobFiles,StartTime,VolumeName,StartFile,"
207    "VolSessionId,VolSessionTime FROM temp";
208
209 char *uar_sel_jobid_temp = "SELECT JobId FROM temp";
210
211 char *uar_sel_all_temp1 = "SELECT * FROM temp1";
212
213 /* Select filesets for this Client */
214 char *uar_sel_fileset = 
215    "SELECT FileSet.FileSetId,FileSet.FileSet,FileSet.MD5 FROM Job,"
216    "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
217    "AND Job.ClientId=%u AND Client.ClientId=%u "
218    "GROUP BY FileSet.FileSetId";
219
220 /* Find MediaType used by this Job */
221 char *uar_mediatype =
222    "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%u "
223    "AND JobMedia.MediaId=Media.MediaId";