void bdb_list_jobmedia_records(JCR *jcr, JobId_t JobId, char *volume, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
void bdb_list_filemedia_records(JCR *jcr, JobId_t JobId, uint32_t FileIndex, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
void bdb_list_joblog_records(JCR *jcr, JobId_t JobId, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
- int bdb_list_sql_query(JCR *jcr, const char *query, DB_LIST_HANDLER *sendit, void *ctx, int verbose, e_list_type type);
+ int bdb_list_sql_query(JCR *jcr, const char *title, const char *query, DB_LIST_HANDLER *sendit, void *ctx, int verbose, e_list_type type);
void bdb_list_client_records(JCR *jcr, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
void bdb_list_copies_records(JCR *jcr, uint32_t limit, char *jobids, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
void bdb_list_events_records(JCR *jcr, EVENTS_DBR *rec, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
/* Functions exported by sql.c for use within the cats directory. */
+void json_list_begin(void *vctx, const char *title);
+void json_list_end(void *vctx, int errcode, const char *errmsg);
int list_result(void *vctx, int cols, char **row);
-int list_result(JCR *jcr, BDB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
+int list_result(JCR *jcr, BDB *mdb, const char *title, DB_LIST_HANDLER *send, void *ctx, e_list_type type);
int get_sql_record_max(JCR *jcr, BDB *mdb);
void list_dashes(BDB *mdb, DB_LIST_HANDLER *send, void *ctx);
if (mdb) mdb->bdb_thread_cleanup()
/* sql.c */
+int db_mint64_handler(void *ctx, int num_fields, char **row);
int db_int64_handler(void *ctx, int num_fields, char **row);
int db_strtime_handler(void *ctx, int num_fields, char **row);
int db_list_handler(void *ctx, int num_fields, char **row);
mdb->bdb_list_filemedia_records(jcr, JobId, FI, sendit, ctx, type)
#define db_list_joblog_records(jcr, mdb, JobId, sendit, ctx, type) \
mdb->bdb_list_joblog_records(jcr, JobId, sendit, ctx, type)
-#define db_list_sql_query(jcr, mdb, query, sendit, ctx, verbose, type) \
- mdb->bdb_list_sql_query(jcr, query, sendit, ctx, verbose, type)
+#define db_list_sql_query(jcr, mdb, title, query, sendit, ctx, verbose, type) \
+ mdb->bdb_list_sql_query(jcr, title, query, sendit, ctx, verbose, type)
#define db_list_client_records(jcr, mdb, sendit, ctx, type) \
mdb->bdb_list_client_records(jcr, sendit, ctx, type)
#define db_list_copies_records(jcr, mdb, limit, jobids, sendit, ctx, type) \
}
return 0;
}
-
+
+/*
+ * Called here to retrieve a 32/64 bit integer from the database.
+ * The returned integer will be extended to 64 bit. It can
+ * return multiple values, but the array has to match the number
+ * of fields to be returned.
+ */
+int db_mint64_handler(void *ctx, int num_fields, char **row)
+{
+ int64_t *tab = (int64_t *)ctx;
+ for (int i = 0; i < num_fields; i++) {
+ if (row[i]) {
+ tab[i] = str_to_int64(row[i]);
+ }
+ }
+ return 0;
+}
+
/*
* Called here to retrieve a btime from the database.
* The returned integer will be extended to 64 bit.
LIST_CTX *ctx = (LIST_CTX *)vctx;
bstrncat(ctx->line, str, sizeof(ctx->line));
}
-
+
int list_result(void *vctx, int nb_col, char **row)
{
SQL_FIELD *field;
return 0;
vertical_list:
-
Dmsg1(800, "list_result starts vertical list at %d fields\n", mdb->sql_num_fields());
mdb->sql_field_seek(0);
for (i = 0; i < mdb->sql_num_fields(); i++) {
send(ctx, tmp.c_str());
first = false;
}
- send(ctx, "}\n");
+ send(ctx, "}");
return 0;
}
-
+
+/* Use this function to start a new JSON list */
+void json_list_begin(DB_LIST_HANDLER *send, void *ctx, const char *title)
+{
+ send(ctx, "{\"type\":\"");
+ send(ctx, title);
+ send(ctx, "\", \"data\":");
+}
+
+/* Use this function to end a JSON list */
+void json_list_end(DB_LIST_HANDLER *send, void *ctx, int errcode, const char *errmsg)
+{
+ send(ctx, ",\"error\":0, \"errmsg\":\"\"}\n");
+}
+
/*
* If full_list is set, we list vertically, otherwise, we
* list on one line horizontally.
* Return number of rows
*/
int
-list_result(JCR *jcr, BDB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type)
+list_result(JCR *jcr, BDB *mdb, const char *title, 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];
-
+
+ if (type == JSON_LIST) {
+ json_list_begin(send, ctx, title);
+ }
+
Dmsg0(800, "list_result starts\n");
if (mdb->sql_num_rows() == 0) {
if (type == JSON_LIST) {
- send(ctx, "[]\n");
+ send(ctx, "[]");
+ json_list_end(send, ctx, 0, "");
} else {
send(ctx, _("No results to list.\n"));
}
}
send(ctx, "}");
}
- send(ctx, "]\n");
+ send(ctx, "]");
+ json_list_end(send, ctx, 0, "");
return mdb->sql_num_rows();
}
/*
* Submit general SQL query
*/
-int BDB::bdb_list_sql_query(JCR *jcr, const char *query, DB_LIST_HANDLER *sendit,
+int BDB::bdb_list_sql_query(JCR *jcr, const char *title, const char *query, DB_LIST_HANDLER *sendit,
void *ctx, int verbose, e_list_type type)
{
bdb_lock();
return 0;
}
- list_result(jcr,this, sendit, ctx, type);
+ list_result(jcr,this, title, sendit, ctx, type);
sql_free_result();
bdb_unlock();
return 1;
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "pool", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "client", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "objecttype", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "object", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "object", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "restoreobject", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "media", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "jobmedia", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "filemedia", sendit, ctx, type);
sql_free_result();
bdb_unlock();
sendit(ctx, _("The catalog contains copies as follows:\n"));
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "copy", sendit, ctx, type);
}
sql_free_result();
if (!QueryDB(jcr, cmd)) {
goto bail_out;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "event", sendit, ctx, type);
bail_out:
bdb_unlock();
goto bail_out;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "joblog", sendit, ctx, type);
sql_free_result();
}
}
sql_data_seek(0);
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "job", sendit, ctx, type);
sql_free_result();
bdb_unlock();
return list;
/*
* List Job totals
*
+ * TODO: Adapt for JSON
*/
void BDB::bdb_list_job_totals(JCR *jcr, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
{
return;
}
- list_result(jcr, this, sendit, ctx, HORZ_LIST);
+ list_result(jcr, this, "jobtotal", sendit, ctx, HORZ_LIST);
sql_free_result();
return;
}
- list_result(jcr, this, sendit, ctx, HORZ_LIST);
+ list_result(jcr, this, "jobtotal", sendit, ctx, HORZ_LIST);
sql_free_result();
bdb_unlock();
goto bail_out;
}
- list_result(jcr, this, sendit, ctx, type);
+ list_result(jcr, this, "snapshot", sendit, ctx, type);
bail_out:
sql_free_result();
}
}
Dmsg1(DT_SQL|50, "q=%s\n", tmp.c_str());
- bdb_list_sql_query(jcr, tmp.c_str(), result_handler, ctx, 0, type);
+ bdb_list_sql_query(jcr, "tag", tmp.c_str(), result_handler, ctx, 0, type);
}
bdb_unlock();
}
-
#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL */
query = substitute_prompts(ua, query, prompt, nprompt);
Dmsg1(100, "Query2=%s\n", query);
if (query[0] == '!') {
- db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
- } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
+ db_list_sql_query(ua->jcr, ua->db, "query", query+1, prtit, ua, 0, VERT_LIST);
+ } else if (!db_list_sql_query(ua->jcr, ua->db, "query", query, prtit, ua, 1, HORZ_LIST)) {
ua->send_msg("%s\n", query);
}
query[0] = 0;
query = substitute_prompts(ua, query, prompt, nprompt);
Dmsg1(100, "Query2=%s\n", query);
if (query[0] == '!') {
- db_list_sql_query(ua->jcr, ua->db, query+1, prtit, ua, 0, VERT_LIST);
- } else if (!db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST)) {
+ db_list_sql_query(ua->jcr, ua->db, "query", query+1, prtit, ua, 0, VERT_LIST);
+ } else if (!db_list_sql_query(ua->jcr, ua->db, "query", query, prtit, ua, 1, HORZ_LIST)) {
ua->error_msg("%s\n", query);
}
}
if (ua->cmd[len-1] == ';') {
ua->cmd[len-1] = 0; /* zap ; */
/* Submit query */
- db_list_sql_query(ua->jcr, ua->db, query.c_str(), prtit, ua, 1, HORZ_LIST);
+ db_list_sql_query(ua->jcr, ua->db, "query", query.c_str(), prtit, ua, 1, HORZ_LIST);
*query.c_str() = 0; /* start new query */
msg = _("Enter SQL query: ");
} else {
POOL_MEM q;
Mmsg(q, uar_print_jobs, jobids.list);
ua->send_msg(_("The restore will use the following job(s) as Base\n"));
- db_list_sql_query(ua->jcr, ua->db, q.c_str(), prtit, ua, 1, HORZ_LIST);
+ db_list_sql_query(ua->jcr, ua->db, "basejobs", q.c_str(), prtit, ua, 1, HORZ_LIST);
}
pm_strcpy(rx->BaseJobIds, jobids.list);
}
}
gui_save = ua->jcr->gui;
ua->jcr->gui = true;
- db_list_sql_query(ua->jcr, ua->db, uar_list_jobs, prtit, ua, 1, HORZ_LIST);
+ db_list_sql_query(ua->jcr, ua->db, "job", uar_list_jobs, prtit, ua, 1, HORZ_LIST);
ua->jcr->gui = gui_save;
done = false;
break;
free(fname);
gui_save = ua->jcr->gui;
ua->jcr->gui = true;
- db_list_sql_query(ua->jcr, ua->db, rx->query, prtit, ua, 1, HORZ_LIST);
+ db_list_sql_query(ua->jcr, ua->db, "job", rx->query, prtit, ua, 1, HORZ_LIST);
ua->jcr->gui = gui_save;
done = false;
break;
}
gui_save = ua->jcr->gui;
ua->jcr->gui = true;
- db_list_sql_query(ua->jcr, ua->db, ua->cmd, prtit, ua, 1, HORZ_LIST);
+ db_list_sql_query(ua->jcr, ua->db, "job", ua->cmd, prtit, ua, 1, HORZ_LIST);
ua->jcr->gui = gui_save;
done = false;
break;
"WHERE Object.ObjectName='%s' AND Object.ObjectCategory='%s' AND Object.ObjectType='%s'",
esc.c_str(), esc_cat.c_str(), esc_type.c_str());
- if (!db_list_sql_query(ua->jcr, ua->db, query.c_str(), prtit, ua, 0, HORZ_LIST)) {
+ if (!db_list_sql_query(ua->jcr, ua->db, "object", query.c_str(), prtit, ua, 0, HORZ_LIST)) {
ua->error_msg(_("SQL Query failed: %s\n"), db_strerror(ua->db));
return 0;
}
if (rx->JobIds[0] != 0) {
/* Display a list of Jobs selected for this restore */
- db_list_sql_query(ua->jcr, ua->db, uar_list_temp, prtit, ua, 1,HORZ_LIST);
+ db_list_sql_query(ua->jcr, ua->db, "job", uar_list_temp, prtit, ua, 1,HORZ_LIST);
ok = true;
} else {
ua->warning_msg(_("No jobs found.\n"));
}
query = get_pool_memory(PM_MESSAGE);
Mmsg(query, list_pool, edit_int64(pr.PoolId, ed1));
- db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
+ db_list_sql_query(ua->jcr, ua->db, "pool", query, prtit, ua, 1, HORZ_LIST);
free_pool_memory(query);
ua->info_msg(_("Pool DB record updated from resource.\n"));
return true;