Пример #1
0
        public static CompileResult Compile(string typeSql, string typeSqlFileName)
        {
            //create the antlr-based lexer and parser
            var lexer = new TypeSqlLexer(new ANTLRStringStream(typeSql));
             var rewriteTokenStream = new TokenRewriteStream(lexer);
            var parser = new TypeSqlParser(rewriteTokenStream);

            //parse the typeSql, producing the AST
            var ast = (CommonTree) parser.typesql().Tree;
             var nodeStream = new CommonTreeNodeStream(ast);

            //transform the AST into raw-sql
             var rawSqlOutput = new RawSqlTransform(nodeStream);
             nodeStream.TokenStream = rewriteTokenStream;
             rawSqlOutput.typeSql();
             string rawSql = rewriteTokenStream.ToString();

            //reset
             lexer.Reset();
             rewriteTokenStream.Reset();
             nodeStream.Reset();
            //and transform the AST into DAO source code
             var daoTransform = new DaoTransform(nodeStream){TemplateGroup = new StringTemplateGroup(
                new StreamReader(typeof(TypeSqlCompiler).Assembly.GetManifestResourceStream("TypeSql.Parsing.DapperDao.stg")),
                typeof (TemplateLexer))};
             var template = daoTransform.typeSql(typeSqlFileName, rawSql).Template;
             string daoSourceCode = template.ToString();

            return new CompileResult(daoSourceCode, rawSql);
        }
Пример #2
0
 protected AntlrParserTest(string sql)
 {
     //act
     var lexer = new TypeSqlLexer(new ANTLRStringStream(sql));
     var parser = new TypeSqlParser(new CommonTokenStream(lexer));
     var parseResult = parser.typesql();
     AST = (CommonTree) parseResult.Tree;
     SqlNode = (CommonTree)AST.GetFirstChildWithType(TypeSqlParser.SQL);
 }
Пример #3
0
        public void OneOutputOneInput()
        {
            //arrange
            const string sql =
                @"SELECT Name:string FROM Users WHERE Id= @id:int";

            //act
            var input = new ANTLRStringStream(sql);
            var lexer = new TypeSqlLexer(input);
            var tokenStream = new CommonTokenStream(lexer);
            var parser = new TypeSqlParser(tokenStream);

            var parseResult = parser.typesql();
        }
Пример #4
0
        public void Template()
        {
            //arrange
            const string sql =
                @"SELECT UserId:int FROM Users";

            //act
            var input = new ANTLRStringStream(sql);
            var lexer = new TypeSqlLexer(input);
            var tokenStream = new CommonTokenStream(lexer);
            var parser = new TypeSqlParser(tokenStream);
            var parseResult = parser.typesql();

            var templateGroup = new StringTemplateGroup(
                new StreamReader(new FileStream(@"..\..\..\TypeSql\Parsing\DapperDao.stg", FileMode.Open)),
                typeof (TemplateLexer));

            var dapperDaoTransform = new DaoTransform(new CommonTreeNodeStream(parseResult.Tree))
                {
                    TemplateGroup = templateGroup
                };

            //var result = dapperDaoTransform.sql("UserIds").Template.ToString();
        }
Пример #5
0
        public void TwoOutputs()
        {
            //arrange
            const string sql =
                @"SELECT UserId:int, UserName:string FROM Users";

            //act
            var input = new ANTLRStringStream(sql);
            var lexer = new TypeSqlLexer(input);
            var tokenStream = new CommonTokenStream(lexer);
            var parser = new TypeSqlParser(tokenStream);

            var parseResult = parser.typesql();
        }