diff --git a/sqlitedict.py b/sqlitedict.py index bc2c832..53c9e33 100755 --- a/sqlitedict.py +++ b/sqlitedict.py @@ -151,12 +151,10 @@ def __init__(self, filename=None, tablename='unnamed', flag='c', raise RuntimeError('Error! The directory does not exist, %s' % dirname) self.filename = filename - if '"' in tablename: - raise ValueError('Invalid tablename %r' % tablename) self.tablename = tablename logger.info("opening Sqlite table %r in %s" % (tablename, filename)) - MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS "%s" (key TEXT PRIMARY KEY, value BLOB)' % self.tablename + MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS %s (key TEXT PRIMARY KEY, value BLOB)' % self.tablename self.conn = SqliteMultithread(filename, autocommit=autocommit, journal_mode=journal_mode) self.conn.execute(MAKE_TABLE) self.conn.commit() @@ -181,29 +179,29 @@ def __len__(self): # We could keep the total count of rows ourselves, by means of triggers, # but that seems too complicated and would slow down normal operation # (insert/delete etc). - GET_LEN = 'SELECT COUNT(*) FROM "%s"' % self.tablename + GET_LEN = 'SELECT COUNT(*) FROM %s' % self.tablename rows = self.conn.select_one(GET_LEN)[0] return rows if rows is not None else 0 def __bool__(self): # No elements is False, otherwise True - GET_MAX = 'SELECT MAX(ROWID) FROM "%s"' % self.tablename + GET_MAX = 'SELECT MAX(ROWID) FROM %s' % self.tablename m = self.conn.select_one(GET_MAX)[0] # Explicit better than implicit and bla bla return True if m is not None else False def iterkeys(self): - GET_KEYS = 'SELECT key FROM "%s" ORDER BY rowid' % self.tablename + GET_KEYS = 'SELECT key FROM %s ORDER BY rowid' % self.tablename for key in self.conn.select(GET_KEYS): yield key[0] def itervalues(self): - GET_VALUES = 'SELECT value FROM "%s" ORDER BY rowid' % self.tablename + GET_VALUES = 'SELECT value FROM %s ORDER BY rowid' % self.tablename for value in self.conn.select(GET_VALUES): yield decode(value[0]) def iteritems(self): - GET_ITEMS = 'SELECT key, value FROM "%s" ORDER BY rowid' % self.tablename + GET_ITEMS = 'SELECT key, value FROM %s ORDER BY rowid' % self.tablename for key, value in self.conn.select(GET_ITEMS): yield key, decode(value) @@ -217,11 +215,11 @@ def items(self): return self.iteritems() if major_version > 2 else list(self.iteritems()) def __contains__(self, key): - HAS_ITEM = 'SELECT 1 FROM "%s" WHERE key = ?' % self.tablename + HAS_ITEM = 'SELECT 1 FROM %s WHERE key = ?' % self.tablename return self.conn.select_one(HAS_ITEM, (key,)) is not None def __getitem__(self, key): - GET_ITEM = 'SELECT value FROM "%s" WHERE key = ?' % self.tablename + GET_ITEM = 'SELECT value FROM %s WHERE key = ?' % self.tablename item = self.conn.select_one(GET_ITEM, (key,)) if item is None: raise KeyError(key) @@ -231,7 +229,7 @@ def __setitem__(self, key, value): if self.flag == 'r': raise RuntimeError('Refusing to write to read-only SqliteDict') - ADD_ITEM = 'REPLACE INTO "%s" (key, value) VALUES (?,?)' % self.tablename + ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.tablename self.conn.execute(ADD_ITEM, (key, encode(value))) def __delitem__(self, key): @@ -240,7 +238,7 @@ def __delitem__(self, key): if key not in self: raise KeyError(key) - DEL_ITEM = 'DELETE FROM "%s" WHERE key = ?' % self.tablename + DEL_ITEM = 'DELETE FROM %s WHERE key = ?' % self.tablename self.conn.execute(DEL_ITEM, (key,)) def update(self, items=(), **kwds): @@ -253,7 +251,7 @@ def update(self, items=(), **kwds): pass items = [(k, encode(v)) for k, v in items] - UPDATE_ITEMS = 'REPLACE INTO "%s" (key, value) VALUES (?, ?)' % self.tablename + UPDATE_ITEMS = 'REPLACE INTO %s (key, value) VALUES (?, ?)' % self.tablename self.conn.executemany(UPDATE_ITEMS, items) if kwds: self.update(kwds) @@ -265,7 +263,7 @@ def clear(self): if self.flag == 'r': raise RuntimeError('Refusing to clear read-only SqliteDict') - CLEAR_ALL = 'DELETE FROM "%s";' % self.tablename # avoid VACUUM, as it gives "OperationalError: database schema has changed" + CLEAR_ALL = 'DELETE FROM %s;' % self.tablename # avoid VACUUM, as it gives "OperationalError: database schema has changed" self.conn.commit() self.conn.execute(CLEAR_ALL) self.conn.commit() diff --git a/tests/test_core.py b/tests/test_core.py index c9f861e..78a4419 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -130,23 +130,6 @@ def attempt_terminate(): with self.assertRaises(RuntimeError): func() - def test_irregular_tablenames(self): - """Irregular table names need to be quoted""" - db = sqlitedict.SqliteDict(':memory:', tablename='9nine') - db['key'] = 'value' - db.commit() - self.assertEqual(db['key'], 'value') - db.close() - - db = sqlitedict.SqliteDict(':memory:', tablename='outer space') - db['key'] = 'value' - db.commit() - self.assertEqual(db['key'], 'value') - db.close() - - self.assertRaisesRegexp(ValueError, r'^Invalid tablename ', - sqlitedict.SqliteDict, ':memory:', '"') - def test_overwrite_using_flag_w(self): """Re-opening of a database with flag='w' destroys only the target table.""" # given,