Mist Enhanced In-Memory MySQL Database

A lightweight, thread-safe SQL database engine with complete MySQL-compatible syntax. Built for speed, simplicity, and seamless integration with nested transactions, savepoints, and full referential integrity.

mist-demo.go
engine := mist.NewSQLEngine()

// Create table with enhanced features
engine.Execute(`CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    name VARCHAR(50),
    birth_date DATE,
    status ENUM('active', 'inactive') DEFAULT 'active',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)`)

// Insert data with new types
engine.Execute("INSERT INTO users (email, name, birth_date, status) VALUES ('[email protected]', 'Alice', '1990-01-01', 'active')")

// Nested transactions with savepoints
engine.Execute("START TRANSACTION")
engine.Execute("INSERT INTO users (email, name) VALUES ('[email protected]', 'Bob')")

engine.Execute("SAVEPOINT sp1")
engine.Execute("UPDATE users SET status = 'inactive' WHERE name = 'Alice'")
engine.Execute("ROLLBACK TO SAVEPOINT sp1") // Undo Alice update

engine.Execute("COMMIT") // Bob remains, Alice unchanged

// GROUP BY with aggregates
result, _ := engine.Execute("SELECT status, COUNT(*) FROM users GROUP BY status")
mist.PrintResult(result)

Core Features

๐Ÿš€ Lightning Fast

In-memory storage ensures blazing fast query execution with zero disk I/O overhead, dependency and latency.

๐Ÿ”— MySQL Compatible

Built with TiDB parser for full MySQL syntax compatibility. Original MySQL schemas work seamlessly without modifications.

๐Ÿ”„ Nested Transactions

Full nested transaction support with savepoints, rollback isolation, proper state management, and ACID properties.

๐Ÿ”— Referential Integrity

Complete foreign key support with automatic constraint validation and cascade actions for maintaining data consistency.

๐Ÿ“Š Modern Data Types

Full support for advanced data types including dates, enums, decimals, and automatic timestamp management.

๐Ÿ›ก๏ธ Thread Safe

Concurrent operations are handled safely, making it perfect for multi-threaded applications.

๐Ÿ” Advanced Querying

Comprehensive query capabilities including joins, subqueries, aggregation, and complex filtering with optimized performance.

๐Ÿ“Š Query Recording

Built-in query recording for debugging, auditing, and performance analysis with thread-safe operations.

๐ŸŽฏ Precise Tracking

Record exact SQL queries as they're executed with immutable logs and zero performance overhead when disabled.

๐Ÿงช Testing & Debug

Perfect for unit testing, compliance logging, performance analysis, and migration tools.

Quick Installation

As a Go Library

go get github.com/abbychau/mist

Standalone Application

git clone https://github.com/abbychau/mist
cd mist
go mod tidy
go build .

Getting Started

package main

import (
    "fmt"
    "log"
    "github.com/abbychau/mist"
)

func main() {
    // Create a new SQL engine
    engine := mist.NewSQLEngine()

    // Create tables with enhanced features
    _, err := engine.Execute(`CREATE TABLE companies (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(255) NOT NULL UNIQUE,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )`)
    if err != nil {
        log.Fatal(err)
    }

    _, err = engine.Execute(`CREATE TABLE products (
        id INT AUTO_INCREMENT PRIMARY KEY,
        company_id INT NOT NULL,
        name VARCHAR(100) NOT NULL,
        price FLOAT,
        category ENUM('Electronics', 'Books', 'Clothing') DEFAULT 'Electronics',
        launch_date DATE,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        UNIQUE KEY unique_name (name),
        FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE
    )`)
    if err != nil {
        log.Fatal(err)
    }

    // Insert data with referential integrity
    engine.Execute("INSERT INTO companies (name) VALUES ('Tech Corp')")
    
    queries := []string{
        "INSERT INTO products (company_id, name, price, category, launch_date) VALUES (1, 'Laptop Pro', 999.99, 'Electronics', '2024-01-15')",
        "INSERT INTO products (company_id, name, price, category, launch_date) VALUES (1, 'SQL Guide', 29.99, 'Books', '2024-02-20')",
        "INSERT INTO products (company_id, name, price, category, launch_date) VALUES (1, 'Wireless Headphones', 199.99, 'Electronics', '2024-03-10')",
    }
    
    for _, query := range queries {
        _, err := engine.Execute(query)
        if err != nil {
            log.Fatal(err)
        }
    }

    // UPDATE automatically updates the updated_at timestamp
    engine.Execute("UPDATE products SET price = 899.99 WHERE name = 'Laptop Pro'")

    // Query with GROUP BY and aggregates
    result, err := engine.Execute(`
        SELECT category, COUNT(*) as product_count, AVG(price) as avg_price
        FROM products 
        WHERE price > 50 
        GROUP BY category
        ORDER BY avg_price DESC
    `)
    if err != nil {
        log.Fatal(err)
    }

    // Print results
    mist.PrintResult(result)
    
    // Foreign key constraints prevent invalid operations
    // This would fail: INSERT INTO products (company_id, name) VALUES (999, 'Invalid Product')
    // This demonstrates referential integrity in action
}
package main

