Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
orm_entity_demo.cpp File Reference

Demonstrates the ORM entity system with field metadata and SQL generation. More...

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "database/orm/entity.h"
#include "database/database_types.h"
Include dependency graph for orm_entity_demo.cpp:

Go to the source code of this file.

Classes

class  user_entity
 
class  post_entity
 

Functions

int main ()
 

Detailed Description

Demonstrates the ORM entity system with field metadata and SQL generation.

This example shows how to:

  • Define an entity class deriving from entity_base
  • Configure field constraints (primary key, not null, unique, index)
  • Generate CREATE TABLE SQL from entity metadata
  • Inspect entity field metadata programmatically
  • Use type-safe field accessors
  • Check type traits for entity and field types

Note: This example focuses on the ORM metadata and schema generation capabilities. It does not require a running database.

Definition in file orm_entity_demo.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 117 of file orm_entity_demo.cpp.

118{
119 std::cout << "=== orm_entity_demo example ===" << std::endl;
120
121 // -------------------------------------------------------
122 // 1. Inspect User entity metadata
123 // -------------------------------------------------------
124 std::cout << "\n--- User entity metadata ---" << std::endl;
125 {
126 user_entity user;
127 const auto& meta = user.get_metadata();
128
129 std::cout << "Table: " << meta.table_name() << std::endl;
130 std::cout << "Fields:" << std::endl;
131
132 for (const auto& field : meta.fields())
133 {
134 std::cout << " " << field.name()
135 << " (" << field.type_name() << ")";
136
137 if (field.is_primary_key())
138 {
139 std::cout << " [PK]";
140 }
141 if (field.is_auto_increment())
142 {
143 std::cout << " [AUTO]";
144 }
145 if (field.is_not_null())
146 {
147 std::cout << " [NOT NULL]";
148 }
149 if (field.is_unique())
150 {
151 std::cout << " [UNIQUE]";
152 }
153 if (field.has_index())
154 {
155 std::cout << " [INDEX]";
156 }
157 std::cout << std::endl;
158 }
159
160 // Primary key info
161 const auto* pk = meta.get_primary_key();
162 if (pk != nullptr)
163 {
164 std::cout << "Primary key: " << pk->name() << std::endl;
165 }
166 }
167
168 // -------------------------------------------------------
169 // 2. Generate CREATE TABLE SQL
170 // -------------------------------------------------------
171 std::cout << "\n--- Generated SQL ---" << std::endl;
172 {
173 user_entity user;
174 std::cout << "User table SQL:" << std::endl;
175 std::cout << user.get_metadata().create_table_sql() << std::endl;
176
177 post_entity post;
178 std::cout << "\nPost table SQL:" << std::endl;
179 std::cout << post.get_metadata().create_table_sql() << std::endl;
180 }
181
182 // -------------------------------------------------------
183 // 3. Generate index SQL
184 // -------------------------------------------------------
185 std::cout << "\n--- Generated index SQL ---" << std::endl;
186 {
187 post_entity post;
188 auto index_sql = post.get_metadata().create_indexes_sql();
189 if (!index_sql.empty())
190 {
191 std::cout << "Post indexes:" << std::endl;
192 std::cout << index_sql << std::endl;
193 }
194 else
195 {
196 std::cout << "No additional indexes defined" << std::endl;
197 }
198 }
199
200 // -------------------------------------------------------
201 // 4. Using field accessors
202 // -------------------------------------------------------
203 std::cout << "\n--- Field accessors ---" << std::endl;
204 {
205 user_entity user;
206
207 // Set field values through accessors
208 user.id = int64_t{1};
209 user.username = std::string("john_doe");
210 user.email = std::string("john@example.com");
211 user.age = int32_t{30};
212 user.is_active = true;
213
214 // Read field values
215 std::cout << "User: "
216 << "id=" << user.id.get()
217 << " username=" << user.username.get()
218 << " email=" << user.email.get()
219 << " age=" << user.age.get()
220 << " active=" << std::boolalpha << user.is_active.get()
221 << std::endl;
222 }
223
224 // -------------------------------------------------------
225 // 5. Type trait checks
226 // -------------------------------------------------------
227 std::cout << "\n--- Type trait checks ---" << std::endl;
228 {
229 std::cout << "is_entity<user_entity>: "
230 << std::boolalpha << is_entity_v<user_entity> << std::endl;
231 std::cout << "is_entity<post_entity>: "
232 << std::boolalpha << is_entity_v<post_entity> << std::endl;
233 std::cout << "is_entity<int>: "
234 << std::boolalpha << is_entity_v<int> << std::endl;
235
236 std::cout << "is_field_type<int32_t>: "
237 << std::boolalpha << is_field_type_v<int32_t> << std::endl;
238 std::cout << "is_field_type<std::string>: "
239 << std::boolalpha << is_field_type_v<std::string> << std::endl;
240 std::cout << "is_field_type<std::vector<int>>: "
241 << std::boolalpha << is_field_type_v<std::vector<int>> << std::endl;
242 }
243
244 std::cout << "\n=== orm_entity_demo example completed ===" << std::endl;
245 return 0;
246}
virtual const entity_metadata & get_metadata() const =0
std::string create_table_sql() const
Definition entity.cpp:123
std::string create_indexes_sql() const
Definition entity.cpp:146
const std::string & table_name() const
Definition entity.h:124

References database::orm::entity_metadata::create_indexes_sql(), database::orm::entity_metadata::create_table_sql(), database::orm::entity_base::get_metadata(), and database::orm::entity_metadata::table_name().

Here is the call graph for this function: