惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
雷峰网
雷峰网
T
Tailwind CSS Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
博客园 - 司徒正美
I
InfoQ
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
小众软件
小众软件
U
Unit 42
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net

博客园 - bluce chen

swif debounce实现 oculus quest2的思考 博文阅读密码验证 - 博客园 用户中心 - 博客园 https点对点转发响应示意图 Share架构的一些心得 flutter输入颜色枚举卡顿假死 n2n网络环境搭建 p2p技术之n2n源码核心简单分析一 分享一个14年写的用户管理类-swift版 MIUI通过xposed自动设置root权限 基于xposed实现android注册系统服务,解决跨进程共享数据问题 2017 UICollectionView swift2模版 angularjs 分页精华代码 Reveal分析IOS界面,plist文件读取 php嵌套数组递归搜索返回数组key 结合阿里云服务器,设置家中jetson tk1随时远程登陆 prism4 StockTrader RI 项目分析一些体会2
sqlite3 根据实体自动生成建表语句
bluce chen · 2014-04-21 · via 博客园 - bluce chen
 
public class BuildSqlTool
    {
        public static string GetCreateTableSql(object t)
        {
            //CREATE TABLE "StructureView2"("Id" INTEGER PRIMARY KEY  AUTOINCREMENT , "IdxNo" VARCHAR, "Name" VARCHAR, "Content" VARCHAR, "Price" FLOAT, "Order" INTEGER)
            string sqlStr = "CREATE TABLE \"{0}\" ({1})";
            Type type = t.GetType(); 
            PropertyInfo[] pi = type.GetProperties();
            string sqlFormat = "\"{0}\" {1}";
            string sqlStr2 = "";
            foreach (PropertyInfo p in pi)
            {
               string name=p.Name;
               object[]abc=p.GetCustomAttributes(true);
               if (name.ToLower()=="id")
               {
                   sqlStr2 += string.Format(sqlFormat, "Id", "INTEGER PRIMARY KEY  AUTOINCREMENT");
               }
               else
               {
                   sqlStr2 +=","+string.Format(sqlFormat, name, SqlType(p));
               } 
            }
            return string.Format(sqlStr, GetTableName(type), sqlStr2);
        }

        static string GetTableName(Type type)
        {
            var tableAttr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault();
            return tableAttr != null ? tableAttr.Name : type.Name;
        }

        static string SqlType(PropertyInfo p)
        {
            Type clrType = p.PropertyType;
            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32))
            {
                return "integer";
            }
            else if (clrType == typeof(UInt32) || clrType == typeof(Int64))
            {
                return "bigint";
            }
            else if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal))
            {
                return "float";
            }
            else if (clrType == typeof(String))
            {
                return "varchar";
                //int len = MaxStringLength(p);
                //return "varchar(" + len + ")";
            }
            else if (clrType == typeof(DateTime))
            {
                return "datetime"; 
            }
            else if (clrType.IsEnum)
            { 
                return "integer";
            }
            else if (clrType == typeof(byte[]))
            {
                return "blob";
            }
            else if (clrType == typeof(Guid))
            {
                return "varchar(36)";
            }
            else
            {
                throw new NotSupportedException("Don't know about " + clrType);
            }
        }
         
        int MaxStringLength(PropertyInfo p)
        {
            var attrs = p.GetCustomAttributes(typeof(MaxLengthAttribute), true);
            if (attrs.Length > 0)
            {
                return ((MaxLengthAttribute)attrs[0]).Value;
            }
            else
            {
                return 140;
            }
        }
    }

主要目的是为了减少见表,同理,可自动生成orm的sql

使用范例

// 获取当前程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 创建类的实例,返回为 object 类型类的完全限定名(即包括命名空间)
object obj = assembly.CreateInstance("ConstructionBudget.Model.VModel1");
string sql = BuildSqlTool.GetCreateTableSql(obj);