From 581dc99cc5edeafa64264f3f362a25b11d1a65bb Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sun, 25 Mar 2007 14:57:59 +0000 Subject: [PATCH] Implement a new .sql command for bat git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4413 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/dird/ua_dotcmds.c | 43 ++++++++++++++++++++++++++++++------ bacula/src/dird/ua_query.c | 13 +++++------ bacula/technotes-2.1 | 1 + 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/bacula/src/dird/ua_dotcmds.c b/bacula/src/dird/ua_dotcmds.c index 48f9edf27b..af8258ff17 100644 --- a/bacula/src/dird/ua_dotcmds.c +++ b/bacula/src/dird/ua_dotcmds.c @@ -68,6 +68,7 @@ static bool levelscmd(UAContext *ua, const char *cmd); static bool getmsgscmd(UAContext *ua, const char *cmd); static bool api_cmd(UAContext *ua, const char *cmd); +static bool sql_cmd(UAContext *ua, const char *cmd); static bool dot_quit_cmd(UAContext *ua, const char *cmd); static bool dot_help_cmd(UAContext *ua, const char *cmd); @@ -87,6 +88,7 @@ static struct cmdstruct commands[] = { { NT_(".msgs"), msgscmd, NULL}, { NT_(".pools"), poolscmd, NULL}, { NT_(".quit"), dot_quit_cmd, NULL}, + { NT_(".sql"), sql_cmd, NULL}, { NT_(".status"), dot_status_cmd, NULL}, { NT_(".storage"), storagecmd, NULL}, { NT_(".types"), typescmd, NULL} @@ -261,13 +263,6 @@ static bool typescmd(UAContext *ua, const char *cmd) return true; } -static int client_backups_handler(void *ctx, int num_field, char **row) -{ - UAContext *ua = (UAContext *)ctx; - bsendmsg(ua, "| %s | %s | %s | %s | %s | %s | %s | %s |\n", - row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]); - return 0; -} /* * If this command is called, it tells the director that we @@ -287,6 +282,14 @@ static bool api_cmd(UAContext *ua, const char *cmd) return true; } +static int client_backups_handler(void *ctx, int num_field, char **row) +{ + UAContext *ua = (UAContext *)ctx; + bsendmsg(ua, "| %s | %s | %s | %s | %s | %s | %s | %s |\n", + row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]); + return 0; +} + /* * Return the backups for this client * @@ -314,6 +317,32 @@ static bool backupscmd(UAContext *ua, const char *cmd) return true; } +static int sql_handler(void *ctx, int num_field, char **row) +{ + UAContext *ua = (UAContext *)ctx; + POOL_MEM rows(PM_MESSAGE); + + for (int i=0; num_field--; i++) { + if (i == 0) { + pm_strcpy(rows, row[0]); + } else { + pm_strcat(rows, row[i]); + } + pm_strcat(rows, "\t"); + } + bsendmsg(ua, rows.c_str()); + return 0; +} + +static bool sql_cmd(UAContext *ua, const char *cmd) +{ + if (!db_sql_query(ua->db, ua->argk[1], sql_handler, (void *)ua)) { + bsendmsg(ua, _("Query failed: %s. ERR=%s\n"), ua->cmd, db_strerror(ua->db)); + return true; + } + return true; +} + static bool levelscmd(UAContext *ua, const char *cmd) diff --git a/bacula/src/dird/ua_query.c b/bacula/src/dird/ua_query.c index 03c5f934d2..4cac6c2ea2 100644 --- a/bacula/src/dird/ua_query.c +++ b/bacula/src/dird/ua_query.c @@ -252,15 +252,14 @@ static POOLMEM *substitute_prompts(UAContext *ua, */ int sqlquerycmd(UAContext *ua, const char *cmd) { - POOLMEM *query = get_pool_memory(PM_MESSAGE); + POOL_MEM query(PM_MESSAGE); int len; const char *msg; if (!open_client_db(ua)) { - free_pool_memory(query); return 1; } - *query = 0; + *query.c_str() = 0; bsendmsg(ua, _("Entering SQL query mode.\n" "Terminate each query with a semicolon.\n" @@ -272,22 +271,20 @@ int sqlquerycmd(UAContext *ua, const char *cmd) if (len == 0) { break; } - query = check_pool_memory_size(query, len + 1); - if (*query != 0) { + if (*query.c_str() != 0) { pm_strcat(query, " "); } pm_strcat(query, ua->cmd); if (ua->cmd[len-1] == ';') { ua->cmd[len-1] = 0; /* zap ; */ /* Submit query */ - db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST); - *query = 0; /* start new query */ + db_list_sql_query(ua->jcr, ua->db, query.c_str(), prtit, ua, 1, HORZ_LIST); + *query.c_str() = 0; /* start new query */ msg = _("Enter SQL query: "); } else { msg = _("Add to SQL query: "); } } - free_pool_memory(query); bsendmsg(ua, _("End query mode.\n")); return 1; } diff --git a/bacula/technotes-2.1 b/bacula/technotes-2.1 index 46934cb502..06137cdad8 100644 --- a/bacula/technotes-2.1 +++ b/bacula/technotes-2.1 @@ -2,6 +2,7 @@ General: 25Mar07 +kes Implement a new .sql command for bat. kes Implement code to turn off pruning in obtaining the next volume. Pruning is turned off for the status dir command, but turned on for SD requests and for the list nextvolume command. -- 2.39.5