private static SdmapCompiler CreateCompilerFromSqlDirectory( string sqlDirectory, IFileSystem fileSystem, bool ensureCompiled) { var compiler = new SdmapCompiler(); foreach (var file in fileSystem.Directory.EnumerateFiles(sqlDirectory, "*.sdmap", SearchOption.AllDirectories)) { var code = fileSystem.File.ReadAllText(file); compiler.AddSourceCode(code); } if (ensureCompiled) { var compileResult = compiler.EnsureCompiled(); if (compileResult.IsFailure) { throw new InvalidProgramException(compileResult.Error); } ; } return(compiler); }
private static void ParseFolder(string folderPathName) { var rt = new SdmapCompiler(); foreach (var file in Directory.GetFiles(folderPathName, "*.sdmap", SearchOption.AllDirectories)) { rt.AddSourceCode(File.ReadAllText(file)); } var sw = new Stopwatch(); sw.Start(); var ok = rt.EnsureCompiled(); sw.Stop(); Console.WriteLine($"Compiled in: {sw.ElapsedMilliseconds}ms."); if (ok.IsFailure) { Console.WriteLine("Compile failed: " + ok.Error); } else { Console.WriteLine("Compile succeed."); } }
public static string Run(string code, string sqlId, object obj = null) { var c = new SdmapCompiler(); c.AddSourceCode(code); return(c.Emit(sqlId, obj)); }
public void ReferencedDefDepInMacro() { var rt = new SdmapCompiler(); rt.AddSourceCode("sql v1{#def<A, 'A'>#def<B, sql{#deps<A>B}>#isNotNull<B, sql {#deps<B>C}}}"); string result = rt.Emit("v1", new { B = "" }); Assert.Equal("ABC", result); }
public void WillLoadOnEqual(bool load) { var rt = new SdmapCompiler(); rt.AddSourceCode("sql v1{#def<A, 'A'>#isEqual<A, true, sql {#deps<A>B}>}"); string result = rt.Emit("v1", new { A = load }); Assert.Equal(load ? "AB" : "", result); }
public void DefDepEmitAll() { var rt = new SdmapCompiler(); rt.AddSourceCode("sql v1{#def<A, 'A'>#def<B, sql{#deps<A>B}>#deps<B>}"); string result = rt.Emit("v1", null); Assert.Equal("AB", result); }
public void WillKeepsOrder() { var rt = new SdmapCompiler(); rt.AddSourceCode("sql v1{#def<C, 'C'>#def<A, 'A'>#def<B, sql{B}>#deps<A,B,C>}"); string result = rt.Emit("v1", null); Assert.Equal("CAB", result); }
public void Smoke() { var rt = new SdmapCompiler(); rt.AddSourceCode("sql v1{#def<id, 'test'>#deps<id>}"); string result = rt.Emit("v1", null); Assert.Equal("test", result); }
public void IncludedDefAlsoWorks() { var rt = new SdmapCompiler(); rt.AddSourceCode("sql v1{#include<v2>3#deps<B>} sql v2{1#def<B, '2'>}"); string result = rt.Emit("v1", null); Assert.Equal("123", result); }
public void Include() { var code = "sql v1{#include<v2>} sql v2{Life is good}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", null); Assert.Equal("Life is good", result); }
public void IncludeWithSql() { var code1 = "namespace ns{sql v1{#include<v2>} sql v2{#iif<P, sql{A}, sql{B}>}}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code1); var result = rt.Emit("ns.v1", new { P = true }); Assert.Equal("A", result); }
public void Simple() { var code = "sql v1{#isEqual<A, true, #prop<B>>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = true, B = "Yes" }); Assert.Equal("Yes", result); }
public void TrueWillEmit() { var code = "sql v1{#if(A){HelloWorld}}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = true }); Assert.Equal("HelloWorld", result); }
public void CanShowValue() { var code = "sql v1{#val<>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", "Hello World"); Assert.Equal("Hello World", result); }
public void IfNotNull() { var code = "sql v1{#isNotEmpty<A, sql{@A}>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = " " }); Assert.Equal(string.Empty, result); }
public void YesNo(bool input, string expected) { var code = "sql v1{#iif<A, 'Yes#', 'No!<>'}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = input }); Assert.Equal(expected, result); }
public void PropInSubSql() { var code = "sql v1{#isNotEmpty<A, sql{#prop<A>}>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = "NotEmpty" }); Assert.Equal("NotEmpty", result); }
public void IncludeSameNs() { var code1 = "namespace ns{sql v1{#include<v2>#include<v2>} sql v2{v2}}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code1); var result = rt.Emit("ns.v1", null); Assert.Equal("v2v2", result); }
public void DateTypeShouldFail() { var code = "sql v1{#iif<A, 'Yes#', 2016-1-1}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.TryEmit("v1", new { A = true }); Assert.False(result.IsSuccess); }
public void isNotEmpty() { var code = "sql v1{#isNotEmpty<A, sql{@A}>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = "NotEmpty" }); Assert.Equal("@A", result); }
public void InsuficientArgumentShouldFail() { var code = "sql v1{#iif<A, 'Yes#'}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.TryEmit("v1", new { A = true }); Assert.False(result.IsSuccess); }
public void CanShowDouble() { var code = "sql v1{#prop<V>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { V = 3.14 }); Assert.Equal("3.14", result); }
public void NotExistPropShouldFail() { var code = "sql v1{#iif<A, 'Yes#', 'hello'}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.TryEmit("v1", new { B = true }); Assert.False(result.IsSuccess); }
public void CanShowEmpty() { var code = "namespace v1{sql v1{#prop<V>}}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1.v1", new { V = (int?)null }); Assert.Equal(string.Empty, result); }
public void EmptyValueTest() { var code = "sql v1{#val<>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", null); Assert.Equal(string.Empty, result); }
public void NoPropTest() { var code = "namespace v1{sql v1{#prop<V>}}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.TryEmit("v1.v1", new { }); Assert.False(result.IsSuccess); }
public void FalseWontEmit() { var code = "sql v1{#if(A){HelloWorld}}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = false }); Assert.Equal("", result); }
public void CanShowString() { var code = "sql v1{#prop<Name>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { Name = "Hello World" }); Assert.Equal("Hello World", result); }
public void IsEqualIntOk() { var code = "sql v1{#isEqual<A, 3, 'Yes'>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.Emit("v1", new { A = 3 }); Assert.Equal("Yes", result); }
public void NestedPropTest() { var code = "sql v1{#prop<A.B>}"; var rt = new SdmapCompiler(); rt.AddSourceCode(code); var result = rt.TryEmit("v1", new { A = new { B = 3 } }); Assert.Equal("3", result.Value); }