Skip to main content

A lightweight DBMS that stores data in TSV files with a simple SQL interface

Project description

TxtSql

A lightweight DBMS that stores data in tab-separated-value (TSV) files with a simple SQL interface.

Architecture

SQL string → Lexer → Tokens → Parser → AST → Executor → Storage (TSV files)
                                  ↓
                            Evaluator (WHERE / HAVING)
Module Role
lexer.py Tokenises SQL text into Token stream
parser.py Recursive-descent parser producing AST nodes (ast.py)
evaluator.py Compiles WHERE/HAVING expressions into row predicates
executor.py Executes statements — coordinates storage and evaluator
storage.py TSV-file CRUD: Table class with atomic writes
engine.py Orchestrator: execute_sql(sql) runs the full pipeline
cli.py Interactive REPL with ASCII table output

Requirements

  • Python >= 3.12
  • openpyxl (for IMPORT from .xlsx files)
pip install openpyxl

Usage

python -m txtsql
Welcome to TxtSql CLI. Type your SQL statement and press Enter.
txtsql> CREATE TABLE employees (id NUMBER, name STRING, salary NUMBER)
txtsql> INSERT INTO employees VALUES (1, 'Alice', 75000)
txtsql> INSERT INTO employees VALUES (2, 'Bob', 62000)
txtsql> SELECT * FROM employees ORDER BY salary DESC
+----+-------+--------+
| id | name  | salary |
+----+-------+--------+
| 1  | Alice | 75000  |
| 2  | Bob   | 62000  |
+----+-------+--------+

SQL Reference

CREATE TABLE

CREATE TABLE table_name (col1 TYPE, col2 TYPE, ...);

Types: STRING, NUMBER

INSERT

INSERT INTO table_name VALUES (val1, val2, ...);
INSERT INTO table_name (col1, col2) VALUES (val1, val2);
INSERT INTO table_name VALUES (1, 'a'), (2, 'b');

Values: 'string', 42, 3.14, NULL, TRUE, FALSE

SELECT

SELECT [DISTINCT] col1, col2, ... FROM table_name
    [WHERE condition]
    [GROUP BY col, ...]
    [HAVING aggregate_condition]
    [ORDER BY col [ASC|DESC], ...]
    [LIMIT n] [OFFSET n];

SELECT COUNT(*) AS cnt FROM table_name;
SELECT dept, AVG(salary) AS avg_sal FROM emp GROUP BY dept;

UPDATE

UPDATE table_name SET col1 = val1, col2 = val2 [WHERE condition];

DELETE

DELETE FROM table_name [WHERE condition];

DROP TABLE

DROP TABLE table_name;

SHOW TABLES / DESCRIBE (system commands)

SHOW TABLES;
DESCRIBE table_name;
  • SHOW TABLES lists all tables with column names and row counts
  • DESCRIBE shows column definitions and row count for a specific table

IMPORT (Excel)

IMPORT table_name FROM 'path/to/file.xlsx';
IMPORT table_name (col1 STRING, col2 NUMBER) FROM 'path/to/file.xlsx';
  • First row must be column headers
  • Types are auto-inferred (NUMBER if all values are numeric, otherwise STRING)
  • Optional column definitions override auto-inference

EXPORT (query results to file)

SELECT * FROM table_name INTO OUTFILE 'path/to/data.xlsx';
SELECT dept, AVG(salary) AS avg FROM emp GROUP BY dept INTO OUTFILE 'report.csv';
  • Format determined by file extension: .xlsx, .csv, .tsv
  • Headers are automatically included in the output
  • Returns number of rows exported

WHERE / HAVING expressions

WHERE salary > 50000 AND name <> 'Bob'
WHERE dept = 'Sales' OR dept = 'Eng'
WHERE score IS NULL
WHERE (a > 1 AND b < 2) OR c = 3
  • =, <>, >, <, >=, <=
  • AND, OR, parentheses
  • IS NULL, IS NOT NULL
  • NULL comparisons always return false (SQL standard)

Storage Format

Tables are stored as TSV files in the working directory:

metadata.txt          — table registry (name, column count, col_name, type, ...)
<table_name>.txt      — data rows (tab-separated, QUOTE_NONNUMERIC quoting)
  • NULL values are stored as empty fields
  • All writes are atomic (temp file + rename)
  • TSV quoting protects strings containing tabs or newlines

Limitations

  • No joins, subqueries, or transactions
  • No indexes — all queries scan the full table
  • Strings containing literal \N are safe (NULL uses empty field, not \N sentinel)
  • AS keyword is mandatory for aggregate aliases: COUNT(*) AS cnt
  • DISTINCT only applies to non-aggregate queries
  • No escaped quotes inside string literals

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

txt_sql-0.2.0.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

txt_sql-0.2.0-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file txt_sql-0.2.0.tar.gz.

File metadata

  • Download URL: txt_sql-0.2.0.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for txt_sql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 001e7f5ffbc990d1da7f1f1095a3e21d473c8a962ce7db31b505275bcc94f1ca
MD5 1181d11a3424ba1b46b17eb71f97349a
BLAKE2b-256 7f019bc129b1dc4fb886f06561906a19b70bf8a6e6f4170f177af91c749a5586

See more details on using hashes here.

Provenance

The following attestation bundles were made for txt_sql-0.2.0.tar.gz:

Publisher: publish.yml on CrispyXYZ/txt-sql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file txt_sql-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: txt_sql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for txt_sql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b93a05db40c784c821894bdbdad2fd30eca894f641e13a6440f72a89762629f
MD5 3e707f18a910c87d23198977a5192d7c
BLAKE2b-256 bb8b1871f160fb20d1d5bb1475a0cf91f9a5b306b1743b21b4aedeb1b150d047

See more details on using hashes here.

Provenance

The following attestation bundles were made for txt_sql-0.2.0-py3-none-any.whl:

Publisher: publish.yml on CrispyXYZ/txt-sql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page