3 * Bacula Director -- User Agent Database Query Commands
5 * Kern Sibbald, December MMI
11 Copyright (C) 2000-2003 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 char *substitute_prompts(UAContext *ua,
36 char *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, char *cmd)
54 POOLMEM *query = get_pool_memory(PM_MESSAGE);
59 char *query_file = director->query_file;
62 free_pool_memory(query);
65 if ((fd=fopen(query_file, "r")) == NULL) {
66 bsendmsg(ua, "Could not open %s: ERR=%s\n", query_file,
68 free_pool_memory(query);
72 start_prompt(ua, _("Available queries:\n"));
73 while (fgets(line, sizeof(line), fd) != NULL) {
75 strip_trailing_junk(line);
76 add_prompt(ua, line+1);
79 if ((item=do_prompt(ua, "", _("Choose a query"), NULL, 0)) < 0) {
81 free_pool_memory(query);
86 while (fgets(line, sizeof(line), fd) != NULL) {
95 bsendmsg(ua, _("Could not find query.\n"));
97 free_pool_memory(query);
101 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);
124 query = check_pool_memory_size(query, len + 1);
129 if (line[len-1] != ';') {
132 line[len-1] = 0; /* zap ; */
134 query = substitute_prompts(ua, query, prompt, nprompt);
135 Dmsg1(100, "Query2=%s\n", query);
136 if (query[0] == '!') {
137 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
138 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
139 bsendmsg(ua, "%s\n", query);
146 query = substitute_prompts(ua, query, prompt, nprompt);
147 Dmsg1(100, "Query2=%s\n", query);
148 if (query[0] == '!') {
149 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
150 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
151 bsendmsg(ua, "%s\n", query);
154 free_pool_memory(query);
155 for (i=0; i<nprompt; i++) {
161 static POOLMEM *substitute_prompts(UAContext *ua,
162 char *query, char **prompt, int nprompt)
172 for (i=0; i<9; i++) {
175 new_query = get_pool_memory(PM_MESSAGE);
176 new_query = check_pool_memory_size(new_query, strlen(query) +100);
179 for (q=query; (p=strchr(q, '%')); ) {
181 while (q < p) { /* copy up to % */
196 n = (int)(*p) - (int)'1';
199 if (!get_cmd(ua, prompt[n])) {
204 len = strlen(ua->cmd);
205 p = (char *)malloc(len * 2 + 1);
206 db_escape_string(p, ua->cmd, len);
208 new_query = check_pool_memory_size(new_query, olen + strlen(p) + 1);
214 bsendmsg(ua, _("Warning prompt %d missing.\n"), n+1);
231 new_query = check_pool_memory_size(new_query, olen + strlen(q) + 1);
236 for (i=0; i<9; i++) {
241 free_pool_memory(query);
246 * Get general SQL query for Catalog
248 int sqlquerycmd(UAContext *ua, char *cmd)
250 POOLMEM *query = get_pool_memory(PM_MESSAGE);
255 free_pool_memory(query);
260 bsendmsg(ua, _("Entering SQL query mode.\n\
261 Terminate each query with a semicolon.\n\
262 Terminate query mode with a blank line.\n"));
263 msg = "Enter SQL query: ";
264 while (get_cmd(ua, msg)) {
265 len = strlen(ua->cmd);
266 Dmsg2(400, "len=%d cmd=%s:\n", len, ua->cmd);
270 query = check_pool_memory_size(query, len + 1);
274 strcat(query, ua->cmd);
275 if (ua->cmd[len-1] == ';') {
276 ua->cmd[len-1] = 0; /* zap ; */
278 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
279 *query = 0; /* start new query */
280 msg = _("Enter SQL query: ");
282 msg = _("Add to SQL query: ");
285 free_pool_memory(query);
286 bsendmsg(ua, _("End query mode.\n"));