Skip to content

Commit

Permalink
Detect supportness.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangacer committed Jan 17, 2016
1 parent 26984d1 commit f0ef2b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
## TODOs

- As a Flask plug-in
- Aware of underlying SQLite FTS query syntax (flag for detection, at least)
- PyPI (?
- Benchmark
- Further tags analytics (e.g. top-k ~ KNN classification)
Expand Down
13 changes: 12 additions & 1 deletion simpletag.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ class __CONSTS__(object):
SQL_STATS = 'SELECT term, documents, occurrences FROM {}_terms WHERE col=0;'
pass


class ns(object):

dbfile = 'simpletag.db'
table = None
conn = None
using_parenthesis_query = False

def resolve_supported_level(self):
sql = 'PRAGMA compile_options;'
csr = self.conn.cursor()
opts = [row[0] for row in csr.execute(sql)]
if 'ENABLE_FTS3' not in opts:
raise RuntimeError('SQLite''s FTS is not enabled')
if 'ENABLE_FTS3_PARENTHESIS' in opts:
self.using_parenthesis_query = True
pass

def get_existing_tbl_type(self, name):
sql = 'select name from sqlite_master where type = "table";'
Expand All @@ -40,6 +50,7 @@ def __priv_init__(self):
conn = sqlite3.connect(self.dbfile)
conn.row_factory = sqlite3.Row
self.conn = conn
self.resolve_supported_level()
pass

def open_table_(self, name):
Expand Down
13 changes: 8 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def setUpClass(cls):
print '===='
print 'sqlite ver', sqlite3.sqlite_version
print '===='
conn = sqlite3.connect(':memory:')
csr = conn.cursor()
cls.SQLITE_COMPILE_OPTS = [row[0] for row in csr.execute('PRAGMA compile_options;')]
conn.close()
pass

def setUp(self):
Expand Down Expand Up @@ -69,14 +65,21 @@ def test_update_list_then_query_ids(self):
self.ns.update(789, [u'中文', u'行不行'])
self.assertEqual([123, 456], [i for i in self.ns.query_ids('is')])
self.assertIn(789, [i for i in self.ns.query_ids(u'行不行')])

def test_update_list_then_query_str_ids(self):
self.ns_str.update('/a/b', ['simpletag', 'is', 'awsome'])
self.ns_str.update('/', ['test', 'is', 'a', 'MUST'])
self.ns_str.update('/b/a', [u'中文', u'行不行'])
self.assertEqual(['/a/b', '/'], [i for i in self.ns_str.query_ids('is')])
self.assertIn('/b/a', [i for i in self.ns_str.query_ids(u'行不行')])
pass

def test_set_query(self):
self.ns.update(123, ['simpletag', 'is', 'awsome'])
self.ns.update(456, ['test', 'is', 'a', 'MUST'])

query = None
if 'ENABLE_FTS3_PARENTHESIS' in self.SQLITE_COMPILE_OPTS:
if self.ns.using_parenthesis_query:
query = 'is NOT awsome'
else:
query = 'is -awsome'
Expand Down

0 comments on commit f0ef2b7

Please sign in to comment.