usql/row.h

107 lines
2.8 KiB
C++

#pragma once
#include "exception.h"
#include "parser.h"
#include <vector>
namespace usql {
struct ColValue {
virtual bool isNull() { return false; };
virtual long getIntValue() = 0;
virtual double getDoubleValue() = 0;
virtual std::string getStringValue() = 0;
virtual int compare(ColValue * other) = 0;
virtual ~ColValue() = default;
};
struct ColNullValue : ColValue {
bool isNull() override { return true; };
long getIntValue() override { throw Exception("Not supported"); };
double getDoubleValue() override { throw Exception("Not supported"); };
std::string getStringValue() override { return "null"; };
int compare(ColValue * other) override;
};
struct ColIntegerValue : ColValue {
ColIntegerValue(long value) : m_integer(value) {};
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
long getIntValue() override { return m_integer; };
double getDoubleValue() override { return (double) m_integer; };
std::string getStringValue() override { return std::to_string(m_integer); };
int compare(ColValue * other) override;
long m_integer;
};
struct ColDoubleValue : ColValue {
ColDoubleValue(double value) : m_double(value) {};
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
long getIntValue() override { return (long) m_double; };
double getDoubleValue() override { return m_double; };
std::string getStringValue() override { return std::to_string(m_double); };
int compare(ColValue * other) override;
double m_double;
};
struct ColStringValue : ColValue {
ColStringValue(const std::string &value) : m_string(value) {};
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
long getIntValue() override { return std::stoi(m_string); };
double getDoubleValue() override { return std::stod(m_string); };
std::string getStringValue() override { return m_string; };
int compare(ColValue * other) override;
std::string m_string;
};
class Row {
public:
Row(int cols_count);
Row(const Row &other);
Row &operator=(Row other);
bool operator==(const Row &other) const {return this->compare(other) == 0; };
void setColumnNull(int col_index);
void setColumnValue(int col_index, long value);
void setColumnValue(int col_index, double value);
void setColumnValue(int col_index, const std::string &value);
void setColumnValue(ColDefNode *col_def, ColValue *col_value);
void setColumnValue(ColDefNode *col_def, ValueNode *col_value);
ColValue &operator[](int i) { return *m_columns[i]; }
ColValue * ith_column(int i) const { return m_columns[i].get(); }
int compare(const Row &other) const;
void print(const std::vector<int> & col_char_sizes);
private:
std::vector<std::unique_ptr<ColValue>> m_columns;
};
} // namespace