Skip to content

Commit 2f15082

Browse files
committed
ruff fixes related to string sql queries
1 parent a26a72c commit 2f15082

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

tibiawikisql/tasks/images.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import requests
1111
from colorama import Fore, Style
12+
from pypika import Parameter, SQLLiteQuery as Query, Table
1213

1314
OUTFIT_NAME_TEMPLATES = [
1415
"Outfit %s Male.gif",
@@ -65,7 +66,9 @@ def save_images(
6566
"""Fetch and save article images for a category."""
6667
extension = category.extension
6768
table = category.parser.table.__tablename__
68-
results = conn.execute(f"SELECT title FROM {table}")
69+
category_table = Table(table)
70+
select_query = Query.from_(category_table).select(category_table.title)
71+
results = conn.execute(select_query.get_sql())
6972
titles = [f"{row[0]}{extension}" for row in results]
7073
os.makedirs(f"images/{table}", exist_ok=True)
7174
cache_info = get_cache_info(table)
@@ -98,7 +101,12 @@ def save_images(
98101
except requests.HTTPError:
99102
failed.append(image.file_name)
100103
continue
101-
conn.execute(f"UPDATE {table} SET image = ? WHERE title = ?", (image_bytes, image.clean_name))
104+
update_query = (
105+
Query.update(category_table)
106+
.set(category_table.image, Parameter("?"))
107+
.where(category_table.title == Parameter("?"))
108+
)
109+
conn.execute(update_query.get_sql(), (image_bytes, image.clean_name))
102110
save_cache_info(table, cache_info)
103111
if failed:
104112
echo(f"{Style.RESET_ALL}\tCould not fetch {len(failed):,} images.{Style.RESET_ALL}")
@@ -112,6 +120,12 @@ def save_images(
112120
def save_maps(conn: sqlite3.Connection | sqlite3.Cursor) -> None:
113121
"""Save map floor image files from TibiaMaps."""
114122
url = "https://tibiamaps.github.io/tibia-map-data/floor-{0:02d}-map.png"
123+
map_table = Table("map")
124+
insert_query = (
125+
Query.into(map_table)
126+
.columns(map_table.z, map_table.image)
127+
.insert(Parameter("?"), Parameter("?"))
128+
)
115129
os.makedirs("images/map", exist_ok=True)
116130
for z in range(16):
117131
try:
@@ -126,7 +140,7 @@ def save_maps(conn: sqlite3.Connection | sqlite3.Cursor) -> None:
126140
image = response.content
127141
with open(f"images/map/{z}.png", "wb") as f:
128142
f.write(image)
129-
conn.execute("INSERT INTO map(z, image) VALUES(?,?)", (z, image))
143+
conn.execute(insert_query.get_sql(), (z, image))
130144

131145

132146
def generate_outfit_image_names(rows: list[tuple[int, str]]) -> tuple[list[str], dict[str, tuple[int, int, str]]]:
@@ -152,9 +166,22 @@ def save_outfit_images(
152166
) -> None:
153167
"""Save outfit image variants into the database."""
154168
table = "outfit"
169+
outfit_table = Table(table)
170+
outfit_image_table = Table("outfit_image")
171+
insert_query = (
172+
Query.into(outfit_image_table)
173+
.columns(
174+
outfit_image_table.outfit_id,
175+
outfit_image_table.addon,
176+
outfit_image_table.sex,
177+
outfit_image_table.image,
178+
)
179+
.insert(Parameter("?"), Parameter("?"), Parameter("?"), Parameter("?"))
180+
)
155181
os.makedirs(f"images/{table}", exist_ok=True)
156182
try:
157-
results = conn.execute(f"SELECT article_id, name FROM {table}")
183+
query = Query.from_(outfit_table).select(outfit_table.article_id, outfit_table.name)
184+
results = conn.execute(query.get_sql())
158185
except sqlite3.Error:
159186
results = []
160187
if not results:
@@ -192,10 +219,7 @@ def save_outfit_images(
192219
failed.append(image.file_name)
193220
continue
194221
article_id, addons, sex = image_info[image.file_name]
195-
conn.execute(
196-
"INSERT INTO outfit_image(outfit_id, addon, sex, image) VALUES(?, ?, ?, ?)",
197-
(article_id, addons, sex, image_bytes),
198-
)
222+
conn.execute(insert_query.get_sql(), (article_id, addons, sex, image_bytes))
199223
save_cache_info(table, cache_info)
200224
if failed:
201225
echo(f"{Style.RESET_ALL}\tCould not fetch {len(failed):,} images.{Style.RESET_ALL}")

0 commit comments

Comments
 (0)