Update sql to prepare for sqlalchemy 2.0 (#71532)
* Update sql to prepare for sqlalchemy 2.0 * config flow as wellpull/71720/head
parent
9cd81db5b3
commit
18bdc70185
|
@ -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)
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue