+dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
+{
+ const struct ovsdb_column **columns;
+ size_t n_columns;
+
+ struct ovsdb_datum **data;
+
+ struct dump_table_aux aux;
+ struct shash_node *node;
+ struct table t;
+ size_t x, y;
+
+ /* Sort columns by name, for reproducibility. */
+ columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
+ n_columns = 0;
+ SHASH_FOR_EACH (node, &ts->columns) {
+ struct ovsdb_column *column = node->data;
+ if (strcmp(column->name, "_version")) {
+ columns[n_columns++] = column;
+ }
+ }
+ qsort(columns, n_columns, sizeof *columns, compare_columns);
+
+ /* Extract data from table. */
+ data = xmalloc(rows->n * sizeof *data);
+ for (y = 0; y < rows->n; y++) {
+ struct shash *row;
+
+ if (rows->elems[y]->type != JSON_OBJECT) {
+ ovs_fatal(0, "row %"PRIuSIZE" in table %s response is not a JSON object: "
+ "%s", y, ts->name, json_to_string(rows->elems[y], 0));
+ }
+ row = json_object(rows->elems[y]);
+
+ data[y] = xmalloc(n_columns * sizeof **data);
+ for (x = 0; x < n_columns; x++) {
+ const struct json *json = shash_find_data(row, columns[x]->name);
+ if (!json) {
+ ovs_fatal(0, "row %"PRIuSIZE" in table %s response lacks %s column",
+ y, ts->name, columns[x]->name);
+ }
+
+ check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
+ &columns[x]->type,
+ json, NULL));
+ }
+ }
+
+ /* Sort rows by column values, for reproducibility. */
+ aux.data = data;
+ aux.columns = columns;
+ aux.n_columns = n_columns;
+ sort(rows->n, compare_rows, swap_rows, &aux);
+
+ /* Add column headings. */
+ table_init(&t);
+ table_set_caption(&t, xasprintf("%s table", ts->name));
+ for (x = 0; x < n_columns; x++) {
+ table_add_column(&t, "%s", columns[x]->name);
+ }
+
+ /* Print rows. */
+ for (y = 0; y < rows->n; y++) {
+ table_add_row(&t);
+ for (x = 0; x < n_columns; x++) {
+ struct cell *cell = table_add_cell(&t);
+ cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
+ cell->type = &columns[x]->type;
+ ovsdb_datum_destroy(&data[y][x], &columns[x]->type);
+ }
+ free(data[y]);
+ }
+ table_print(&t, &table_style);
+ table_destroy(&t);
+
+ free(data);
+ free(columns);
+}
+
+static void
+do_dump(struct jsonrpc *rpc, const char *database,
+ int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ struct jsonrpc_msg *request, *reply;
+ struct ovsdb_schema *schema;
+ struct json *transaction;
+
+ const struct shash_node **tables;
+ size_t n_tables;
+
+ size_t i;
+
+ schema = fetch_schema(rpc, database);
+ tables = shash_sort(&schema->tables);
+ n_tables = shash_count(&schema->tables);
+
+ /* Construct transaction to retrieve entire database. */
+ transaction = json_array_create_1(json_string_create(database));
+ for (i = 0; i < n_tables; i++) {
+ const struct ovsdb_table_schema *ts = tables[i]->data;
+ struct json *op, *columns;
+ struct shash_node *node;
+
+ columns = json_array_create_empty();
+ SHASH_FOR_EACH (node, &ts->columns) {
+ const struct ovsdb_column *column = node->data;
+
+ if (strcmp(column->name, "_version")) {
+ json_array_add(columns, json_string_create(column->name));
+ }
+ }
+
+ op = json_object_create();
+ json_object_put_string(op, "op", "select");
+ json_object_put_string(op, "table", tables[i]->name);
+ json_object_put(op, "where", json_array_create_empty());
+ json_object_put(op, "columns", columns);
+ json_array_add(transaction, op);
+ }
+
+ /* Send request, get reply. */
+ request = jsonrpc_create_request("transact", transaction, NULL);
+ check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
+
+ /* Print database contents. */
+ if (reply->result->type != JSON_ARRAY
+ || reply->result->u.array.n != n_tables) {
+ ovs_fatal(0, "reply is not array of %"PRIuSIZE" elements: %s",
+ n_tables, json_to_string(reply->result, 0));
+ }
+ for (i = 0; i < n_tables; i++) {
+ const struct ovsdb_table_schema *ts = tables[i]->data;
+ const struct json *op_result = reply->result->u.array.elems[i];
+ struct json *rows;
+
+ if (op_result->type != JSON_OBJECT
+ || !(rows = shash_find_data(json_object(op_result), "rows"))
+ || rows->type != JSON_ARRAY) {
+ ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
+ "member array: %s",
+ ts->name, json_to_string(op_result, 0));
+ }
+
+ dump_table(ts, &rows->u.array);
+ }
+
+ jsonrpc_msg_destroy(reply);
+ free(tables);
+ ovsdb_schema_destroy(schema);
+}
+
+static void
+do_help(struct jsonrpc *rpc OVS_UNUSED, const char *database OVS_UNUSED,
+ int argc OVS_UNUSED, char *argv[] OVS_UNUSED)