blob: ca871ef32dac03e78776a95242b1063745f869c4 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* License); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef TSFILE_CLI_OUTPUT_FORMAT_H
#define TSFILE_CLI_OUTPUT_FORMAT_H
#include <cstdio>
#include <map>
#include <ostream>
#include <string>
#include <vector>
#include "cli/cli_args.h"
#include "common/db_common.h"
namespace tsfile_cli {
enum class OutputFormat { kCsv, kTsv, kJson, kTable };
OutputFormat resolve_format(ParsedArgs::Format f, bool stdout_is_tty);
// Translate a storage-engine error code (common::E_*) into a human-readable
// phrase so CLI diagnostics carry meaning instead of a bare numeric code.
const char* error_code_message(int code);
const char* tsdatatype_name(common::TSDataType t);
const char* tsencoding_name(common::TSEncoding e);
const char* compression_name(common::CompressionType c);
// Replace each maximal ill-formed UTF-8 subpart with U+FFFD (Unicode 3.9.6).
std::string replace_invalid_utf8(const std::string& s);
std::string csv_escape(const std::string& field);
std::string json_escape(const std::string& s);
std::string table_escape(const std::string& s);
// Headers must remain unique even when the result has no rows.
std::string validate_column_names(const std::vector<std::string>& names);
// Metadata repeats object/column names across rows. Only distinct raw names
// that render identically conflict; columns belong to their original object.
class OutputNameValidator {
public:
bool add_object(const std::string& name);
bool add_column(const std::string& object, const std::string& name);
const std::string& error() const { return error_; }
private:
bool add_name(std::map<std::string, std::string>& names,
const std::string& name, const char* kind);
std::map<std::string, std::string> objects_;
std::map<std::string, std::map<std::string, std::string>> columns_;
std::string error_;
};
class RowWriter {
public:
RowWriter(std::ostream& out, OutputFormat fmt,
std::vector<std::string> header,
std::vector<common::TSDataType> types, bool no_header);
~RowWriter();
bool write(const std::vector<std::string>& cells,
const std::vector<bool>& is_null);
bool write(const std::vector<std::string>& cells,
const std::vector<bool>& is_null,
const std::vector<common::TSDataType>& row_types);
bool finish();
// Header validation failures are reported before any output is written.
const std::string& error() const { return error_; }
private:
bool ensure_header();
bool emits_json_bare(common::TSDataType type) const;
std::string format_cell(common::TSDataType type,
const std::string& cell) const;
std::ostream& out_;
OutputFormat fmt_;
std::vector<std::string> header_;
std::vector<std::string> json_keys_;
std::string error_;
std::vector<common::TSDataType> types_;
bool no_header_;
bool header_done_ = false;
std::FILE* spool_ = nullptr;
std::vector<size_t> table_widths_;
};
} // namespace tsfile_cli
#endif // TSFILE_CLI_OUTPUT_FORMAT_H