Commit d4f2a396 authored by Philipp Middendorf's avatar Philipp Middendorf
Browse files

Add readme

parent d47b0e0b
# graphviz-sql-hs
* graphviz-sql-hs
** What’s this?
This takes as /input/ an SQL schema file (meaning a file containing =CREATE TABLE= statements) and outputs a [[https://graphviz.org/][Graphviz]] =dot= file. Example input:
#+begin_src sql
CREATE TABLE "Sample" (
id INTEGER NOT NULL,
target_id INTEGER,
compounds JSON,
micrograph TEXT,
protocol TEXT,
modified DATETIME NOT NULL,
attributi JSON NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(target_id) REFERENCES "Target" (id)
)
CREATE TABLE "Target" (
id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
short_name VARCHAR(255) NOT NULL,
molecular_weight FLOAT,
uniprot_id VARCHAR(64),
PRIMARY KEY (id)
)
#+end_src
When called with:
#+begin_example
stack run < test-file.sql | dot -Tpng -o /tmp/file.png
#+end_example
The resulting image looks like this:
[[./test-data/testfile.png]]
So it draws nice arrows and stuff.
** Usage with SQLAlchemy
If you have all your data in [[https://www.sqlalchemy.org/][SQLAlchemy]] you can let SQLAlchemy output the create table statements. Here’s the code to do that:
#+begin_src python
def _metadata_dump(sql, *multiparams, **params):
print(sql.compile(dialect=self.engine.dialect))
engine = create_engine(
"sqlite://", strategy="mock", executor=_metadata_dump
)
metadata.create_all(engine)
#+end_src
This uses an in-memory SQLite database, so it works in all cases, and =graphviz-sql-hs= can parse this format.
** Acknowledgements
This is basically a Python port of [[https://github.com/rm-hull/sql_graphviz][sql_graphviz]] (which only works with Postgre). Why did I rewrite it? I wanted it to be easily accessible from SQLAlchemy, and parsing stuff in Python is a pain.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment