Added dynamic query types

This commit is contained in:
Dr. Patrick Urbanke
2025-03-19 06:25:00 +01:00
parent 5e4f03f8b2
commit 1f004253e6
6 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
#ifndef SQLGEN_DYNAMIC_COLUMN_HPP_
#define SQLGEN_DYNAMIC_COLUMN_HPP_
#include <string>
#include <vector>
#include "Type.hpp"
namespace sqlgen::dynamic {
struct Column {
std::string alias;
std::string name;
Type type;
};
} // namespace sqlgen::dynamic
#endif

View File

@@ -0,0 +1,19 @@
#ifndef SQLGEN_DYNAMIC_SELECTFROM_HPP_
#define SQLGEN_DYNAMIC_SELECTFROM_HPP_
#include <string>
#include <vector>
#include "Column.hpp"
#include "Table.hpp"
namespace sqlgen::dynamic {
struct SelectFrom {
Table table;
std::vector<Column> columns;
};
} // namespace sqlgen::dynamic
#endif

17
include/dynamic/Table.hpp Normal file
View File

@@ -0,0 +1,17 @@
#ifndef SQLGEN_DYNAMIC_TABLE_HPP_
#define SQLGEN_DYNAMIC_TABLE_HPP_
#include <string>
#include <vector>
namespace sqlgen::dynamic {
struct Table {
std::string alias;
std::string name;
std::string schema = "public";
};
} // namespace sqlgen::dynamic
#endif

18
include/dynamic/Type.hpp Normal file
View File

@@ -0,0 +1,18 @@
#ifndef SQLGEN_DYNAMIC_TABLE_TYPE_HPP_
#define SQLGEN_DYNAMIC_TABLE_TYPE_HPP_
#include <variant>
#include "types.hpp"
namespace sqlgen::dynamic {
using Type =
std::variant<types::Unknown, types::Boolean, types::Float32, types::Float64,
types::Int8, types::Int16, types::Int32, types::Int64,
types::Text, types::Timestamp, types::TimestampWithTZ,
types::VarChar>;
} // namespace sqlgen::dynamic
#endif

View File

@@ -0,0 +1,31 @@
#ifndef SQLGEN_DYNAMIC_SELECT_HPP_
#define SQLGEN_DYNAMIC_SELECT_HPP_
#include <string>
#include <vector>
#include "Column.hpp"
#include "SelectFrom.hpp"
#include "Table.hpp"
namespace sqlgen::dynamic {
SelectFrom select(const std::string& _table,
const std::vector<std::string>& _columns) {
std::vector<Column> columns;
for (const auto& name : _columns) {
columns.emplace_back(Column{.name = name});
}
return SelectFrom{.table = Table{.name = _table}, .columns = columns};
}
SelectFrom select(const std::string& _schema, const std::string& _table,
const std::vector<std::string>& _columns) {
std::vector<Column> columns;
for (const auto& name : _columns) {
columns.emplace_back(Column{.name = name});
}
return SelectFrom{.table = Table{.name = _table, .schema = _schema},
.columns = columns};
}
} // namespace sqlgen::dynamic

66
include/dynamic/types.hpp Normal file
View File

@@ -0,0 +1,66 @@
#ifndef SQLGEN_DYNAMIC_TABLE_TYPES_HPP_
#define SQLGEN_DYNAMIC_TABLE_TYPES_HPP_
#include <cstdint>
#include <string>
namespace sqlgen::dynamic::types {
struct Properties {
bool primary = false;
bool nullable = false;
};
// To be used as the default value.
struct Unknown {};
struct Boolean {
Properties properties;
};
struct Float32 {
Properties properties;
};
struct Float64 {
Properties properties;
};
struct Int8 {
Properties properties;
};
struct Int16 {
Properties properties;
};
struct Int32 {
Properties properties;
};
struct Int64 {
Properties properties;
};
struct Text {
Properties properties;
};
struct Timestamp {
std::string tz;
Properties properties;
};
struct TimestampWithTZ {
std::string tz;
Properties properties;
};
struct VarChar {
uint16_t length;
Properties properties;
};
} // namespace sqlgen::dynamic::types
#endif