def add(self, url, content_type, label=None, thumb=''): if label is None: label = url label = unquote(label) thumb = unquote(thumb) execute = 'INSERT INTO {0!s} (addon_id, url, content_type, label, thumbnail) VALUES (?, ?, ?, ?, ?)'.format( self.TABLE) inserted = DATABASE.execute( execute, (self.ID, str(url), str(content_type), label, thumb)) if inserted == 1: execute = 'SELECT COUNT(*) FROM {0!s} WHERE addon_id=?'.format( self.TABLE) result = int(DATABASE.fetch(execute, (self.ID, ))[0][0]) if result > self.size_limit(): execute = 'DELETE FROM {0!s} WHERE ROWID = (SELECT MIN(ROWID) FROM {0!s}) AND addon_id=?'.format( self.TABLE) result, rowcount = DATABASE.execute_w_rowcount( execute, (self.ID, )) if rowcount < 1: execute = 'DELETE * FROM {0!s} WHERE addon_id=?'.format( self.TABLE) result, rowcount = DATABASE.execute_w_rowcount( execute, (self.ID, )) if rowcount < 1: result = DATABASE.execute('DROP TABLE {0!s}'.format( self.TABLE)) self.vacuum() self.create_table() if rowcount > 0: self.vacuum()
def clear(self, ctype=None): if ctype is None: result = DATABASE.execute('DROP TABLE {0!s}'.format(self.TABLE), '') else: result = DATABASE.execute( 'DELETE FROM {0!s} WHERE content_type=?'.format(self.TABLE), (ctype, )) if result == 1: self.vacuum() kodi.notify(msg=kodi.i18n('history_cleared'), sound=False) else: kodi.notify(msg=kodi.i18n('fail_history_clear'), sound=False)
def change_thumb(self, row_id, thumb): execute = 'UPDATE {0!s} SET thumbnail=? WHERE id=? AND addon_id=?'.format( self.TABLE) result = DATABASE.execute(execute, (unquote(thumb), row_id, self.ID)) if result != 1: kodi.notify(msg=kodi.i18n('thumbchange_failed'), sound=False) return result
def rename_row_id(self, row_id, label): execute = 'UPDATE {0!s} SET label=? WHERE id=? AND addon_id=?'.format( self.TABLE) result = DATABASE.execute(execute, (label, row_id, self.ID)) if result != 1: kodi.notify(msg=kodi.i18n('rename_failed'), sound=False) return result
def delete_row_id(self, row_id): execute = 'DELETE FROM {0!s} WHERE id=? AND addon_id=?'.format( self.TABLE) result, rowcount = DATABASE.execute_w_rowcount(execute, (row_id, self.ID)) if result != 1: kodi.notify(msg=kodi.i18n('delete_failed'), sound=False) return result, rowcount
def delete_url(self, url): execute = 'DELETE FROM {0!s} WHERE url=? AND addon_id=?'.format( self.TABLE) result, rowcount = DATABASE.execute_w_rowcount(execute, (url, self.ID)) if result != 1: kodi.notify(msg=kodi.i18n('delete_failed'), sound=False) if rowcount > 0: self.vacuum() return result, rowcount
def get(self, include_ids=False, row_id=None): if row_id is None: execute = 'SELECT * FROM {0!s} WHERE addon_id=? ORDER BY id DESC'.format( self.TABLE) selected = DATABASE.fetch(execute, (self.ID, )) else: execute = 'SELECT * FROM {0!s} WHERE id=? AND addon_id=?'.format( self.TABLE) selected = DATABASE.fetch(execute, (row_id, self.ID)) results = [] if selected: for id_key, addon_id, query, content_type, label, thumbnail in selected: if not include_ids: results.extend([(unquote(query), content_type, label, unquote(thumbnail))]) else: results.extend([(id_key, unquote(query), content_type, label, unquote(thumbnail))]) return results else: return []
def vacuum(self, table=None): if table is None: table = self.TABLE DATABASE.execute('VACUUM {0!s}'.format(table))
def create_table(self): DATABASE.execute( 'CREATE TABLE IF NOT EXISTS {0!s} (id INTEGER PRIMARY KEY AUTOINCREMENT, ' 'addon_id, url, content_type TEXT DEFAULT "video", label TEXT DEFAULT "Unknown", ' 'thumbnail TEXT DEFAULT "", CONSTRAINT unq UNIQUE (addon_id, url, content_type) )' .format(self.TABLE), '') DATABASE.execute( '''CREATE TRIGGER IF NOT EXISTS default_label_url AFTER INSERT ON {0!s} WHEN new.label="Unknown" BEGIN UPDATE {0!s} SET label=new.url WHERE id=new.id; END ; '''.format(self.TABLE), '') DATABASE.execute( 'ALTER TABLE {0!s} ADD COLUMN thumbnail TEXT DEFAULT ""'.format( self.TABLE), '', suppress=True) exists = DATABASE.fetch( 'SELECT name FROM sqlite_master WHERE type="table" AND name=?', (self.OLD_TABLE, )) if exists: DATABASE.execute( 'INSERT INTO {0!s} (addon_id, url) SELECT addon_id, url FROM {1!s}' .format(self.TABLE, self.OLD_TABLE), '') DATABASE.execute( 'ALTER TABLE {0!s} RENAME TO {1!s}'.format( self.OLD_TABLE, '{0!s}_bak'.format(self.OLD_TABLE)), '')