示例#1
0
def create_ad_hoc_field(cls, db_type):
    import infi.clickhouse_orm.fields as orm_fields

    # Enums
    if db_type.startswith('Enum'):
        db_type = 'String'  # enum.Eum is not comparable
    # Arrays
    if db_type.startswith('Array'):
        inner_field = cls.create_ad_hoc_field(db_type[6:-1])
        return orm_fields.ArrayField(inner_field)
    # FixedString
    if db_type.startswith('FixedString'):
        db_type = 'String'

    if db_type == 'LowCardinality(String)':
        db_type = 'String'

    if db_type.startswith('DateTime'):
        db_type = 'DateTime'

    if db_type.startswith('Nullable'):
        inner_field = cls.create_ad_hoc_field(db_type[9:-1])
        return orm_fields.NullableField(inner_field)

    # db_type for Deimal comes like 'Decimal(P, S) string where P is precision and S is scale'
    if db_type.startswith('Decimal'):
        nums = [int(n) for n in db_type[8:-1].split(',')]
        return orm_fields.DecimalField(nums[0], nums[1])

    # Simple fields
    name = db_type + 'Field'
    if not hasattr(orm_fields, name):
        raise NotImplementedError('No field class for %s' % db_type)
    return getattr(orm_fields, name)()
 def create_ad_hoc_field(cls, db_type):
     import infi.clickhouse_orm.fields as orm_fields
     # Enums
     if db_type.startswith('Enum'):
         return orm_fields.BaseEnumField.create_ad_hoc_field(db_type)
     # DateTime with timezone
     if db_type.startswith('DateTime('):
         # Some functions return DateTimeField with timezone in brackets
         return orm_fields.DateTimeField()
     # Arrays
     if db_type.startswith('Array'):
         inner_field = cls.create_ad_hoc_field(db_type[6:-1])
         return orm_fields.ArrayField(inner_field)
     # FixedString
     if db_type.startswith('FixedString'):
         length = int(db_type[12:-1])
         return orm_fields.FixedStringField(length)
     # Decimal
     if db_type.startswith('Decimal'):
         precision, scale = [
             int(n.strip()) for n in db_type[8:-1].split(',')
         ]
         return orm_fields.DecimalField(precision, scale)
     # Nullable
     if db_type.startswith('Nullable'):
         inner_field = cls.create_ad_hoc_field(db_type[9:-1])
         return orm_fields.NullableField(inner_field)
     # Simple fields
     name = db_type + 'Field'
     if not hasattr(orm_fields, name):
         raise NotImplementedError('No field class for %s' % db_type)
     return getattr(orm_fields, name)()