Little fixes
This commit is contained in:
parent
e25a5aae20
commit
9e1ca9dd43
1 changed files with 29 additions and 9 deletions
|
|
@ -15,7 +15,7 @@ class _ExtendedConnection(Connection):
|
||||||
|
|
||||||
class ColumnCondition:
|
class ColumnCondition:
|
||||||
def __init__(
|
def __init__(
|
||||||
self, column: str, value: Any, isString: bool = False, isLike: bool = False
|
self, column: str, value: Any, isString: bool = False, isLike: bool = True
|
||||||
):
|
):
|
||||||
self.column = column
|
self.column = column
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
@ -48,7 +48,7 @@ class DBConnector:
|
||||||
def __init__(self, conninfo: str):
|
def __init__(self, conninfo: str):
|
||||||
self._pool = ConnectionPool(conninfo, connection_class=_ExtendedConnection)
|
self._pool = ConnectionPool(conninfo, connection_class=_ExtendedConnection)
|
||||||
|
|
||||||
if not self.tableExists("tables_metadata"):
|
if not self.__tableExistsInternal("tables_metadata"):
|
||||||
columns = [
|
columns = [
|
||||||
PrimaryUUIDColumnDefinition("table_id"),
|
PrimaryUUIDColumnDefinition("table_id"),
|
||||||
TextColumnDefinition("table_name"),
|
TextColumnDefinition("table_name"),
|
||||||
|
|
@ -103,6 +103,20 @@ class DBConnector:
|
||||||
result = conn.execute(stmt).fetchone()
|
result = conn.execute(stmt).fetchone()
|
||||||
return False if result is None else result["exists"]
|
return False if result is None else result["exists"]
|
||||||
|
|
||||||
|
def __tableExistsInternal(self, table_name: str):
|
||||||
|
stmt = SQL(
|
||||||
|
"""
|
||||||
|
SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_name = {}
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
).format(Literal(table_name))
|
||||||
|
with self.connection() as conn:
|
||||||
|
result = conn.execute(stmt).fetchone()
|
||||||
|
return False if result is None else result["exists"]
|
||||||
|
|
||||||
def createTable(
|
def createTable(
|
||||||
self,
|
self,
|
||||||
table_name: str,
|
table_name: str,
|
||||||
|
|
@ -151,11 +165,17 @@ class DBConnector:
|
||||||
def filterFromTable(
|
def filterFromTable(
|
||||||
self, table_name: str, columns: list[str], where: list[ColumnCondition]
|
self, table_name: str, columns: list[str], where: list[ColumnCondition]
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
stmt = SQL("SELECT {} FROM {} WHERE {}").format(
|
if len(columns) == 1 and columns[0] == "*":
|
||||||
SQL(", ").join(map(lambda c: Identifier(c), columns)),
|
stmt = SQL("SELECT * FROM {} WHERE {}").format(
|
||||||
Identifier(table_name),
|
Identifier(table_name),
|
||||||
SQL(", ").join(map(lambda cp: cp.sql(), where)),
|
SQL(" AND ").join(map(lambda cp: cp.sql(), where)),
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
stmt = SQL("SELECT {} FROM {} WHERE {}").format(
|
||||||
|
SQL(", ").join(map(lambda c: Identifier(c), columns)),
|
||||||
|
Identifier(table_name),
|
||||||
|
SQL(" AND ").join(map(lambda cp: cp.sql(), where)),
|
||||||
|
)
|
||||||
with self.connection() as conn:
|
with self.connection() as conn:
|
||||||
return conn.execute(stmt).fetchall()
|
return conn.execute(stmt).fetchall()
|
||||||
|
|
||||||
|
|
@ -165,7 +185,7 @@ class DBConnector:
|
||||||
stmt = SQL("UPDATE {} SET {} WHERE {}").format(
|
stmt = SQL("UPDATE {} SET {} WHERE {}").format(
|
||||||
Identifier(table_name),
|
Identifier(table_name),
|
||||||
SQL(", ").join(map(lambda cp: cp.sql(), columns)),
|
SQL(", ").join(map(lambda cp: cp.sql(), columns)),
|
||||||
SQL(", ").join(map(lambda cp: cp.sql(), where)),
|
SQL(" AND ").join(map(lambda cp: cp.sql(), where)),
|
||||||
)
|
)
|
||||||
with self.connection() as conn:
|
with self.connection() as conn:
|
||||||
conn.execute(stmt)
|
conn.execute(stmt)
|
||||||
|
|
@ -188,7 +208,7 @@ class DBConnector:
|
||||||
def deleteFromTable(self, table_name: str, where: list[ColumnCondition]):
|
def deleteFromTable(self, table_name: str, where: list[ColumnCondition]):
|
||||||
stmt = SQL("DELETE FROM {} WHERE {}").format(
|
stmt = SQL("DELETE FROM {} WHERE {}").format(
|
||||||
Identifier(table_name),
|
Identifier(table_name),
|
||||||
SQL(", ").join(map(lambda cp: cp.sql(), where)),
|
SQL(" AND ").join(map(lambda cp: cp.sql(), where)),
|
||||||
)
|
)
|
||||||
with self.connection() as conn:
|
with self.connection() as conn:
|
||||||
conn.execute(stmt)
|
conn.execute(stmt)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue