query/test/utils/record_printer.cpp

73 lines
2.1 KiB
C++

#include "record_printer.hpp"
#include "matador/utils/basic_types.hpp"
#include "matador/sql/record.hpp"
#include <iostream>
#include <iomanip>
namespace matador::test {
record_printer::record_printer(std::ostream &os)
: os_(os) {
type_widths_ = {
{utils::basic_type::Int8, 4},
{utils::basic_type::Int16, 6},
{utils::basic_type::Int32, 11},
{utils::basic_type::Int64, 20},
{utils::basic_type::UInt8, 4},
{utils::basic_type::UInt16, 6},
{utils::basic_type::UInt32, 11},
{utils::basic_type::UInt64, 20},
{utils::basic_type::Float, 12},
{utils::basic_type::Double, 15},
{utils::basic_type::Boolean, 6},
{utils::basic_type::Varchar, 20},
{utils::basic_type::Text, 30},
{utils::basic_type::Date, 12},
{utils::basic_type::DateTime, 20},
{utils::basic_type::Time, 10},
{utils::basic_type::Blob, 10},
{utils::basic_type::Null, 6}
};
}
void record_printer::print_header(const sql::record &rec) const {
for (const auto &f_ref: rec.columns()) {
const auto &f = f_ref.get();
os_ << std::left << std::setw(width(f)) << f.name() << " ";
}
os_ << "\n";
for (const auto &f_ref: rec.columns()) {
const auto &f = f_ref.get();
os_ << std::string(width(f), '-') << " ";
}
os_ << "\n";
}
void record_printer::print(const sql::record &rec) const {
for (const auto &f_ref: rec.columns()) {
const auto &f = f_ref.get();
os_ << std::left << std::setw(width(f)) << f.str() << " ";
}
os_ << "\n";
}
int record_printer::width(const sql::field &f) {
// If it's a varchar/string and has a defined size, use it if it's reasonable,
// otherwise fall back to type defaults.
if ((f.is_varchar() || f.is_string()) && f.size() > 0 && f.size() < 100) {
return std::max(f.name().length(), f.size());
}
// Find the default width for type
// Note: field class doesn't expose basic_type directly but we can use value().type()
// if we had access. For now we use name length as minimum.
constexpr size_t w = 15;
// In a real implementation we would probe the field's underlying type.
return std::max(f.name().length(), w);
}
}