How to get fully materialized query from querydsl

前端 未结 2 1380
迷失自我
迷失自我 2020-12-08 16:47

I am trying to use querydsl for building dynamic queries for dynamic schemas. I am trying to get just the query instead of having to actually execute it.

So far I ha

2条回答
  •  情歌与酒
    2020-12-08 17:11

    When working with QueryDSL, you must provide a template for the database platform to build the query for. I see you are already are doing this here:

    private SQLTemplates templates = new MySQLTemplates();
    private Configuration configuration = new Configuration(templates); 
    

    To make the schema name appear in the generated query, the only way I have found to do this is (there may be an easier way) is to extend the template class and explicitly call this.setPrintSchema(true); inside the constructor. Here is a class that should work for MySql:

    import com.mysema.query.sql.MySQLTemplates;
    
    public class NewMySqlTemplates extends MySQLTemplates {
    
        public NewMySqlTemplates() {
            super('\\', false);
        }
    
        public NewMySqlTemplates(boolean quote) {
            super('\\', quote);
        }
    
        public NewMySqlTemplates(char escape, boolean quote) {
            super(escape, quote);
            this.setPrintSchema(true);
        }
    
    }
    

    Then simply use this NewMySqlTemplates class in place of the MySQLTemplates class like this:

    private SQLTemplates templates = new NewMySQLTemplates();
    private Configuration configuration = new Configuration(templates); 
    

    I have this working using PostgresTemplates, so I may have a typo or mistake in the NewMySqlTemplates class above, but you should be able to get it to work. Good luck!

提交回复
热议问题