diff options
-rw-r--r-- | paste/db.py | 13 | ||||
-rw-r--r-- | paste/store.py | 8 |
2 files changed, 15 insertions, 6 deletions
diff --git a/paste/db.py b/paste/db.py index bef3c46..f1071e8 100644 --- a/paste/db.py +++ b/paste/db.py @@ -7,7 +7,7 @@ from typing import Optional, Union migrations = [ """CREATE TABLE file ( - hash BLOB UNIQUE GENERATED ALWAYS AS (sha256(content)) STORED NOT NULL, + hash BLOB UNIQUE GENERATED ALWAYS AS (DATA_HASH(content)) STORED NOT NULL, content BLOB NOT NULL, created_at INTEGER DEFAULT (unixepoch('now')) ) STRICT; @@ -15,7 +15,7 @@ migrations = [ CREATE UNIQUE INDEX file_hash_ix ON file ( hash ); CREATE TABLE link ( - name_hash BLOB UNIQUE GENERATED ALWAYS AS (sha256(name)) STORED NOT NULL, + name_hash BLOB UNIQUE GENERATED ALWAYS AS (DATA_HASH(name)) STORED NOT NULL, name TEXT NOT NULL, content_type TEXT NOT NULL DEFAULT "text/plain", file_hash BLOB NOT NULL, @@ -38,6 +38,12 @@ def _sha256_udf(b: Union[bytes, str]) -> bytes: return sha256(b).digest() +def _data_hash_udf(b: Union[bytes, str]) -> bytes: + if isinstance(b, str): + b = b.encode() + return sha256(b).digest() + + def get_version(conn: sqlite3.Connection) -> int: (user_version,) = conn.execute("PRAGMA user_version").fetchone() return user_version @@ -71,6 +77,9 @@ def connect( conn.execute("PRAGMA journal_mode = WAL") conn.row_factory = sqlite3.Row conn.create_function(name="sha256", narg=1, func=_sha256_udf, deterministic=True) + conn.create_function( + name="DATA_HASH", narg=1, func=_data_hash_udf, deterministic=True + ) migrate(conn, migrations) try: yield conn diff --git a/paste/store.py b/paste/store.py index bae578f..26643cf 100644 --- a/paste/store.py +++ b/paste/store.py @@ -11,7 +11,7 @@ def put(conn: Connection, name: str, content: bytes, content_type: str): """ INSERT INTO link ( name, content_type, file_hash - ) VALUES (?, ?, sha256(?)) + ) VALUES (?, ?, DATA_HASH(?)) ON CONFLICT DO UPDATE SET content_type = excluded.content_type, @@ -25,7 +25,7 @@ def get(conn: Connection, name: str): """SELECT link.content_type, file.hash, file.content FROM link JOIN file ON file.hash = link.file_hash - WHERE name_hash = sha256(?)""", + WHERE name_hash = DATA_HASH(?)""", (name,), ).fetchone() return row @@ -41,7 +41,7 @@ def head(conn: Connection, name: str): END FROM link JOIN file ON file.hash = link.file_hash - WHERE name_hash = sha256(?)""", + WHERE name_hash = DATA_HASH(?)""", (name,), ).fetchone() return row @@ -49,4 +49,4 @@ def head(conn: Connection, name: str): def delete(conn: Connection, name: str): with conn: - conn.execute("DELETE FROM link WHERE name_hash = sha256(?)", (name,)) + conn.execute("DELETE FROM link WHERE name_hash = DATA_HASH(?)", (name,)) |