# minidb A tiny in-process table store with JSON persistence. Pure Python 3, standard library only. ## Library ```python from minidb import Column, Database, JsonFileStorage db = Database(JsonFileStorage("people.json")) # or Database() for an in-memory database people = db.create_table("people", [ Column("name", "str", nullable=False), Column("age", "int"), Column("city", "str"), ]) person_id = people.insert(name="Ann", age=31, city="Berlin") db.save() people.select(where=lambda row: row["age"] is not None and row["age"] > 30, order_by="name", limit=10) people.update({"city": "Rome"}, where=lambda row: row["city"] == "Berlin") people.delete(where=lambda row: row["age"] is None) people.count() ``` * Column types are `int`, `float`, `str` and `bool`. Every table has an integer `id` column that is assigned automatically. * `select(where=None, columns=None, order_by=None, descending=False, limit=None)` returns copies of the rows as dictionaries, in insertion order unless `order_by` is given. Rows with NULL in the `order_by` column come last. * `where` is a callable that receives the row dictionary; `None` means every row. * `Table.create_index(column)` adds a hash index that speeds up `Table.find(column, value)`. * Column names must be identifiers and must not be one of the reserved words `AND OR NOT IN BETWEEN LIKE ILIKE IS NULL TRUE FALSE`. ## Command line ```text python3 -m minidb.cli --db people.json create people name:str:required age:int city:str python3 -m minidb.cli --db people.json insert people name=Ann age=31 city=Berlin python3 -m minidb.cli --db people.json select people --columns id,name --order-by age --desc --limit 5 python3 -m minidb.cli --db people.json update people --id 1 city=Rome python3 -m minidb.cli --db people.json delete people --id 1 python3 -m minidb.cli --db people.json import-csv people people.csv python3 -m minidb.cli --db people.json export-csv people out.csv ``` * `select` prints CSV (header line, then one line per row; NULL is an empty field, booleans are `true` / `false`). * `column=` with nothing after the equals sign sets NULL. * `update` and `delete` print `updated N` / `deleted N`. * Errors print `error: ...` on standard error and the exit status is 2. ## Tests ```text python3 -m unittest discover -s tests ``` ## Query language `where` may also be a string in the query language specified in `SPEC.md`. It is available from `Table.select`, `Table.update`, `Table.delete` and `Table.count`, from `minidb.query.compile_where`, and as `--where` on the command line. ```python people.select(where="age >= 18 AND city IN ('Berlin', 'Rome') AND NOT name LIKE 'A%'") people.update({"city": "Oslo"}, where="note IS NULL") people.count(where="age BETWEEN 20 AND 30") ``` ```text python3 -m minidb.cli --db people.json select people --where "age > 30 OR city = 'Rome'" --order-by age python3 -m minidb.cli --db people.json update people --where "city IS NULL" city=Unknown python3 -m minidb.cli --db people.json delete people --where "age < 18" ``` * Operators: `= != <> < <= > >=`, `IN (...)`, `NOT IN`, `BETWEEN a AND b`, `LIKE` / `ILIKE` with `%` and `_`, `IS NULL`, `IS NOT NULL`. * Precedence: `NOT` binds tighter than `AND`, which binds tighter than `OR`; parentheses group. * Literals: integers, floats, `TRUE` / `FALSE`, `NULL` and single-quoted strings (`''` for a quote). Identifiers are column names. * Comparisons with NULL are unknown and unknown rows are not selected; use `IS NULL`. * Comparing a string with a number raises `QueryTypeError` when the first offending row is evaluated; an unknown column raises `QueryNameError` and a malformed expression `QuerySyntaxError` (with the 0-based `position`) before any row is examined. * `update` and `delete` on the command line take either `--id N` or `--where EXPR`.