50 created_at = std::chrono::system_clock::now();
55 return !username.get().empty() &&
56 !email.get().empty() &&
57 email.get().find(
'@') != std::string::npos;
62 std::cout <<
"User ID: " <<
id.get()
63 <<
", Username: " << username.get()
64 <<
", Email: " << email.get()
65 <<
", Active: " << (is_active.get() ?
"Yes" :
"No") << std::endl;
70void User::initialize_metadata() {
71 metadata_.add_field(id_field());
72 metadata_.add_field(username_field());
73 metadata_.add_field(email_field());
74 metadata_.add_field(full_name_field());
75 metadata_.add_field(created_at_field());
76 metadata_.add_field(is_active_field());
101 return stock_quantity.get() > 0 && is_available.get();
105 std::cout <<
"Product: " << name.get()
106 <<
", Price: $" << price.get()
107 <<
", Stock: " << stock_quantity.get()
108 <<
", Available: " << (is_available.get() ?
"Yes" :
"No") << std::endl;
112void Product::initialize_metadata() {
113 metadata_.add_field(id_field());
114 metadata_.add_field(name_field());
115 metadata_.add_field(description_field());
116 metadata_.add_field(price_field());
117 metadata_.add_field(stock_quantity_field());
118 metadata_.add_field(is_available_field());
122 std::cout <<
"=== Entity Definition Demonstration ===" << std::endl;
126 user.username =
"john_doe";
127 user.email =
"john@example.com";
128 user.full_name =
"John Doe";
130 std::cout <<
"Created user entity:" << std::endl;
132 std::cout <<
"Is valid: " << (user.
is_valid() ?
"Yes" :
"No") << std::endl;
136 std::cout <<
"\nUser table metadata:" << std::endl;
137 std::cout <<
"Table name: " << user_meta.
table_name() << std::endl;
138 std::cout <<
"Field count: " << user_meta.fields().size() << std::endl;
140 for (
const auto& field : user_meta.fields()) {
141 std::cout <<
" - " << field.name() <<
" (" << field.type_name() <<
")";
142 if (field.is_primary_key()) std::cout <<
" [PRIMARY KEY]";
143 if (field.is_unique()) std::cout <<
" [UNIQUE]";
144 if (field.is_not_null()) std::cout <<
" [NOT NULL]";
145 if (field.has_index()) std::cout <<
" [INDEXED]";
146 std::cout << std::endl;
151 std::cout <<
"\n=== Schema Management Demonstration ===" << std::endl;
155 std::cout <<
"Registering entities..." << std::endl;
156 entity_mgr->register_entity<
User>();
157 entity_mgr->register_entity<
Product>();
160 const auto& user_metadata = entity_mgr->get_metadata<
User>();
161 const auto& product_metadata = entity_mgr->get_metadata<
Product>();
163 std::cout <<
"Registered entities:" << std::endl;
164 std::cout <<
" - " << user_metadata.
table_name() << std::endl;
165 std::cout <<
" - " << product_metadata.
table_name() << std::endl;
168 std::cout <<
"\nGenerated SQL for User table:" << std::endl;
169 std::cout << user_metadata.create_table_sql() << std::endl;
171 std::cout <<
"\nGenerated SQL for Product table:" << std::endl;
172 std::cout << product_metadata.create_table_sql() << std::endl;
178 std::cout <<
"\nNote: In production, call entity_mgr->create_tables(db) to create actual tables." << std::endl;
180 }
catch (
const std::exception& e) {
181 std::cout <<
"Error in schema management: " << e.what() << std::endl;
186 std::cout <<
"\n=== Type Safety Demonstration ===" << std::endl;
194 user.username =
"alice";
195 user.is_active =
true;
198 product.price = 29.99;
199 product.stock_quantity = 50;
201 std::cout <<
"Type-safe field access:" << std::endl;
202 std::cout <<
"User ID (int64_t): " << user.id.get() << std::endl;
203 std::cout <<
"Product price (double): " << product.price.get() << std::endl;
206 std::cout <<
"\nField metadata access:" << std::endl;
207 std::cout <<
"Username field name: " << user.username.metadata().name() << std::endl;
208 std::cout <<
"Username constraints: ";
209 if (user.username.metadata().is_unique()) std::cout <<
"UNIQUE ";
210 if (user.username.metadata().is_not_null()) std::cout <<
"NOT NULL ";
211 if (user.username.metadata().has_index()) std::cout <<
"INDEXED ";
212 std::cout << std::endl;
216 std::cout <<
"\n=== Entity Query Demonstration ===" << std::endl;
221 std::cout <<
"Query API demonstration (requires database connection):" << std::endl;
224 std::cout <<
"\nExample query operations:" << std::endl;
225 std::cout <<
"1. Find active users:" << std::endl;
226 std::cout <<
" auto users = entity_mgr->query<User>(db)" << std::endl;
227 std::cout <<
" .where(\"is_active = true\")" << std::endl;
228 std::cout <<
" .order_by(\"username\")" << std::endl;
229 std::cout <<
" .execute();" << std::endl;
231 std::cout <<
"\n2. Find products by price range:" << std::endl;
232 std::cout <<
" auto products = entity_mgr->query<Product>(db)" << std::endl;
233 std::cout <<
" .where(\"price >= 10.0 AND price <= 100.0\")" << std::endl;
234 std::cout <<
" .where(\"is_available = true\")" << std::endl;
235 std::cout <<
" .limit(10)" << std::endl;
236 std::cout <<
" .execute();" << std::endl;
238 std::cout <<
"\n3. Aggregation queries:" << std::endl;
239 std::cout <<
" auto count = entity_mgr->query<User>(db).count();" << std::endl;
240 std::cout <<
" auto avg_price = entity_mgr->query<Product>(db).avg(\"price\");" << std::endl;
242 }
catch (
const std::exception& e) {
243 std::cout <<
"Error in query demonstration: " << e.what() << std::endl;
248 std::cout <<
"\n=== Entity Lifecycle Demonstration ===" << std::endl;
252 user.username =
"demo_user";
253 user.email =
"demo@example.com";
254 user.full_name =
"Demo User";
257 product.name =
"Demo Product";
258 product.description =
"A sample product for demonstration";
259 product.price = 19.99;
260 product.stock_quantity = 100;
262 std::cout <<
"Created entities:" << std::endl;
267 std::cout <<
"\nValidation results:" << std::endl;
268 std::cout <<
"User is valid: " << (user.
is_valid() ?
"Yes" :
"No") << std::endl;
269 std::cout <<
"Product is in stock: " << (product.
is_in_stock() ?
"Yes" :
"No") << std::endl;
272 std::cout <<
"\nLifecycle operations (requires database):" << std::endl;
273 std::cout <<
"1. user.save() - Insert/update entity" << std::endl;
274 std::cout <<
"2. user.load() - Load from database by primary key" << std::endl;
275 std::cout <<
"3. user.update() - Update existing record" << std::endl;
276 std::cout <<
"4. user.remove() - Delete from database" << std::endl;
280 std::cout <<
"=== ORM Framework Demonstration ===" << std::endl;
281 std::cout <<
"This sample demonstrates the C++20 concepts-based ORM framework" << std::endl;
282 std::cout <<
"with type-safe entity definitions and automatic schema management." << std::endl;
286 auto context = std::make_shared<database_context>();
287 auto entity_mgr = context->get_entity_manager();
295 std::cout <<
"\n=== ORM Framework Features Summary ===" << std::endl;
296 std::cout <<
"✓ C++20 concepts-based entity definitions" << std::endl;
297 std::cout <<
"✓ Compile-time type safety" << std::endl;
298 std::cout <<
"✓ Automatic schema generation" << std::endl;
299 std::cout <<
"✓ Field constraints and metadata" << std::endl;
300 std::cout <<
"✓ Type-safe field accessors" << std::endl;
301 std::cout <<
"✓ Entity lifecycle management" << std::endl;
302 std::cout <<
"✓ Query builder integration" << std::endl;
304 std::cout <<
"\nFor complete functionality, connect to a database and use:" << std::endl;
305 std::cout <<
" entity_mgr->create_tables(db);" << std::endl;
306 std::cout <<
" auto results = entity_mgr->query<EntityType>(db)...execute();" << std::endl;
308 }
catch (
const std::exception& e) {
309 std::cout <<
"Error: " << e.what() << std::endl;
Base class for all ORM entities.
virtual const entity_metadata & get_metadata() const =0
virtual std::string table_name() const =0
Dependency injection container for database system components.
#define ENTITY_TABLE(table_name)
#define ENTITY_METADATA()
#define ENTITY_FIELD(type, name,...)
field_constraint auto_increment()
field_constraint unique()
field_constraint not_null()
field_constraint primary_key()
field_constraint default_now()
void demonstrate_schema_management(std::shared_ptr< entity_manager > entity_mgr)
void demonstrate_entity_queries(std::shared_ptr< entity_manager > entity_mgr)
void demonstrate_entity_lifecycle()
void demonstrate_entity_definition()
void demonstrate_type_safety()