2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
21 * Bacula Director -- User Agent Database Query Commands
23 * Kern Sibbald, December MMI
30 extern DIRRES *director;
32 static POOLMEM *substitute_prompts(UAContext *ua,
33 POOLMEM *query, char **prompt, int nprompt);
36 * Read a file containing SQL queries and prompt
37 * the user to select which one.
42 * *prompt for subst %1
43 * *prompt for subst %2
45 * SQL statement possibly terminated by ;
48 int query_cmd(UAContext *ua, const char *cmd)
51 POOLMEM *query = get_pool_memory(PM_MESSAGE);
58 char *query_file = director->query_file;
59 bool must_prompt = true;
61 if (!open_client_db(ua)) {
64 if ((fd=bfopen(query_file, "rb")) == NULL) {
66 ua->error_msg(_("Could not open %s: ERR=%s\n"), query_file,
71 start_prompt(ua, _("Available queries:\n"));
72 while (fgets(line, sizeof(line), fd) != NULL) {
74 strip_trailing_junk(line);
75 add_prompt(ua, line+1);
80 val = strtol(ua->argk[1], NULL, 10) - 1;
85 ua->error_msg(_("Invalid command line query item specified.\n"));
92 if (must_prompt && (item=do_prompt(ua, "", _("Choose a query"), NULL, 0)) < 0) {
97 while (fgets(line, sizeof(line), fd) != NULL) {
106 ua->error_msg(_("Could not find query.\n"));
110 for (i=0; i<9; i++) {
113 while (fgets(line, sizeof(line), fd) != NULL) {
114 if (line[0] == '#') {
117 if (line[0] == ':') {
120 strip_trailing_junk(line);
122 if (line[0] == '*') { /* prompt */
124 ua->error_msg(_("Too many prompts in query, max is 9.\n"));
128 prompt[nprompt++] = bstrdup(line+1);
133 pm_strcat(query, " ");
135 pm_strcat(query, line);
136 if (line[len-1] != ';') {
139 line[len-1] = 0; /* zap ; */
141 query = substitute_prompts(ua, query, prompt, nprompt);
142 Dmsg1(100, "Query2=%s\n", query);
143 if (query[0] == '!') {
144 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
145 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
146 ua->send_msg("%s\n", query);
153 query = substitute_prompts(ua, query, prompt, nprompt);
154 Dmsg1(100, "Query2=%s\n", query);
155 if (query[0] == '!') {
156 db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
157 } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
158 ua->error_msg("%s\n", query);
166 free_pool_memory(query);
167 for (i=0; i<nprompt; i++) {
173 static POOLMEM *substitute_prompts(UAContext *ua,
174 POOLMEM *query, char **prompt, int nprompt)
184 for (i=0; i<9; i++) {
187 new_query = get_pool_memory(PM_FNAME);
189 for (q=query; (p=strchr(q, '%')); ) {
191 olen = o - new_query;
192 new_query = check_pool_memory_size(new_query, olen + p - q + 10);
193 o = new_query + olen;
194 while (q < p) { /* copy up to % */
208 n = (int)(*p) - (int)'1';
211 if (!get_cmd(ua, prompt[n])) {
216 len = strlen(ua->cmd);
217 p = (char *)malloc(len * 2 + 1);
218 db_escape_string(ua->jcr, ua->db, p, ua->cmd, len);
220 olen = o - new_query;
221 new_query = check_pool_memory_size(new_query, olen + strlen(p) + 10);
222 o = new_query + olen;
227 ua->error_msg(_("Warning prompt %d missing.\n"), n+1);
242 olen = o - new_query;
243 new_query = check_pool_memory_size(new_query, olen + strlen(q) + 10);
244 o = new_query + olen;
249 for (i=0; i<9; i++) {
254 free_pool_memory(query);
259 * Get general SQL query for Catalog
261 int sqlquery_cmd(UAContext *ua, const char *cmd)
263 POOL_MEM query(PM_MESSAGE);
267 if (!open_new_client_db(ua)) {
272 ua->send_msg(_("Entering SQL query mode.\n"
273 "Terminate each query with a semicolon.\n"
274 "Terminate query mode with a blank line.\n"));
275 msg = _("Enter SQL query: ");
276 while (get_cmd(ua, msg)) {
277 len = strlen(ua->cmd);
278 Dmsg2(400, "len=%d cmd=%s:\n", len, ua->cmd);
282 if (*query.c_str() != 0) {
283 pm_strcat(query, " ");
285 pm_strcat(query, ua->cmd);
286 if (ua->cmd[len-1] == ';') {
287 ua->cmd[len-1] = 0; /* zap ; */
289 db_list_sql_query(ua->jcr, ua->db, query.c_str(), prtit, ua, 1, HORZ_LIST);
290 *query.c_str() = 0; /* start new query */
291 msg = _("Enter SQL query: ");
293 msg = _("Add to SQL query: ");
296 ua->send_msg(_("End query mode.\n"));