2 # -*- coding: utf-8 -*-
5 Copyright (C) 2007 Christoph Würstle
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
27 _moduleLogger = logging.getLogger("speichern")
30 class Speichern(object):
33 home_dir = os.path.expanduser('~')
34 filename = os.path.join(home_dir, ".quicknote.dat")
35 self.d = shelve.open(filename)
38 def speichereDirekt(self, schluessel, daten):
39 self.d[schluessel] = daten
40 _moduleLogger.info("speichereDirekt "+str(schluessel)+" "+str(daten)+" lesen: "+str(self.d[schluessel]))
42 def ladeDirekt(self, schluessel, default = ""):
43 if (self.d.has_key(schluessel) == True):
44 data = self.d[schluessel]
49 def speichereSQL(self, sql, tupel = None, commit = True, host = "self", log = True, pcdatum = None, rowid = ""):
51 programSQLError = True
55 self.cur.execute(sql, tupel)
56 programSQLError = False
62 strtupel.append(str(t))
65 pcdatum = int(time.time())
66 self.cur.execute("INSERT INTO logtable ( pcdatum, sql, param, host, rowid ) VALUES (?, ?, ?, ?, ?)", (pcdatum, sql, " <<Tren-ner>> ".join(strtupel), host, str(rowid) ))
72 s = str(sys.exc_info())
73 if s.find(" already exists") == -1:
74 if (programSQLError == True):
75 _moduleLogger.error("speichereSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
77 _moduleLogger.error("speichereSQL-Exception in Logging!!!! :"+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
83 def ladeSQL(self, sql, tupel = None):
89 self.cur.execute(sql, tupel)
90 return self.cur.fetchall()
92 _moduleLogger.error("ladeSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
95 def ladeHistory(self, sql_condition, param_condition):
96 sql = "SELECT * FROM logtable WHERE sql LIKE '%"+str(sql_condition)+"%' AND param LIKE '%"+str(param_condition)+"%'"
97 rows = self.ladeSQL(sql)
101 datum = time.strftime("%d.%m.%y %H:%M:%S", (time.localtime(row[1])))
102 erg.append([row[1], datum, row[2], row[3], row[3].split(" <<Tren-ner>> ")])
109 except StandardError:
113 except StandardError:
116 db = self.ladeDirekt("datenbank")
118 home_dir = os.path.expanduser('~')
120 #on hildon user not home-dir but /home/user/MyDocs
121 if home_dir == "/home/user":
122 if os.path.exists(home_dir+os.sep+"MyDocs/"):
123 home_dir = home_dir+os.sep+"MyDocs/"
124 db = os.path.join(home_dir, "quicknote.s3db")
126 self.conn = sqlite3.connect(db)
127 self.cur = self.conn.cursor()
129 sql = "CREATE TABLE logtable (id INTEGER PRIMARY KEY AUTOINCREMENT, pcdatum INTEGER , sql TEXT, param TEXT, host TEXT, rowid TEXT)"
130 self.cur.execute(sql)
132 except StandardError:
135 #Add rowid line (not in old versions included)
137 sql = "ALTER TABLE logtable ADD rowid TEXT"
138 self.cur.execute(sql)
140 except StandardError:
145 sql = "CREATE TABLE notes (noteid TEXT, pcdatum INTEGER , category TEXT, note TEXT)"
146 self.cur.execute(sql)
148 except StandardError:
151 def saveNote(self, noteid, note, category, pcdatum = None):
154 sql = "SELECT noteid, pcdatum, category, note FROM notes WHERE noteid = ?"
155 rows = self.ladeSQL(sql, (noteid, ))
157 if rows is None or len(rows) == 0:
158 sql = "INSERT INTO notes (noteid, pcdatum, category, note) VALUES (?, ?, ?, ?)"
160 pcdatum = int(time.time())
161 self.speichereSQL(sql, (noteid, pcdatum, category, note), rowid = noteid)
163 sql = "UPDATE notes SET category = ?, note = ?, pcdatum = ? WHERE noteid = ?"
164 self.speichereSQL(sql, (category, note, str(int(time.time())), noteid), rowid = noteid)
166 def loadNote(self, noteid):
167 if noteid is None or str(noteid) == "":
168 return (None, None, None)
169 sql = "SELECT noteid, pcdatum, category, note FROM notes WHERE noteid = ?"
170 rows = self.ladeSQL(sql, (noteid, ))
171 if rows is None or len(rows) == 0:
174 noteid, pcdatum, category, note = rows[0]
175 return (noteid, pcdatum, category, note)
177 def delNote(self, noteid):
178 sql = "DELETE FROM notes WHERE noteid = ?"
179 self.speichereSQL(sql, (noteid, ), rowid = noteid)
181 def searchNotes(self, searchstring, category):
182 sql = "SELECT noteid, category, note FROM notes WHERE note like ? AND category like ? ORDER BY note"
183 rows = self.ladeSQL(sql, ("%"+searchstring+"%", category))
184 if rows is None or len(rows) == 0:
189 def getNoteHistory(self, noteid):
190 return self.ladeHistory("UPDATE notes ", noteid)
195 except StandardError:
199 except StandardError:
203 except StandardError:
205 _moduleLogger.info("Alle Data saved")