[MerlinSkinThemes] - add support for layouts
[enigma2-plugins.git] / skipintro / src / SkipIntroDatabase.py
1 import sqlite3
2
3 SI_DATABASE_PATH = '/etc/enigma2/SkipIntro.db'
4 SI_DATABASE_VERSION = '1.0'
5
6 class SIDatabase:
7         def __init__(self):
8                 self._dbfilepath = SI_DATABASE_PATH
9                 self._srDBConn = None
10                 self.connect()
11
12         def __del__(self):
13                 self.close()
14
15         def connect(self):
16                 self._srDBConn = sqlite3.connect(self._dbfilepath)
17                 self._srDBConn.isolation_level = None
18                 self._srDBConn.text_factory = lambda x: str(x.decode("utf-8"))
19
20         def close(self):
21                 if self._srDBConn:
22                         self._srDBConn.close()
23                         self._srDBConn = None
24
25         def initialize(self):
26                 cur = self._srDBConn.cursor()
27                 cur.execute("CREATE TABLE IF NOT EXISTS dbInfo ("
28                                         "Key TEXT NOT NULL UNIQUE, "
29                                         "Value TEXT NOT NULL DEFAULT '')")
30                 cur.execute("INSERT OR IGNORE INTO dbInfo (Key, Value) VALUES ('Version', ?)", [SI_DATABASE_VERSION])
31
32                 cur.execute("CREATE TABLE IF NOT EXISTS 'skipTimes' ("
33                                         "'name' TEXT UNIQUE, "
34                                         "'skipTime' INTEGER)")
35
36                 cur.close()
37
38         def beginTransaction(self):
39                 cur = self._srDBConn.cursor()
40                 cur.execute("BEGIN TRANSACTION")
41                 cur.close()
42
43         def commit(self):
44                 cur = self._srDBConn.cursor()
45                 cur.execute("COMMIT")
46                 cur.close()
47
48         def rollback(self):
49                 cur = self._srDBConn.cursor()
50                 cur.execute("ROLLBACK")
51                 cur.close()
52
53         def getVersion(self):
54                 dbVersion = None
55                 try:
56                         cur = self._srDBConn.cursor()
57                         cur.execute("SELECT Value FROM dbInfo WHERE Key='Version'")
58                         row = cur.fetchone()
59                         if row:
60                                 (dbVersion,) = row
61                         cur.close()
62                 except:
63                         pass
64
65                 return dbVersion
66
67         def hasSkipTime(self, name):
68                 cur = self._srDBConn.cursor()
69                 cur.execute("SELECT COUNT(*) FROM skipTimes WHERE name LIKE ?", [name + '%'])
70                 result = (cur.fetchone()[0] > 0)
71                 cur.close()
72                 return result
73
74         def setSkipTime(self, name, skipTime):
75                 cur = self._srDBConn.cursor()
76                 cur.execute("REPLACE INTO skipTimes (name, skipTime) VALUES (?, ?)", (name, skipTime))
77                 cur.close()
78
79         def getSkipTime(self, name, season):
80                 print "=== getSkipTime", name, season
81                 skipTime = 0
82                 dbname = ""
83                 cur = self._srDBConn.cursor()
84                 cur.execute("SELECT skipTime, name FROM skipTimes WHERE name = ?", [name + season])
85                 row = cur.fetchone()
86                 if row:
87                         (skipTime, dbname, ) = row
88                 else:
89                         cur.execute("SELECT skipTime, name FROM skipTimes WHERE name LIKE ? order by name COLLATE NOCASE", [name + "%"])
90                         row = cur.fetchone()
91                         if row:
92                                 (skipTime, dbname, ) = row
93                 cur.close()
94                 return int(skipTime), dbname
95
96         def renameSeries(self, oldname, newname):
97                 cur = self._srDBConn.cursor()
98                 cur.execute("UPDATE skipTimes SET name = ? WHERE name = ?", (newname, oldname))
99                 cur.close()
100
101         def removeSkipTime(self, name):
102                 cur = self._srDBConn.cursor()
103                 cur.execute("DELETE FROM skipTimes WHERE name = ?", [name])
104                 cur.close()
105
106         def getAllSkipTimes(self):
107                 cur = self._srDBConn.cursor()
108                 cur.execute("SELECT * FROM skipTimes order by name COLLATE NOCASE")
109                 entries = cur.fetchall()
110                 cur.close()
111                 return entries