mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-02 07:30:17 -06:00
Started developing Limit and OrderBy
This commit is contained in:
@@ -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
16
include/sqlgen/col.hpp
Normal 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
|
||||
16
include/sqlgen/dynamic/Limit.hpp
Normal file
16
include/sqlgen/dynamic/Limit.hpp
Normal 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
|
||||
20
include/sqlgen/dynamic/OrderBy.hpp
Normal file
20
include/sqlgen/dynamic/OrderBy.hpp
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user