diff --git a/ext_open/searcher.py b/ext_open/searcher.py index 62a5064..5195067 100644 --- a/ext_open/searcher.py +++ b/ext_open/searcher.py @@ -10,16 +10,22 @@ DB_PATH = osp.join(osp.dirname(__file__),'ext.db') ext_database = sqlite3.connect(DB_PATH) def init_db(): cur = ext_database.cursor() - cur.execute('CREATE TABLE IF NOT EXISTS updates ( `ext` TET, `update_time` INTEGER, `file_count` INTEGER);') + cur.execute('CREATE TABLE IF NOT EXISTS updates ( `ext` TEXT, `update_time` INTEGER, `file_count` INTEGER);') def regexp(expr, item): return re.search(expr,item) is not None +def _in(expr,item): + return (1 if item in expr else 0) ext_database.create_function("REGEXP", 2, regexp) +ext_database.create_function("INSIDE", 2, _in) + class Searcher: + def __init__(self,ext): self.cur = ext_database.cursor() self.ext = ext self.index = '{}_index'.format(ext) self.__init_table__() + def __init_table__(self): self.cur.execute('CREATE TABLE IF NOT EXISTS {} (`name` TEXT,`fullpath` TEXT, `update_time` INTEGER,UNIQUE(name,fullpath));'.format(self.ext)) self.cur.execute('CREATE INDEX IF NOT EXISTS {} ON {} ( `name`, `fullpath` )'.format(self.index,self.ext)) @@ -28,6 +34,7 @@ class Searcher: {'ext':self.ext} ) self.commit() + def update(self,update_mtimes): self.cur.execute('SELECT MAX(update_time) FROM updates WHERE ext=?;',[self.ext]) last_update = self.cur.fetchone()[0] @@ -42,22 +49,32 @@ class Searcher: self.cur.executemany('INSERT OR IGNORE INTO {} VALUES (?,?,?)'.format(self.ext),(row + [_time] for row in files)) self.cur.execute('INSERT INTO updates VALUES (?, ?, ?)',[self.ext,_time,len(files)]) self.commit() + def commit(self): ext_database.commit() + def close(self): ext_database.close() - def search(query,regex=False): + + def search(self,query,regex=False): if regex: - self.cur.execute('SELECT * FROM ? WHERE name REGEXP ?;',[self.ext,query]) + self.cur.execute('SELECT name,fullpath FROM {} WHERE name REGEXP ?;'.format(self.ext),[query]) else: - self.cur.execute('SELECT * FROM ? WHERE name LIKE ?;',[self.ext,query]) + self.cur.execute('SELECT name,fullpath FROM {} WHERE INSIDE(name,?)=1;'.format(self.ext),[query]) return self.cur.fetchall() + def clean(self,backhistory): - self.cur.execute('SELECT update_time FROM updates WHERE ext=? ORDER BY update_time DESC LIMIT ?;',[backhistory+1]) + self.cur.execute('SELECT update_time FROM updates WHERE ext=? ORDER BY update_time DESC LIMIT ?;',[self.ext,backhistory+1]) res = self.cur.fetchall() threshold = res[-1][0] - cur.execute('DELETE FROM {} WHERE update_time < ?;'.format(self.ext),[threshold]) + self.cur.execute('DELETE FROM {} WHERE update_time < ?;'.format(self.ext),[threshold]) + self.commit() + + def clear(self): + self.cur.execute('DELETE FROM {};'.format(self.ext)) + self.commit() + if __name__ == "__main__": init_db() s = Searcher('py') - s.update(True) \ No newline at end of file + # s.update(True) \ No newline at end of file diff --git a/ext_open/update.py b/ext_open/update.py index d41ca1d..d2368fa 100644 --- a/ext_open/update.py +++ b/ext_open/update.py @@ -47,7 +47,7 @@ def build_ext(ext,paths): for path in paths: if path.endswith(ext): name = osp.basename(path) - ret.append([name,path]) + ret.append([name,path]) return ret def update(ext,starts,after,do_update_mtimes = False):