-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathengine.py
More file actions
38 lines (34 loc) · 1.4 KB
/
Copy pathengine.py
File metadata and controls
38 lines (34 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from sqlalchemy import create_engine, Engine
from csvpath.util.config import Config
from csvpath.util.box import Box
class Db:
@classmethod
def get(cls, config: Config) -> Engine:
dialect = config.get(section="sql", name="dialect")
conn = config.get(section="sql", name="connection_string")
return cls.get_engine(conn=conn, dialect=dialect)
@classmethod
def get_engine(cls, *, conn:str, dialect:str="sqlite") -> Engine:
box = Box()
engine = box.get(key=Box.SQL_ENGINE)
if engine is None:
#
# atm there's nothing different about the dialects, but we'll
# leave the ifs just for docs purposes.
#
if dialect == "sqlite":
# sqlite:///example.db
engine = create_engine(conn)
elif dialect == "postgres":
# postgresql+psycopg2://user:password@localhost/dbname
engine = create_engine(conn)
elif dialect == "mysql":
# mysql+pymysql://user:password@localhost/dbname
engine = create_engine(conn)
elif dialect == "sql_server":
# mssql+pyodbc://user:password@localhost/dbname
engine = create_engine(conn)
else:
raise ValueError("Unknown RDBMS dialect %s", dialect)
box.add(Box.SQL_ENGINE, engine)
return engine