import json import psycopg2.extras from psycopg2._psycopg import connection # picture create and delete def create_picture( conn: connection, source: str, external_id: str, url: str, metadata: dict ): with conn.cursor() as cur: cur.execute( """ insert into picrinth.pictures (source, external_id, url, metadata, created_at) values (%s, %s, %s, %s, now()) returning id """, (source, external_id, url, json.dumps(metadata)), ) result = cur.fetchone() conn.commit() if result is None: return None return result[0] def delete_picture( conn: connection, id: int ): with conn.cursor() as cur: cur.execute( """ delete from picrinth.pictures where id = %s """, (id,), ) conn.commit() return cur.rowcount > 0 # picture receiving def get_picture( conn: connection, id: int ): with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur: cur.execute( """ select id, source, external_id, url, metadata, created_at from picrinth.pictures where id = %s """, (id,), ) return cur.fetchone() def get_all_pictures_ids( conn: connection, ): with conn.cursor() as cur: cur.execute( """ select id from picrinth.pictures """, ) return [element for (element,) in cur.fetchall()]