How to get fully materialized query from querydsl

前端 未结 2 1369
迷失自我
迷失自我 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:09

    To enable schema printing use the following pattern

    SQLTemplates templates = MySQLTemplates.builder()
        .printSchema()
        .build();
    

    SQLTemplates subclasses were used before, but since some time the builder pattern is the official way to customize the templates http://www.querydsl.com/static/querydsl/3.3.1/reference/html/ch02s03.html#d0e904

    And to enable direct serialization of literals use

    //configuration level
    configuration.setUseLiterals(true);
    
    //query level
    configuration.setUseLiterals(true);
    

    Here is a full example

    // configuration
    SQLTemplates templates = MySQLTemplates.builder()
        .printSchema()
        .build();
    Configuration configuration = new Configuration(templates);
    
    // querying
    SQLQuery sqlQuery = new SQLQuery(connection, configuration)
        .from(userPath).where(idPath.eq(1l)).limit(10);
    sqlQuery.setUseLiterals(true);    
    String query = sqlQuery.getSQL(usernamePath).getSQL();
    

    If you always just want the SQL query string out, move setUseLiterals from query to configuration.

    Concerning the usage of Querydsl expressions the usage of code generation like documented here is advised http://www.querydsl.com/static/querydsl/3.3.1/reference/html/ch02s03.html

    It will make your code typesafe, compact and readable.

    If you want to try Querydsl without code generation you can replace

    Path<Object> userPath = new PathImpl<Object>(Object.class, variable);
    

    with

    Path<Object> userPath = new RelationalPathBase<Object>(Object.class, variable, schema, table);
    
    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题