How dingmaotu/mql-sqlite3 works
Project Type and Purpose
• This is an open-source binding library that exposes the native SQLite3 C API to the MQL language (MetaTrader 4/5).
• Its goal is to let MQL scripts call SQLite operations (open, query, backup, etc.) via a small header-only wrapper and a precompiled Windows DLL.
File-Structure Analysis
• Include/SQLite3/: MQL header files (.mqh) implementing classes and functions mirroring SQLite3’s C API (SQLite3, Statement, Blob, Backup, Vfs, plus common/defines/imports).
• Library/MT4 & Library/MT5: Precompiled 32-bit and 64-bit SQLite3 DLLs.
• Scripts/Test/: Example MQL4 script(s) (.mq4/.mq5) that demonstrate initialization, table creation, querying, and cleanup.
Core Components
a. MetaTrader Runtime
• Hosts MQL scripts, loads DLLs from terminal’s Library folder, compiles and executes .mqh-backed code.
b. MQL Script
• TestSQLite3.mq4/.mq5 call methods like SQLite3::initialize(), Statement::step(), SQLite3::shutdown().
c. Wrapper Layer (Include/SQLite3 .mqh files)
• Provides MQL classes matching C API:
• SQLite3.mqh (connection)
• Statement.mqh (SQL statements)
• Blob.mqh, Backup.mqh, Vfs.mqh, etc.
• Imports.mqh defines DLL-imported functions.
d. SQLite3 DLL
• Precompiled native library implementing the actual SQLite3 engine. Two flavors: MT4 (32-bit) and MT5 (64-bit).
e. Database File
• A .db file stored under MQL4\Files or MQL5\Files (persistent storage on disk).
Component Relationships and Data Flow
• MQL Script → Wrapper Layer
Includes .mqh files; calls class methods (e.g., db.step()).
• Wrapper Layer → SQLite3 DLL
Via Windows DLL imports; marshals MQL data to UTF-8 and calls into native code.
• SQLite3 DLL → Storage
Reads/writes the .db file on disk, manages pages, indexes, transactions.
• DLL → Wrapper Layer
Returns status codes, result rows, blobs, errors.
• Wrapper Layer → MQL Script
Converts results back to MQL types, exposes them to the script.
Architectural Patterns and Principles
• Foreign Function Interface (FFI): MQL ↔ DLL boundary.
• Wrapper/Facade: Header classes abstract low-level C calls into idiomatic MQL methods.
• Version Compatibility: Single headers support both MT4 and MT5; separate DLLs for word-size.
• UTF-8 Unification: All strings converted to/from UTF-8 at the boundary.