Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::orm::entity_metadata Class Reference

Metadata for entire entities including table mapping and relationships. More...

#include <entity.h>

Collaboration diagram for database::orm::entity_metadata:
Collaboration graph

Public Member Functions

 entity_metadata (const std::string &table_name)
 
void add_field (const field_metadata &field)
 
const std::vector< field_metadata > & fields () const
 
const std::string & table_name () const
 
const field_metadataget_primary_key () const
 
std::vector< const field_metadata * > get_indexes () const
 
std::vector< const field_metadata * > get_foreign_keys () const
 
std::string create_table_sql () const
 
std::string create_indexes_sql () const
 

Private Attributes

std::string table_name_
 
std::vector< field_metadatafields_
 

Detailed Description

Metadata for entire entities including table mapping and relationships.

Definition at line 117 of file entity.h.

Constructor & Destructor Documentation

◆ entity_metadata()

database::orm::entity_metadata::entity_metadata ( const std::string & table_name)
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 82 of file entity.cpp.

84 {
85 }
const std::string & table_name() const
Definition entity.h:124

Member Function Documentation

◆ add_field()

void database::orm::entity_metadata::add_field ( const field_metadata & field)
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 87 of file entity.cpp.

88 {
89 fields_.push_back(field);
90 }
std::vector< field_metadata > fields_
Definition entity.h:135

References fields_.

Referenced by TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ create_indexes_sql()

std::string database::orm::entity_metadata::create_indexes_sql ( ) const
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 146 of file entity.cpp.

147 {
148 std::ostringstream oss;
149 auto indexes = get_indexes();
150
151 for (const auto* index : indexes) {
152 if (!index->index_name().empty()) {
153 oss << "CREATE INDEX IF NOT EXISTS " << index->index_name()
154 << " ON " << table_name_ << "(" << index->name() << ");\n";
155 } else {
156 oss << "CREATE INDEX IF NOT EXISTS idx_" << table_name_
157 << "_" << index->name() << " ON " << table_name_
158 << "(" << index->name() << ");\n";
159 }
160 }
161
162 return oss.str();
163 }
std::vector< const field_metadata * > get_indexes() const
Definition entity.cpp:101

References get_indexes(), database::orm::index, and table_name_.

Referenced by main(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_table_sql()

std::string database::orm::entity_metadata::create_table_sql ( ) const
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 123 of file entity.cpp.

124 {
125 std::ostringstream oss;
126 oss << "CREATE TABLE IF NOT EXISTS " << table_name_ << " (\n";
127
128 bool first = true;
129 for (const auto& field : fields_) {
130 if (!first) oss << ",\n";
131 oss << " " << field.to_sql_definition();
132 first = false;
133 }
134
135 // Add foreign key constraints
136 auto foreign_keys = get_foreign_keys();
137 for (const auto* fk : foreign_keys) {
138 oss << ",\n FOREIGN KEY (" << fk->name() << ") REFERENCES "
139 << fk->foreign_table() << "(" << fk->foreign_field() << ")";
140 }
141
142 oss << "\n)";
143 return oss.str();
144 }
std::vector< const field_metadata * > get_foreign_keys() const
Definition entity.cpp:112

References fields_, get_foreign_keys(), and table_name_.

Referenced by main(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fields()

const std::vector< field_metadata > & database::orm::entity_metadata::fields ( ) const
inline
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 123 of file entity.h.

123{ return fields_; }

References fields_.

◆ get_foreign_keys()

std::vector< const field_metadata * > database::orm::entity_metadata::get_foreign_keys ( ) const
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 112 of file entity.cpp.

113 {
114 std::vector<const field_metadata*> foreign_keys;
115 for (const auto& field : fields_) {
116 if (field.is_foreign_key()) {
117 foreign_keys.push_back(&field);
118 }
119 }
120 return foreign_keys;
121 }

References fields_.

Referenced by create_table_sql().

Here is the caller graph for this function:

◆ get_indexes()

std::vector< const field_metadata * > database::orm::entity_metadata::get_indexes ( ) const
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 101 of file entity.cpp.

102 {
103 std::vector<const field_metadata*> indexes;
104 for (const auto& field : fields_) {
105 if (field.has_index()) {
106 indexes.push_back(&field);
107 }
108 }
109 return indexes;
110 }

References fields_.

Referenced by create_indexes_sql().

Here is the caller graph for this function:

◆ get_primary_key()

const field_metadata * database::orm::entity_metadata::get_primary_key ( ) const
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h.

Definition at line 92 of file entity.cpp.

93 {
94 auto it = std::find_if(fields_.begin(), fields_.end(),
95 [](const field_metadata& field) {
96 return field.is_primary_key();
97 });
98 return (it != fields_.end()) ? &(*it) : nullptr;
99 }

References fields_.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ table_name()

const std::string & database::orm::entity_metadata::table_name ( ) const
inline
Examples
/home/runner/work/database_system/database_system/database/orm/entity.h, and orm_framework_demo.cpp.

Definition at line 124 of file entity.h.

124{ return table_name_; }

References table_name_.

Referenced by demonstrate_entity_definition(), and main().

Here is the caller graph for this function:

Member Data Documentation

◆ fields_

std::vector<field_metadata> database::orm::entity_metadata::fields_
private

◆ table_name_

std::string database::orm::entity_metadata::table_name_
private

The documentation for this class was generated from the following files: