Added the ability to query for database pragmas and other db-specific

variables.
This commit is contained in:
Tom Keffer
2015-04-14 16:22:30 -07:00
parent 7ca00fe345
commit ee84e47204
4 changed files with 51 additions and 3 deletions

View File

@@ -121,6 +121,16 @@ class Connection(object):
should raise an exception of type weedb.ProgrammingError if the table does not exist."""
raise NotImplementedError
def get_variable(self, var_name):
"""Return a database specific operational variable. Generally, things like
pragmas, or optimization-related variables.
It returns a 2-way tuple:
(variable-name, variable-value)
If the variable does not exist, it returns None.
"""
raise NotImplemented
def begin(self):
raise NotImplementedError

View File

@@ -169,6 +169,18 @@ class Connection(weedb.Connection):
column_list = [row[1] for row in self.genSchemaOf(table)]
return column_list
@guard
def get_variable(self, var_name):
cursor = self.connection.cursor()
try:
cursor.execute("SHOW VARIABLES LIKE '%s';" % var_name)
row = cursor.fetchone()
# This is actually a 2-way tuple (variable-name, variable-value),
# or None, if the variable does not exist.
return row
finally:
cursor.close()
@guard
def begin(self):
"""Begin a transaction."""
@@ -182,7 +194,6 @@ class Connection(weedb.Connection):
def rollback(self):
self.connection.rollback()
class Cursor(object):
"""A wrapper around the MySQLdb cursor object"""

View File

@@ -153,6 +153,16 @@ class Connection(weedb.Connection):
raise weedb.ProgrammingError("No such table %s" % table)
return column_list
@guard
def get_variable(self, var_name):
cursor = self.connection.cursor()
try:
cursor.execute("PRAGMA %s;" % var_name)
row = cursor.fetchone()
return None if row is None else (var_name, row[0])
finally:
cursor.close()
@guard
def begin(self):
self.connection.execute("BEGIN TRANSACTION")

View File

@@ -197,17 +197,34 @@ class TestSqlite(Common):
self.db_dict = sqlite_db_dict
super(TestSqlite, self).__init__(*args, **kwargs)
def test_variable(self):
weedb.create(self.db_dict)
_connect = weedb.connect(self.db_dict)
_v = _connect.get_variable('journal_mode')
self.assertEqual(_v[1].lower(), 'delete')
_v = _connect.get_variable('foo')
self.assertEqual(_v, None)
_connect.close()
class TestMySQL(Common):
def __init__(self, *args, **kwargs):
self.db_dict = mysql_db_dict
super(TestMySQL, self).__init__(*args, **kwargs)
def test_variable(self):
weedb.create(self.db_dict)
_connect = weedb.connect(self.db_dict)
_v = _connect.get_variable('lower_case_table_names')
self.assertTrue(_v[1] in ['0', '1', '2'], "Unknown lower_case_table_names value")
_v = _connect.get_variable('foo')
self.assertEqual(_v, None)
_connect.close()
def suite():
tests = ['test_drop', 'test_double_create', 'test_no_db', 'test_no_tables',
'test_create', 'test_bad_table', 'test_select', 'test_bad_select',
'test_rollback', 'test_transaction']
'test_rollback', 'test_transaction', 'test_variable']
return unittest.TestSuite(map(TestSqlite, tests) + map(TestMySQL, tests))
if __name__ == '__main__':