def mysql2field(self, mysql_field_string): import re regexp = re.compile( "(?P<data_type>[A-Z]+)(?P<length>\(([0-9]+)(?:,([0-9]+))?\))?\s*(UNSIGNED)?.*?(?:\s(NULL|NOT NULL))?" ) # Improve # regexp.findall("VARCHAR(3,4) unsigned zerofill null".upper()) # [('VARCHAR', '(3,4)', '3', '4', 'UNSIGNED', 'NULL')] res = regexp.findall(mysql_field_string.upper().strip()) if res: field_type = res[0][0] field_unsigned = res[0][4] if field_unsigned: column_type = "_".join((field_unsigned.lower(), field_type.lower())) else: column_type = field_type.lower() if res[0][1] != "": if res[0][3]: type_size = (int(res[0][2]), int(res[0][3])) else: type_size = (int(res[0][2]),) else: type_size = None self.transformers = transform_factory( "transformers/", force_output_type=column_type, force_output_writer="mysql", nullable=self.isnull, type_size=type_size, ) else: print mysql_field_string.upper().strip()
def __init__(self, name, force_text=False, raise_on_change=True, mysql_definition=None, isnull=False): self.field_name = name self.isnull = isnull self.force_text = force_text self.raise_on_change = raise_on_change # Set field parameters from MySQL string if mysql_definition: self.mysql2field(mysql_definition) else: self.transformers = transform_factory("transformers/", force_output_writer="mysql", nullable=self.isnull) self.field_type = self.transformers.get_best_definition()