首先感谢QQ群的各位大大:
原始的自己打的代码:
% % Column_Type:二进制的MySQL的数据类型 % 输入值有:<<"int(11)">>,<<"decimal(10,2)">>,<<"varchar(XX)">>,<<"float">> % 返回值对应的为 int ,decimal ,varchar ,float % % get_column_type(Column_Type)-> case re:run(Column_Type,"int",[caseless]) of {match,_} -> integer; _ -> case re:run(Type,"decimal",[caseless]) of {match,_} -> decimal; _ -> case re:run(Type,"varchar",[caseless]) of {match,_} -> varchar; _-> case re:run(Type,"float",[caseless]) of {match,_} -> float; _-> binary end end end end.
又长又丑还一层层的嵌套,看着头疼。。。。
方案一:
方案二:
-define(COLUMN_TYPES,[{"int",integer},{"varchar",varchar}]). get_column_type(ColumnType) -> get_column_type(ColumnType,?COLUMN_TYPES). get_column_type(ColumnType,[Column_KeyValue|COLUMN_TYPES]) -> {MatchKey,Type} = Column_KeyValue, case re:run(ColumnType, MatchKey,[caseless]) of {match,_} -> Type; _ -> get_column_type(ColumnType,COLUMN_TYPES) end; get_column_type(_ColumnType,_)-> unknown.