This script will run the provided SQL file, as well as populate the created table with data based on column annotations
--dialect=sqlite
DROP TABLE IF EXISTS foo;
CREATE TABLE foo(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
year INT NOT NULL, --{row[auto()]} same as the column name
name TEXT NOT NULL, --{row["Person Name"]} has different name in the CSV
university TEXT NOT NULL, --{"URFU"} just a constant
group_name TEXT NOT NULL --{arg("group")} console argument
);
For successfull SQL parsing,
the file must contain a comment in the form --dialect=...
An column annotation is:
- A comment to the right of the column definition (on the same line)
- The actuall annotation is located inside the curly braces:
{...}
Any text outside the curly braces is ignored - Represents the right half of a python expression
lambda row: ...
which receives a dictionary and returns the value to be inserted
into the corresponding column
auto()
- returns the corresponding column namearg(name: str)
- retrieves a cli argument by name (see below)
row["Person Name"]
- Named column from CSVrow[auto()]
- In the example above, is equivalent torow["field"]
arg("group")
-
For the cli commandcsvload.py ... --args group=MEOW
returns the string "MEOW"
csvload.py db sql data [--args [ARGS ...]]
Where
db
- is either a sqlalchemy url,
likesqlite:///result.db
orpostgresql://username:password@host:port/database
- or a json file of type:
{ "drivername": "postgresql", "username": "username", "password": "password", "host": "host", "port": 5432 }
- is either a sqlalchemy url,
sql
is an annotated SQL filedata
is a CSV fileargs
is a list of key-value pairs, like
--args uni=URFU year=2023