import (
    "fmt"
    "github.com/abbychau/mist"
)

func main() {
    engine := mist.NewSQLEngine()
    
    // 1. Enhanced Data Types & Constraints
    engine.Execute(`CREATE TABLE companies (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(255) NOT NULL UNIQUE,
        founded_date DATE,
        type ENUM('startup', 'corporation', 'nonprofit') DEFAULT 'startup',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    )`)
    
    // 2. Foreign Key Relationships with Actions
    engine.Execute(`CREATE TABLE employees (
        id INT AUTO_INCREMENT PRIMARY KEY,
        company_id INT NOT NULL,
        email VARCHAR(255) NOT NULL UNIQUE,
        name VARCHAR(100),
        hire_date DATE,
        status ENUM('active', 'inactive', 'terminated') DEFAULT 'active',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE
    )`)
    
    // 3. Data with Enhanced Types
    engine.Execute("INSERT INTO companies (name, founded_date, type) VALUES ('Tech Corp', '2020-01-15', 'startup')")
    engine.Execute("INSERT INTO employees (company_id, email, name, hire_date, status) VALUES (1, '[email protected]', 'John Smith', '2024-01-01', 'active')")
    
    // 4. Automatic Timestamp Updates
    engine.Execute("UPDATE employees SET name = 'John A. Smith' WHERE id = 1")
    // ^ This automatically updates the updated_at field
    
    // 5. GROUP BY with Enhanced Aggregates
    result, _ := engine.Execute(`
        SELECT 
            c.type,
            COUNT(e.id) as employee_count,
            AVG(DATEDIFF(CURDATE(), e.hire_date)) as avg_tenure_days
        FROM companies c
        LEFT JOIN employees e ON c.id = e.company_id
        GROUP BY c.type
        HAVING employee_count > 0
    `)
    
    fmt.Println("Company statistics by type:")
    mist.PrintResult(result)
    
    // 6. Referential Integrity in Action
    // Foreign key constraints prevent orphaned records:
    // This would fail: engine.Execute("INSERT INTO employees (company_id, email, name) VALUES (999, '[email protected]', 'Test User')")
    
    // CASCADE DELETE: Deleting a company removes all its employees
    // engine.Execute("DELETE FROM companies WHERE id = 1") // Would cascade to employees
    
    // 7. UNIQUE Constraints prevent duplicates
    // This would fail: engine.Execute("INSERT INTO employees (company_id, email, name) VALUES (1, '[email protected]', 'Another John')")
    
    // 8. ENUM Validation
    // This would fail: engine.Execute("UPDATE employees SET status = 'invalid_status' WHERE id = 1")
    
    fmt.Println("\nโœ… All enhanced features working correctly!")
    fmt.Println("๐Ÿ”‘ Foreign key constraints ensure data integrity")
    fmt.Println("๐Ÿ“… DATE types handle proper date validation")
    fmt.Println("๐ŸŽฏ ENUM types enforce valid values")
    fmt.Println("๐Ÿ”’ UNIQUE constraints prevent duplicates")
    fmt.Println("โฐ Timestamps update automatically")
    fmt.Println("๐Ÿ“Š GROUP BY works with all aggregate functions")
}
package main

