From 329303a50582259047c1e856107d54769e63164f Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Sat, 5 Feb 2011 20:40:32 +0100 Subject: [PATCH] Use old list_result() in db_list_xxx for good formating --- bacula/src/cats/cats.h | 1 + bacula/src/cats/sql.c | 118 +++++++++++++++++++++++++++++++++++++ bacula/src/cats/sql_list.c | 92 +++++++++++++---------------- 3 files changed, 160 insertions(+), 51 deletions(-) diff --git a/bacula/src/cats/cats.h b/bacula/src/cats/cats.h index 6105fa2b67..d6bd623be9 100644 --- a/bacula/src/cats/cats.h +++ b/bacula/src/cats/cats.h @@ -558,6 +558,7 @@ public: * Some functions exported by sql.c for use within the cats directory. */ int list_result(void *vctx, int cols, char **row); +void list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type); void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx); int get_sql_record_max(JCR *jcr, B_DB *mdb); bool check_tables_version(JCR *jcr, B_DB *mdb); diff --git a/bacula/src/cats/sql.c b/bacula/src/cats/sql.c index 143c82e967..3bb8a9c170 100644 --- a/bacula/src/cats/sql.c +++ b/bacula/src/cats/sql.c @@ -568,6 +568,124 @@ vertical_list: return 0; } +/* + * If full_list is set, we list vertically, otherwise, we + * list on one line horizontally. + */ +void +list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type) +{ + SQL_FIELD *field; + SQL_ROW row; + int i, col_len, max_len = 0; + char buf[2000], ewc[30]; + + Dmsg0(800, "list_result starts\n"); + if (sql_num_rows(mdb) == 0) { + send(ctx, _("No results to list.\n")); + return; + } + + Dmsg1(800, "list_result starts looking at %d fields\n", sql_num_fields(mdb)); + /* determine column display widths */ + sql_field_seek(mdb, 0); + for (i = 0; i < sql_num_fields(mdb); i++) { + Dmsg1(800, "list_result processing field %d\n", i); + field = sql_fetch_field(mdb); + if (!field) { + break; + } + col_len = cstrlen(field->name); + if (type == VERT_LIST) { + if (col_len > max_len) { + max_len = col_len; + } + } else { + if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */ + field->max_length += (field->max_length - 1) / 3; + } + if (col_len < (int)field->max_length) { + col_len = field->max_length; + } + if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) { + col_len = 4; /* 4 = length of the word "NULL" */ + } + field->max_length = col_len; /* reset column info */ + } + } + + Dmsg0(800, "list_result finished first loop\n"); + if (type == VERT_LIST) { + goto vertical_list; + } + + Dmsg1(800, "list_result starts second loop looking at %d fields\n", sql_num_fields(mdb)); + list_dashes(mdb, send, ctx); + send(ctx, "|"); + sql_field_seek(mdb, 0); + for (i = 0; i < sql_num_fields(mdb); i++) { + Dmsg1(800, "list_result looking at field %d\n", i); + field = sql_fetch_field(mdb); + if (!field) { + break; + } + max_len = max_length(field->max_length); + bsnprintf(buf, sizeof(buf), " %-*s |", max_len, field->name); + send(ctx, buf); + } + send(ctx, "\n"); + list_dashes(mdb, send, ctx); + + Dmsg1(800, "list_result starts third loop looking at %d fields\n", sql_num_fields(mdb)); + while ((row = sql_fetch_row(mdb)) != NULL) { + sql_field_seek(mdb, 0); + send(ctx, "|"); + for (i = 0; i < sql_num_fields(mdb); i++) { + field = sql_fetch_field(mdb); + if (!field) { + break; + } + max_len = max_length(field->max_length); + if (row[i] == NULL) { + bsnprintf(buf, sizeof(buf), " %-*s |", max_len, "NULL"); + } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) { + bsnprintf(buf, sizeof(buf), " %*s |", max_len, + add_commas(row[i], ewc)); + } else { + bsnprintf(buf, sizeof(buf), " %-*s |", max_len, row[i]); + } + send(ctx, buf); + } + send(ctx, "\n"); + } + list_dashes(mdb, send, ctx); + return; + +vertical_list: + + Dmsg1(800, "list_result starts vertical list at %d fields\n", sql_num_fields(mdb)); + while ((row = sql_fetch_row(mdb)) != NULL) { + sql_field_seek(mdb, 0); + for (i = 0; i < sql_num_fields(mdb); i++) { + field = sql_fetch_field(mdb); + if (!field) { + break; + } + if (row[i] == NULL) { + bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL"); + } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) { + bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, + add_commas(row[i], ewc)); + } else { + bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]); + } + send(ctx, buf); + } + send(ctx, "\n"); + } + return; +} + /* * Open a new connexion to mdb catalog. This function is used * by batch and accurate mode. diff --git a/bacula/src/cats/sql_list.c b/bacula/src/cats/sql_list.c index 8b042f840e..e951e36e56 100644 --- a/bacula/src/cats/sql_list.c +++ b/bacula/src/cats/sql_list.c @@ -55,11 +55,8 @@ int db_list_sql_query(JCR *jcr, B_DB *mdb, const char *query, DB_LIST_HANDLER *sendit, void *ctx, int verbose, e_list_type type) { - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); - db_lock(mdb); - - if (!db_big_sql_query(mdb, query, list_result, &lctx)) { + if (!sql_query(mdb, query, QF_STORE_RESULT)) { Mmsg(mdb->errmsg, _("Query failed: %s\n"), sql_strerror(mdb)); if (verbose) { sendit(ctx, mdb->errmsg); @@ -68,8 +65,7 @@ int db_list_sql_query(JCR *jcr, B_DB *mdb, const char *query, DB_LIST_HANDLER *s return 0; } - lctx.send_dashes(); - + list_result(jcr, mdb, sendit, ctx, type); sql_free_result(mdb); db_unlock(mdb); return 1; @@ -80,7 +76,6 @@ db_list_pool_records(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type) { char esc[MAX_ESCAPE_NAME_LENGTH]; - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); db_lock(mdb); mdb->db_escape_string(jcr, esc, pdbr->Name, strlen(pdbr->Name)); @@ -109,12 +104,12 @@ db_list_pool_records(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr, } } - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } - lctx.send_dashes(); + list_result(jcr, mdb, sendit, ctx, type); sql_free_result(mdb); db_unlock(mdb); @@ -123,8 +118,6 @@ db_list_pool_records(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr, void db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type) { - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); - db_lock(mdb); if (type == VERT_LIST) { Mmsg(mdb->cmd, "SELECT ClientId,Name,Uname,AutoPrune,FileRetention," @@ -135,11 +128,12 @@ db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, "FROM Client ORDER BY ClientId"); } - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } - lctx.send_dashes(); + + list_result(jcr, mdb, sendit, ctx, type); sql_free_result(mdb); db_unlock(mdb); @@ -156,7 +150,6 @@ db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr, { char ed1[50]; char esc[MAX_ESCAPE_NAME_LENGTH]; - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); db_lock(mdb); mdb->db_escape_string(jcr, esc, mdbr->VolumeName, strlen(mdbr->VolumeName)); @@ -197,12 +190,12 @@ db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr, } } - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } - lctx.send_dashes(); + list_result(jcr, mdb, sendit, ctx, type); sql_free_result(mdb); db_unlock(mdb); @@ -212,8 +205,6 @@ void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type) { char ed1[50]; - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); - db_lock(mdb); if (type == VERT_LIST) { if (JobId > 0) { /* do by JobId */ @@ -239,13 +230,12 @@ void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId, "FROM JobMedia,Media WHERE Media.MediaId=JobMedia.MediaId"); } } - - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } - lctx.send_dashes(); + list_result(jcr, mdb, sendit, ctx, type); sql_free_result(mdb); db_unlock(mdb); @@ -255,7 +245,6 @@ void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId, void db_list_copies_records(JCR *jcr, B_DB *mdb, uint32_t limit, char *JobIds, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type) { - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); POOL_MEM str_limit(PM_MESSAGE); POOL_MEM str_jobids(PM_MESSAGE); @@ -278,17 +267,19 @@ void db_list_copies_records(JCR *jcr, B_DB *mdb, uint32_t limit, char *JobIds, "WHERE Job.Type = '%c' %s ORDER BY Job.PriorJobId DESC %s", (char) JT_JOB_COPY, str_jobids.c_str(), str_limit.c_str()); - if (JobIds && JobIds[0]) { - sendit(ctx, _("These JobIds have copies as follows:\n")); - } else { - sendit(ctx, _("The catalog contains copies as follows:\n")); - } - - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { goto bail_out; } - lctx.send_dashes(); + if (sql_num_rows(mdb)) { + if (JobIds && JobIds[0]) { + sendit(ctx, _("These JobIds have copies as follows:\n")); + } else { + sendit(ctx, _("The catalog contains copies as follows:\n")); + } + + list_result(jcr, mdb, sendit, ctx, type); + } sql_free_result(mdb); @@ -300,7 +291,6 @@ void db_list_joblog_records(JCR *jcr, B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type) { char ed1[50]; - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); if (JobId <= 0) { return; @@ -313,12 +303,11 @@ void db_list_joblog_records(JCR *jcr, B_DB *mdb, uint32_t JobId, Mmsg(mdb->cmd, "SELECT LogText FROM Log " "WHERE Log.JobId=%s", edit_int64(JobId, ed1)); } - - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { goto bail_out; } - lctx.send_dashes(); + list_result(jcr, mdb, sendit, ctx, type); sql_free_result(mdb); @@ -340,7 +329,6 @@ db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, char ed1[50]; char limit[100]; char esc[MAX_ESCAPE_NAME_LENGTH]; - LIST_CTX lctx(jcr, mdb, sendit, ctx, type); db_lock(mdb); if (jr->limit > 0) { @@ -394,12 +382,11 @@ db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, "FROM Job ORDER BY StartTime,JobId ASC%s", limit); } } - - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } - lctx.send_dashes(); + list_result(jcr, mdb, sendit, ctx, type); sql_free_result(mdb); db_unlock(mdb); @@ -412,19 +399,18 @@ db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void db_list_job_totals(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx) { - LIST_CTX lctx(jcr, mdb, sendit, ctx, HORZ_LIST); - db_lock(mdb); /* List by Job */ Mmsg(mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) " "AS Files,sum(JobBytes) AS Bytes,Name AS Job FROM Job GROUP BY Name"); - if (!db_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } - lctx.send_dashes(); + + list_result(jcr, mdb, sendit, ctx, HORZ_LIST); sql_free_result(mdb); @@ -432,13 +418,12 @@ db_list_job_totals(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, vo Mmsg(mdb->cmd, "SELECT count(*) AS Jobs,sum(JobFiles) " "AS Files,sum(JobBytes) As Bytes FROM Job"); - lctx.empty(); - if (!db_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } - lctx.send_dashes(); + list_result(jcr, mdb, sendit, ctx, HORZ_LIST); sql_free_result(mdb); db_unlock(mdb); @@ -447,8 +432,8 @@ db_list_job_totals(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, vo void db_list_files_for_job(JCR *jcr, B_DB *mdb, JobId_t jobid, DB_LIST_HANDLER *sendit, void *ctx) { - LIST_CTX lctx(jcr, mdb, sendit, ctx, HORZ_LIST); char ed1[50]; + LIST_CTX lctx(jcr, mdb, sendit, ctx, HORZ_LIST); db_lock(mdb); @@ -482,10 +467,10 @@ db_list_files_for_job(JCR *jcr, B_DB *mdb, JobId_t jobid, DB_LIST_HANDLER *sendi } if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { - db_unlock(mdb); - return; + db_unlock(mdb); + return; } - + lctx.send_dashes(); sql_free_result(mdb); @@ -495,8 +480,8 @@ db_list_files_for_job(JCR *jcr, B_DB *mdb, JobId_t jobid, DB_LIST_HANDLER *sendi void db_list_base_files_for_job(JCR *jcr, B_DB *mdb, JobId_t jobid, DB_LIST_HANDLER *sendit, void *ctx) { - LIST_CTX lctx(jcr, mdb, sendit, ctx, HORZ_LIST); char ed1[50]; + LIST_CTX lctx(jcr, mdb, sendit, ctx, HORZ_LIST); db_lock(mdb); @@ -521,11 +506,16 @@ db_list_base_files_for_job(JCR *jcr, B_DB *mdb, JobId_t jobid, DB_LIST_HANDLER * edit_int64(jobid, ed1)); } - if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); return; } + if (!db_big_sql_query(mdb, mdb->cmd, list_result, &lctx)) { + db_unlock(mdb); + return; + } + lctx.send_dashes(); sql_free_result(mdb); -- 2.39.5