rewrite values in db

This commit is contained in:
callebtc 2022-11-25 15:11:58 +01:00 committed by dni ⚡
parent 6d16ac0f0a
commit 2ec150fd13

View File

@ -72,30 +72,41 @@ class Connection(Compat):
if self.type in {POSTGRES, COCKROACH}: if self.type in {POSTGRES, COCKROACH}:
query = query.replace("%", "%%") query = query.replace("%", "%%")
query = query.replace("?", "%s") query = query.replace("?", "%s")
return query
def rewrite_values(self, values):
# strip html # strip html
CLEANR = re.compile("<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});") CLEANR = re.compile("<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});")
def cleanhtml(raw_html): def cleanhtml(raw_html):
if type(raw_html) == str:
cleantext = re.sub(CLEANR, "", raw_html) cleantext = re.sub(CLEANR, "", raw_html)
return cleantext return cleantext
else:
return raw_html
query = cleanhtml(query) # tuple to list and back to tuple
values = tuple([cleanhtml(l) for l in list(values)])
return query return values
async def fetchall(self, query: str, values: tuple = ()) -> list: async def fetchall(self, query: str, values: tuple = ()) -> list:
result = await self.conn.execute(self.rewrite_query(query), values) result = await self.conn.execute(
self.rewrite_query(query), self.rewrite_values(values)
)
return await result.fetchall() return await result.fetchall()
async def fetchone(self, query: str, values: tuple = ()): async def fetchone(self, query: str, values: tuple = ()):
result = await self.conn.execute(self.rewrite_query(query), values) result = await self.conn.execute(
self.rewrite_query(query), self.rewrite_values(values)
)
row = await result.fetchone() row = await result.fetchone()
await result.close() await result.close()
return row return row
async def execute(self, query: str, values: tuple = ()): async def execute(self, query: str, values: tuple = ()):
return await self.conn.execute(self.rewrite_query(query), values) return await self.conn.execute(
self.rewrite_query(query), self.rewrite_values(values)
)
class Database(Compat): class Database(Compat):