import (
    "fmt"
    "log"
    "github.com/abbychau/mist"
)

func main() {
    engine := mist.NewSQLEngine()

    // Setup test table
    engine.Execute(`CREATE TABLE accounts (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(50) NOT NULL,
        balance DECIMAL(10,2) NOT NULL DEFAULT 0.00
    )`)

    engine.Execute("INSERT INTO accounts (name, balance) VALUES ('Alice', 1000.00)")
    engine.Execute("INSERT INTO accounts (name, balance) VALUES ('Bob', 500.00)")

    fmt.Println("=== Basic Transactions ===")

    // Basic transaction with commit
    engine.Execute("START TRANSACTION")
    engine.Execute("UPDATE accounts SET balance = balance - 100 WHERE name = 'Alice'")
    engine.Execute("UPDATE accounts SET balance = balance + 100 WHERE name = 'Bob'")
    engine.Execute("COMMIT") // Changes are permanent

    fmt.Println("=== Nested Transactions ===")

    // Nested transactions with selective rollback
    engine.Execute("START TRANSACTION") // Level 1
    engine.Execute("UPDATE accounts SET balance = balance - 50 WHERE name = 'Alice'")

    engine.Execute("BEGIN") // Level 2 (nested)
    engine.Execute("UPDATE accounts SET balance = balance + 200 WHERE name = 'Bob'")
    engine.Execute("INSERT INTO accounts (name, balance) VALUES ('Charlie', 300.00)")
    engine.Execute("ROLLBACK") // Rollback level 2 only (Charlie gone, Bob's +200 undone)

    engine.Execute("COMMIT") // Commit level 1 (Alice's -50 remains)

    fmt.Println("=== Savepoints ===")

    // Advanced savepoint usage
    engine.Execute("START TRANSACTION")
    engine.Execute("UPDATE accounts SET balance = balance - 25 WHERE name = 'Alice'")

    engine.Execute("SAVEPOINT sp1") // Create savepoint
    engine.Execute("UPDATE accounts SET balance = balance + 75 WHERE name = 'Bob'")
    engine.Execute("INSERT INTO accounts (name, balance) VALUES ('David', 150.00)")

    engine.Execute("SAVEPOINT sp2") // Another savepoint
    engine.Execute("UPDATE accounts SET balance = balance * 1.1 WHERE name = 'Alice'") // 10% bonus

    // Rollback to sp1 (undoes David insert, Bob +75, and Alice bonus)
    engine.Execute("ROLLBACK TO SAVEPOINT sp1")

    engine.Execute("RELEASE SAVEPOINT sp1") // Clean up savepoint
    engine.Execute("COMMIT") // Commit remaining changes

    // Query final state
    result, _ := engine.Execute("SELECT name, balance FROM accounts ORDER BY name")
    fmt.Println("\nFinal account balances:")
    mist.PrintResult(result)

    fmt.Println("\n=== Transaction Error Handling ===")

    // Demonstrate transaction isolation
    engine.Execute("START TRANSACTION")
    engine.Execute("UPDATE accounts SET balance = 999999 WHERE name = 'Alice'")

    // In a real scenario, you might check business rules here
    // and rollback if they fail
    engine.Execute("ROLLBACK") // Undo the unrealistic balance

    fmt.Println("โœ… Transaction features:")
    fmt.Println("  ๐Ÿ”„ Nested transactions with independent rollback")
    fmt.Println("  ๐Ÿ“ Named savepoints for fine-grained control")
    fmt.Println("  ๐Ÿ”’ Full ACID properties within memory scope")
    fmt.Println("  ๐Ÿ›ก๏ธ Automatic state restoration on rollback")
    fmt.Println("  ๐ŸŽฏ Thread-safe transaction management")
}
package main

import (
    "fmt"
    "github.com/abbychau/mist"
)

