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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 小摘

针对TianvCms的搜索优化文章源码(无版权, 随便用) 针对C#的正规表达式测试程序源码下载(无版权, 随便用) 简易的可视化自定义表单程序源码下载(无版权, 随便用) 主要手段 - 浅谈搜索优化 为什么要优化 - 浅谈搜索优化 TianvCMS后台截图 从“铁道部12306网站被曝出现SQL漏洞”想到 TianvCMS部分官方插件 小议数据框架的封装 多编辑器支持的实现方式 想要开源了 - 发布在tianvcms发布以前 对IE9浏览器开发者预览版的一些感觉 大家帮忙看看如果面对这个升级文档该怎么处理,或者怎样才能处理得快一些(问在tianvcms改版前) 好久没更新了, 等待着把项目做简单 贴图不说话 从Windows Mac版想到 何去何从 快速开发之我见 - 送给开发效率不断下降的朋友们(二)
分享T4代码生成及源码(sqlite版),欢迎新手参考、修改(无版权)
小摘 · 2012-06-29 · via 博客园 - 小摘

代码生成器多少还是要打开关闭, 蛮麻烦的,

近来都直接用T4生成, 方便,快捷,

发出来,有需要的人可以参考,无版权

有心人可以帮忙弄个access版本, 和sql版本

2013年2月17日修正(解决需要手动目录的问题):

