Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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.");
            }
        }
Exemplo n.º 3
0
        public static string Run(string code, string sqlId, object obj = null)
        {
            var c = new SdmapCompiler();

            c.AddSourceCode(code);
            return(c.Emit(sqlId, obj));
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 20
0
        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);
        }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 23
0
        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);
        }
Exemplo n.º 24
0
        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);
        }
Exemplo n.º 25
0
        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);
        }
Exemplo n.º 26
0
        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);
        }
Exemplo n.º 27
0
        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);
        }
Exemplo n.º 28
0
        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);
        }
Exemplo n.º 29
0
        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);
        }
Exemplo n.º 30
0
        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);
        }