3 * Bacula Director -- User Agent Database Query Commands
5 * Kern Sibbald, December MMI
11 Copyright (C) 2001-2004 Kern Sibbald and John Walker
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.
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.
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,
33 extern DIRRES *director;
35 static POOLMEM *substitute_prompts(UAContext *ua,
36 POOLMEM *query, char **prompt, int nprompt);
39 * Read a file containing SQL queries and prompt
40 * the user to select which one.
45 * *prompt for subst %1
46 * *prompt for subst %2
48 * SQL statement possibly terminated by ;
51 int querycmd(UAContext *ua, const char *cmd)
54 POOLMEM *query = get_pool_memory(PM_MESSAGE);
59 char *query_file = director->query_file;
64 if ((fd=fopen(query_file, "r")) == NULL) {
65 bsendmsg(ua, "Could not open %s: ERR=%s\n", query_file,
70 start_prompt(ua, _("Available queries:\n"));
71 while (fgets(line, sizeof(line), fd) != NULL) {
73 strip_trailing_junk(line);
74 add_prompt(ua, line+1);
77 if ((item=do_prompt(ua, "", _("Choose a query"), NULL, 0)) < 0) {
82 while (fgets(line, sizeof(line), fd) != NULL) {
91 bsendmsg(ua, _("Could not find query.\n"));
98 while (fgets(line, sizeof(line), fd) != NULL) {
102 if (line[0] == ':') {
105 strip_trailing_junk(line);
107 if (line[0] == '*') { /* prompt */
109 bsendmsg(ua, _("Too many prompts in query, max is 9.\n"));
113 prompt[nprompt++] = bstrdup(line+1);
118 pm_strcat(query, " ");
120 pm_strcat(query, line);
121 if (line[len-1] != ';') {
124 line[len-1] = 0; /* zap ; */
126 query = substitute_prompts(ua, query, prompt, nprompt);
127 Dmsg1(100, "Query2=%s\n", query);
128 if (query[0] == '!') {
129 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
130 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
131 bsendmsg(ua, "%s\n", query);
138 query = substitute_prompts(ua, query, prompt, nprompt);
139 Dmsg1(100, "Query2=%s\n", query);
140 if (query[0] == '!') {
141 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
142 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
143 bsendmsg(ua, "%s\n", query);
151 free_pool_memory(query);
152 for (i=0; i<nprompt; i++) {
158 static POOLMEM *substitute_prompts(UAContext *ua,
159 POOLMEM *query, char **prompt, int nprompt)
169 for (i=0; i<9; i++) {
172 new_query = get_pool_memory(PM_FNAME);
174 for (q=query; (p=strchr(q, '%')); ) {
176 olen = o - new_query;
177 new_query = check_pool_memory_size(new_query, olen + p - q + 10);
178 o = new_query + olen;
179 while (q < p) { /* copy up to % */
193 n = (int)(*p) - (int)'1';
196 if (!get_cmd(ua, prompt[n])) {
201 len = strlen(ua->cmd);
202 p = (char *)malloc(len * 2 + 1);
203 db_escape_string(p, ua->cmd, len);
205 olen = o - new_query;
206 new_query = check_pool_memory_size(new_query, olen + strlen(p) + 10);
207 o = new_query + olen;
212 bsendmsg(ua, _("Warning prompt %d missing.\n"), n+1);
227 olen = o - new_query;
228 new_query = check_pool_memory_size(new_query, olen + strlen(q) + 10);
229 o = new_query + olen;
234 for (i=0; i<9; i++) {
239 free_pool_memory(query);
244 * Get general SQL query for Catalog
246 int sqlquerycmd(UAContext *ua, const char *cmd)
248 POOLMEM *query = get_pool_memory(PM_MESSAGE);
253 free_pool_memory(query);
258 bsendmsg(ua, _("Entering SQL query mode.\n"
259 "Terminate each query with a semicolon.\n"
260 "Terminate query mode with a blank line.\n"));
261 msg = "Enter SQL query: ";
262 while (get_cmd(ua, msg)) {
263 len = strlen(ua->cmd);
264 Dmsg2(400, "len=%d cmd=%s:\n", len, ua->cmd);
268 query = check_pool_memory_size(query, len + 1);
270 pm_strcat(query, " ");
272 pm_strcat(query, ua->cmd);
273 if (ua->cmd[len-1] == ';') {
274 ua->cmd[len-1] = 0; /* zap ; */
276 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
277 *query = 0; /* start new query */
278 msg = _("Enter SQL query: ");
280 msg = _("Add to SQL query: ");
283 free_pool_memory(query);
284 bsendmsg(ua, _("End query mode.\n"));