py.lib

John Y. 2017-04-09 Child:8dcee5f5df4f

0:f35759c0fdd0 Go to Latest

py.lib/awNet/db/sqlite.py

init

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/awNet/db/sqlite.py	Sun Apr 09 12:13:01 2017 +0300
     1.3 @@ -0,0 +1,54 @@
     1.4 +# -*- coding: utf-8 -*-
     1.5 +# Dep
     1.6 +from sqlite3 import connect as _sqlite_connect
     1.7 +# Exceptions
     1.8 +from sqlite3 import Warning, Error, DatabaseError, IntegrityError, ProgrammingError
     1.9 +
    1.10 +# --- # PROCS # --- #
    1.11 +def GetDB(db_file):
    1.12 +	def func():
    1.13 +		return DB(db_file)
    1.14 +	return func
    1.15 +
    1.16 +# --- # CLASS # --- #
    1.17 +class DB:
    1.18 +	def __init__(self, db_file):
    1.19 +		self._con = None
    1.20 +		self._con = _sqlite_connect(db_file)
    1.21 +		self.q = self._con.execute
    1.22 +		self.qm = self._con.executemany
    1.23 +		self.commit = self._con.commit
    1.24 +		self.rollback = self._con.rollback
    1.25 +		
    1.26 +		# DB PREP
    1.27 +		self.cq("PRAGMA journal=WAL")
    1.28 +	
    1.29 +	def 
    1.30 +	
    1.31 +	def __del__(self):
    1.32 +		if self._con == None: return
    1.33 +		try:
    1.34 +			self.rollback()     
    1.35 +			self._con.close()
    1.36 +		except Error:
    1.37 +			pass
    1.38 +		except Warning:
    1.39 +			pass
    1.40 +	
    1.41 +	def cq(self, *a, **wa):
    1.42 +		try:
    1.43 +			res = self.q(*a, **wa)
    1.44 +			self.commit()
    1.45 +			return res
    1.46 +		except Exception as e:
    1.47 +			self.rollback()
    1.48 +			raise e
    1.49 +		
    1.50 +	def cqm(self, *a, **wa):
    1.51 +		try:
    1.52 +			res = self.qm(*a, **wa)
    1.53 +			self.commit()
    1.54 +			return res
    1.55 +		except Exception as e:
    1.56 +			self.rollback()
    1.57 +			raise e
    1.58 \ No newline at end of file