From 149c55b4d3ffbefa55cd2ec54cd947b0422f3723 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Sun, 19 Jul 2020 01:50:48 -0700 Subject: [PATCH] Use lowercase and underscores in sql tables column names (#90) --- init.sql | 26 +++++++++---------- squeakserver/server/postgres_db.py | 40 ++++++++++++++++-------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/init.sql b/init.sql index c072ab55..5bcf72e8 100644 --- a/init.sql +++ b/init.sql @@ -1,21 +1,21 @@ CREATE TABLE IF NOT EXISTS squeak ( hash CHAR(64) PRIMARY KEY, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - nVersion INTEGER NOT NULL, - hashEncContent CHAR(64) NOT NULL, - hashReplySqk CHAR(64) NOT NULL, - hashBlock CHAR(64) NOT NULL, - nBlockHeight INTEGER NOT NULL, - vchScriptPubKey bytea NOT NULL, - vchEncryptionKey bytea NOT NULL, - encDatakey CHAR(256) NOT NULL, + n_version INTEGER NOT NULL, + hash_enc_content CHAR(64) NOT NULL, + hash_reply_sqk CHAR(64) NOT NULL, + hash_block CHAR(64) NOT NULL, + n_block_height INTEGER NOT NULL, + vch_script_pub_key bytea NOT NULL, + vch_encryption_key bytea NOT NULL, + enc_data_key CHAR(256) NOT NULL, iv CHAR(32) NOT NULL, - nTime INTEGER NOT NULL, - nNonce BIGINT NOT NULL, - encContent CHAR(2272) NOT NULL, -- Encrypted content length is always 1136 bytes (2272 hex characters). - vchScriptSig bytea NOT NULL, + n_time INTEGER NOT NULL, + n_nonce BIGINT NOT NULL, + enc_content CHAR(2272) NOT NULL, -- Encrypted content length is always 1136 bytes (2272 hex characters). + vch_script_sig bytea NOT NULL, address VARCHAR(35) NOT NULL, -- Maximum length of a bitcoin address is 35. - vchDecryptionKey bytea NOT NULL, + vch_decryption_key bytea NOT NULL, block_header bytea ); diff --git a/squeakserver/server/postgres_db.py b/squeakserver/server/postgres_db.py index cab7cebd..0ef296d2 100644 --- a/squeakserver/server/postgres_db.py +++ b/squeakserver/server/postgres_db.py @@ -2,6 +2,8 @@ import logging from contextlib import contextmanager from psycopg2 import pool +from psycopg2.extras import DictCursor + from squeak.core import CSqueak from squeakserver.server.squeak_profile import SqueakProfile @@ -22,7 +24,7 @@ class PostgresDb: def get_cursor(self): con = self.connection_pool.getconn() try: - yield con.cursor() + yield con.cursor(cursor_factory=DictCursor) con.commit() finally: self.connection_pool.putconn(con) @@ -48,7 +50,7 @@ class PostgresDb: def insert_squeak(self, squeak): """ Insert a new squeak. """ sql = """ - INSERT INTO squeak(hash, nVersion, hashEncContent, hashReplySqk, hashBlock, nBlockHeight, vchScriptPubKey, vchEncryptionKey, encDatakey, iv, nTime, nNonce, encContent, vchScriptSig, address, vchDecryptionKey) + INSERT INTO squeak(hash, n_version, hash_enc_content, hash_reply_sqk, hash_block, n_block_height, vch_script_pub_key, vch_encryption_key, enc_data_key, iv, n_time, n_nonce, enc_content, vch_script_sig, address, vch_decryption_key) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING hash;""" @@ -91,20 +93,20 @@ class PostgresDb: row = curs.fetchone() squeak = CSqueak( - nVersion=row[2], - hashEncContent=bytes.fromhex(row[3]), - hashReplySqk=bytes.fromhex(row[4]), - hashBlock=bytes.fromhex(row[5]), - nBlockHeight=row[6], - vchScriptPubKey=row[7], - vchEncryptionKey=row[8], - encDatakey=bytes.fromhex(row[9]), - iv=bytes.fromhex((row[10])), - nTime=row[11], - nNonce=row[12], - encContent=bytes.fromhex((row[13])), - vchScriptSig=row[14], - vchDecryptionKey=row[16], + nVersion=row['n_version'], + hashEncContent=bytes.fromhex(row['hash_enc_content']), + hashReplySqk=bytes.fromhex(row['hash_reply_sqk']), + hashBlock=bytes.fromhex(row['hash_block']), + nBlockHeight=row['n_block_height'], + vchScriptPubKey=row['vch_script_pub_key'], + vchEncryptionKey=row['vch_encryption_key'], + encDatakey=bytes.fromhex(row['enc_data_key']), + iv=bytes.fromhex((row['iv'])), + nTime=row['n_time'], + nNonce=row['n_nonce'], + encContent=bytes.fromhex((row['enc_content'])), + vchScriptSig=row['vch_script_sig'], + vchDecryptionKey=row['vch_decryption_key'], ) block_header = row[17] return SqueakEntry(squeak=squeak, block_header=block_header) @@ -160,9 +162,9 @@ class PostgresDb: sql = """ SELECT hash FROM squeak WHERE address IN %s - AND nBlockHeight >= %s - AND nBlockHeight <= %s - AND vchDecryptionKey IS NOT NULL + AND n_block_height >= %s + AND n_block_height <= %s + AND vch_decryption_key IS NOT NULL AND block_header IS NOT NULL; """ addresses_tuple = tuple(addresses)