py.lib

Yohn Y. 2020-07-24 Child:50ff84d0bc56

13:cab7fedf8432 Go to Latest

py.lib/db/sqlite.py

.. Реструктуризация

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/db/sqlite.py	Fri Jul 24 14:40:37 2020 +0300
     1.3 @@ -0,0 +1,34 @@
     1.4 +# coding: utf-8
     1.5 +import sqlite3
     1.6 +
     1.7 +class DB:
     1.8 +	def __init__(self, db_file):
     1.9 +		self._con = sqlite3.connect(db_file)
    1.10 +		self._ex = self._con.execute
    1.11 +		self.commit = self._con.commit
    1.12 +		self.rollback = self._con.rollback
    1.13 +		
    1.14 +		# DB PREP
    1.15 +		self._ex("PRAGMA journal=WAL")
    1.16 +		self.commit()
    1.17 +	
    1.18 +	def __del__(self):
    1.19 +		try:
    1.20 +			self.rollback()     
    1.21 +			self._con.close()
    1.22 +		except:
    1.23 +			pass
    1.24 +	
    1.25 +	def __call__(self, *a, **kwa):
    1.26 +		cur = self._con.cursor()
    1.27 +		cur.execute(*a, **kwa)
    1.28 +		return cur
    1.29 +	
    1.30 +	def cq(self, *a, **wa):
    1.31 +		try:
    1.32 +			res = self(*a, **wa)
    1.33 +			self.commit()
    1.34 +			return res
    1.35 +		except Exception as e:
    1.36 +			self.rollback()
    1.37 +			raise e