From 2f2441822ea1f716865074493fd0812d28989ecf Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Sun, 9 Feb 2025 22:07:22 +1100 Subject: [PATCH] fix: Some select request formats --- main.py | 56 +++++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/main.py b/main.py index 0fbacf8..026ee75 100644 --- a/main.py +++ b/main.py @@ -54,7 +54,7 @@ dbSave = clickhouse_connect.create_client( database=DB_NAME ) -dbGet = Client( +dbGet = clickhouse_connect.get_client( host=DB_HOST, user=DB_USER, password=DB_PASSWORD, @@ -149,54 +149,40 @@ def saveBlock(blockData): def getNewestBlock() -> int: """Returns the height of the newest block in the database""" - dbNB = Client( - host=DB_HOST, - user=DB_USER, - password=DB_PASSWORD, - database=DB_NAME - ) - newestBlock = dbNB.execute("SELECT height FROM blocks ORDER BY height DESC LIMIT 1") - if newestBlock: - return int(newestBlock[0]) - return -1 + newestBlock = dbGet.query("SELECT height FROM blocks ORDER BY height DESC LIMIT 1").result + return int(newestBlock[0][0]) if newestBlock else -1 def dbCheck(): # For the first 100 blocks, check for transactions for i in range(100): - block = dbGet.execute("SELECT * FROM blocks WHERE height = %s", (i,)) + block = dbGet.query(f"SELECT * FROM blocks WHERE height = {i}").result if not block: return - block = Block(block) - print(block) + print(Block(block[0])) def getBlock(height) -> Block | None: - block = dbGet.execute("SELECT * FROM blocks WHERE height = %s", (height,)) - - if not block: - return None - return Block(block) + """Fetch a block by height""" + block = dbGet.query(f"SELECT * FROM blocks WHERE height = {height}").result + return Block(block[0]) if block else None + + +def getTransaction(tx_hash) -> Transaction | None: + """Fetch a transaction by hash""" + tx = dbGet.query(f"SELECT * FROM transactions WHERE hash = '{tx_hash}'").result + return Transaction(tx[0]) if tx else None -def getTransaction(hash) -> Transaction | None: - tx = dbGet.execute("SELECT * FROM transactions WHERE hash = %s", (hash,)) - if not tx: - return None - return Transaction(tx) - def getTransactions(height) -> list[Transaction] | None: - txs = dbGet.execute("SELECT * FROM transactions WHERE block = %s", (height,)) - if not txs: - return None - # Convert to list of Transaction objects - return [Transaction(tx) for tx in txs] + """Fetch all transactions for a given block height""" + txs = dbGet.query(f"SELECT * FROM transactions WHERE block = {height}").result + return [Transaction(tx) for tx in txs] if txs else None + def getNameFromHash(nameHash): - # Connect to db - name = dbGet.execute("SELECT * FROM names WHERE nameHash = %s", (nameHash,)) - if not name: - return -1 - return Name(name) + """Fetch a name record by nameHash""" + name = dbGet.query(f"SELECT * FROM names WHERE nameHash = '{nameHash}'").result + return Name(name[0]) if name else -1 def getNamesFromBlock(height): transactions = getTransactions(height)