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

ORM framework demonstration with C++20 concepts-based entities. More...

#include <iostream>
#include <string>
#include <chrono>
#include <memory>
#include "database/database_manager.h"
#include "database/orm/entity.h"
#include "database/core/database_context.h"
Include dependency graph for orm_framework_demo.cpp:

Go to the source code of this file.

Classes

class  User
 
class  Product
 

Functions

void demonstrate_entity_definition ()
 
void demonstrate_schema_management (std::shared_ptr< entity_manager > entity_mgr)
 
void demonstrate_type_safety ()
 
void demonstrate_entity_queries (std::shared_ptr< entity_manager > entity_mgr)
 
void demonstrate_entity_lifecycle ()
 
int main ()
 

Detailed Description

ORM framework demonstration with C++20 concepts-based entities.

Definition in file orm_framework_demo.cpp.

Function Documentation

◆ demonstrate_entity_definition()

void demonstrate_entity_definition ( )
Examples
orm_framework_demo.cpp.

Definition at line 121 of file orm_framework_demo.cpp.

121 {
122 std::cout << "=== Entity Definition Demonstration ===" << std::endl;
123
124 // Create User entity
125 User user;
126 user.username = "john_doe";
127 user.email = "john@example.com";
128 user.full_name = "John Doe";
129
130 std::cout << "Created user entity:" << std::endl;
131 user.print_info();
132 std::cout << "Is valid: " << (user.is_valid() ? "Yes" : "No") << std::endl;
133
134 // Access field metadata
135 const auto& user_meta = user.get_metadata();
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;
139
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;
147 }
148}
void print_info() const
bool is_valid() const
virtual const entity_metadata & get_metadata() const =0
const std::string & table_name() const
Definition entity.h:124

References database::orm::entity_base::get_metadata(), User::is_valid(), User::print_info(), and database::orm::entity_metadata::table_name().

Referenced by main().

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

◆ demonstrate_entity_lifecycle()

void demonstrate_entity_lifecycle ( )
Examples
orm_framework_demo.cpp.

Definition at line 247 of file orm_framework_demo.cpp.

247 {
248 std::cout << "\n=== Entity Lifecycle Demonstration ===" << std::endl;
249
250 // Create new entities
251 User user;
252 user.username = "demo_user";
253 user.email = "demo@example.com";
254 user.full_name = "Demo User";
255
256 Product product;
257 product.name = "Demo Product";
258 product.description = "A sample product for demonstration";
259 product.price = 19.99;
260 product.stock_quantity = 100;
261
262 std::cout << "Created entities:" << std::endl;
263 user.print_info();
264 product.print_info();
265
266 // Demonstrate validation
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;
270
271 // In a real application with database connection:
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;
277}
bool is_in_stock() const
void print_info() const

References Product::is_in_stock(), User::is_valid(), Product::print_info(), and User::print_info().

Referenced by main().

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

◆ demonstrate_entity_queries()

void demonstrate_entity_queries ( std::shared_ptr< entity_manager > entity_mgr)
Examples
orm_framework_demo.cpp.

Definition at line 215 of file orm_framework_demo.cpp.

215 {
216 std::cout << "\n=== Entity Query Demonstration ===" << std::endl;
217
218 try {
219 // Note: This demonstrates the API structure
220 // In production, you would have an actual database connection
221 std::cout << "Query API demonstration (requires database connection):" << std::endl;
222
223 // Example query building (conceptual)
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;
230
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;
237
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;
241
242 } catch (const std::exception& e) {
243 std::cout << "Error in query demonstration: " << e.what() << std::endl;
244 }
245}

Referenced by main().

Here is the caller graph for this function:

◆ demonstrate_schema_management()

void demonstrate_schema_management ( std::shared_ptr< entity_manager > entity_mgr)
Examples
orm_framework_demo.cpp.

Definition at line 150 of file orm_framework_demo.cpp.

150 {
151 std::cout << "\n=== Schema Management Demonstration ===" << std::endl;
152
153 try {
154 // Register entities with the manager
155 std::cout << "Registering entities..." << std::endl;
156 entity_mgr->register_entity<User>();
157 entity_mgr->register_entity<Product>();
158
159 // Get metadata for registered entities
160 const auto& user_metadata = entity_mgr->get_metadata<User>();
161 const auto& product_metadata = entity_mgr->get_metadata<Product>();
162
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;
166
167 // Generate CREATE TABLE SQL (would be used with actual database)
168 std::cout << "\nGenerated SQL for User table:" << std::endl;
169 std::cout << user_metadata.create_table_sql() << std::endl;
170
171 std::cout << "\nGenerated SQL for Product table:" << std::endl;
172 std::cout << product_metadata.create_table_sql() << std::endl;
173
174 // In a real application, you would:
175 // auto db = get_database_connection();
176 // entity_mgr->create_tables(db);
177
178 std::cout << "\nNote: In production, call entity_mgr->create_tables(db) to create actual tables." << std::endl;
179
180 } catch (const std::exception& e) {
181 std::cout << "Error in schema management: " << e.what() << std::endl;
182 }
183}
virtual std::string table_name() const =0

References database::orm::entity_base::table_name().

Referenced by main().

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

◆ demonstrate_type_safety()

void demonstrate_type_safety ( )
Examples
orm_framework_demo.cpp.

Definition at line 185 of file orm_framework_demo.cpp.

185 {
186 std::cout << "\n=== Type Safety Demonstration ===" << std::endl;
187
188 // Create entities with type-safe field access
189 User user;
190 Product product;
191
192 // Type-safe assignments
193 user.id = 1; // int64_t
194 user.username = "alice"; // std::string
195 user.is_active = true; // bool
196
197 product.id = 100;
198 product.price = 29.99; // double
199 product.stock_quantity = 50; // int32_t
200
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;
204
205 // Demonstrate field metadata access
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;
213}

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 279 of file orm_framework_demo.cpp.

279 {
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;
283
284 try {
285 // Create database context and entity manager
286 auto context = std::make_shared<database_context>();
287 auto entity_mgr = context->get_entity_manager();
288
292 demonstrate_entity_queries(entity_mgr);
294
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;
303
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;
307
308 } catch (const std::exception& e) {
309 std::cout << "Error: " << e.what() << std::endl;
310 return 1;
311 }
312
313 return 0;
314}
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()

References demonstrate_entity_definition(), demonstrate_entity_lifecycle(), demonstrate_entity_queries(), demonstrate_schema_management(), and demonstrate_type_safety().

Here is the call graph for this function: