sqlserver 注释提取工具
小程序大智慧,sqlserver 注释提取工具
开篇背景
我习惯在写表的创建脚本时将注释直接写在脚本里,比如
/*账套*/CREATE TABLE [dbo].[AccountingBook]( [IDNO] NVARCHAR (255) NOT NULL, /*ID*/ [BH] NVARCHAR (255) NULL, /*业务编号*/ [Name] NVARCHAR (255) NOT NULL, /*名称*/ [Decription] NVARCHAR (255) NULL, /*描述*/ [Owner] NVARCHAR (255) NOT NULL, /*所属*/ CONSTRAINT [PK_AccountingBook] PRIMARY KEY CLUSTERED ([IDNO] ASC)) |
这样写很直观,如果在vs里创建一个数据库项目,把表的创建脚本放在里面进行管理,就非常方便的。
由于习惯用自己的Orm框架,所以DTO也就是那些数据映射实体我都是用codeSmith生成,生成这些DTO对象时,我想共用我的那些注释,那么我该怎么办呢,之前,我需要把这些注释复制出来写成一些注释创建的脚本,像这样
exec sp_addextendedproperty N'MS_Description', N'字段描述', N'user', N'dbo', N'table', N'表名', N'column', N'字段名' |
添加注释的目的是除了在使用数据库连接工具时方便查看表和字段的说明外,还可以使用CodeSmith生成代码的时候就可以通过编写模版生成带注释的映射DTO 对象,如下
/// <summary>
/// 业务编号
/// </summary>
[Column(ColumnName=Columns.BH,FullName=Columns.BHFullName,Index=1,CType=typeof(string),Description="业务编号")]
[DataMember(Order = 1)]
public virtual string BH{get;set;}
但是由于表创建脚本里的注释不能直接写入到数据库的表和字段中,所以注释的创建脚本我需要再写一次,我觉得比较不爽,于是我决定写个小工具从表的创建脚本里面抽取那些本来就写好的注释,从而减小重复机械的工作,也防止错误的发生。
这样一个程序非常简单,下面说一下思路
实现
一,写好带注释的表脚本,并提取这些信息
格式按文章开始讲到的那样写好,即"/*注释*/",“*”可以是多个,比如"/*********注释***********/",并将这些脚本存放到相同根目录
要提取这些注释,最大的功臣非正则表达式莫属了

a.提取表头的注释,也就是表名的解释正则表达式
private readonly Regex _tableReg = new Regex(@"/[\*]+([^\*]*)[\*]+/[\r\n\s]*CREATE TABLE \[dbo\].\[(\w*)\]");
b.提取列的注释正则表达式
private readonly Regex _columnsReg = new Regex(@"\[([\w]*)\][^\/]*/[\*]+([^\*]*)[\*]+/");
二,递归查找到这些表的创建脚本,将每个生成注释脚本的字符串连接起来

Func<string, List<string>> getFolderSqlNotes = null;
getFolderSqlNotes = path =>
{
var listAll = new List<string>();
var files = Directory.GetFiles(path);
var dirs = Directory.GetDirectories(path);
foreach (string t in dirs)
{
listAll.AddRange(getFolderSqlNotes(t)); ;
}
var listStr = files.Where(m => m.EndsWith(".sql")).ToList().Select(GetDescriptionSql).ToList();
listAll.AddRange(listStr);
return listAll;
};
var list = getFolderSqlNotes(Path);
return string.Join("\r\n", list);

三,执行生成好的脚本(如下图),注释创建完成

四,用codesimth 生成映射类(已带注释)

