Coder Social home page Coder Social logo

overlord / sqlusertypegenerator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rinat-khairullin/sqlusertypegenerator

0.0 1.0 0.0 120 KB

Generates SQL user-defined type (UDT) from C# class

License: MIT License

C# 91.76% Batchfile 1.02% PowerShell 1.39% TSQL 5.81% Smalltalk 0.02%

sqlusertypegenerator's Introduction

SqlUserTypeGenerator

Generates SQL user-defined table type (UDT) from C# class

For example, for this class:

[SqlUserType(TypeName = "t_users")]
public class User
{
	public int Id { get; set; }
	public string Name { get; set; }
	public DateTime? DateOfBirth { get; set; }
}

following file will be generated

--autogenerated by SqlUserTypeGenerator v1.0.0.0

create type [t_users] as table (
	Id int not null,
	Name nvarchar(50) not null,
	DateOfBirth datetime null
)
go

Usage

  1. Install package:
Install-Package SqlUserTypeGenerator
  1. add SqlUserType attribute with optional sql type name to class

  2. build project; new file with sql type definition will be generated to GeneratedSqlTypes folder in project folder

Types mapping

.NET type SQL type Note
Int64 bigint
string nvarchar
Boolean bit
DateTime datetime Can be tweaked to datetime2 or date
Double float
Int32 int
Decimal numeric
Guid uniqueidentifier
Byte[] varbinary
Byte tinyint
Enum int

Complex types (classes and interfaces) are ignored

Column properties

  • Use SqlColumnAttribute to define column properties (length and nullability for nvarchar, precision and scale for numeric).

  • Use SqlDateColumnAttribute to specify exact sql-type for DateTime column (datetime, datetime2 or date).

[SqlUserType(TypeName = "t_example")]
public class Example
{
	[SqlColumn(Length = 42)]
	public string NotNullString { get; set; }
	[SqlColumn(Length = 10, Nullable = true)]
	public string NullString { get; set; }
	// string with max length
	[SqlColumn(Length = SqlColumnAttribute.MaxLength)]
	public string StringMax { get; set; }
	[SqlColumn(Presicion = 7, Scale = 3)]
	public decimal Decimal { get; set; }
	[SqlDateColumn(SqlDateType.DateTime)]
	public DateTime ExplicitDateTime { get; set; }
	[SqlDateColumn(SqlDateType.DateTime2)]
	public DateTime ExplicitDateTime2 { get; set; }
	[SqlColumn(Name = "custom_name")]
	public int? CustomName { get; set; }
}
create type [t_example] as table ( 
	NotNullStringField nvarchar(42) not null,
	NullStringField nvarchar(10) null,
	StringMaxField nvarchar(max) not null,
	DecimalField numeric(7, 3) not null,
	ExplicitDateTime datetime not null,
	ExplicitDateTime2 datetime2 not null,
	custom_name int null
)

Notes

  • Output folder for generated files can be customized by setting SqlUserTypeGenerator_TargetFolder property in csproj file.
  • Type pre-create and post-create code can be set via SqlUserTypeGenerator_TypePreCreateCode and SqlUserTypeGenerator_TypePostCreateCode properties; $typename$ string in this code will replaced to generated type name.

(see file tools\SqlUserTypeGenerator_GlobalProps.props file in sample DbClassesWithCustomSqlFolder project)

sqlusertypegenerator's People

Contributors

overlord avatar rinat-khairullin avatar

Watchers

 avatar

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.