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 and included
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) {
73 ua->error_msg(_("Could not open %s: ERR=%s\n"), query_file,
78 start_prompt(ua, _("Available queries:\n"));
79 while (fgets(line, sizeof(line), fd) != NULL) {
81 strip_trailing_junk(line);
82 add_prompt(ua, line+1);
85 if ((item=do_prompt(ua, "", _("Choose a query"), NULL, 0)) < 0) {
90 while (fgets(line, sizeof(line), fd) != NULL) {
99 ua->error_msg(_("Could not find query.\n"));
103 for (i=0; i<9; i++) {
106 while (fgets(line, sizeof(line), fd) != NULL) {
107 if (line[0] == '#') {
110 if (line[0] == ':') {
113 strip_trailing_junk(line);
115 if (line[0] == '*') { /* prompt */
117 ua->error_msg(_("Too many prompts in query, max is 9.\n"));
121 prompt[nprompt++] = bstrdup(line+1);
126 pm_strcat(query, " ");
128 pm_strcat(query, line);
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 ua->send_msg("%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 ua->error_msg("%s\n", query);
159 free_pool_memory(query);
160 for (i=0; i<nprompt; i++) {
166 static POOLMEM *substitute_prompts(UAContext *ua,
167 POOLMEM *query, char **prompt, int nprompt)
177 for (i=0; i<9; i++) {
180 new_query = get_pool_memory(PM_FNAME);
182 for (q=query; (p=strchr(q, '%')); ) {
184 olen = o - new_query;
185 new_query = check_pool_memory_size(new_query, olen + p - q + 10);
186 o = new_query + olen;
187 while (q < p) { /* copy up to % */
201 n = (int)(*p) - (int)'1';
204 if (!get_cmd(ua, prompt[n])) {
209 len = strlen(ua->cmd);
210 p = (char *)malloc(len * 2 + 1);
211 db_escape_string(ua->jcr, ua->db, p, ua->cmd, len);
213 olen = o - new_query;
214 new_query = check_pool_memory_size(new_query, olen + strlen(p) + 10);
215 o = new_query + olen;
220 ua->error_msg(_("Warning prompt %d missing.\n"), n+1);
235 olen = o - new_query;
236 new_query = check_pool_memory_size(new_query, olen + strlen(q) + 10);
237 o = new_query + olen;
242 for (i=0; i<9; i++) {
247 free_pool_memory(query);
252 * Get general SQL query for Catalog
254 int sqlquerycmd(UAContext *ua, const char *cmd)
256 POOL_MEM query(PM_MESSAGE);
260 if (!open_client_db(ua)) {
265 ua->send_msg(_("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 if (*query.c_str() != 0) {
276 pm_strcat(query, " ");
278 pm_strcat(query, ua->cmd);
279 if (ua->cmd[len-1] == ';') {
280 ua->cmd[len-1] = 0; /* zap ; */
282 db_list_sql_query(ua->jcr, ua->db, query.c_str(), prtit, ua, 1, HORZ_LIST);
283 *query.c_str() = 0; /* start new query */
284 msg = _("Enter SQL query: ");
286 msg = _("Add to SQL query: ");
289 ua->send_msg(_("End query mode.\n"));