use sqlite text_factory option if set

This commit is contained in:
abi 2014-12-15 23:35:05 +01:00
parent 961d4e8f0a
commit 39ce9c354c

View File

@ -52,12 +52,19 @@ class SQLitePlugin(object):
name = 'sqlite' name = 'sqlite'
api = 2 api = 2
''' python3 moves unicode to str '''
try:
unicode
except NameError:
unicode = str
def __init__(self, dbfile=':memory:', autocommit=True, dictrows=True, def __init__(self, dbfile=':memory:', autocommit=True, dictrows=True,
keyword='db'): keyword='db', text_factory=unicode):
self.dbfile = dbfile self.dbfile = dbfile
self.autocommit = autocommit self.autocommit = autocommit
self.dictrows = dictrows self.dictrows = dictrows
self.keyword = keyword self.keyword = keyword
self.text_factory = text_factory
def setup(self, app): def setup(self, app):
''' Make sure that other installed plugins don't affect the same ''' Make sure that other installed plugins don't affect the same
@ -91,6 +98,7 @@ class SQLitePlugin(object):
autocommit = g('autocommit', self.autocommit) autocommit = g('autocommit', self.autocommit)
dictrows = g('dictrows', self.dictrows) dictrows = g('dictrows', self.dictrows)
keyword = g('keyword', self.keyword) keyword = g('keyword', self.keyword)
text_factory = g('keyword', self.text_factory)
# Test if the original callback accepts a 'db' keyword. # Test if the original callback accepts a 'db' keyword.
# Ignore it if it does not need a database handle. # Ignore it if it does not need a database handle.
@ -101,6 +109,8 @@ class SQLitePlugin(object):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
# Connect to the database # Connect to the database
db = sqlite3.connect(dbfile) db = sqlite3.connect(dbfile)
# set text factory
db.text_factory = text_factory
# This enables column access by name: row['column_name'] # This enables column access by name: row['column_name']
if dictrows: if dictrows:
db.row_factory = sqlite3.Row db.row_factory = sqlite3.Row