Git Product home page Git Product logo

sdmap's Introduction

sdmap NuGet

A template engine for writing dynamic sql: vstool

sdmap has it's own benifics over other dynamic SQL package/framework (like iBatis.NET):

  • Very simple Domain-Specific-Language(or DSL) to enhance the dynamic SQL expression
  • Implemented by Common-Intermediate-Language(or CIL) to ensure the performance
  • Visual Studio integrated, with code-highlight, code-folding and navigate-to features supported
  • Support all majoy databases like MySQL, SQL Server, SQLite (whenever Dapper supports :))
  • Even is able to extend to databases that is not relational, like Neo4j
  • Fully unit test covered.

NuGet Package:

How to use? (with Dapper)

  1. Install package: sdmap.ext.Dapper

You can just only install sdmap.ext.Dapper since it will automatically install all dependencies including sdmap and sdmap.ext.

  1. Create a empty text file, and renames it into .sdmap
  2. Set the file "Build operation" from "None" to "Embedded Resource" in item Property Window.
  3. Write some SQL statements into sdmap file, a minimum sdmap file may looks as following:
sql GetUserById
{
    SELECT * FROM [User] WHERE Id = @Id
}

Note: the namespace NS { ... } is not required.

  1. Initialize sdmap by this code(the Program means the assembly where sdmap be located):
DbConnectionExtensions.SetEmbeddedSqlAssembly(typeof(Program).Assembly);

Note:

  • This code MUST be running before any database operations.
  • This code ONLY need to execute ONCE per process.
  • There are many other ways to initialize sdmap, you SHOULD pick up one of those depending on what you need:
    • SetSqlDirectory - initialize from a physical on-disk folder
    • SetSqlDirectoryAndWatch - initialize from a physical on-disk folder, and watch changes when edit those folder sdmap files
    • SetEmbeddedSqlAssembly - initialize from a single assembly, and sdmap automatically parse all the resource file that ends with .sdmap
    • SetEmbeddedSqlAssemblies - initialize from multiple assemblies, and sdmap automatically parse all the resource file that ends with .sdmap
    • SetSqlEmiter - advanced, you can write your own ISdmapEmiter.
  1. And you're good to go!
  2. If you want to emit the SQL statement manully, you may want to call:
string finalSqlToExecute = DbConnectionExtensions.EmitSql(sqlMapId, parameterObject);

Visual Studio 2022 extension:

https://marketplace.visualstudio.com/items?itemName=sdmapvstool.sdmapvstool

How to Compile

  1. Install .NET Core SDK(https://www.microsoft.com/net/download/core)
  2. Download code(git clone https://github.com/sdcb/sdmap.git)
  3. JRE 1.6+ is preferred for building performance purpose but not required
  4. dotnet restore
  5. dotnet build

Release notes:

https://github.com/sdcb/sdmap/blob/master/ReleaseNotes.md

Document/Wiki

https://github.com/sdcb/sdmap/wiki

sdmap's People

Contributors

ishepherd avatar jtone123 avatar sdcb avatar zadykian 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  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  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  avatar  avatar  avatar

sdmap's Issues

[core] The ! operator priority lower than && ||

The ! operator priority should be highest, the recent commit(fabbf06) fixed it, but not deploy to nuget.

if you write #if(!A || B), you will mean #if(!(A || B)
To achieve #if(!A || B), you need to write #if((!A) || B) for the moment.

[core] #isEqual cannot compare double/float types

If you write: #isEqual<A, 3, sql {...}>, you will not get correct SQL expected, this is because following code:


Using double.Equals(int) to compare double and int, this will always returns false (as in reference source):
https://referencesource.microsoft.com/#mscorlib/system/double.cs,148

We can do workaround by using decimal typed property instead of int property:
decimal A int A

#isNotEmpty的wiki描述与实际不符

add content if has property AND property value is not empty/null
实际代码中

        [Macro("isNotEmpty")]
        [MacroArguments(SdmapTypes.Syntax, SdmapTypes.StringOrSql)]
        public static Result<string> IsNotEmpty(OneCallContext context,
            string ns, object self, object[] arguments)
        {
            if (self == null) return RequireNotNull();

            var prop = GetProp(self, arguments[0]);
            if (prop == null) return RequirePropNotNull(arguments[0]);

            string syntax = (string)arguments[0];
            if (!IsEmpty(GetPropValue(self, syntax)))
                return MacroUtil.EvalToString(arguments[1], context, self);
            return Empty;
        }

if (prop == null) return RequirePropNotNull(arguments[0]);
强制要求非空

[core] Support foreach macro

a each, iterate is necessary when batch generate sql:

  • example 1:
#iterate<',', sql{
(#prop<AffiliateId>,#prop<ServiceCategoryId>)
}>
  • example 2:
#each<Prop, ',', sql{
}>

[core] support #Prop coding style

Currently must using #prop in sdmap, we can make it simple by just typing #PropertyName.

  • example:
INSERT INTO MyTable(Id, Name)
#foreach<Items, sql{
	VALUES(#Id, N'#Name')
}>

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.