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.
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);
return 0;
}
- lctx.send_dashes();
-
+ list_result(jcr, mdb, sendit, ctx, type);
sql_free_result(mdb);
db_unlock(mdb);
return 1;
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));
}
}
- 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);
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,"
"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);
{
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));
}
}
- 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);
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 */
"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);
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);
"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);
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;
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);
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) {
"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);
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);
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);
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);
}
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);
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);
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);