Skip to content

Commit

Permalink
Fixed #70 -- introspection get_table_description fail to get col length
Browse files Browse the repository at this point in the history
can_introspect_max_length is True by default in django DatabaseFeatures
django-firebird failed to retrive the correct field length when the charset is UTF8 over a char field type
  • Loading branch information
maxirobaina committed Apr 19, 2017
1 parent c061e6f commit ea54b2c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
32 changes: 26 additions & 6 deletions firebird/creation.py
Expand Up @@ -24,6 +24,12 @@ def sql_remove_table_constraints(self, model, references_to_delete, style):
return super(DatabaseCreation, self).sql_remove_table_constraints(model, references_to_delete, style)
return []

def _check_active_connection(self, verbosity):
if self.connection:
if verbosity >= 1:
print("Closing active connection")
self.connection.close()

def _get_connection_params(self, **overrides):
settings_dict = self.connection.settings_dict
conn_params = {'charset': 'UTF8'}
Expand All @@ -42,19 +48,33 @@ def _get_connection_params(self, **overrides):
conn_params.update(overrides)
return conn_params

def _check_active_connection(self, verbosity):
if self.connection:
if verbosity >= 1:
print("Closing active connection")
self.connection.close()
def _get_creation_params(self, **overrides):
settings_dict = self.connection.settings_dict
params = {'charset': 'UTF8'}
if settings_dict['USER']:
params['user'] = settings_dict['USER']
if settings_dict['PASSWORD']:
params['password'] = settings_dict['PASSWORD']

test_settings = settings_dict.get('TEST')
if test_settings:
if test_settings['NAME']:
params['database'] = settings_dict['NAME']
if test_settings['CHARSET']:
params['charset'] = test_settings['CHARSET']
if test_settings['PAGE_SIZE']:
params['page_size'] = test_settings['PAGE_SIZE']
params.update(overrides)
return params

def _create_database(self, test_database_name, verbosity):
self._check_active_connection(verbosity)
params = self._get_connection_params(database=test_database_name)
params = self._get_creation_params(database=test_database_name)
connection = Database.create_database("""
CREATE DATABASE '%(database)s'
USER '%(user)s'
PASSWORD '%(password)s'
PAGE_SIZE %(page_size)s
DEFAULT CHARACTER SET %(charset)s;""" % params)
connection.execute_immediate("CREATE EXCEPTION teste '';")
connection.commit()
Expand Down
11 changes: 9 additions & 2 deletions firebird/introspection.py
@@ -1,3 +1,4 @@
import datetime
from collections import namedtuple

from django.utils import six
Expand Down Expand Up @@ -82,7 +83,13 @@ def get_table_description(self, cursor, table_name):
else
f.rdb$field_type
end as type_code
, f.rdb$field_length
, case
when (f.rdb$field_type in (14,37)) then
f.rdb$character_length
else
f.rdb$field_length
end as field_length
, f.rdb$field_precision
, f.rdb$field_scale * -1
, rf.rdb$null_flag
Expand All @@ -96,7 +103,7 @@ def get_table_description(self, cursor, table_name):
""" % (tbl_name,))
items = []
for r in cursor.fetchall():
# name type_code display_size internal_size precision scale null_ok
# name type_code display_size internal_size precision scale null_ok + default
items.append(FieldInfo(r[0], r[1], r[2], r[2] or 0, r[3], r[4], not (r[5] == 1), r[6]))
return items

Expand Down
2 changes: 1 addition & 1 deletion tests/test_main/lookup/tests.py
Expand Up @@ -643,7 +643,7 @@ def test_regex_non_string(self):
Ensure that a regex lookup does not fail on non-string fields
"""
Season.objects.create(year=2013, gt=444)
self.assertQuerysetEqual(Season.objects.filter(gt__regex=r'^444$'),
self.assertQuerysetEqual(Season.objects.filter(gt__regex=r'444'),
['<Season: 2013>'])

def test_regex_non_ascii(self):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_main/test_main/settings.py
Expand Up @@ -12,13 +12,13 @@
'PASSWORD': 'masterkey',
'HOST': '127.0.0.1',
'PORT': '',
'OPTIONS': {'charset': 'ISO8859_1'},
'OPTIONS': {'charset': 'UTF8'},

'TEST': {
'NAME': 'django-test-default',
'CHARSET': 'ISO8859_1',
'SERIALIZE': False

'CHARSET': 'UTF8',
'SERIALIZE': False,
'PAGE_SIZE': 8192
}
},
'other': {
Expand All @@ -28,13 +28,13 @@
'PASSWORD': 'masterkey',
'HOST': '127.0.0.1',
'PORT': '',
'OPTIONS': {'charset': 'ISO8859_1'},
'OPTIONS': {'charset': 'UTF8'},

'TEST': {
'NAME': 'django-test-other',
'CHARSET': 'ISO8859_1',
'SERIALIZE': False

'CHARSET': 'UTF8',
'SERIALIZE': False,
'PAGE_SIZE': 8192
}
}

Expand Down

0 comments on commit ea54b2c

Please sign in to comment.