Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Table name quoting" #51

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions sqlitedict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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()
Expand Down
17 changes: 0 additions & 17 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down