Coder Social home page Coder Social logo

yuanrui / codegenerator Goto Github PK

View Code? Open in Web Editor NEW
36.0 4.0 22.0 12.89 MB

一个小而美的代码生成器,基于 Mono T4 引擎,支持数据库:SQL Server、MySql、Oracle、SQLite. CodeGenerator is a template base on generator for database, syntax base on Mono T4 engine. Supported databases are SQL Server, MySQL, Oracle, SQLite.

Home Page: https://github.com/yuanrui/CodeGenerator

License: MIT License

C# 99.86% Thrift 0.14%
codegenerator t4 c-sharp entity-framework thrift layui easyui mono-t4 autocode scaffolding

codegenerator's Introduction

AutoCode

README in English

一个小而美的代码生成器,用于创建前后端代码,辅助项目开发,提高软件开发效率。代码生成器采用C#语言编写,采用 mono t4模板引擎,内置多个代码模板,可生成MyBatis、JPA、Asp.Net Mvc、EasyUI、LayUI、Entity Framework等代码。 main ui

用户指南

系统要求

操作系统:Windows XP +, Windows Server 2003 +

软件环境:.NET 4.0 +

支持数据库

  • Sql Server

  • Oracle

  • MySql

  • SQLite

使用说明

  1. 项目首页下载最新版本
  2. 解压文件,运行AutoCode.exe;
  3. 设置数据源,勾选数据表;
  4. 运行生成代码;
  5. 拷贝Output文件夹中的源码到项目解决方案。

文件夹用途说明:Config存放配置文件,Templates存放模板文件,Output存放生成的代码。

生成的源码所依赖的类库见:https://github.com/yuanrui/Examples

模板配置

用于代码生成的T4模板文件约定存放在Templates文件夹,生成代码时每个模板文件将生成对应的源代码,不同的模板可以使用文件夹进行归类。模板需要遵循T4模板语法,文件命名以.tt或.ttinclude结尾,一个简单的模板配置如下所示。

<#@ template language="C#" hostSpecific="true" debug="false" #>
<#@ output encoding="utf-8" extension=".cs" #>
<#@ include file="../TemplateFileManager.ttinclude" #>
<# 
	CustomHost host = (CustomHost)(Host);
	Table table = host.Table;
    var manager = Manager.Create(host, GenerationEnvironment);
	manager.StartNewFile(table.DisplayName + "Entity.generated.cs", host.GetValue("OutputPath").ToString() + "\\Samples\\Generated");
#>
//------------------------------------------------------------------------------
// <copyright file="<#= table.DisplayName #>Entity.cs">
//    Copyright (c) <#= DateTime.Now.ToString("yyyy") #>, All rights reserved.
// </copyright>
// <author><#= Environment.UserName #></author>
// <date><#= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") #></date>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace Banana.Data
{
    /// <summary>
    /// <#= string.IsNullOrEmpty(table.Comment) ? string.Format("Table/View [{0}] map to [{1}] entity class", table.Name, table.DisplayName) : table.Comment #>
    /// </summary>
    public partial class <#= table.DisplayName #>Entity : ICloneable
    {
    <# 
        foreach(var column in table.Columns)
        {
    #>
        /// <summary>
        /// get or set <#= column.Comment #>
        /// </summary>
        public virtual <#= column.TypeName #> <#= column.Name #> { get; set; }

    <#
        }
    #>
        public virtual <#= table.DisplayName #>Entity CloneFrom(<#= table.DisplayName #>Entity thatObj)
        {
            if (thatObj == null)
            {
                throw new ArgumentNullException("thatObj");
            }

    <# 
        foreach(var column in table.Columns)
        {
    #>
            this.<#= column.Name #> = thatObj.<#= column.Name #>;
    <#
        }
    #>

            return this;
        }

        public virtual <#= table.DisplayName #>Entity CloneTo(<#= table.DisplayName #>Entity thatObj)
        {
            if (thatObj == null)
            {
                throw new ArgumentNullException("thatObj");
            }

    <# 
        foreach(var column in table.Columns)
        {
    #>
            thatObj.<#= column.Name #> = this.<#= column.Name #>;
    <#
        }
    #>

            return thatObj;
        }

        public virtual <#= table.DisplayName #>Entity Clone()
        {
            var thatObj = new <#= table.DisplayName #>Entity();

            return this.CloneTo(thatObj);
        }

        object ICloneable.Clone()
        {
            return this.Clone();
        }
    }
}
<# 
	manager.EndBlock(); 
#>

host.Table对象的类型为Table,包含属性字段如下:

序号 名称 类型 说明
1 Id String 编号
2 Name String 表名称
3 Comment String 表注释
4 DisplayName String 显示名称
5 Owner String 数据库
6 PrimaryKeyIsNumber Boolean 主键是否数字类型
7 Columns IList 列集合
8 PrimaryKeyColumns IList 主键列集合
9 NonPrimaryKeyColumns IList 非主键列集合

Column类型包含属性字段如下:

序号 名称 类型 说明
1 Id String 编号
2 Name String 字段名称
3 RawType String 字段原始类型
4 DataType DbType .NET DbType
5 Type Type .NET Type
6 TypeName String 字段类型名称
7 Comment String 字段注释
8 IsPrimaryKey Boolean 是否主键
9 IsForeignKey Boolean 是否外键
10 IsUnique Boolean 是否唯一
11 IsNullable Boolean 是否可为空
12 Length Int32 长度
13 Precision Int16 精度
14 Scale Int16 精度范围
15 Index Int32 索引编号
16 Table Table Table对象

