3 * Bacula Director -- User Agent Database Query Commands
5 * Kern Sibbald, December MMI
10 Copyright (C) 2001-2005 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 version 2 as ammended with additional clauses defined in the
15 file LICENSE in the main source directory.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 the file LICENSE for additional details.
27 extern DIRRES *director;
29 static POOLMEM *substitute_prompts(UAContext *ua,
30 POOLMEM *query, char **prompt, int nprompt);
33 * Read a file containing SQL queries and prompt
34 * the user to select which one.
39 * *prompt for subst %1
40 * *prompt for subst %2
42 * SQL statement possibly terminated by ;
45 int querycmd(UAContext *ua, const char *cmd)
48 POOLMEM *query = get_pool_memory(PM_MESSAGE);
53 char *query_file = director->query_file;
58 if ((fd=fopen(query_file, "r")) == NULL) {
59 bsendmsg(ua, "Could not open %s: ERR=%s\n", query_file,
64 start_prompt(ua, _("Available queries:\n"));
65 while (fgets(line, sizeof(line), fd) != NULL) {
67 strip_trailing_junk(line);
68 add_prompt(ua, line+1);
71 if ((item=do_prompt(ua, "", _("Choose a query"), NULL, 0)) < 0) {
76 while (fgets(line, sizeof(line), fd) != NULL) {
85 bsendmsg(ua, _("Could not find query.\n"));
92 while (fgets(line, sizeof(line), fd) != NULL) {
99 strip_trailing_junk(line);
101 if (line[0] == '*') { /* prompt */
103 bsendmsg(ua, _("Too many prompts in query, max is 9.\n"));
107 prompt[nprompt++] = bstrdup(line+1);
112 pm_strcat(query, " ");
114 pm_strcat(query, line);
115 if (line[len-1] != ';') {
118 line[len-1] = 0; /* zap ; */
120 query = substitute_prompts(ua, query, prompt, nprompt);
121 Dmsg1(100, "Query2=%s\n", query);
122 if (query[0] == '!') {
123 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
124 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
125 bsendmsg(ua, "%s\n", query);
132 query = substitute_prompts(ua, query, prompt, nprompt);
133 Dmsg1(100, "Query2=%s\n", query);
134 if (query[0] == '!') {
135 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
136 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
137 bsendmsg(ua, "%s\n", query);
145 free_pool_memory(query);
146 for (i=0; i<nprompt; i++) {
152 static POOLMEM *substitute_prompts(UAContext *ua,
153 POOLMEM *query, char **prompt, int nprompt)
163 for (i=0; i<9; i++) {
166 new_query = get_pool_memory(PM_FNAME);
168 for (q=query; (p=strchr(q, '%')); ) {
170 olen = o - new_query;
171 new_query = check_pool_memory_size(new_query, olen + p - q + 10);
172 o = new_query + olen;
173 while (q < p) { /* copy up to % */
187 n = (int)(*p) - (int)'1';
190 if (!get_cmd(ua, prompt[n])) {
195 len = strlen(ua->cmd);
196 p = (char *)malloc(len * 2 + 1);
197 db_escape_string(p, ua->cmd, len);
199 olen = o - new_query;
200 new_query = check_pool_memory_size(new_query, olen + strlen(p) + 10);
201 o = new_query + olen;
206 bsendmsg(ua, _("Warning prompt %d missing.\n"), n+1);
221 olen = o - new_query;
222 new_query = check_pool_memory_size(new_query, olen + strlen(q) + 10);
223 o = new_query + olen;
228 for (i=0; i<9; i++) {
233 free_pool_memory(query);
238 * Get general SQL query for Catalog
240 int sqlquerycmd(UAContext *ua, const char *cmd)
242 POOLMEM *query = get_pool_memory(PM_MESSAGE);
247 free_pool_memory(query);
252 bsendmsg(ua, _("Entering SQL query mode.\n"
253 "Terminate each query with a semicolon.\n"
254 "Terminate query mode with a blank line.\n"));
255 msg = "Enter SQL query: ";
256 while (get_cmd(ua, msg)) {
257 len = strlen(ua->cmd);
258 Dmsg2(400, "len=%d cmd=%s:\n", len, ua->cmd);
262 query = check_pool_memory_size(query, len + 1);
264 pm_strcat(query, " ");
266 pm_strcat(query, ua->cmd);
267 if (ua->cmd[len-1] == ';') {
268 ua->cmd[len-1] = 0; /* zap ; */
270 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
271 *query = 0; /* start new query */
272 msg = _("Enter SQL query: ");
274 msg = _("Add to SQL query: ");
277 free_pool_memory(query);
278 bsendmsg(ua, _("End query mode.\n"));