func main() {
    engine := mist.NewSQLEngine()
    
    // Setup initial data
    engine.Execute("CREATE TABLE orders (id INT, customer VARCHAR(50), amount FLOAT)")
    
    // Start recording all queries
    engine.StartRecording()
    
    // Execute business logic queries
    engine.Execute("INSERT INTO orders VALUES (1, 'Alice', 299.99)")
    engine.Execute("INSERT INTO orders VALUES (2, 'Bob', 149.50)")
    engine.Execute("UPDATE orders SET amount = 249.99 WHERE id = 1")
    engine.Execute("SELECT customer, SUM(amount) FROM orders GROUP BY customer")
    
    // Stop recording
    engine.EndRecording()
    
    // Analyze recorded queries
    queries := engine.GetRecordedQueries()
    fmt.Printf("Business logic executed %d queries:\n", len(queries))
    
    for i, query := range queries {
        fmt.Printf("%d. %s\n", i+1, query)
    }
    
    // Use for debugging, testing, or audit logs
    // Perfect for understanding query execution patterns
}
# Start interactive mode
./mist -i

# Create table with enhanced features:
mist> CREATE TABLE companies (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL UNIQUE,
    type ENUM('startup', 'corporation') DEFAULT 'startup',
    founded DATE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Table 'companies' created successfully.

mist> CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    company_id INT NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    hire_date DATE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE
);
Table 'employees' created successfully.

# Insert data with enhanced types:
mist> INSERT INTO companies (name, type, founded) VALUES ('Tech Corp', 'startup', '2020-01-15');
1 row inserted.

mist> INSERT INTO employees (company_id, email, hire_date) VALUES (1, '[email protected]', '2024-01-01');
1 row inserted.

# UPDATE automatically updates timestamp:
mist> UPDATE employees SET email = '[email protected]' WHERE id = 1;
1 row updated. (updated_at automatically set)

# GROUP BY with aggregates:
mist> SELECT c.type, COUNT(e.id) as employee_count FROM companies c LEFT JOIN employees e ON c.id = e.company_id GROUP BY c.type;
+-----------+----------------+
| type      | employee_count |
+-----------+----------------+
| startup   | 1              |
+-----------+----------------+
-- Enhanced data types and constraints
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    username VARCHAR(50) NOT NULL UNIQUE,
    full_name VARCHAR(100),
    birth_date DATE,
    status ENUM('active', 'inactive', 'suspended') DEFAULT 'active',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Foreign key relationships with CASCADE actions
CREATE TABLE departments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    manager_id INT,
    FOREIGN KEY (manager_id) REFERENCES users(id) ON DELETE SET NULL
);

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    department_id INT,
    hire_date DATE NOT NULL,
    salary DECIMAL(10,2),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE SET NULL
);

-- Complex JOINs with enhanced GROUP BY
SELECT 
    d.name AS department,
    u.status,
    COUNT(e.id) AS employee_count,
    AVG(e.salary) AS avg_salary,
    MIN(e.hire_date) AS earliest_hire,
    MAX(e.hire_date) AS latest_hire
FROM departments d
LEFT JOIN employees e ON d.id = e.department_id
LEFT JOIN users u ON e.user_id = u.id
GROUP BY d.name, u.status
HAVING COUNT(e.id) > 0
ORDER BY avg_salary DESC;

-- Date operations and ENUM validation
INSERT INTO users (email, username, full_name, birth_date, status) 
VALUES ('[email protected]', 'johnsmith', 'John Smith', '1985-06-15', 'active');

-- UPDATE automatically updates timestamp
UPDATE users SET full_name = 'John A. Smith' WHERE username = 'johnsmith';

-- Foreign key constraints ensure referential integrity
-- This would succeed: 
INSERT INTO employees (user_id, department_id, hire_date, salary) 
VALUES (1, 1, '2024-01-15', 75000.00);

-- This would fail due to foreign key constraint:
-- INSERT INTO employees (user_id, department_id, hire_date, salary) 
-- VALUES (999, 1, '2024-01-15', 75000.00);

-- CASCADE DELETE: Deleting a user cascades to employee records
-- SET NULL: Deleting a department sets employee department_id to NULL
DELETE FROM users WHERE id = 1; -- Cascades to employees table

