schema creation progress
This commit is contained in:
+17
-21
@@ -84,38 +84,34 @@ utils::result<void, utils::error> schema::create() const {
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> schema::drop() const {
|
||||
std::vector<std::string> drop_sql_commands;
|
||||
auto c = pool_.acquire();
|
||||
// drop table constraints
|
||||
for (const auto &node: repo_) {
|
||||
auto ctx = query::query::drop()
|
||||
.table(node->name())
|
||||
.compile(*c);
|
||||
for (const auto& cons : node->info().constraints()) {
|
||||
auto ctx = query::query::alter()
|
||||
.table(node->name())
|
||||
.drop_constraint(cons.name())
|
||||
.compile(*c);
|
||||
|
||||
drop_sql_commands.push_back(ctx.sql);
|
||||
// determine constraints and drop them
|
||||
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
||||
const auto& endpoint = *(it->second);
|
||||
if (endpoint.type() == object::relation_type::BELONGS_TO) {
|
||||
continue;
|
||||
}
|
||||
const auto drop_fk_ctx = query::query::alter()
|
||||
.table( endpoint.node().name() )
|
||||
.drop_constraint( "FK_" + endpoint.node().name() + "_" + endpoint.foreign_endpoint()->field_name() )
|
||||
.compile(*c);
|
||||
std::cout << drop_fk_ctx.sql << std::endl;
|
||||
if (auto result = c->execute(drop_fk_ctx.sql); !result) {
|
||||
std::cout << ctx.sql << std::endl;
|
||||
if (auto result = c->execute(ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// execute additional commands (e.g. ALTER TABLE ADD FK)
|
||||
for (const auto &sql: drop_sql_commands) {
|
||||
std::cout << sql << std::endl;
|
||||
if (auto result = c->execute(sql); !result) {
|
||||
// drop table
|
||||
for (const auto &node: repo_) {
|
||||
auto ctx = query::query::drop()
|
||||
.table(node->name())
|
||||
.compile(*c);
|
||||
|
||||
std::cout << ctx.sql << std::endl;
|
||||
if (auto result = c->execute(ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
|
||||
+86
-35
@@ -20,59 +20,110 @@ session::session(session_context&& ctx)
|
||||
|
||||
utils::result<void, utils::error> session::create_schema() const {
|
||||
// Step 1: Build dependency graph
|
||||
std::unordered_map<std::string, std::vector<std::string> > dependency_graph;
|
||||
std::unordered_map<std::string, std::pair<int,object::repository::node_ptr>> in_degree;
|
||||
// std::unordered_map<std::string, std::vector<std::string> > dependency_graph;
|
||||
// std::unordered_map<std::string, std::pair<int,object::repository::node_ptr>> in_degree;
|
||||
|
||||
for (const auto &node: *schema_) {
|
||||
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
||||
dependency_graph[node->name()].push_back(it->second->node().name());
|
||||
// for (const auto &node: *schema_) {
|
||||
// for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
||||
// dependency_graph[node->name()].push_back(it->second->node().name());
|
||||
//
|
||||
// if (const auto dit = in_degree.find(it->second->node().name()); dit == in_degree.end()) {
|
||||
// in_degree[it->second->node().name()] = std::make_pair(1, it->second->node_ptr());
|
||||
// } else {
|
||||
// in_degree[it->second->node().name()].first++;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Ensure the current node exists in the graph representation
|
||||
// if (in_degree.find(node->name()) == in_degree.end()) {
|
||||
// in_degree[node->name()] = std::make_pair(0, node);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// for (const auto &it : dependency_graph) {
|
||||
// std::cout << "Dependency graph " << it.first << std::endl;
|
||||
// for (const auto &neighbor: it.second) {
|
||||
// std::cout << " " << neighbor << std::endl;
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
// }
|
||||
|
||||
if (const auto dit = in_degree.find(it->second->node().name()); dit == in_degree.end()) {
|
||||
in_degree[it->second->node().name()] = std::make_pair(1, it->second->node_ptr());
|
||||
} else {
|
||||
in_degree[it->second->node().name()].first++;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the current node exists in the graph representation
|
||||
if (in_degree.find(node->name()) == in_degree.end()) {
|
||||
in_degree[node->name()] = std::make_pair(0, node);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &it : dependency_graph) {
|
||||
std::cout << "Dependency graph " << it.first << std::endl;
|
||||
for (const auto &neighbor: it.second) {
|
||||
std::cout << " " << neighbor << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::vector<std::string> fk_sql_commands;
|
||||
auto c = cache_.pool().acquire();
|
||||
for (const auto &node: *schema_) {
|
||||
auto ctx = query::query::create()
|
||||
.table(node->name())
|
||||
.columns(node->info().attributes())
|
||||
.constraints(node->info().constraints())
|
||||
.compile(*c);
|
||||
|
||||
// for ( const auto& [sql, command] : ctx.additional_commands ) {
|
||||
// fk_sql_commands.push_back( sql );
|
||||
// }
|
||||
std::cout << ctx.sql << std::endl;
|
||||
if (auto result = c->execute(ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
|
||||
// execute additional commands (e.g. ALTER TABLE ADD FK)
|
||||
for (const auto &sql: fk_sql_commands) {
|
||||
std::cout << sql << std::endl;
|
||||
if (auto result = c->execute(sql); !result) {
|
||||
// create table constraints
|
||||
for (const auto &node: *schema_) {
|
||||
for (const auto& cons : node->info().constraints()) {
|
||||
auto ctx = build_add_constraint_context(node->name(), cons, c);
|
||||
|
||||
std::cout << ctx.sql << std::endl;
|
||||
if (auto result = c->execute(ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
sql::query_context session::build_add_constraint_context(const std::string& table_name, const class object::constraint& cons, const sql::connection_ptr &conn) {
|
||||
if (cons.is_foreign_key_constraint()) {
|
||||
return query::query::alter()
|
||||
.table(table_name)
|
||||
.add_constraint( cons.name() )
|
||||
.foreign_key(cons.column_name())
|
||||
.references(cons.ref_table_name(), {cons.ref_column_name()})
|
||||
.compile(*conn);
|
||||
}
|
||||
if (cons.is_primary_key_constraint()) {
|
||||
return query::query::alter()
|
||||
.table(table_name)
|
||||
.add_constraint( cons.name() )
|
||||
.primary_key(cons.column_name())
|
||||
.compile(*conn);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> session::drop_schema() const {
|
||||
auto c = cache_.pool().acquire();
|
||||
// drop table constraints
|
||||
for (const auto &node: *schema_) {
|
||||
for (const auto& cons : node->info().constraints()) {
|
||||
auto ctx = query::query::alter()
|
||||
.table(node->name())
|
||||
.drop_constraint(cons.name())
|
||||
.compile(*c);
|
||||
|
||||
std::cout << ctx.sql << std::endl;
|
||||
if (auto result = c->execute(ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// drop table
|
||||
for (const auto &node: *schema_) {
|
||||
auto ctx = query::query::drop()
|
||||
.table(node->name())
|
||||
.compile(*c);
|
||||
|
||||
std::cout << ctx.sql << std::endl;
|
||||
if (auto result = c->execute(ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
|
||||
@@ -35,10 +35,6 @@ query_add_key_constraint_intermediate query_alter_table_intermediate::add_constr
|
||||
return {context_};
|
||||
}
|
||||
|
||||
executable_query query_alter_table_intermediate::add_constraint( const class object::constraint& constraint ) {
|
||||
return {context_};
|
||||
}
|
||||
|
||||
executable_query query_alter_table_intermediate::drop_constraint( const std::string& name ) {
|
||||
context_->parts.push_back(std::make_unique<internal::query_drop_key_constraint_part>(name));
|
||||
return {context_};
|
||||
|
||||
@@ -36,9 +36,6 @@ sql::query_context query_compiler::compile(const query_data &data,
|
||||
}
|
||||
|
||||
std::string handle_column(sql::query_context &ctx, const sql::dialect *d, const query_data &data, const column &col) {
|
||||
// ctx.result_vars.emplace_back(col.column_name());
|
||||
const auto& column_table = col.table().get();
|
||||
// ctx.column_aliases.insert({column_table->has_alias() ? column_table->alias() : column_table->name() + "." + col.column_name(), col.alias()});
|
||||
if (col.is_function()) {
|
||||
ctx.prototype.emplace_back(col.has_alias() ? col.alias() : col.column_name());
|
||||
ctx.prototype.back().change_type(utils::basic_type::type_int32);
|
||||
@@ -153,7 +150,6 @@ void query_compiler::visit(internal::query_select_part &part) {
|
||||
void query_compiler::visit(internal::query_from_part &part) {
|
||||
query_.table_name = part.table().name();
|
||||
query_.sql += " " + build_table_name(part.token(), *dialect_, part.table());
|
||||
// query_.table_aliases.insert({query_.table_name, part.table().alias()});
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_join_part &part)
|
||||
@@ -177,40 +173,33 @@ void query_compiler::visit(internal::query_group_by_part &part) {
|
||||
query_.sql += " " + dialect_->group_by() + " " + prepare_identifier(*dialect_, part.column());
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_order_by_part &part)
|
||||
{
|
||||
void query_compiler::visit(internal::query_order_by_part &part) {
|
||||
query_.sql += " " + dialect_->order_by() +
|
||||
" " + prepare_criteria(*dialect_, part.column());
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_order_by_asc_part &/*order_by_asc_part*/)
|
||||
{
|
||||
void query_compiler::visit(internal::query_order_by_asc_part &/*order_by_asc_part*/) {
|
||||
query_.sql += " " + dialect_->asc();
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_order_by_desc_part &/*order_by_desc_part*/)
|
||||
{
|
||||
void query_compiler::visit(internal::query_order_by_desc_part &/*order_by_desc_part*/) {
|
||||
query_.sql += " " + dialect_->desc();
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_offset_part &part)
|
||||
{
|
||||
void query_compiler::visit(internal::query_offset_part &part) {
|
||||
query_.sql += " " + dialect_->offset() + " " + std::to_string(part.offset());
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_limit_part &part)
|
||||
{
|
||||
void query_compiler::visit(internal::query_limit_part &part) {
|
||||
query_.sql += " " + dialect_->limit() + " " + std::to_string(part.limit());
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_insert_part &/*insert_part*/)
|
||||
{
|
||||
void query_compiler::visit(internal::query_insert_part &/*insert_part*/) {
|
||||
query_.command = sql::sql_command::SQL_INSERT;
|
||||
query_.sql = dialect_->insert();
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_into_part &part)
|
||||
{
|
||||
void query_compiler::visit(internal::query_into_part &part) {
|
||||
query_.table_name = part.table().name();
|
||||
query_.sql += " " + dialect_->into() +
|
||||
" " + dialect_->prepare_identifier_string(part.table().name());
|
||||
@@ -383,14 +372,12 @@ void query_compiler::visit(internal::query_set_part &part) {
|
||||
query_.sql += result;
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_drop_table_part &part)
|
||||
{
|
||||
void query_compiler::visit(internal::query_drop_table_part &part) {
|
||||
query_.table_name = part.table().name();
|
||||
query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name);
|
||||
}
|
||||
|
||||
std::string build_create_column(const object::attribute &col, const sql::dialect &d)
|
||||
{
|
||||
std::string build_create_column(const object::attribute &col, const sql::dialect &d) {
|
||||
std::string result = d.prepare_identifier_string(col.name()) + " " + d.data_type_at(col.type());
|
||||
if (col.attributes().size() > 0) {
|
||||
result.append("(" + std::to_string(col.attributes().size()) + ")");
|
||||
@@ -433,8 +420,7 @@ std::string build_constraint(const class object::constraint& cons, const sql::di
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string query_compiler::build_table_name(const sql::dialect_token token, const sql::dialect &d, const table& t)
|
||||
{
|
||||
std::string query_compiler::build_table_name(const sql::dialect_token token, const sql::dialect &d, const table& t) {
|
||||
return d.token_at(token) + " " +
|
||||
(!d.default_schema_name().empty() ? d.prepare_identifier_string(d.default_schema_name()) + "." : "") +
|
||||
d.prepare_identifier_string(t.name()) +
|
||||
|
||||
Reference in New Issue
Block a user