File size: 10,058 Bytes
d708e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
database.py
-----------
Lightweight SQLite persistence layer.

Tracks: sheets (uploaded sprite sheets), sprites (every detected/extracted
sprite with its full metadata + workflow status), and categories (built-in
+ user-added custom categories).

SQLite is more than sufficient here -- this is a single-Space, low-write-
concurrency workload (interactive review by essentially one operator at a
time), and it gives us real persistence across app restarts without an
external service dependency.
"""

from __future__ import annotations

import json
import sqlite3
import threading
import time
import uuid
from contextlib import contextmanager
from dataclasses import dataclass, field

from config import DB_PATH, DEFAULT_CATEGORIES, DEFAULT_STYLES

_lock = threading.Lock()


def _connect() -> sqlite3.Connection:
    conn = sqlite3.connect(str(DB_PATH), check_same_thread=False)
    conn.row_factory = sqlite3.Row
    conn.execute("PRAGMA journal_mode=WAL;")
    return conn


@contextmanager
def get_conn():
    with _lock:
        conn = _connect()
        try:
            yield conn
            conn.commit()
        finally:
            conn.close()


def init_db() -> None:
    with get_conn() as conn:
        conn.execute(
            """
            CREATE TABLE IF NOT EXISTS sheets (
                id TEXT PRIMARY KEY,
                filename TEXT NOT NULL,
                width INTEGER,
                height INTEGER,
                sprite_count INTEGER DEFAULT 0,
                created_at REAL,
                zip_batch_id TEXT
            )
            """
        )
        conn.execute(
            """
            CREATE TABLE IF NOT EXISTS sprites (
                id TEXT PRIMARY KEY,
                sheet_id TEXT NOT NULL,
                source_file TEXT NOT NULL,
                sprite_index INTEGER,
                x INTEGER, y INTEGER, width INTEGER, height INTEGER,
                png_path TEXT NOT NULL,
                png_path_transparent TEXT,
                sha256 TEXT,
                phash TEXT,
                quality_json TEXT,
                confidence REAL,
                confidence_reasons TEXT,
                component_count INTEGER,
                category TEXT DEFAULT 'unknown',
                style TEXT DEFAULT 'unknown',
                tags TEXT DEFAULT '[]',
                status TEXT DEFAULT 'pending',
                is_duplicate_of TEXT,
                duplicate_similarity REAL,
                created_at REAL,
                updated_at REAL
            )
            """
        )
        conn.execute(
            """
            CREATE TABLE IF NOT EXISTS categories (
                name TEXT PRIMARY KEY,
                kind TEXT NOT NULL DEFAULT 'category'
            )
            """
        )
        conn.execute("CREATE INDEX IF NOT EXISTS idx_sprites_status ON sprites(status)")
        conn.execute("CREATE INDEX IF NOT EXISTS idx_sprites_sheet ON sprites(sheet_id)")
        conn.execute("CREATE INDEX IF NOT EXISTS idx_sprites_sha256 ON sprites(sha256)")

        for cat in DEFAULT_CATEGORIES:
            conn.execute("INSERT OR IGNORE INTO categories (name, kind) VALUES (?, 'category')", (cat,))
        for style in DEFAULT_STYLES:
            conn.execute("INSERT OR IGNORE INTO categories (name, kind) VALUES (?, 'style')", (style,))


def new_id(prefix: str = "sprite") -> str:
    return f"{prefix}_{uuid.uuid4().hex[:12]}"


# ---------------------------------------------------------------------------
# Sheets
# ---------------------------------------------------------------------------

def insert_sheet(sheet_id: str, filename: str, width: int, height: int, zip_batch_id: str | None = None) -> None:
    with get_conn() as conn:
        conn.execute(
            "INSERT INTO sheets (id, filename, width, height, sprite_count, created_at, zip_batch_id) VALUES (?,?,?,?,?,?,?)",
            (sheet_id, filename, width, height, 0, time.time(), zip_batch_id),
        )


def update_sheet_sprite_count(sheet_id: str, count: int) -> None:
    with get_conn() as conn:
        conn.execute("UPDATE sheets SET sprite_count = ? WHERE id = ?", (count, sheet_id))


def list_sheets() -> list[sqlite3.Row]:
    with get_conn() as conn:
        return conn.execute("SELECT * FROM sheets ORDER BY created_at DESC").fetchall()


# ---------------------------------------------------------------------------
# Sprites
# ---------------------------------------------------------------------------

@dataclass
class SpriteRecord:
    id: str
    sheet_id: str
    source_file: str
    sprite_index: int
    x: int
    y: int
    width: int
    height: int
    png_path: str
    png_path_transparent: str | None
    sha256: str
    phash: str
    quality: dict
    confidence: float
    confidence_reasons: list[str]
    component_count: int
    category: str = "unknown"
    style: str = "unknown"
    tags: list[str] = field(default_factory=list)
    status: str = "pending"
    is_duplicate_of: str | None = None
    duplicate_similarity: float | None = None


def insert_sprite(rec: SpriteRecord) -> None:
    now = time.time()
    with get_conn() as conn:
        conn.execute(
            """
            INSERT INTO sprites (
                id, sheet_id, source_file, sprite_index, x, y, width, height,
                png_path, png_path_transparent, sha256, phash, quality_json,
                confidence, confidence_reasons, component_count, category,
                style, tags, status, is_duplicate_of, duplicate_similarity,
                created_at, updated_at
            ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
            """,
            (
                rec.id, rec.sheet_id, rec.source_file, rec.sprite_index,
                rec.x, rec.y, rec.width, rec.height,
                rec.png_path, rec.png_path_transparent, rec.sha256, rec.phash,
                json.dumps(rec.quality), rec.confidence, json.dumps(rec.confidence_reasons),
                rec.component_count, rec.category, rec.style, json.dumps(rec.tags),
                rec.status, rec.is_duplicate_of, rec.duplicate_similarity,
                now, now,
            ),
        )


def update_sprite_status(sprite_id: str, status: str) -> None:
    with get_conn() as conn:
        conn.execute("UPDATE sprites SET status = ?, updated_at = ? WHERE id = ?", (status, time.time(), sprite_id))


def update_sprite_fields(sprite_id: str, **fields) -> None:
    if not fields:
        return
    allowed = {"category", "style", "tags", "status", "x", "y", "width", "height", "png_path", "png_path_transparent"}
    sets = []
    values = []
    for k, v in fields.items():
        if k not in allowed:
            continue
        if k == "tags" and isinstance(v, list):
            v = json.dumps(v)
        sets.append(f"{k} = ?")
        values.append(v)
    if not sets:
        return
    sets.append("updated_at = ?")
    values.append(time.time())
    values.append(sprite_id)
    with get_conn() as conn:
        conn.execute(f"UPDATE sprites SET {', '.join(sets)} WHERE id = ?", values)


def get_sprite(sprite_id: str) -> sqlite3.Row | None:
    with get_conn() as conn:
        return conn.execute("SELECT * FROM sprites WHERE id = ?", (sprite_id,)).fetchone()


def list_sprites(status: str | None = None, sheet_id: str | None = None, limit: int = 5000) -> list[sqlite3.Row]:
    query = "SELECT * FROM sprites WHERE 1=1"
    params: list = []
    if status:
        query += " AND status = ?"
        params.append(status)
    if sheet_id:
        query += " AND sheet_id = ?"
        params.append(sheet_id)
    query += " ORDER BY sheet_id, sprite_index LIMIT ?"
    params.append(limit)
    with get_conn() as conn:
        return conn.execute(query, params).fetchall()


def existing_hashes_for_dup_check(exclude_status: tuple[str, ...] = ("rejected",)) -> list[dict]:
    placeholders = ",".join("?" * len(exclude_status))
    with get_conn() as conn:
        rows = conn.execute(
            f"SELECT id, sha256, phash FROM sprites WHERE status NOT IN ({placeholders})",
            exclude_status,
        ).fetchall()
    return [{"id": r["id"], "sha256": r["sha256"], "phash": r["phash"]} for r in rows]


def counts_by_status() -> dict:
    with get_conn() as conn:
        rows = conn.execute("SELECT status, COUNT(*) as c FROM sprites GROUP BY status").fetchall()
    out = {"pending": 0, "accepted": 0, "rejected": 0, "uploaded": 0}
    for r in rows:
        out[r["status"]] = r["c"]
    return out


def count_possible_duplicates() -> int:
    with get_conn() as conn:
        row = conn.execute(
            "SELECT COUNT(*) as c FROM sprites WHERE is_duplicate_of IS NOT NULL AND status != 'rejected'"
        ).fetchone()
    return row["c"] if row else 0


def mark_duplicate(sprite_id: str, of_sprite_id: str, similarity: float) -> None:
    with get_conn() as conn:
        conn.execute(
            "UPDATE sprites SET is_duplicate_of = ?, duplicate_similarity = ?, updated_at = ? WHERE id = ?",
            (of_sprite_id, similarity, time.time(), sprite_id),
        )


def clear_duplicate_flag(sprite_id: str) -> None:
    with get_conn() as conn:
        conn.execute(
            "UPDATE sprites SET is_duplicate_of = NULL, duplicate_similarity = NULL, updated_at = ? WHERE id = ?",
            (time.time(), sprite_id),
        )


# ---------------------------------------------------------------------------
# Categories
# ---------------------------------------------------------------------------

def list_categories(kind: str = "category") -> list[str]:
    with get_conn() as conn:
        rows = conn.execute("SELECT name FROM categories WHERE kind = ? ORDER BY name", (kind,)).fetchall()
    return [r["name"] for r in rows]


def add_custom_category(name: str, kind: str = "category") -> None:
    name = name.strip().lower().replace(" ", "_")
    if not name:
        return
    with get_conn() as conn:
        conn.execute("INSERT OR IGNORE INTO categories (name, kind) VALUES (?, ?)", (name, kind))