用python从数据库生成c#代码
用python从数据库生成c#代码
捍卫机密 发表于5个月前
用python从数据库生成c#代码
  • 发表于 5个月前
  • 阅读 11
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云实验室 1小时搭建人工智能应用,让技术更容易入门 免费体验 >>>   

摘要: 偷懒的结晶!

懒惰的我要改变世界! 还有个题外话名言:寂寞驱动世界!----我

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by weilai on 2016/11/25


import pymysql


def format_db(formatter):
    conn = pymysql.connect(host='172.17.1.241', port=3306, user='root123', passwd='root123', db='businesssystemreport', charset='UTF8')
    cur = conn.cursor()
    cur.execute("SHOW FULL COLUMNS FROM businesssystemreport.productandcompanystatisday;")
    for i in cur:
        formatter(i)
    cur.close()
    conn.close()


def template1(field):
    code_temp = 'modelBase.AddUpdateFields("{field_name}", {obj}.{field_name});////{remark}'
    code_result = code_temp.format(obj='model', field_name=field[0], remark=field[8])
    print(code_result)


def template2(field):
    code_temp = 'modelBase.AddUpdateFields("{field_name}", {f_vaule});////{remark}'
    code_result = code_temp.format(obj='', field_name=field[0], remark=field[8], f_vaule='null')
    print(code_result)


def table_to_json(field):
    code_temp = '"{field_name}":"",'
    code_result = code_temp.format(field_name=field[0])
    print(code_result)


def format_field(field):
    # result.KeyID = dataReader["KeyID"] == DBNull.Value ? string.Empty : Convert.ToString(dataReader["KeyID"].ToString());
    code_temp = '{obj}.{name} = dr["{name}"] == DBNull.Value?{default}:Convert.{converter}(dr["{name}"].ToString());////{remark}'
    convert_default_value, convert_fun = value_convert(field[1])
    code_result = code_temp.format(obj = 'result', name=field[0], default = convert_default_value, converter = convert_fun, remark = field[8])
    print(code_result)


def table_to_model(field):
    # result.KeyID = dataReader["KeyID"] == DBNull.Value ? string.Empty : Convert.ToString(dataReader["KeyID"].ToString());
    code_temp = '/// <summary>\n/// {remark}\n/// </summary>\npublic {type} {name} {{ get; set; }} = {default};\n'
    convert_default_value, convert_fun = value_type(field[1])
    code_result = code_temp.format(obj = 'result', name=field[0], default = convert_default_value, type = convert_fun, remark = field[8])
    print(code_result)


def value_convert(ft):
    if len(ft) >= 3 and ft[0:3] == 'int':
        default_value = '0'
        convert_fun = 'ToInt32'
        return default_value, convert_fun
    if len(ft) >= 7 and ft[0:7] == 'varchar':
        default_value = 'string.Empty'
        convert_fun = 'ToString'
        return default_value, convert_fun
    if len(ft) >= 4 and ft[0:4] == 'char':
        default_value = 'string.Empty'
        convert_fun = 'ToString'
        return default_value, convert_fun
    if len(ft) >= 7 and ft[0:7] == 'tinyint':
        default_value = '0'
        convert_fun = 'ToInt32'
        return default_value, convert_fun
    if len(ft) >= 4 and ft[0:4] == 'date':
        default_value = 'new DateTime()'
        convert_fun = 'ToDateTime'
        return default_value, convert_fun
    if len(ft) >= 7 and ft[0:7] == 'decimal':
        default_value = '0.0m'
        convert_fun = 'ToDecimal'
        return default_value, convert_fun
    return '', ''


def value_type(ft):
    if len(ft) >= 3 and ft[0:3] == 'int':
        default_value = '0'
        convert_fun = 'int'
        return default_value, convert_fun
    if len(ft) >= 7 and ft[0:7] == 'varchar':
        default_value = 'string.Empty'
        convert_fun = 'string'
        return default_value, convert_fun
    if len(ft) >= 4 and ft[0:4] == 'char':
        default_value = 'string.Empty'
        convert_fun = 'string'
        return default_value, convert_fun
    if len(ft) >= 7 and ft[0:7] == 'tinyint':
        default_value = '0'
        convert_fun = 'int'
        return default_value, convert_fun
    if len(ft) >= 4 and ft[0:4] == 'date':
        default_value = 'new DateTime()'
        convert_fun = 'DateTime'
        return default_value, convert_fun
    if len(ft) >= 7 and ft[0:7] == 'decimal':
        default_value = '0.0m'
        convert_fun = 'decimal'
        return default_value, convert_fun
    return '', ''

format_db(table_to_model)

# format_db(table_to_json)

# format_db(template1)

# format_db(template2)

 

标签: python c#
共有 人打赏支持
粉丝 4
博文 107
码字总数 29117
×
捍卫机密
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: