You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.4 KiB

  1. import sqlite3
  2. import os
  3. import re
  4. import update
  5. # https://stackoverflow.com/a/5365533
  6. osp = os.path
  7. DB_PATH = osp.join(osp.dirname(__file__),'ext.db')
  8. ext_database = sqlite3.connect(DB_PATH)
  9. def init_db():
  10. cur = ext_database.cursor()
  11. cur.execute('CREATE TABLE "updates" ( `ext` TEXT, `update_time` INTEGER, `file_count` INTEGER, PRIMARY KEY(`ext`) )')
  12. def regexp(expr, item):
  13. return re.search(expr,item) is not None
  14. ext_database.create_function("REGEXP", 2, regexp)
  15. class Searcher:
  16. def __init__(self,ext):
  17. self.cur = ext_database.cursor()
  18. self.ext = ext
  19. self.index = '{}_index'.format(ext)
  20. self.__init_table__()
  21. def __init_table__(self):
  22. self.cur.execute('CREATE TABLE IF NOT EXISTS ? (`name` TEXT,`fullpath` TEXT,UNIQUE(name,fullpath));',[self.ext])
  23. self.cur.execute('CREATE INDEX IF NOT EXISTS ? ON ? ( `name`, `fullpath` )',[self.index,self.ext])
  24. def _update_(self,update_mtimes):
  25. # update logic
  26. self.cur.executemany('INSERT OR IGNORE INTO ? VALUES (?,?)',params)
  27. self.cur.execute('INSERT INTO updates VALUES (?, ?, ?)',[self.ext,time,count])
  28. def _commit_(self):
  29. ext_database.commit()
  30. def search(query,regex=False):
  31. if regex:
  32. self.cur.execute('SELECT * FROM ? WHERE name REGEXP ?',[self.ext,query])
  33. else:
  34. self.cur.execute('SELECT * FROM ? WHERE name LIKE ?',[self.ext,query])
  35. return self.cur.fetchall()