TODO List

codegenerator's People

Contributors

yuanrui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

codegenerator's Issues

Sql

Sql Server

IF OBJECT_ID (N'all_columns_info', N'V') IS NOT NULL
	DROP VIEW all_columns_info
GO
-- ==============================================================
--      Author: YuanRui
-- Create date: 2015-7-8
-- Description:	查看当前数据库的数据表中每一列的具体信息
-- ==============================================================
CREATE VIEW all_columns_info
AS
WITH colConsCTE(TABLE_NAME, COLUMN_NAME, CONSTRAINT_TYPE) AS (
     SELECT A.TABLE_NAME, A.COLUMN_NAME, B.CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE A
     LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS B ON A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.TABLE_NAME = B.TABLE_NAME 
)
SELECT a.object_id AS TableID, a.name AS TableName, b.column_id AS ID, b.name as Name, d.value AS Comment , c.name AS DBType, b.max_length AS 'Length',
	CASE WHEN (SELECT COUNT(1) FROM colConsCTE cte WHERE b.name = cte.COLUMN_NAME AND a.name = cte.TABLE_NAME AND cte.CONSTRAINT_TYPE ='PRIMARY KEY') > 0 THEN 1 ELSE 0 END AS IsPrimaryKey,
	CASE WHEN (SELECT COUNT(1) FROM colConsCTE cte WHERE b.name = cte.COLUMN_NAME AND a.name = cte.TABLE_NAME AND cte.CONSTRAINT_TYPE ='FOREIGN KEY') > 0 THEN 1 ELSE 0 END AS IsForeignKey,
	b.IS_NULLABLE as 'IsNullAble'
FROM sys.tables a 
LEFT JOIN sys.columns b on a.object_id = b.object_id
LEFT JOIN sys.types c on b.user_type_id = c.user_type_id
LEFT JOIN sys.extended_properties d on a.object_id = d.major_id and d.minor_id = b.column_id

GO

Oracle

WITH colConsCTE AS (
     SELECT a.*, b.CONSTRAINT_TYPE FROM user_cons_columns a
     LEFT JOIN user_constraints b ON a.constraint_name = b.constraint_name AND a.table_name = b.table_name 
     WHERE a.TABLE_NAME = :TableName
)

SELECT tab.COLUMN_ID AS Id, tab.TABLE_NAME AS TableName, tab.COLUMN_NAME AS ColumnName, col.COMMENTS AS Remark
, tab.DATA_TYPE AS DbType
, tab.DATA_LENGTH AS Length
, tab.DATA_PRECISION AS Precision
, tab.DATA_SCALE AS Scale
, CASE tab.NULLABLE WHEN 'Y' THEN 1 ELSE 0 END AS IsNullable
, CASE WHEN (SELECT COUNT(1) FROM colConsCTE cte WHERE tab.COLUMN_NAME = cte.COLUMN_NAME AND tab.TABLE_NAME = cte.TABLE_NAME AND cte.CONSTRAINT_TYPE ='P') > 0 THEN 1 ELSE 0 END AS IsPrimaryKey
, CASE WHEN (SELECT COUNT(1) FROM colConsCTE cte WHERE tab.COLUMN_NAME = cte.COLUMN_NAME AND tab.TABLE_NAME = cte.TABLE_NAME AND cte.CONSTRAINT_TYPE ='R') > 0 THEN 1 ELSE 0 END AS IsForeignKey
, CASE WHEN (SELECT COUNT(1) FROM colConsCTE cte WHERE tab.COLUMN_NAME = cte.COLUMN_NAME AND tab.TABLE_NAME = cte.TABLE_NAME AND cte.CONSTRAINT_TYPE ='U') > 0 THEN 1 ELSE 0 END AS IsUnique
, CASE WHEN (SELECT COUNT(1) FROM colConsCTE cte WHERE tab.COLUMN_NAME = cte.COLUMN_NAME AND tab.TABLE_NAME = cte.TABLE_NAME AND cte.CONSTRAINT_TYPE ='O') > 0 THEN 1 ELSE 0 END AS IsReadOnly
, 0 AS IsIdentity
FROM USER_TAB_COLUMNS tab
LEFT JOIN USER_COL_COMMENTS col ON tab.table_name = col.table_name AND tab.COLUMN_NAME = col.COLUMN_NAME
WHERE tab.table_name = :TableName

Importing older projects metadata from Excel into YuanRui

Hello this a nice place to keep the meta data of my projects. I used to have an excel spreadsheet with the table names & two Cols for the name and type, I would like to import that into yaunrui - so I can generate the projects from here.

NameCol
TypeCol

How can I template this excel sheet to a simple C# POCO model class with the name and type

.net 7 core

Hello any updates for .net 7 core, and also use either bootstrap 5 or admin lte 4

TODO List

  1. I18N.
  2. Select part of template file to generate.
  3. Remove dependency dll, integration mono t4
  4. Support MySql.
  5. Add template:LayUI, Vue etc.
  6. Complete documentation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.