Started developing Limit and OrderBy

This commit is contained in:
Dr. Patrick Urbanke
2025-04-29 08:42:29 +02:00
parent 79558f0cd4
commit 25bc4253d7
7 changed files with 95 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
#include "sqlgen/Ref.hpp"
#include "sqlgen/Result.hpp"
#include "sqlgen/Varchar.hpp"
#include "sqlgen/col.hpp"
#include "sqlgen/read.hpp"
#include "sqlgen/write.hpp"

16
include/sqlgen/col.hpp Normal file
View File

@@ -0,0 +1,16 @@
#ifndef SQLGEN_COL_HPP_
#define SQLGEN_COL_HPP_
#include <rfl.hpp>
namespace sqlgen {
template <rfl::internal::StringLiteral _name>
struct Col {};
template <rfl::internal::StringLiteral _name>
const auto col = Col<_name>{};
} // namespace sqlgen
#endif

View File

@@ -0,0 +1,16 @@
#ifndef SQLGEN_DYNAMIC_LIMIT_HPP_
#define SQLGEN_DYNAMIC_LIMIT_HPP_
#include <vector>
#include "Column.hpp"
namespace sqlgen::dynamic {
struct Limit {
size_t val;
};
} // namespace sqlgen::dynamic
#endif

View File

@@ -0,0 +1,20 @@
#ifndef SQLGEN_DYNAMIC_ORDERBY_HPP_
#define SQLGEN_DYNAMIC_ORDERBY_HPP_
#include <vector>
#include "Column.hpp"
namespace sqlgen::dynamic {
struct OrderBy {
struct Wrapper {
Column column;
bool desc = false;
};
std::vector<Wrapper> columns;
};
} // namespace sqlgen::dynamic
#endif

View File

@@ -1,10 +1,13 @@
#ifndef SQLGEN_DYNAMIC_SELECTFROM_HPP_
#define SQLGEN_DYNAMIC_SELECTFROM_HPP_
#include <optional>
#include <string>
#include <vector>
#include "Column.hpp"
#include "Limit.hpp"
#include "OrderBy.hpp"
#include "Table.hpp"
namespace sqlgen::dynamic {
@@ -12,6 +15,8 @@ namespace sqlgen::dynamic {
struct SelectFrom {
Table table;
std::vector<Column> columns;
std::optional<OrderBy> order_by = std::nullopt;
std::optional<Limit> limit = std::nullopt;
};
} // namespace sqlgen::dynamic

View File

@@ -122,7 +122,12 @@ std::string Connection::select_from_to_sql(
const dynamic::SelectFrom& _stmt) const noexcept {
using namespace std::ranges::views;
const auto order_by_to_str = [](const auto& _w) -> std::string {
return "\"" + _w.column.name + "\"" + (_w.desc ? " DESC" : "");
};
std::stringstream stream;
stream << "SELECT ";
stream << internal::strings::join(
", ", internal::collect::vector(_stmt.columns | transform(get_name) |
@@ -131,7 +136,20 @@ std::string Connection::select_from_to_sql(
if (_stmt.table.schema) {
stream << wrap_in_quotes(*_stmt.table.schema) << ".";
}
stream << wrap_in_quotes(_stmt.table.name) << ";";
stream << wrap_in_quotes(_stmt.table.name);
if (_stmt.order_by) {
stream << " ORDER BY "
<< internal::strings::join(
", ", internal::collect::vector(_stmt.order_by->columns |
transform(order_by_to_str)));
}
if (_stmt.limit) {
stream << " LIMIT " << _stmt.limit->val;
}
stream << ";";
return stream.str();
}

View File

@@ -143,6 +143,10 @@ std::string Connection::select_from_to_sql(
return "\"" + _col.name + "\"";
};
const auto order_by_to_str = [](const auto& _w) -> std::string {
return "\"" + _w.column.name + "\"" + (_w.desc ? " DESC" : "");
};
std::stringstream stream;
stream << "SELECT ";
stream << internal::strings::join(
@@ -152,7 +156,20 @@ std::string Connection::select_from_to_sql(
if (_stmt.table.schema) {
stream << "\"" << *_stmt.table.schema << "\".";
}
stream << "\"" << _stmt.table.name << "\";";
stream << "\"" << _stmt.table.name << "\"";
if (_stmt.order_by) {
stream << " ORDER BY "
<< internal::strings::join(
", ", internal::collect::vector(_stmt.order_by->columns |
transform(order_by_to_str)));
}
if (_stmt.limit) {
stream << " LIMIT " << _stmt.limit->val;
}
stream << ";";
return stream.str();
}