Skip to content

Commit

Permalink
Pg schemas (#95)
Browse files Browse the repository at this point in the history
* allow pg schemas usage
  • Loading branch information
eloyfelix authored Feb 26, 2023
1 parent 79f7616 commit bed59f4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions FPSim2/FPSim2.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ def __init__(
fps_sort: bool = False,
storage_backend: str = "pytables",
conn_url: str = "",
table_name: str = ""
table_name: str = "",
pg_schema: str = ""
) -> None:
super(FPSim2Engine, self).__init__(
fp_filename=fp_filename,
storage_backend=storage_backend,
in_memory_fps=in_memory_fps,
fps_sort=fps_sort,
conn_url=conn_url,
table_name=table_name
table_name=table_name,
pg_schema=pg_schema
)
self.empty_sim = np.ndarray((0,), dtype=[("mol_id", "<u4"), ("coeff", "<f4")])
self.empty_subs = np.ndarray((0,), dtype="<u4")
Expand Down
2 changes: 1 addition & 1 deletion FPSim2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
except Exception as e:
pass

__version__ = "0.4.3"
__version__ = "0.4.4"
3 changes: 2 additions & 1 deletion FPSim2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(
fps_sort: bool,
conn_url: str,
table_name: str,
pg_schema: str,
) -> None:

self.fp_filename = fp_filename
Expand All @@ -41,7 +42,7 @@ def __init__(
raise ValueError(
"FPSim2 sqla engine only works for PostgreSQL, MySQL and Oracle (experimental)"
)
self.storage = SqlaStorageBackend(conn_url, table_name)
self.storage = SqlaStorageBackend(conn_url, table_name, pg_schema)

@property
def fps(self):
Expand Down
15 changes: 11 additions & 4 deletions FPSim2/io/backends/sqla.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
select,
insert,
func,
text,
)
from sqlalchemy.orm import declarative_base, DeclarativeMeta
import numpy as np
Expand Down Expand Up @@ -94,16 +95,22 @@ def create_db_table(
)
conn.commit()


class SqlaStorageBackend(BaseStorageBackend):
def __init__(self, conn_url: str, table_name: str = "fpsim2_fingerprints") -> None:
def __init__(self, conn_url: str, table_name: str, pg_schema: str) -> None:
super(SqlaStorageBackend, self).__init__()
self.conn_url = conn_url
self.pg_schema = pg_schema

engine = create_engine(conn_url)
metadata = MetaData()
if engine.dialect.name == "postgresql" and pg_schema:
metadata = MetaData(schema=pg_schema)
else:
metadata = MetaData()
metadata.reflect(engine)
self.sqla_table = metadata.tables[table_name]
if engine.dialect.name == "postgresql" and pg_schema:
self.sqla_table = metadata.tables[f"{pg_schema}.{table_name}"]
else:
self.sqla_table = metadata.tables[table_name]

self.in_memory_fps = True
self.name = "sqla"
Expand Down

0 comments on commit bed59f4

Please sign in to comment.