Пример #1
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();
        }
Пример #2
0
 /** <summary>
  *  Load a group with a specified superGroup.  Groups with
  *  region definitions must know their supergroup to find templates
  *  during parsing.
  *  </summary>
  */
 public virtual StringTemplateGroup LoadGroup( string groupName,
     Type templateLexer,
     StringTemplateGroup superGroup)
 {
     StringTemplateGroup group = null;
     TextReader br = null;
     // group file format defaults to <...>
     Type lexer = typeof( AngleBracketTemplateLexer );
     if ( templateLexer != null )
     {
         lexer = templateLexer;
     }
     try
     {
         br = Locate( groupName + ".stg" );
         if ( br == null )
         {
             Error( "no such group file " + groupName + ".stg" );
             return null;
         }
         group = new StringTemplateGroup( br, lexer, _errors, superGroup );
         br.Close();
         br = null;
     }
     catch ( IOException ioe )
     {
         Error( "can't load group " + groupName, ioe );
     }
     finally
     {
         if ( br != null )
         {
             try
             {
                 br.Close();
             }
             catch ( IOException ioe2 )
             {
                 Error( "Cannot close template group file: " + groupName + ".stg", ioe2 );
             }
         }
     }
     return group;
 }
Пример #3
0
 public virtual StringTemplateGroup LoadGroup( string groupName,
     StringTemplateGroup superGroup)
 {
     return LoadGroup( groupName, null, superGroup );
 }
Пример #4
0
 public static StringTemplateGroup LoadGroup( string name,
     Type lexer,
     StringTemplateGroup superGroup)
 {
     if ( _groupLoader != null )
     {
         return _groupLoader.LoadGroup( name, lexer, superGroup );
     }
     return null;
 }
Пример #5
0
 public static StringTemplateGroup LoadGroup( string name,
     StringTemplateGroup superGroup)
 {
     return LoadGroup( name, null, superGroup );
 }
Пример #6
0
 /** <summary>
  *  Create a group from the input stream, but use a nondefault lexer
  *  to break the templates up into chunks.  This is usefor changing
  *  the delimiter from the default $...$ to &lt;...>, for example.
  *  </summary>
  */
 public StringTemplateGroup( TextReader r,
     Type lexer,
     IStringTemplateErrorListener errors,
     StringTemplateGroup superGroup)
 {
     this._templatesDefinedInGroupFile = true;
     // if no lexer specified, then assume <...> when loading from group file
     if ( lexer == null )
     {
         lexer = typeof( AngleBracketTemplateLexer );
     }
     TemplateLexerClass = lexer;
     if ( errors != null )
     {
         // always have to have a listener
         this._listener = errors;
     }
     SuperGroup = superGroup;
     ParseGroup( r );
     _nameToGroupMap[_name] = this;
     VerifyInterfaceImplementations();
 }
        /** <summary>
         *  Return a list of all template names missing from group that are defined
         *  in this interface.  Return null if all is well.
         *  </summary>
         */
        public virtual IList<string> GetMissingTemplates( StringTemplateGroup group )
        {
            string[] missing =
                _templates.Values
                .Where( template => !template.optional && !group.IsDefined( template.name ) )
                .Select( template => template.name )
                .ToArray();

            return ( missing.Length == 0 ) ? null : missing;
        }
 /** <summary>
  *  Return a list of all template sigs that are present in the group, but
  *  that have wrong formal argument lists.  Return null if all is well.
  *  </summary>
  */
 public virtual IList<string> GetMismatchedTemplates( StringTemplateGroup group )
 {
     List<string> mismatched = new List<string>();
     foreach ( TemplateDefinition d in _templates.Values )
     {
         if ( group.IsDefined( d.name ) )
         {
             StringTemplate defST = group.GetTemplateDefinition( d.name );
             var formalArgs = defST.FormalArguments;
             bool ack = false;
             if ( ( d.formalArgs != null && formalArgs == null ) ||
                 ( d.formalArgs == null && formalArgs != null ) ||
                 d.formalArgs.Count != formalArgs.Count )
             {
                 ack = true;
             }
             if ( !ack )
             {
                 foreach ( var arg in formalArgs )
                 {
                     FormalArgument arg2;
                     if ( !d.formalArgs.TryGetValue( arg.name, out arg2 ) || arg2 == null )
                     {
                         ack = true;
                         break;
                     }
                 }
             }
             if ( ack )
             {
                 //System.out.println(d.formalArgs+"!="+formalArgs);
                 mismatched.Add( GetTemplateSignature( d ) );
             }
         }
     }
     if ( mismatched.Count == 0 )
     {
         mismatched = null;
     }
     return mismatched;
 }