3 * Bacula Director -- User Agent Database Query Commands
5 * Kern Sibbald, December MMI
10 Bacula® - The Network Backup Solution
12 Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
14 The main author of Bacula is Kern Sibbald, with contributions from
15 many others, a complete list can be found in the file AUTHORS.
16 This program is Free Software; you can redistribute it and/or
17 modify it under the terms of version two of the GNU General Public
18 License as published by the Free Software Foundation plus additions
19 that are listed in the file LICENSE.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 Bacula® is a registered trademark of John Walker.
32 The licensor of Bacula is the Free Software Foundation Europe
33 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34 Switzerland, email:ftf@fsfeurope.org.
40 extern DIRRES *director;
42 static POOLMEM *substitute_prompts(UAContext *ua,
43 POOLMEM *query, char **prompt, int nprompt);
46 * Read a file containing SQL queries and prompt
47 * the user to select which one.
52 * *prompt for subst %1
53 * *prompt for subst %2
55 * SQL statement possibly terminated by ;
58 int querycmd(UAContext *ua, const char *cmd)
61 POOLMEM *query = get_pool_memory(PM_MESSAGE);
66 char *query_file = director->query_file;
68 if (!open_client_db(ua)) {
71 if ((fd=fopen(query_file, "rb")) == NULL) {
72 bsendmsg(ua, _("Could not open %s: ERR=%s\n"), query_file,
77 start_prompt(ua, _("Available queries:\n"));
78 while (fgets(line, sizeof(line), fd) != NULL) {
80 strip_trailing_junk(line);
81 add_prompt(ua, line+1);
84 if ((item=do_prompt(ua, "", _("Choose a query"), NULL, 0)) < 0) {
89 while (fgets(line, sizeof(line), fd) != NULL) {
98 bsendmsg(ua, _("Could not find query.\n"));
102 for (i=0; i<9; i++) {
105 while (fgets(line, sizeof(line), fd) != NULL) {
106 if (line[0] == '#') {
109 if (line[0] == ':') {
112 strip_trailing_junk(line);
114 if (line[0] == '*') { /* prompt */
116 bsendmsg(ua, _("Too many prompts in query, max is 9.\n"));
120 prompt[nprompt++] = bstrdup(line+1);
125 pm_strcat(query, " ");
127 pm_strcat(query, line);
128 if (line[len-1] != ';') {
131 line[len-1] = 0; /* zap ; */
133 query = substitute_prompts(ua, query, prompt, nprompt);
134 Dmsg1(100, "Query2=%s\n", query);
135 if (query[0] == '!') {
136 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
137 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
138 bsendmsg(ua, "%s\n", query);
145 query = substitute_prompts(ua, query, prompt, nprompt);
146 Dmsg1(100, "Query2=%s\n", query);
147 if (query[0] == '!') {
148 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
149 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
150 bsendmsg(ua, "%s\n", query);
158 free_pool_memory(query);
159 for (i=0; i<nprompt; i++) {
165 static POOLMEM *substitute_prompts(UAContext *ua,
166 POOLMEM *query, char **prompt, int nprompt)
176 for (i=0; i<9; i++) {
179 new_query = get_pool_memory(PM_FNAME);
181 for (q=query; (p=strchr(q, '%')); ) {
183 olen = o - new_query;
184 new_query = check_pool_memory_size(new_query, olen + p - q + 10);
185 o = new_query + olen;
186 while (q < p) { /* copy up to % */
200 n = (int)(*p) - (int)'1';
203 if (!get_cmd(ua, prompt[n])) {
208 len = strlen(ua->cmd);
209 p = (char *)malloc(len * 2 + 1);
210 db_escape_string(p, ua->cmd, len);
212 olen = o - new_query;
213 new_query = check_pool_memory_size(new_query, olen + strlen(p) + 10);
214 o = new_query + olen;
219 bsendmsg(ua, _("Warning prompt %d missing.\n"), n+1);
234 olen = o - new_query;
235 new_query = check_pool_memory_size(new_query, olen + strlen(q) + 10);
236 o = new_query + olen;
241 for (i=0; i<9; i++) {
246 free_pool_memory(query);
251 * Get general SQL query for Catalog
253 int sqlquerycmd(UAContext *ua, const char *cmd)
255 POOLMEM *query = get_pool_memory(PM_MESSAGE);
259 if (!open_client_db(ua)) {
260 free_pool_memory(query);
265 bsendmsg(ua, _("Entering SQL query mode.\n"
266 "Terminate each query with a semicolon.\n"
267 "Terminate query mode with a blank line.\n"));
268 msg = _("Enter SQL query: ");
269 while (get_cmd(ua, msg)) {
270 len = strlen(ua->cmd);
271 Dmsg2(400, "len=%d cmd=%s:\n", len, ua->cmd);
275 query = check_pool_memory_size(query, len + 1);
277 pm_strcat(query, " ");
279 pm_strcat(query, ua->cmd);
280 if (ua->cmd[len-1] == ';') {
281 ua->cmd[len-1] = 0; /* zap ; */
283 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
284 *query = 0; /* start new query */
285 msg = _("Enter SQL query: ");
287 msg = _("Add to SQL query: ");
290 free_pool_memory(query);
291 bsendmsg(ua, _("End query mode.\n"));