query builder create progress
This commit is contained in:
parent
9e2d1358bb
commit
b9d9609c28
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef QUERY_DIALECT_HPP
|
#ifndef QUERY_DIALECT_HPP
|
||||||
#define QUERY_DIALECT_HPP
|
#define QUERY_DIALECT_HPP
|
||||||
|
|
||||||
|
#include "matador/sql/types.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
@ -60,6 +62,7 @@ public:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const std::string& token_at(token_t token) const;
|
const std::string& token_at(token_t token) const;
|
||||||
|
const std::string& data_type_at(data_type_t type) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare sql dialect identifier for execution
|
* Prepare sql dialect identifier for execution
|
||||||
|
|
@ -115,6 +118,7 @@ private:
|
||||||
{token_t::DROP, "DROP"},
|
{token_t::DROP, "DROP"},
|
||||||
{token_t::REMOVE, "DELETE"},
|
{token_t::REMOVE, "DELETE"},
|
||||||
{token_t::INSERT, "INSERT"},
|
{token_t::INSERT, "INSERT"},
|
||||||
|
{token_t::TABLE, "TABLE"},
|
||||||
{token_t::INTO, "INTO"},
|
{token_t::INTO, "INTO"},
|
||||||
{token_t::VALUES, "VALUES"},
|
{token_t::VALUES, "VALUES"},
|
||||||
{token_t::UPDATE, "UPDATE"},
|
{token_t::UPDATE, "UPDATE"},
|
||||||
|
|
@ -145,6 +149,31 @@ private:
|
||||||
{token_t::STRING_QUOTE, "'"},
|
{token_t::STRING_QUOTE, "'"},
|
||||||
{token_t::NONE, ""}
|
{token_t::NONE, ""}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using data_type_to_string_map = std::unordered_map<data_type_t, std::string>;
|
||||||
|
data_type_to_string_map data_types_ {
|
||||||
|
{data_type_t::type_char, "TINYINT"},
|
||||||
|
{data_type_t::type_short, "SMALLINT"},
|
||||||
|
{data_type_t::type_int, "INTEGER"},
|
||||||
|
{data_type_t::type_long, "BIGINT"},
|
||||||
|
{data_type_t::type_long_long, "BIGINT"},
|
||||||
|
{data_type_t::type_unsigned_char, "TINYINT"},
|
||||||
|
{data_type_t::type_unsigned_short, "INTEGER"},
|
||||||
|
{data_type_t::type_unsigned_int, "BIGINT"},
|
||||||
|
{data_type_t::type_unsigned_long, "BIGINT"},
|
||||||
|
{data_type_t::type_unsigned_long_long, "BIGINT"},
|
||||||
|
{data_type_t::type_float, "FLOAT"},
|
||||||
|
{data_type_t::type_double, "DOUBLE"},
|
||||||
|
{data_type_t::type_bool, "BOOLEAN"},
|
||||||
|
{data_type_t::type_char_pointer, "VARCHAR"},
|
||||||
|
{data_type_t::type_varchar, "VARCHAR"},
|
||||||
|
{data_type_t::type_text, "TEXT"},
|
||||||
|
{data_type_t::type_date, "DATE"},
|
||||||
|
{data_type_t::type_time, "DATETIME"},
|
||||||
|
{data_type_t::type_blob, "BLOB"},
|
||||||
|
{data_type_t::type_null, "NULL"},
|
||||||
|
{data_type_t::type_unknown, "UNKNOWN"}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.cpp
2
main.cpp
|
|
@ -11,7 +11,7 @@ int main() {
|
||||||
|
|
||||||
sql::query_builder query(d);
|
sql::query_builder query(d);
|
||||||
|
|
||||||
query.create().table("person", {
|
std::cout << query.create().table("person", {
|
||||||
{ "id", sql::data_type_traits<unsigned long>::builtin_type() },
|
{ "id", sql::data_type_traits<unsigned long>::builtin_type() },
|
||||||
{ "name", sql::data_type_traits<std::string>::builtin_type(255), 255 },
|
{ "name", sql::data_type_traits<std::string>::builtin_type(255), 255 },
|
||||||
{ "id", sql::data_type_traits<unsigned long>::builtin_type() }
|
{ "id", sql::data_type_traits<unsigned long>::builtin_type() }
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,16 @@
|
||||||
#include "matador/utils/string.hpp"
|
#include "matador/utils/string.hpp"
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
const std::string& dialect::token_at(dialect::token_t token) const {
|
const std::string& dialect::token_at(dialect::token_t token) const
|
||||||
|
{
|
||||||
return tokens_.at(token);
|
return tokens_.at(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string &dialect::data_type_at(data_type_t type) const
|
||||||
|
{
|
||||||
|
return data_types_.at(type);
|
||||||
|
}
|
||||||
|
|
||||||
std::string dialect::prepare_identifier(const std::string &str) const
|
std::string dialect::prepare_identifier(const std::string &str) const
|
||||||
{
|
{
|
||||||
std::string result(str);
|
std::string result(str);
|
||||||
|
|
|
||||||
|
|
@ -72,8 +72,25 @@ query_builder &query_builder::table(const std::string &table, std::initializer_l
|
||||||
transition_to(state_t::QUERY_TABLE);
|
transition_to(state_t::QUERY_TABLE);
|
||||||
|
|
||||||
query_parts_.emplace_back(dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table) + " ");
|
query_parts_.emplace_back(dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table) + " ");
|
||||||
// auto cols =
|
|
||||||
|
|
||||||
|
std::string result = "(";
|
||||||
|
|
||||||
|
for (const auto &col : columns) {
|
||||||
|
result.append(dialect_.prepare_identifier(col.name()) + " " + dialect_.data_type_at(col.type()));
|
||||||
|
if (col.attributes().size() > 0) {
|
||||||
|
result.append("(" + std::to_string(col.attributes().size()) +")");
|
||||||
|
}
|
||||||
|
if (is_constraint_set(col.attributes().options(), utils::constraints::NOT_NULL)) {
|
||||||
|
result.append(" NOT NULL");
|
||||||
|
}
|
||||||
|
if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||||
|
result.append(" PRIMARY KEY");
|
||||||
|
}
|
||||||
|
result.append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
result += ")";
|
||||||
|
query_parts_.emplace_back(result);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue