Convert SQL to TOML
Max file size 100mb.
SQL vs TOML Format Comparison
| Aspect | SQL (Source Format) | TOML (Target Format) |
|---|---|---|
| Format Overview |
SQL
Structured Query Language
The standard language for managing and querying relational databases. SQL encompasses DDL (CREATE, ALTER, DROP), DML (SELECT, INSERT, UPDATE, DELETE), and DCL (GRANT, REVOKE) statements. Used universally across all major relational database management systems including MySQL, PostgreSQL, Oracle, SQL Server, and SQLite. Database Language ISO Standard |
TOML
Tom's Obvious Minimal Language
A minimal configuration file format created by Tom Preston-Werner in 2013. Designed to be easy to read with obvious semantics. Maps unambiguously to a hash table and supports typed values including strings, integers, floats, booleans, dates, arrays, and tables. Widely adopted in Rust (Cargo.toml) and Python (pyproject.toml) ecosystems. Configuration Format Formally Specified |
| Technical Specifications |
Structure: Declarative statements and queries
Standard: ISO/IEC 9075 (SQL:2023) Encoding: UTF-8, varies by RDBMS Statements: DDL, DML, DCL, TCL Extensions: .sql |
Structure: Key-value pairs with tables and arrays
Encoding: UTF-8 required Type System: Strings, integers, floats, booleans, dates Nesting: Tables and array of tables Extensions: .toml |
| Syntax Examples |
SQL data definition and insertion: CREATE TABLE servers ( id INT PRIMARY KEY, hostname VARCHAR(255), port INT DEFAULT 8080, active BOOLEAN DEFAULT TRUE ); INSERT INTO servers VALUES (1, 'web01.local', 80, TRUE); |
TOML configuration structure: [servers.schema] columns = ["id", "hostname", "port", "active"] [[servers.data]] id = 1 hostname = "web01.local" port = 80 active = true |
| Content Support |
|
|
| Advantages |
|
|
| Disadvantages |
|
|
| Common Uses |
|
|
| Best For |
|
|
| Version History |
Introduced: 1974 (IBM System R)
ISO Standard: SQL:2023 (latest revision) Status: Active, continuously evolving Key Milestones: SQL-92, SQL:1999, SQL:2011 |
Introduced: 2013 (Tom Preston-Werner)
Current Version: TOML v1.0.0 (2021) Status: Stable, formally specified Evolution: Reached 1.0 stability milestone |
| Software Support |
Databases: MySQL, PostgreSQL, Oracle, SQL Server, SQLite
Tools: DBeaver, pgAdmin, MySQL Workbench, DataGrip Languages: All major languages via drivers/ORMs Cloud: AWS RDS, Azure SQL, Google Cloud SQL |
Rust: toml crate (native support)
Python: tomllib (stdlib 3.11+), tomli JavaScript: @iarna/toml, toml-js Other: Go, Java, C#, Ruby libraries |
Why Convert SQL to TOML?
Converting SQL files to TOML format is valuable when you need to extract database schema definitions, configuration data, or seed data from SQL scripts and represent them as clean, readable configuration files. TOML's minimal syntax and strong typing make it an excellent target for database metadata that needs to be consumed by modern build systems, deployment tools, or application configuration frameworks.
SQL table definitions translate naturally into TOML tables and arrays. Column definitions become key-value pairs with type information, table constraints become nested sections, and INSERT statements become arrays of tables representing data records. The resulting TOML file provides a clear, human-readable representation of the database structure that can be version-controlled, diffed, and reviewed more easily than raw SQL.
This conversion is particularly useful in infrastructure-as-code workflows where database configurations need to be managed alongside application settings. Instead of maintaining separate SQL migration files, teams can define database schemas in TOML and generate SQL from configuration. The TOML format supports comments for documentation, native data types for type safety, and a clean structure that makes configuration reviews straightforward.
The conversion also benefits projects that use TOML as their primary configuration format (like Rust's Cargo.toml or Python's pyproject.toml). By converting SQL seed data to TOML, you can bundle database initialization data with your project configuration, creating a single source of truth for application settings and initial data in a format that is easy to edit and maintain.
Key Benefits of Converting SQL to TOML:
- Readable Configuration: Transform database schemas into clean, human-readable config files
- Strong Typing: TOML preserves data types (integers, booleans, dates) from SQL definitions
- Version Control Friendly: TOML files produce clean diffs for code review workflows
- Infrastructure as Code: Manage database schemas alongside application configuration
- Ecosystem Integration: Works with Cargo, pyproject.toml, and modern build systems
- Comment Support: Add inline documentation to schema definitions
- Unambiguous Parsing: TOML's formal specification ensures consistent interpretation
Practical Examples
Example 1: Database Schema to Configuration
Input SQL file (schema.sql):
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE roles (
id SERIAL PRIMARY KEY,
name VARCHAR(30) NOT NULL,
description TEXT
);
Output TOML file (schema.toml):
[tables.users] primary_key = "id" [[tables.users.columns]] name = "id" type = "SERIAL" primary_key = true [[tables.users.columns]] name = "username" type = "VARCHAR(50)" nullable = false unique = true [[tables.users.columns]] name = "email" type = "VARCHAR(255)" nullable = false [[tables.users.columns]] name = "is_active" type = "BOOLEAN" default = true [[tables.users.columns]] name = "created_at" type = "TIMESTAMP" default = "CURRENT_TIMESTAMP" [tables.roles] primary_key = "id" [[tables.roles.columns]] name = "id" type = "SERIAL" primary_key = true [[tables.roles.columns]] name = "name" type = "VARCHAR(30)" nullable = false [[tables.roles.columns]] name = "description" type = "TEXT"
Example 2: Seed Data to Configuration
Input SQL file (seed.sql):
INSERT INTO settings (key, value, category) VALUES
('app.name', 'MyApplication', 'general'),
('app.version', '2.5.0', 'general'),
('db.pool_size', '25', 'database'),
('db.timeout', '30', 'database'),
('cache.enabled', 'true', 'performance'),
('cache.ttl', '3600', 'performance');
Output TOML file (seed.toml):
[general] "app.name" = "MyApplication" "app.version" = "2.5.0" [database] "db.pool_size" = 25 "db.timeout" = 30 [performance] "cache.enabled" = true "cache.ttl" = 3600
Example 3: Server Configuration Query
Input SQL file (servers.sql):
INSERT INTO servers (hostname, port, environment, ssl_enabled, max_connections)
VALUES
('api-01.prod.example.com', 443, 'production', TRUE, 1000),
('api-02.prod.example.com', 443, 'production', TRUE, 1000),
('staging.example.com', 8443, 'staging', TRUE, 200);
Output TOML file (servers.toml):
[[servers]] hostname = "api-01.prod.example.com" port = 443 environment = "production" ssl_enabled = true max_connections = 1000 [[servers]] hostname = "api-02.prod.example.com" port = 443 environment = "production" ssl_enabled = true max_connections = 1000 [[servers]] hostname = "staging.example.com" port = 8443 environment = "staging" ssl_enabled = true max_connections = 200
Frequently Asked Questions (FAQ)
Q: What is TOML format?
A: TOML (Tom's Obvious Minimal Language) is a configuration file format created by Tom Preston-Werner in 2013. It uses simple key-value pairs, tables (sections), and arrays to represent structured data. TOML supports native data types including strings, integers, floats, booleans, and RFC 3339 dates. It reached version 1.0 in 2021 and is widely used in Rust (Cargo.toml) and Python (pyproject.toml) projects.
Q: How are SQL tables mapped to TOML structures?
A: SQL table definitions are converted to TOML tables with column definitions as arrays of tables. Each column becomes a table entry with name, type, and constraint properties. INSERT statements are converted to arrays of tables where each row becomes a table entry with column names as keys. The hierarchical TOML structure preserves the relational organization of the SQL schema.
Q: Are SQL data types preserved in TOML?
A: Yes, TOML's strong typing maps well to SQL data types. SQL INTEGER values become TOML integers, BOOLEAN values become TOML booleans, VARCHAR/TEXT values become TOML strings, and TIMESTAMP values can be represented as TOML date-time values. TOML's native type system provides more type safety than text-based formats like CSV or plain text.
Q: Can I convert SQL queries (SELECT) to TOML?
A: The converter primarily handles DDL statements (CREATE TABLE) and DML data (INSERT INTO) since these contain structured data that maps well to TOML. SELECT queries describe operations rather than data, so they are converted to descriptive TOML sections with query metadata. For best results, use SQL files containing table definitions and insert statements.
Q: How are SQL constraints represented in TOML?
A: SQL constraints like PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY, and DEFAULT values are converted to boolean or string properties within each column's TOML table entry. For example, a NOT NULL constraint becomes nullable = false, and a DEFAULT value becomes a default key. Foreign key relationships are represented as nested tables with referenced table and column information.
Q: Is the TOML output compatible with Cargo.toml or pyproject.toml?
A: The generated TOML follows standard TOML v1.0 syntax and can be parsed by any TOML-compliant parser. However, the structure is specific to database schema representation and would need adaptation to match Cargo.toml or pyproject.toml conventions. The output is ideal for custom configuration files, database migration tools, or schema documentation.
Q: How are SQL comments handled?
A: SQL comments (-- single-line and /* multi-line */) are preserved as TOML comments (# prefix). This maintains documentation and context from the original SQL file. TOML's comment syntax is simpler but equally effective for inline documentation, making the converted file self-documenting.
Q: Can I convert the TOML back to SQL?
A: While this converter handles SQL-to-TOML conversion, the structured TOML output retains enough information to regenerate SQL. The column types, constraints, and data values are all preserved as typed TOML values. Many ORM tools and migration frameworks can generate SQL from configuration files, making round-trip conversion feasible with appropriate tooling.