Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
integrated/migration_from_legacy.cpp

This example demonstrates the key differences between the legacy and integrated APIs with working code examples, covering connection setup, query execution, async operations, monitoring, and configuration.

// BSD 3-Clause License
// Copyright (c) 2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <iostream>
#include <iomanip>
using namespace database::integrated;
void print_header(const std::string& title) {
std::cout << "\n" << std::string(70, '=') << "\n";
std::cout << title << "\n";
std::cout << std::string(70, '=') << "\n\n";
}
void print_section(const std::string& title) {
std::cout << "\n" << title << "\n";
std::cout << std::string(title.length(), '-') << "\n\n";
}
int main() {
print_header("Migration Guide: Legacy API to Integrated System");
std::cout << "This guide demonstrates the key improvements in the integrated\n";
std::cout << "database system compared to the legacy API.\n";
// ========================================
// Example 1: Basic Connection
// ========================================
print_section("1. Connection Setup");
std::cout << "LEGACY API:\n";
std::cout << " - Manual configuration required\n";
std::cout << " - Multiple initialization calls\n";
std::cout << " - Exception-based error handling\n\n";
std::cout << "INTEGRATED API:\n";
std::cout << " - Builder pattern with smart defaults\n";
std::cout << " - Single call initialization\n";
std::cout << " - Result<T> for explicit error handling\n\n";
std::cout << "Example:\n";
auto db_result = unified_database_system::create_builder()
.enable_logging(db_log_level::info, "./logs")
.enable_monitoring(true)
.set_pool_size(2, 10)
.build();
std::unique_ptr<unified_database_system> db;
if (db_result.is_ok()) {
db = std::move(db_result.value());
std::cout << "Database instance created with integrated API\n";
}
// ========================================
// Example 2: Query Execution
// ========================================
print_section("2. Query Execution");
std::cout << "LEGACY API:\n";
std::cout << " - Exceptions for error handling\n";
std::cout << " - Empty result vs error ambiguity\n\n";
std::cout << "INTEGRATED API:\n";
std::cout << " - Result<T> makes success/failure explicit\n";
std::cout << " - Structured error information\n\n";
std::cout << "Example (without actual DB connection):\n";
auto result = db->execute("SELECT 1");
if (result.is_ok()) {
std::cout << "✅ Query succeeded\n";
} else {
std::cout << "❌ Query failed (expected without DB):\n";
std::cout << " Error: " << result.error().message << "\n";
}
// ========================================
// Example 3: Async Operations
// ========================================
print_section("3. Async Operations");
std::cout << "LEGACY API:\n";
std::cout << " - Manual thread management required\n";
std::cout << " - Complex exception handling\n\n";
std::cout << "INTEGRATED API:\n";
std::cout << " - Built-in async support\n";
std::cout << " - Thread pool managed automatically\n";
std::cout << " - Consistent Result<T> return type\n\n";
std::cout << "Example:\n";
std::cout << " auto future = db->execute_async(\"SELECT * FROM users\");\n";
std::cout << " // Do other work...\n";
std::cout << " auto result = future.get();\n\n";
// ========================================
// Example 4: Monitoring
// ========================================
print_section("4. Built-in Monitoring");
std::cout << "LEGACY API:\n";
std::cout << " - Manual instrumentation needed\n";
std::cout << " - External monitoring setup\n\n";
std::cout << "INTEGRATED API:\n";
std::cout << " - Built-in metrics collection\n";
std::cout << " - Health check API\n";
std::cout << " - Automatic slow query detection\n\n";
auto metrics = db->get_metrics();
std::cout << "Current Metrics:\n";
std::cout << " Total Queries: " << metrics.total_queries << "\n";
std::cout << " Pool Size: " << metrics.pool_size << "\n";
std::cout << " Queries/sec: " << std::fixed << std::setprecision(2)
<< metrics.queries_per_second << "\n\n";
auto health = db->check_health();
std::cout << "Health Status: ";
switch (health.status) {
case health_status::healthy:
std::cout << "✅ Healthy\n";
break;
case health_status::degraded:
std::cout << "⚠️ Degraded\n";
break;
default:
std::cout << "❌ Unhealthy\n";
}
// ========================================
// Example 5: Configuration
// ========================================
print_section("5. Configuration");
std::cout << "LEGACY API:\n";
std::cout << " - Scattered configuration methods\n";
std::cout << " - Manual dependency wiring\n\n";
std::cout << "INTEGRATED API:\n";
std::cout << " - Unified builder pattern\n";
std::cout << " - Automatic dependency injection\n";
std::cout << " - Smart defaults reduce boilerplate\n\n";
std::cout << "Example:\n";
std::cout << " auto db = unified_database_system::create_builder()\n";
std::cout << " .set_connection_string(\"...\")\n";
std::cout << " .set_pool_size(5, 20)\n";
std::cout << " .enable_logging(db_log_level::info, \"./logs\")\n";
std::cout << " .enable_monitoring(true)\n";
std::cout << " .enable_async(8)\n";
std::cout << " .build();\n\n";
// ========================================
// Migration Strategy
// ========================================
print_section("6. Migration Strategy");
std::cout << "Gradual Migration Phases:\n\n";
std::cout << "PHASE 1 - Coexistence:\n";
std::cout << " ✓ Add integrated system alongside legacy code\n";
std::cout << " ✓ Both APIs work in the same application\n";
std::cout << " ✓ No breaking changes\n\n";
std::cout << "PHASE 2 - New Features:\n";
std::cout << " ✓ Use integrated API for all new development\n";
std::cout << " ✓ Team learns new API gradually\n";
std::cout << " ✓ Legacy code continues working\n\n";
std::cout << "PHASE 3 - High-Value Migration:\n";
std::cout << " ✓ Migrate code that benefits most from new features\n";
std::cout << " ✓ Focus on async, monitoring requirements first\n";
std::cout << " ✓ Measure performance improvements\n\n";
std::cout << "PHASE 4 - Complete Migration:\n";
std::cout << " ✓ Migrate remaining legacy code\n";
std::cout << " ✓ Remove legacy dependencies\n";
std::cout << " ✓ Full feature set available\n\n";
std::cout << "Estimated Timeline: 2-4 weeks for typical codebase\n\n";
// ========================================
// Key Benefits
// ========================================
print_section("7. Key Benefits");
std::cout << "✅ Improved:\n";
std::cout << " • Error handling - Explicit Result<T> instead of exceptions\n";
std::cout << " • Configuration - Builder pattern with smart defaults\n";
std::cout << " • Async support - Built-in, no manual thread management\n";
std::cout << " • Observability - Integrated logging and monitoring\n";
std::cout << " • Code clarity - More explicit and self-documenting\n\n";
std::cout << "🔧 New Features:\n";
std::cout << " • Health check API\n";
std::cout << " • Comprehensive metrics collection\n";
std::cout << " • Slow query detection\n";
std::cout << " • Zero-config defaults\n";
std::cout << " • Automatic resource management\n\n";
std::cout << "⚠️ Compatibility:\n";
std::cout << " • Query syntax unchanged\n";
std::cout << " • Connection strings same format\n";
std::cout << " • Transaction semantics preserved\n";
std::cout << " • Can coexist with legacy code\n\n";
// ========================================
// Resources
// ========================================
print_section("8. Additional Resources");
std::cout << "Documentation:\n";
std::cout << " • INTEGRATION.md - Complete integration guide\n";
std::cout << " • ARCHITECTURE.md - System architecture\n";
std::cout << " • samples/integrated/ - Working examples\n\n";
std::cout << "Examples:\n";
std::cout << " • basic_usage - Getting started\n";
std::cout << " • async_queries - Async operations\n";
std::cout << " • monitoring - Metrics and health checks\n\n";
print_header("Migration Guide Complete");
std::cout << "Key Takeaways:\n";
std::cout << " 1. New API is more explicit and safer\n";
std::cout << " 2. Migration can be gradual\n";
std::cout << " 3. Built-in observability saves development time\n";
std::cout << " 4. Better defaults reduce configuration burden\n";
std::cout << " 5. Improved async support simplifies concurrent code\n\n";
std::cout << "Ready to migrate? Start with Phase 1 (coexistence)!\n\n";
return 0;
}
void print_header(const std::string &title)
void print_section(const std::string &title)
Zero-configuration database system with integrated adapters (Phase 6)