Allow column sql types to be tested from the table handler
issue9645 review312421002
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
* Allow column sql types to be tested from the table handler
|
||||
* Add support for database connection parameters to configuration URI
|
||||
* Use immutable datastructures for Dict and MultiSelection fields
|
||||
* Skip warnings for non-interactive operations
|
||||
|
||||
@@ -221,6 +221,18 @@ class TableHandler(TableHandlerInterface):
|
||||
'ALTER "' + column_name + '" TYPE ' + column_type)
|
||||
self._update_definitions(columns=True)
|
||||
|
||||
def column_is_type(self, column_name, type_, *, size=-1):
|
||||
db_type = self._columns[column_name]['typname'].upper()
|
||||
|
||||
database = Transaction().database
|
||||
base_type = database.sql_type(type_).base.upper()
|
||||
if base_type == 'VARCHAR':
|
||||
same_size = self._columns[column_name]['size'] == size
|
||||
else:
|
||||
same_size = True
|
||||
|
||||
return base_type == db_type and same_size
|
||||
|
||||
def db_default(self, column_name, value):
|
||||
if value in [True, False]:
|
||||
test = str(value).lower()
|
||||
|
||||
@@ -138,7 +138,7 @@ class TableHandler(TableHandlerInterface):
|
||||
size = match.group(3) and int(match.group(3)) or 0
|
||||
else:
|
||||
typname = type_.upper()
|
||||
size = -1
|
||||
size = None
|
||||
self._columns[column] = {
|
||||
'notnull': notnull,
|
||||
'hasdef': hasdef,
|
||||
@@ -170,6 +170,18 @@ class TableHandler(TableHandlerInterface):
|
||||
def alter_type(self, column_name, column_type):
|
||||
self._recreate_table({column_name: {'typname': column_type}})
|
||||
|
||||
def column_is_type(self, column_name, type_, *, size=-1):
|
||||
db_type = self._columns[column_name]['typname'].upper()
|
||||
|
||||
database = Transaction().database
|
||||
base_type = database.sql_type(type_).base.upper()
|
||||
if base_type == 'VARCHAR':
|
||||
same_size = self._columns[column_name]['size'] == size
|
||||
else:
|
||||
same_size = True
|
||||
|
||||
return base_type == db_type and same_size
|
||||
|
||||
def db_default(self, column_name, value):
|
||||
warnings.warn('Unable to set default on column with SQLite backend')
|
||||
|
||||
|
||||
@@ -84,6 +84,18 @@ class TableHandlerInterface(object):
|
||||
'''
|
||||
raise NotImplementedError
|
||||
|
||||
def column_is_type(self, column_name, type_, *, size=-1):
|
||||
'''
|
||||
Return True if the column is of type type_
|
||||
|
||||
:param column_name: the column name
|
||||
:param type_: the generic name of the type
|
||||
:param size: if `type` is VARCHAR you can specify its size.
|
||||
Defaults to -1 which will won't match any size
|
||||
:return: a boolean
|
||||
'''
|
||||
raise NotImplementedError
|
||||
|
||||
def db_default(self, column_name, value):
|
||||
'''
|
||||
Set a default on a column
|
||||
|
||||
Reference in New Issue
Block a user