Copyright (C) 2008 Christoph Würstle
"""
+from __future__ import with_statement
+
+import sys
+import os
import time
import sqlite3
import shelve
-import sys
import string
import shutil
-import os
import logging
try:
self.d = shelve.open(filename)
self.openDB()
+ def openDB(self):
+ try:
+ self.cur.close()
+ except:
+ pass
+ try:
+ self.conn.close()
+ except:
+ pass
+
+ db = self.ladeDirekt("datenbank")
+ if db == "":
+ home_dir = os.path.expanduser('~')
+ db = os.path.join(home_dir, "multilist.s3db")
+
+ datum = time.strftime("%Y-%m-%d--", (time.localtime(time.time())))+str(int(time.time()))+"--"
+ if os.path.exists(db) and os.path.exists(os.path.dirname(db)+os.sep+"backup"):
+ try:
+ shutil.copyfile(db, str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db))
+ except:
+ _moduleLogger.info("Achtung Backup-Datei NICHT (!!!) angelegt!")
+
+ self.conn = sqlite3.connect(db)
+ self.cur = self.conn.cursor()
+ try:
+ sql = "CREATE TABLE logtable (id INTEGER PRIMARY KEY AUTOINCREMENT, pcdatum INTEGER , sql TEXT, param TEXT, host TEXT, rowid TEXT)"
+ self.cur.execute(sql)
+ self.conn.commit()
+ except:
+ pass
+
+ #Add rowid line (not in old versions included)
+ try:
+ sql = "ALTER TABLE logtable ADD rowid TEXT"
+ self.cur.execute(sql)
+ self.conn.commit()
+ except:
+ pass
+
def close(self):
try:
self.d.close()
_moduleLogger.info("speichereDirekt "+str(schluessel)+" "+str(daten)+" lesen: "+str(self.d[schluessel]))
def ladeDirekt(self, schluessel, default = ""):
- #print "ladeDirekt", schluessel, "Schluessel vorhanden", self.d.has_key(schluessel)
- if (self.d.has_key(schluessel) == True):
+ if self.d.has_key(schluessel):
data = self.d[schluessel]
- #print data
return data
else:
return default
def speichereSQL(self, sql, tupel = None, commit = True, host = "self", log = True, pcdatum = None, rowid = ""):
- #print "speichereSQL:", sql, tupel
try:
programSQLError = True
- if (tupel == None):
+ if tupel is None:
self.cur.execute(sql)
else:
self.cur.execute(sql, tupel)
programSQLError = False
- #print int(time.time()), sql, pickle.dumps(tupel), host
- if (log == True):
+ if log:
strtupel = []
- if (tupel is not None):
+ if tupel is not None:
for t in tupel:
strtupel.append(str(t))
- if pcdatum == None:
+ if pcdatum is None:
pcdatum = int(time.time())
self.cur.execute("INSERT INTO logtable ( pcdatum, sql, param, host, rowid ) VALUES (?, ?, ?, ?, ?)", (pcdatum, sql, string.join(strtupel, " <<Tren-ner>> "), host, str(rowid) ))
if commit:
return True
except:
s = str(sys.exc_info())
- if (s.find(" already exists") == -1):
- #if len(s)>0:
- if (programSQLError == True):
+ if s.find(" already exists") == -1:
+ if programSQLError:
_moduleLogger.error("speichereSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
else:
_moduleLogger.error("speichereSQL-Exception in Logging!!!! :"+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
self.conn.commit()
def ladeSQL(self, sql, tupel = None):
- #print sql, tupel
try:
- if (tupel == None):
+ if tupel is None:
self.cur.execute(sql)
else:
self.cur.execute(sql, tupel)
pcdatum = int(time.time())-exceptTheLastXSeconds
sql = "DELETE FROM logtable WHERE rowid = ? AND pcdatum<? AND sql LIKE '%"+str(sql_condition)+"%'"
self.speichereSQL(sql, (rowid, pcdatum, ))
-
- def openDB(self):
- try:
- self.cur.close()
- except:
- pass
- try:
- self.conn.close()
- except:
- pass
-
- db = self.ladeDirekt("datenbank")
- if db == "":
- home_dir = os.path.expanduser('~')
- db = os.path.join(home_dir, "multilist.s3db")
-
- datum = time.strftime("%Y-%m-%d--", (time.localtime(time.time())))+str(int(time.time()))+"--"
- if (os.path.exists(db))and(os.path.exists(os.path.dirname(db)+os.sep+"backup/")):
- try:
- shutil.copyfile(db, str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db))
- #_moduleLogger.debug(str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db))
- except:
- _moduleLogger.info("Achtung Backup-Datei NICHT (!!!) angelegt!")
- #print db, str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db)
-
- self.conn = sqlite3.connect(db)
- self.cur = self.conn.cursor()
- try:
- sql = "CREATE TABLE logtable (id INTEGER PRIMARY KEY AUTOINCREMENT, pcdatum INTEGER , sql TEXT, param TEXT, host TEXT, rowid TEXT)"
- self.cur.execute(sql)
- self.conn.commit()
- except:
- pass
-
- #Add rowid line (not in old versions included)
- try:
- sql = "ALTER TABLE logtable ADD rowid TEXT"
- self.cur.execute(sql)
- self.conn.commit()
- except:
- pass