|
| 1 | +/************************************************************\ |
| 2 | + * Copyright 2024 Lawrence Livermore National Security, LLC |
| 3 | + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | + * |
| 5 | + * This file is part of the Flux resource manager framework. |
| 6 | + * For details, see https://github.com/flux-framework. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: LGPL-3.0 |
| 9 | +\************************************************************/ |
| 10 | + |
| 11 | +#if HAVE_CONFIG_H |
| 12 | +#include "config.h" |
| 13 | +#endif |
| 14 | +#ifdef HAVE_ARGZ_ADD |
| 15 | +#include <argz.h> |
| 16 | +#else |
| 17 | +#include "src/common/libmissing/argz.h" |
| 18 | +#endif |
| 19 | +#include <unistd.h> |
| 20 | + |
| 21 | +#include "builtin.h" |
| 22 | + |
| 23 | +static void query_continuation (flux_future_t *f, void *arg) |
| 24 | +{ |
| 25 | + flux_reactor_t *r = flux_future_get_reactor (f); |
| 26 | + const char *row; |
| 27 | + |
| 28 | + if (flux_rpc_get (f, &row) < 0) { |
| 29 | + if (errno == ENODATA) |
| 30 | + flux_reactor_stop (r); |
| 31 | + else { |
| 32 | + log_msg ("%s", future_strerror (f, errno)); |
| 33 | + flux_reactor_stop_error (r); |
| 34 | + } |
| 35 | + return; |
| 36 | + } |
| 37 | + printf ("%s\n", row); |
| 38 | + flux_future_reset (f); |
| 39 | +} |
| 40 | + |
| 41 | +static int cmd_sql (optparse_t *p, int ac, char *av[]) |
| 42 | +{ |
| 43 | + int n = optparse_option_index (p); |
| 44 | + int e; |
| 45 | + char *argz = NULL; |
| 46 | + size_t argz_len = 0; |
| 47 | + flux_t *h; |
| 48 | + flux_future_t *f; |
| 49 | + int rc; |
| 50 | + |
| 51 | + log_init ("flux-sql"); |
| 52 | + if (n == ac) { |
| 53 | + optparse_print_usage (p); |
| 54 | + exit (1); |
| 55 | + } |
| 56 | + /* Make one SQL query string from one or more query arguments |
| 57 | + */ |
| 58 | + if ((e = argz_create (&av[n], &argz, &argz_len)) != 0) |
| 59 | + log_errn_exit (e, "error processing arguments"); |
| 60 | + argz_stringify (argz, argz_len, ' '); |
| 61 | + |
| 62 | + if (!(h = builtin_get_flux_handle (p))) |
| 63 | + log_err_exit ("could not contact flux broker"); |
| 64 | + |
| 65 | + if (!(f = flux_rpc_pack (h, |
| 66 | + "job-sql.query", |
| 67 | + 0, |
| 68 | + FLUX_RPC_STREAMING, |
| 69 | + "{s:s}", |
| 70 | + "query", argz)) |
| 71 | + || flux_future_then (f, -1, query_continuation, p) < 0) |
| 72 | + log_err_exit ("error sending query"); |
| 73 | + rc = flux_reactor_run (flux_get_reactor (h), 0); |
| 74 | + flux_future_destroy (f); |
| 75 | + |
| 76 | + flux_close (h); |
| 77 | + free (argz); |
| 78 | + return rc; |
| 79 | +} |
| 80 | + |
| 81 | +int subcommand_sql_register (optparse_t *p) |
| 82 | +{ |
| 83 | + if (optparse_reg_subcommand (p, |
| 84 | + "sql", |
| 85 | + cmd_sql, |
| 86 | + "[OPTIONS...] QUERY", |
| 87 | + "Query the SQL job database", |
| 88 | + 0, |
| 89 | + NULL) != OPTPARSE_SUCCESS) |
| 90 | + return -1; |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +/* |
| 95 | + * vi: ts=4 sw=4 expandtab |
| 96 | + */ |
0 commit comments