核心代码
namespace GenrerateSqlDescription
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public class SqlNotesGenerator
{
private readonly Regex _tableReg = new Regex(@"/[\*]+([^\*]*)[\*]+/[\r\n\s]*CREATE TABLE \[dbo\].\[(\w*)\]");
private readonly Regex _columnsReg = new Regex(@"\[([\w]*)\][^\/]*/[\*]+([^\*]*)[\*]+/");
private const string TableDescriptionCrateSqlFormat = "EXEC sp_addextendedproperty 'MS_Description','{0}','user ',dbo,'table',[{1}];\r\n";
private const string ColumnDescriptionCrateSqlFormat = "EXEC sp_addextendedproperty 'MS_Description','{0}','user',dbo,'table',[{1}],'column','{2}';\r\n";
private const string TableDescriptionDropSqlFormat = "EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table',[{0}];\r\n";
private const string ColumnescriptionDropSqlFormat = "EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table',[{0}],'column','{1}';\r\n";
private const string CheckTableExistsSqlFormat = "IF OBJECT_ID(N'{0}',N'U') IS NOT NULL \r\nBEGIN \r\n";
private const string EndStr = "END";
private const string Tab = " ";
public bool GenDrop { get; set; }
public string Path { get; set; }
private string GetDescriptionSql(string path)
{
var sb = new StringBuilder();
var fs = File.OpenRead(path);
var fileRd = new StreamReader(fs);
var str = fileRd.ReadToEnd();
var tableMatch = _tableReg.Match(str);
if (tableMatch.Length < 2) return string.Empty;
var tableName = tableMatch.Groups[2].Value;
var tableDes = tableMatch.Groups[1].Value;
if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(tableDes)) return string.Empty;
var columnStr = str.Substring(str.IndexOf("(", StringComparison.Ordinal));
var matches = _columnsReg.Matches(columnStr);
sb.AppendFormat(CheckTableExistsSqlFormat, tableName);
sb.Append(Tab);
if (GenDrop)
sb.AppendFormat(TableDescriptionDropSqlFormat, tableName);
else
sb.AppendFormat(TableDescriptionCrateSqlFormat, tableDes, tableName);
foreach (Match match in matches)
{
var columnName = match.Groups[1].Value;
var description = match.Groups[2].Value;
if (string.IsNullOrEmpty(columnName) || string.IsNullOrEmpty(description))
continue;
sb.Append(Tab);
if (GenDrop)
sb.AppendFormat(ColumnescriptionDropSqlFormat, tableName, columnName);
else
sb.AppendFormat(ColumnDescriptionCrateSqlFormat, description, tableName, columnName);
}
sb.AppendLine(EndStr);
return sb.ToString();
}
public string GetGenerateSql()
{
Func<string, List<string>> getFolderSqlNotes = null;
getFolderSqlNotes = path =>
{
var listAll = new List<string>();
var files = Directory.GetFiles(path);
var dirs = Directory.GetDirectories(path);
foreach (string t in dirs)
{
listAll.AddRange(getFolderSqlNotes(t)); ;
}
var listStr = files.Where(m => m.EndsWith(".sql")).ToList().Select(GetDescriptionSql).ToList();
listAll.AddRange(listStr);
return listAll;
};
var list = getFolderSqlNotes(Path);
return string.Join("\r\n", list);
}
}
}
SqlNotesGenerator
CodeSimith模版
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="OutputFileCodeTemplate" Debug="False" Description="Template description here." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Optional="True" Description="the table name" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
//------------------------------------------------------------------------------
// <auto-generated>
// This code generated by the tool, do not propose to amend
// Generation time:<%=DateTime.Now%>
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Data;
using System.Runtime.Serialization;
using XDbFramework;
using BPM_M01.DataAccessLayer.Base;
using System.Xml.Serialization;
using System.Diagnostics;
using System.CodeDom.Compiler;
namespace <%=Namespace%>
{
[Table(TableName = <%=this.SourceTable.Name%>.Table.Name ,Descripton = "<%=this.SourceTable.Description%>",GenreratePK = <%=GenreratePK.ToString().ToLower()%>)]
[GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[DebuggerStepThroughAttribute()]
public partial class <%=this.SourceTable.Name%> : DtoBase
{
public static class Table
{
public const string Name = "<%=this.SourceTable.Name%>";
}
public static class Columns
{
<%for(int i=0; i<this.SourceTable.Columns.Count;i++)
{
string colName=this.SourceTable.Columns[i].Name;%>
public const string <%=colName%> = "<%=colName%>";
public const string <%=colName%>FullName =Table.Name + "." + "<%=colName%>";
<%}%>
}
<%for(int i=0; i<this.SourceTable.Columns.Count;i++)
{
int namelength=this.SourceTable.Columns[i].Name.Length;
string colName=this.SourceTable.Columns[i].Name;
if(colName == "ID" || colName == "IDNO") continue;
string titleCaseColumName = colName.Substring(0,1).ToUpper() + colName.Substring(1,colName.Length-1);
%>
/// <summary>
/// <%=this.SourceTable.Columns[i].Description%>
/// </summary>
<%if(this.SourceTable.Columns[i].IsPrimaryKeyMember){%>
[Column(KeyType = KeyTypeEnum.PrimaryKey,FullName="<%=this.SourceTable.Name%>.<%=this.SourceTable.Columns[i].Name%>", ColumnName="<%=this.SourceTable.Columns[i].Name%>",Index=<%=i%>,Description="<%=this.SourceTable.Columns[i].Description%>")]
<%}%>
<%else{%>
<%if(this.SourceTable.Columns[i].IsForeignKeyMember)%>
<%//
foreach(TableKeySchema tks in SourceTable.ForeignKeys)
{
//
foreach(MemberColumnSchema mcs in tks.ForeignKeyMemberColumns)
{
//
if(mcs.Name == SourceTable.Columns[i].Name)
{
TableSchema ts= tks.PrimaryKeyTable;%>
[Column(ColumnName=Columns.<%=colName%>,KeyType = KeyTypeEnum.ForeignKey,FullName=Columns.<%=colName%>FullName,CType=typeof(<%=GetCSharpVariableType(this.SourceTable.Columns[i])%>),ForeignKeyTableName="<%=ts.Name%>",ForeignKeyFiledName="<%=ts.PrimaryKey.MemberColumns[0].Name%>", DbType=<%=GetSqlDBType(this.SourceTable.Columns[i].NativeType)%> Index=<%=i%>,Description="<%=this.SourceTable.Columns[i].Description%>")]
<% break;
}
}
}
%>
<%else{%>
[Column(ColumnName=Columns.<%=colName%>,FullName=Columns.<%=colName%>FullName,Index=<%=i%>,CType=typeof(<%=GetCSharpVariableType(this.SourceTable.Columns[i])%>),Description="<%=this.SourceTable.Columns[i].Description%>")]
<%}%>
<%}%>
[DataMember(Order = <%=i%>)]
public virtual <%=GetCSharpVariableType(this.SourceTable.Columns[i])%> <%=titleCaseColumName%>{get;set;}
<%}%>
}
}
<script runat="template">
// Override the OutputFile property and assign our specific settings to it.
[FileDialog(FileDialogType.Save, Title="Select Output File", Filter="C# Files (*.cs)|*.cs", DefaultExtension=".cs")]
public override string OutputFile
{
get {return base.OutputFile;}
set {base.OutputFile = value;}
}
private string _Namespace;
public string Namespace
{
get{return _Namespace;}
set{_Namespace = value;}
}
public bool GenreratePK{get;set;}
</script>
<script runat="template">
public string GetSqlDBType(string type)
{
switch(type)
{
case "int": return "SqlDbType.Int,";
case "bit": return "SqlDbType.Bit,";
case "bigint": return "SqlDbType.BigInt,";
case "tinyint": return "SqlDbType.TinyInt,";
case "nvarchar": return "SqlDbType.NVarChar,";
case "date": return "SqlDbType.Date,";
case "datetime": return "SqlDbType.DateTime,";
case "char": return "SqlDbType.Char,";
case "decimal": return "SqlDbType.Decimal,";
case "float": return "SqlDbType.Float,";
case "image": return "SqlDbType.Image,";
case "money": return "SqlDbType.Money,";
case "nchar": return "SqlDbType.NChar,";
case "ntext": return "SqlDbType.NText,";
case "real": return "SqlDbType.Real,";
case "smalldatetime": return "SqlDbType.SmallDateTime,";
case "smallint": return "SqlDbType.SmallInt,";
case "smallmoney": return "SqlDbType.SmallMoney,";
case "text": return "SqlDbType.Text,";
case "timestamp": return "SqlDbType.Timestamp,";
case "udt": return "SqlDbType.Udt,";
case "uniqueidentifier": return "SqlDbType.UniqueIdentifier,";
case "varbinary": return "SqlDbType.VarBinary,";
case "varchar": return "SqlDbType.VarChar,";
case "variant": return "SqlDbType.Variant,";
case "xml": return "SqlDbType.Xml,";
default : return "";
}
}
public string GetCSharpVariableType(ColumnSchema column)
{
switch (column.DataType)
{
case DbType.AnsiString: return "string";
case DbType.AnsiStringFixedLength: return "string";
case DbType.Binary: return "byte[]";
case DbType.Boolean: return "bool?";
case DbType.Byte: return "byte";
case DbType.Currency: return "decimal?";
case DbType.Date: return "DateTime?";
case DbType.DateTime: return "DateTime?";
case DbType.Decimal: return "decimal?";
case DbType.Double: return "double?";
case DbType.Guid: return "Guid";
case DbType.Int16: return "short?";
case DbType.Int32: return "int?";
case DbType.Int64: return "long?";
case DbType.Object: return "object";
case DbType.SByte: return "sbyte";
case DbType.Single: return "float?";
case DbType.String: return "string";
case DbType.StringFixedLength: return "string";
case DbType.Time: return "TimeSpan";
case DbType.UInt16: return "ushort?";
case DbType.UInt32: return "uint?";
case DbType.UInt64: return "ulong?";
case DbType.VarNumeric: return "decimal?";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
</script>
代码
下载
GenrerateSqlDescription_8_28.zip
sqlserver 注释提取工具的更多相关文章
- 小程序大智慧,sqlserver 注释提取工具
原文:小程序大智慧,sqlserver 注释提取工具 开篇背景 我习惯在写表的创建脚本时将注释直接写在脚本里,比如 /*账套*/ CREATE TABLE [dbo].[AccountingBook] ...
- php文档注释提取工具phpdocumentor的使用
phpDocumentor, 分为文档性注释, 和 非文档性注释; 命令为: phpdoc -h, -f, -d.... 提取/ 生成 程序的注释文档, 实际上有很多种工具, 如: doc++, do ...
- Visual Studio 2008(C#)XML注释提取成帮助文档的方法
Visual Studio 2008(C#)XML注释提取成帮助文档的方法 1.给方法和类添加XML注释 可以手动添加,具体规则可以看MSDN:http://msdn.microsoft.co ...
- Sqlite表结构读取工具,word批量转html,在线云剪贴板,文件批量提取工具;
工欲善其事必先利其器,本周为您推荐工具排行 Sqlite表结构读取工具,word批量转html,在线云剪贴板,文件批量提取工具: 本周我们又要发干货了,准备好接受了吗? 为什么是干货,就是因为 ...
- Map工具系列-07-TFS变更集提取工具
所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...
- Android开发学习之路-Palette颜色提取工具类使用
视频(要FQ):https://www.youtube.com/watch?v=5u0dtzXL3PQ Palette是一个在support-v7包中的一个颜色提取工具类,用法比较简单,而且是谷歌官方 ...
- manifest资源提取工具
因业务需要,写了个manifest资源提取工具,该机制是将html文件作为入口文件进行资源抓取.原理是先简单扫html token,然后直接遍历每个tag token是否属于需要的资源(css,js, ...
- Day 16: Goose Extractor —— 好用的文章提取工具
Day 16: Goose Extractor -- 好用的文章提取工具 Day 16: Goose Extractor -- 好用的文章提取工具
- SQLSERVER图片查看工具SQL Image Viewer5.5.0.156
原文:SQLSERVER图片查看工具SQL Image Viewer5.5.0.156 SQLSERVER图片查看工具SQL Image Viewer5.5.0.156 在2013年某一次北京SQL ...
随机推荐
- C++ 对象模型具体评论(特别easy理解力)
c++对象模型系列 转 一.指针与引用 一 概括 指针和引用,在C++的软件开发中非经常见,假设能恰当的使用它们可以极大的提 高整个软件的效率,可是非常多的C++学习者对它们的各种使用情况并非都了解, ...
- utf8 和 UTF-8 在使用中的差别
在使用中经常遇到utf-8和utf8,如今最终弄明确他们的使用不同之处了,如今来和大家分享一下,以下我们看一下utf8 和 UTF-8 有什么差别 "UTF-8"是标准写法,ph ...
- 微信公众平台消息接口开发-封装weixin.class.php
原文:微信公众平台消息接口开发-封装weixin.class.php 一.封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处 ...
- AngularJS应用开发思维之1:声明式界面
这篇博客之前承接上一篇:http://www.cnblogs.com/xuema/p/4335180.html 重写示例:模板.指令和视图 AngularJS最显著的特点是用静态的HTML文档,就可以 ...
- Objective-c中的单例
单例是指静态分配的实例,就是只开辟一块内存,不会重新开辟内存,而 iphone sdk 中全是这种实例,例如[UIApplication sharedApplication] 返回一个指向代表应用程序 ...
- ios 安装OpenFire
1.开发xmpp官网下载 2.打开openfire.pkg 3.点击继续 4.成功安装后打开偏好设置 ->双击poenfire->弹出窗体[好] 5.随后会弹出以下这个视图 开启 strr ...
- springbatch操作CSV文件
一.需求分析 使用Spring Batch对CSV文件进行读写操作: 读取一个含有四个字段的CSV文件(id, name, age, score), 对文件做简单的处理, 然后输出到还有一个csv文件 ...
- DOM2级事件对象、添加事件、阻止默认事件、阻止冒泡事件、获取事件对象目标的兼容处理
事件对象——兼容处理 /* * 功能: 事件对象兼容 * 参数: 表示常规浏览器的事件对象e */ function getEvent(e) { // 如果存在e存在,直接返回,否则返回window. ...
- 阐述 QUEST CENTRAL FOR DB2 八罪
作为一个从事oracle plsql发展2猿 - 年计划,现在,在退出DB2数据仓库项目. 同PL/SQL Developer参考,下文PLSQL,阐述QUEST CENTRAL FOR DB2 5. ...
- C# 截取图片区域,并返回所截取的图片
/// <summary> /// 截取图片区域,返回所截取的图片 /// </summary> /// <param name="SrcImage" ...