<#@ template debug="false" hostspecific="true" language="C#" #><#@ output extension=".cs" encoding="utf-8"#>
<#@ assembly name="System.Configuration" #>
<#@ assembly name="System.Core.DLL" #>
<#@ assembly name="System.Data.DLL" #>
<#@ assembly name="System.Web.DLL" #>
<#@ assembly name="System.Xml.DLL" #>
<#@ import namespace="System"#>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Data"#>
<#@ import namespace="System.Data.Common"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Reflection"#>
<#@ import namespace="System.Text"#>
<#
    var assembly = Assembly.LoadFile(this.Host.ResolvePath("..\\Bin\\System.Data.SQLite.dll"));

    StringBuilder sb = new StringBuilder();
    using (DbConnection conn = (DbConnection)assembly.CreateInstance("System.Data.SQLite.SQLiteConnection"))
    {
        string dbPath = this.Host.ResolvePath("..\\App_Data\\cms.s3db");
        conn.ConnectionString = @"Data Source=" + dbPath;

        if (conn.State != ConnectionState.Open) 
            conn.Open();

        DataTable dt = conn.GetSchema("Columns");

        List<Column> columns = new List<Column>();
        foreach (DataRow dr in dt.Rows)
        {
            Column column = getColumn(dr);
            columns.Add(column);
        }

        List<Table> tables = new List<Table>();
        foreach (string name in columns.Select(t => t.table).Distinct().OrderBy(t=>t))
        {
            Table table = getTable(name, columns);
            tables.Add(table);
        }

        sb.Append(@"using System;
using Tianv.Data;");
        foreach (Table table in tables)
        {
                if (string.Equals(table.name, "sqlite_sequence", StringComparison.OrdinalIgnoreCase)) continue;
                else if (table.name.StartsWith("_")) continue;
                
                StringBuilder inters = new StringBuilder();
                if (table.interfaces != null)
                {
                    foreach (string temp in table.interfaces)
                    {
                        inters.Append(",");
                        inters.Append(temp);
                    }
                }

                sb.Append(@"

public partial class " + table.name + @" : DbItem<" + table.name + @">");
                sb.Append(inters);
                sb.Append(@"
{");

                foreach (Column column in table.columns)
                {
                    if (string.Equals(column.name, "id", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                    string t = @"
    public {1} {0} { get; set; }";
                    sb.Append(t.Replace("{0}", column.name)
                    .Replace("{1}", getType(column.type)));
                }
                sb.Append(@"
}");
        }
    }
#>
<#=sb.ToString()#>
<#+
    public enum DataType { BOOL, INT, LONG, DATETIME, DECIMAL, STRING, TEXT, UNKNOW }
    public class Column
    {
        public string table { get; set; }
        public bool key { get; set; }
        public string name { get; set; }
        public int position { get; set; }
        public bool nullable { get; set; }
        public DataType type { get; set; }
        public int length { get; set; }
        public bool autoincrement { get; set; }
        public bool unique { get; set; }
    }
    public class Table
    {
        public string name { get; set; }
        public List<Column> columns { get; set; }
        public List<string> interfaces { get; set; }
    }
    public DataType getDataType(DataRow dr)
    {
        string type = (string)dr["DATA_TYPE"];
        switch (type.ToLower())
        {
            case "bit": return DataType.BOOL;
            case "int": return DataType.INT;
            case "integer": return DataType.LONG;
            case "datetime": return DataType.DATETIME;
            case "decimal": return DataType.DECIMAL;
            case "nvarchar": return DataType.STRING;
            case "ntext": return DataType.TEXT;
            default: return DataType.UNKNOW;
        }
    }
    public string getType( DataType type )
    {
        switch( type )
        {
            case DataType.BOOL:
                return "bool";
            case DataType.INT:
                return "int";
            case DataType.LONG:
                return "int";
            case DataType.DATETIME:
                return "DateTime";
            case DataType.DECIMAL:
                return "decimal";
            case DataType.STRING:
            case DataType.TEXT:
                return "string";
            default:
                return "object";
        }
    }
    private Column getColumn(DataRow dr)
    {
        return new Column()
        {
            table = (string)dr["TABLE_NAME"],
            key = (bool)dr["PRIMARY_KEY"],
            name = (string)dr["COLUMN_NAME"],
            position = Convert.ToInt32(dr["ORDINAL_POSITION"]),
            nullable = (bool)dr["IS_NULLABLE"],
            type = getDataType(dr),
            length = Convert.ToInt32(dr["CHARACTER_MAXIMUM_LENGTH"]),
            autoincrement = (bool)dr["AUTOINCREMENT"],
            unique = (bool)dr["UNIQUE"]
        };
    }
    private Table getTable(string name, List<Column> columns)
    {
        Table table = new Table()
        {
            name = name,
            columns = columns.Where(t => t.table == name).ToList()
        };
        List<string> cs = table.columns
            .Select(t => t.name)
            .ToList();                                                                                                                                                                                                                                                                                 
        
        Dictionary<string, string[]> inters = getInterfaces();
        foreach (string key in inters.Keys)
        {
            bool isInh = true;
            foreach (string temp in inters[key])
            {
                if (!cs.Contains(temp))
                {
                    isInh = false;
                    break;
                }
            }
            if (isInh)
            {
                if (table.interfaces == null)
                    table.interfaces = new List<string>();
                table.interfaces.Add(key);
            }
        }
        return table;
    }

    private Dictionary<string, string[]> Interfaces;
    public Dictionary<string, string[]> getInterfaces()
    {
        if (Interfaces == null)
        {
            Interfaces = new Dictionary<string, string[]>();
            Interfaces.Add("ITree", new string[] { "Sort", "ParentId", "Depth" });
            Interfaces.Add("ITitle", new string[] { "Title" });
            Interfaces.Add("ISort", new string[] { "Sort" });
            Interfaces.Add("IStyle", new string[] { "Code_RequireJs", "Code_Js", "Code_RequireCss", "Code_Css" });
            Interfaces.Add("IEnable", new string[] { "Enable" });
            Interfaces.Add("ITime", new string[] { "Time_Create", "Time_Update" });
            Interfaces.Add("ISeo", new string[] { "Seo_File", "Seo_Title", "Seo_Description", "Seo_Keywords" });
            Interfaces.Add("IItem", new string[] { "TopicId", "Title", "Sort" });//, "Enable", "Time_Enable"
        }
        return Interfaces;
    }
    
#>