Update sql to prepare for sqlalchemy 2.0 (#71532)

* Update sql to prepare for sqlalchemy 2.0

* config flow as well
pull/71720/head
J. Nick Koston 2022-05-11 22:45:47 -05:00 committed by GitHub
parent 9cd81db5b3
commit 18bdc70185
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 6 deletions

View File

@ -45,15 +45,15 @@ def validate_sql_select(value: str) -> str | None:
def validate_query(db_url: str, query: str, column: str) -> bool:
"""Validate SQL query."""
try:
engine = sqlalchemy.create_engine(db_url)
sessmaker = scoped_session(sessionmaker(bind=engine))
engine = sqlalchemy.create_engine(db_url, future=True)
sessmaker = scoped_session(sessionmaker(bind=engine, future=True))
except SQLAlchemyError as error:
raise error
sess: scoped_session = sessmaker()
try:
result: Result = sess.execute(query)
result: Result = sess.execute(sqlalchemy.text(query))
for res in result.mappings():
data = res[column]
_LOGGER.debug("Return value from query: %s", data)

View File

@ -111,8 +111,8 @@ async def async_setup_entry(
value_template.hass = hass
try:
engine = sqlalchemy.create_engine(db_url)
sessmaker = scoped_session(sessionmaker(bind=engine))
engine = sqlalchemy.create_engine(db_url, future=True)
sessmaker = scoped_session(sessionmaker(bind=engine, future=True))
except SQLAlchemyError as err:
_LOGGER.error("Can not open database %s", {redact_credentials(str(err))})
return
@ -179,7 +179,7 @@ class SQLSensor(SensorEntity):
self._attr_extra_state_attributes = {}
sess: scoped_session = self.sessionmaker()
try:
result = sess.execute(self._query)
result = sess.execute(sqlalchemy.text(self._query))
except SQLAlchemyError as err:
_LOGGER.error(
"Error executing query %s: %s",