From e41d200e81e00da115305db833e3ca2853719e13 Mon Sep 17 00:00:00 2001 From: Muzahid Date: Thu, 11 Mar 2021 16:26:10 +0530 Subject: [PATCH] add sql file for the query --- src/class_sqlThread.py | 81 +++++++++++++------------- src/sql/run.sql | 96 +++++++++++++++++++++++++++++++ src/tests/sql/create_function.sql | 19 ++++++ 3 files changed, 154 insertions(+), 42 deletions(-) create mode 100644 src/sql/run.sql create mode 100644 src/tests/sql/create_function.sql diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index 613a9298..48e16a83 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -51,7 +51,7 @@ class UpgradeDB(): def run_migrations(self, file): try: - root_path = os.path.dirname(os.path.dirname(__file__)) + # root_path = os.path.dirname(os.path.dirname(__file__)) sql_file = open(os.path.join(root_path, "src/sql/init_version_{}.sql".format(file))) sql_as_string = sql_file.read() self.cur.executescript(sql_as_string) @@ -236,14 +236,7 @@ class UpgradeDB(): logger.debug( 'In messages.dat database, updating address column to UNIQUE' ' in the addressbook table.') - # self.cur.execute( - # '''ALTER TABLE addressbook RENAME TO old_addressbook''') - # self.cur.execute( - # '''CREATE TABLE addressbook''' - # ''' (label text, address text, UNIQUE(address) ON CONFLICT IGNORE)''') - # self.cur.execute( - # '''INSERT INTO addressbook SELECT label, address FROM old_addressbook;''') - # self.cur.execute('''DROP TABLE old_addressbook''') + class sqlThread(threading.Thread, UpgradeDB): """A thread for all SQL operations""" @@ -261,39 +254,43 @@ class sqlThread(threading.Thread, UpgradeDB): self.cur.execute('PRAGMA secure_delete = true') try: - self.cur.execute( - '''CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text,''' - ''' received text, message text, folder text, encodingtype int, read bool, sighash blob,''' - ''' UNIQUE(msgid) ON CONFLICT REPLACE)''') - self.cur.execute( - '''CREATE TABLE sent (msgid blob, toaddress text, toripe blob, fromaddress text, subject text,''' - ''' message text, ackdata blob, senttime integer, lastactiontime integer,''' - ''' sleeptill integer, status text, retrynumber integer, folder text, encodingtype int, ttl int)''') - self.cur.execute( - '''CREATE TABLE subscriptions (label text, address text, enabled bool)''') - self.cur.execute( - '''CREATE TABLE addressbook (label text, address text, UNIQUE(address) ON CONFLICT IGNORE)''') - self.cur.execute( - '''CREATE TABLE blacklist (label text, address text, enabled bool)''') - self.cur.execute( - '''CREATE TABLE whitelist (label text, address text, enabled bool)''') - self.cur.execute( - '''CREATE TABLE pubkeys (address text, addressversion int, transmitdata blob, time int,''' - ''' usedpersonally text, UNIQUE(address) ON CONFLICT REPLACE)''') - self.cur.execute( - '''CREATE TABLE inventory (hash blob, objecttype int, streamnumber int, payload blob,''' - ''' expirestime integer, tag blob, UNIQUE(hash) ON CONFLICT REPLACE)''') - self.cur.execute( - '''INSERT INTO subscriptions VALUES''' - '''('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') - self.cur.execute( - '''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''') - self.cur.execute('''INSERT INTO settings VALUES('version','11')''') - self.cur.execute('''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( - int(time.time()),)) - self.cur.execute( - '''CREATE TABLE objectprocessorqueue''' - ''' (objecttype int, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''') + sql_file = open(os.path.join(root_path, "src/sql/{}.sql".format("run"))) + sql_as_string = sql_file.read() + self.cur.executescript(sql_as_string) + + # self.cur.execute( + # '''CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text,''' + # ''' received text, message text, folder text, encodingtype int, read bool, sighash blob,''' + # ''' UNIQUE(msgid) ON CONFLICT REPLACE)''') + # self.cur.execute( + # '''CREATE TABLE sent (msgid blob, toaddress text, toripe blob, fromaddress text, subject text,''' + # ''' message text, ackdata blob, senttime integer, lastactiontime integer,''' + # ''' sleeptill integer, status text, retrynumber integer, folder text, encodingtype int, ttl int)''') + # self.cur.execute( + # '''CREATE TABLE subscriptions (label text, address text, enabled bool)''') + # self.cur.execute( + # '''CREATE TABLE addressbook (label text, address text, UNIQUE(address) ON CONFLICT IGNORE)''') + # self.cur.execute( + # '''CREATE TABLE blacklist (label text, address text, enabled bool)''') + # self.cur.execute( + # '''CREATE TABLE whitelist (label text, address text, enabled bool)''') + # self.cur.execute( + # '''CREATE TABLE pubkeys (address text, addressversion int, transmitdata blob, time int,''' + # ''' usedpersonally text, UNIQUE(address) ON CONFLICT REPLACE)''') + # self.cur.execute( + # '''CREATE TABLE inventory (hash blob, objecttype int, streamnumber int, payload blob,''' + # ''' expirestime integer, tag blob, UNIQUE(hash) ON CONFLICT REPLACE)''') + # self.cur.execute( + # '''INSERT INTO subscriptions VALUES''' + # '''('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') + # self.cur.execute( + # '''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''') + # self.cur.execute('''INSERT INTO settings VALUES('version','11')''') + # self.cur.execute('''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( + # int(time.time()),)) + # self.cur.execute( + # '''CREATE TABLE objectprocessorqueue''' + # ''' (objecttype int, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''') self.conn.commit() logger.info('Created messages database file') except Exception as err: diff --git a/src/sql/run.sql b/src/sql/run.sql new file mode 100644 index 00000000..41731f4f --- /dev/null +++ b/src/sql/run.sql @@ -0,0 +1,96 @@ + -- + -- Temp Table structure for table `inventory_backup` + -- + + CREATE TEMP TABLE `inventory_backup` ( + `hash` blob NOT NULL, + `objecttype` text DEFAULT NULL, + `streamnumber` int NOT NULL, + `receivedtime` int NOT NULL, + `payload` blob DEFAULT NULL, + -- `integer` integer NOT NULL, + -- `tag` blob DEFAULT NULL, + UNIQUE(hash) ON CONFLICT REPLACE + ) ; + + -- + -- Dumping data for table `inventory_backup` + -- + + INSERT INTO `inventory_backup` SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory; + + + -- + -- Drop table `inventory` + -- + + DROP TABLE inventory; + + + -- + -- Table structure for table `inventory` + -- + + + CREATE TABLE `inventory` ( + `hash` blob NOT NULL, + `objecttype` text DEFAULT NULL, + `streamnumber` int NOT NULL, + `receivedtime` int NOT NULL, + `payload` blob DEFAULT NULL, + UNIQUE(hash) ON CONFLICT REPLACE + ) ; + + + -- + -- Dumping data for table `inventory` + -- + + INSERT INTO inventory SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory_backup; + + -- + -- Drop data for table `inventory_backup` + -- + + DROP TABLE inventory_backup; + + + + + + + + + self.cur.execute( + '''CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text,''' + ''' received text, message text, folder text, encodingtype int, read bool, sighash blob,''' + ''' UNIQUE(msgid) ON CONFLICT REPLACE)''') + self.cur.execute( + '''CREATE TABLE sent (msgid blob, toaddress text, toripe blob, fromaddress text, subject text,''' + ''' message text, ackdata blob, senttime integer, lastactiontime integer,''' + ''' sleeptill integer, status text, retrynumber integer, folder text, encodingtype int, ttl int)''') + self.cur.execute( + '''CREATE TABLE subscriptions (label text, address text, enabled bool)''') + self.cur.execute( + '''CREATE TABLE addressbook (label text, address text, UNIQUE(address) ON CONFLICT IGNORE)''') + self.cur.execute( + '''CREATE TABLE blacklist (label text, address text, enabled bool)''') + self.cur.execute( + '''CREATE TABLE whitelist (label text, address text, enabled bool)''') + self.cur.execute( + '''CREATE TABLE pubkeys (address text, addressversion int, transmitdata blob, time int,''' + ''' usedpersonally text, UNIQUE(address) ON CONFLICT REPLACE)''') + self.cur.execute( + '''CREATE TABLE inventory (hash blob, objecttype int, streamnumber int, payload blob,''' + ''' expirestime integer, tag blob, UNIQUE(hash) ON CONFLICT REPLACE)''') + self.cur.execute( + '''INSERT INTO subscriptions VALUES''' + '''('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') + self.cur.execute( + '''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''') + self.cur.execute('''INSERT INTO settings VALUES('version','11')''') + self.cur.execute('''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( + int(time.time()),)) + self.cur.execute( + '''CREATE TABLE objectprocessorqueue''' + ''' (objecttype int, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''') diff --git a/src/tests/sql/create_function.sql b/src/tests/sql/create_function.sql new file mode 100644 index 00000000..183d5601 --- /dev/null +++ b/src/tests/sql/create_function.sql @@ -0,0 +1,19 @@ +-- -- +-- -- Table structure for table `testhash` +-- -- + + +CREATE TEMPORARY TABLE `testhash` ( + `addressversion` int DEFAULT NULL, + `hash` blob DEFAULT NULL, + `address` text DEFAULT NULL, + UNIQUE(address) ON CONFLICT IGNORE +) ; + + +-- -- +-- -- Dumping data for table `testhash` +-- -- + +INSERT INTO testhash (addressversion, hash) VALUES(1, "21122112211221122112"); +