-- Advanced Transaction Control
START TRANSACTION;
  INSERT INTO users (email, username, full_name) VALUES ('[email protected]', 'manager1', 'Jane Manager');

  SAVEPOINT before_dept_changes;
    INSERT INTO departments (name, manager_id) VALUES ('Engineering', LAST_INSERT_ID());

    BEGIN; -- Nested transaction
      INSERT INTO employees (user_id, department_id, hire_date, salary) VALUES (2, 1, '2024-01-01', 120000);
      UPDATE employees SET salary = salary * 1.1 WHERE department_id = 1; -- 10% raise
    ROLLBACK; -- Rollback nested transaction (no raises applied)

  -- Keep department but rollback to before department changes if needed
  -- ROLLBACK TO SAVEPOINT before_dept_changes;

  RELEASE SAVEPOINT before_dept_changes;
COMMIT; -- Commit all remaining changes

๐Ÿš€ MySQL-Compatible Daemon Server

Starting the Daemon

# Start on default MySQL port (3306)
go run . -d
# or
go run . --daemon

# Start on custom port
go run . -d --port 3307
go run . --daemon --port 8080

# Build and run as standalone
go build -o mistdb .
./mistdb -d --port 3306

# Run in background for production
nohup ./mistdb -d --port 3306 > mist.log 2>&1 &

Connecting to the Daemon

# Using telnet (most common for testing)
telnet localhost 3306

# Using netcat
nc localhost 3306

# Any TCP client can connect
# The daemon uses a simple text-based protocol

Key Features

  • โœ… MySQL-Compatible port (3306)
  • โœ… Multiple concurrent connections
  • โœ… Real-time query performance timing
  • โœ… Graceful shutdown handling
  • โœ… Full SQL support with all Mist features
  • โœ… Connection tracking and logging

Interactive Session Example

telnet localhost 3306
$ telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome to Mist MySQL-compatible database (Connection #1)
Type 'help' for commands, 'quit' to exit

mist> CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    price DECIMAL(10,2),
    category ENUM('Electronics', 'Books', 'Clothing') DEFAULT 'Electronics',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Table products created successfully
Query OK (234.5ยตs)

mist> INSERT INTO products (name, price, category) VALUES 
    ('Laptop Pro', 999.99, 'Electronics'),
    ('SQL Guide', 29.99, 'Books'),
    ('Wireless Mouse', 49.99, 'Electronics');
Insert successful
Query OK (123.2ยตs)

mist> SELECT category, COUNT(*) as count, AVG(price) as avg_price 
      FROM products 
      GROUP BY category 
      ORDER BY avg_price DESC;
+-------------+-------+-----------+
| category    | count | avg_price |
+-------------+-------+-----------+
| Electronics | 2     | 524.99    |
| Books       | 1     | 29.99     |
+-------------+-------+-----------+
2 rows in set (89.3ยตs)

mist> SELECT name FROM products 
      WHERE price > (SELECT AVG(price) FROM products);
+-----------+
| name      |
+-----------+
| Laptop Pro|
+-----------+
1 row in set (156.7ยตs)

mist> help
Available SQL commands:
- CREATE TABLE, ALTER TABLE, DROP TABLE
- INSERT, SELECT, UPDATE, DELETE  
- START TRANSACTION, COMMIT, ROLLBACK
- CREATE INDEX, DROP INDEX, SHOW INDEX
- SHOW TABLES
Type 'quit' to exit

mist> quit
Bye!
Connection closed by foreign host.

Supported Operations

๐Ÿ—๏ธ Complete SQL Operations

Full support for table operations (CREATE/ALTER/DROP), data manipulation (INSERT/UPDATE/DELETE), and advanced querying with JOINs, subqueries, and aggregates. Includes AUTO_INCREMENT, UNIQUE constraints, and foreign key relationships with CASCADE actions.

๐Ÿ”„ Advanced Transactions

Full nested transaction support with unlimited depth, savepoints, and ACID properties. Thread-safe transaction management with ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT capabilities for fine-grained control.

๐Ÿ—‚๏ธ Modern Data Types

Native support for DATE, ENUM, DECIMAL, TIMESTAMP with automatic ON UPDATE CURRENT_TIMESTAMP, and comprehensive referential integrity enforcement with foreign key constraints and validation.