python - 将实体类属性转换为SQL字段

1.对JAVA代码中的private属性进行过滤
2.将这些属性修改为对应的sql
# 去掉前缀空格,";"和"private"替换为空
# 将属性中JAVA类型修改为对应的SQL类型
# 将字段名称中大写字母转换为"_+小写字母"
def property_transfer(line):
    res ="";
    line = line.replace(";","").replace("private","").strip()
    attribute_type = line.split(" ")[0]
    attribute_name = line.split(" ")[1]
    # 对属性名进行处理
    attribute_name_res = "" 
    for char in attribute_name:
        if char.isupper() :
            attribute_name_res+="_"+char.lower()
        else:
            attribute_name_res+=char
    # 对属性类型进行处理
    attribute_type_res = "" 
    if "Long" in attribute_type:
        attribute_type_res = "bigint(50),"
    elif "Integer" in attribute_type:
        attribute_type_res = "bigint(20),"
    elif "String" in attribute_type:
        attribute_type_res = "varchar(255),"
    else:
        attribute_type_res = "ERROR"
    res = attribute_name_res +" "+attribute_type_res
    return res


# 未处理的信息
rubbish = []
# 文件地址
input_file_path = r"C:\Users\panyu\Desktop\jupyter\private-filter.txt"

# 读取文件内容
with open(input_file_path,'r',encoding='utf-8') as lines:
    for line in lines:   
        if "private" in line:
            print(property_transfer(line))
        else:
            # 没有的信息
            rubbish.append(line.replace("\n", "").replace("\t", ""))
          


# 对没有处理的行打印
print()
print("未处理的信息: ")
for info in rubbish:
    print("  "+info)


版权声明:本文为qq_39839745原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。