# launchbadge/sqlx architecture

> Architecture diagram of the GitHub repository launchbadge/sqlx, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/launchbadge/sqlx
- Repository: https://github.com/launchbadge/sqlx

## Overview

##### Main Components

a) Core Components:
- SQLx Core (sqlx-core): The foundation layer containing shared functionality
- Database Drivers: Separate modules for PostgreSQL, MySQL, SQLite
- Query Macros System (sqlx-macros): Compile-time query checking
- CLI Tools (sqlx-cli): Migration and utility commands
- Connection Pool Management
- Runtime Abstraction Layer

b) External Integration Points:
- Async Runtimes (Tokio, async-std, actix)
- TLS Backends (native-tls, rustls)
- Database Servers (PostgreSQL, MySQL, SQLite)

##### Component Relationships

**Application Layer**

- ▼
**High-Level API Layer**

- ▼
**Database-Specific Drivers Layer**

- ▼
**Core Infrastructure Layer**

- ▼
**Runtime & TLS Layer**

##### Detailed Component Breakdown

Core Layer (Blue):
**┌─────────────────────────────────────────┐**
- SQLx Core                  │
- ┤
- Connection Management                 │
- Pool Implementation                   │
- Query Execution                       │
- Type System                          │
- Error Handling                       │
- ┘

Database Drivers (Green):
**┌─────────────┐ ┌─────────────┐ ┌─────────────┐**
- PostgreSQL │ │    MySQL    │ │   SQLite    │
- ┘ └─────────────┘ └─────────────┘

Build-time Components (Orange):
**┌─────────────────┐ ┌─────────────────┐**
- Query Macros   │ │    SQLx CLI     │
- ┘ └─────────────────┘

##### Key Interactions to Highlight

- Compile-time query checking flow
- Connection pooling mechanism
- Transaction handling
- Migration system
- Runtime abstraction

##### Implementation Details

Use these symbols:
- Double lines: API boundaries

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef core fill:#b3d9ff,stroke:#333,stroke-width:2px
    classDef driver fill:#90EE90,stroke:#333,stroke-width:2px
    classDef macro fill:#FFB366,stroke:#333,stroke-width:2px
    classDef runtime fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef external fill:#lightgray,stroke:#333,stroke-width:2px
    
    %% Core Layer
    subgraph CoreLayer
        Core["SQLx Core"]:::core
        TypeSystem["Type System"]:::core
        ErrorHandling["Error Handling"]:::core
        ConnMgmt["Connection Management"]:::core
        Pool["Pool Implementation"]:::core
    end
    
    %% Database Drivers Layer
    subgraph DriversLayer
        Postgres["PostgreSQL Driver"]:::driver
        MySQL["MySQL Driver"]:::driver
        SQLite["SQLite Driver"]:::driver
    end
    
    %% Build-time Components
    subgraph BuildTools
        Macros["Query Macros"]:::macro
        MacrosCore["Macros Core"]:::macro
        CLI["SQLx CLI"]:::macro
        Migrations["Migration Tools"]:::macro
    end
    
    %% Runtime & TLS Layer
    subgraph RuntimeLayer
        RuntimeCore["Runtime Core"]:::runtime
        TokioRT["Tokio Runtime"]:::runtime
        AsyncStdRT["Async-std Runtime"]:::runtime
        TLSCore["TLS Core"]:::runtime
        NativeTLS["Native TLS"]:::runtime
        Rustls["Rustls"]:::runtime
    end
    
    %% External Systems
    subgraph ExternalSystems
        PostgresDB["PostgreSQL Server"]:::external
        MySQLDB["MySQL Server"]:::external
        SQLiteDB["SQLite Database"]:::external
    end
    
    %% Relationships
    Core --> TypeSystem
    Core --> ErrorHandling
    Core --> ConnMgmt
    Core --> Pool
    
    Postgres --> Core
    MySQL --> Core
    SQLite --> Core
    
    Postgres --> PostgresDB
    MySQL --> MySQLDB
    SQLite --> SQLiteDB
    
    Macros --> MacrosCore
    MacrosCore --> Core
    
    CLI --> Migrations
    CLI --> Core
    
    RuntimeCore --> TokioRT
    RuntimeCore --> AsyncStdRT
    
    TLSCore --> NativeTLS
    TLSCore --> Rustls
    
    Core --> RuntimeCore
    Core --> TLSCore
    
    %% Click Events
    click Core "https://github.com/launchbadge/sqlx/tree/main/sqlx-core/src/"
    click TypeSystem "https://github.com/launchbadge/sqlx/tree/main/sqlx-core/src/types/"
    click ErrorHandling "https://github.com/launchbadge/sqlx/blob/main/sqlx-core/src/error.rs"
    click Postgres "https://github.com/launchbadge/sqlx/tree/main/sqlx-postgres/"
    click MySQL "https://github.com/launchbadge/sqlx/tree/main/sqlx-mysql/"
    click SQLite "https://github.com/launchbadge/sqlx/tree/main/sqlx-sqlite/"
    click Macros "https://github.com/launchbadge/sqlx/tree/main/sqlx-macros/"
    click MacrosCore "https://github.com/launchbadge/sqlx/tree/main/sqlx-macros-core/"
    click CLI "https://github.com/launchbadge/sqlx/tree/main/sqlx-cli/"
    click Migrations "https://github.com/launchbadge/sqlx/blob/main/sqlx-cli/src/migrate.rs"
    click Pool "https://github.com/launchbadge/sqlx/tree/main/sqlx-core/src/pool/"
    click RuntimeCore "https://github.com/launchbadge/sqlx/tree/main/sqlx-core/src/rt/"
    click TokioRT "https://github.com/launchbadge/sqlx/tree/main/sqlx-core/src/rt/rt_tokio/"
    click AsyncStdRT "https://github.com/launchbadge/sqlx/tree/main/sqlx-core/src/rt/rt_async_std/"
    click TLSCore "https://github.com/launchbadge/sqlx/tree/main/sqlx-core/src/net/tls/"
    click NativeTLS "https://github.com/launchbadge/sqlx/blob/main/sqlx-core/src/net/tls/tls_native_tls.rs"
    click Rustls "https://github.com/launchbadge/sqlx/blob/main/sqlx-core/src/net/tls/tls_rustls.rs"
    
    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Database Drivers"]:::driver
        L3["Build Tools"]:::macro
        L4["Runtime Components"]:::runtime
        L5["External Systems"]:::external
    end
```
