Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate constructor with var and schema parameters #1877

Closed
natros opened this issue May 9, 2016 · 4 comments
Closed

Generate constructor with var and schema parameters #1877

natros opened this issue May 9, 2016 · 4 comments

Comments

@natros
Copy link
Contributor

natros commented May 9, 2016

I have a scenario where the database (mysql) is composed of a master schema and a schema for each client. The tables in each schema have the same structure.

mysql
  |
  +- master (db)
  |
  +- client_1 (db)
  |       |
  |       +- user (table)
  |       .
  |       .
  |
  +- client_2 (db)
  |       |
  |       +- user (table)
  |       .
  |       .
  .
  .
  .
  +- client_n (db)

I guess the solution is to construct the Q types with the schema

QUser u1 = new QUser("u", "client_1", "user");
QUser u2 = new QUser("u", "client_2", "user");

My work would be simplified if I had a constructor with only the variable and the schema:

    public QUser(String variable, String schema) {
        super(User.class, forVariable(variable), schema, "USER");
        addMetadata();
    }

Using QueryDSL SQL 4.1.0

@timowest
Copy link
Member

timowest commented May 15, 2016

Maybe (schema, table, variable) and (schema, table, pathmetadata) could be used instead in addition to the existing (variable) and (pathmetadata) constructors?

@timowest
Copy link
Member

Would you be interested to provide a PR for it? It's mostly customizing this class https://github.com/querydsl/querydsl/blob/master/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetaDataSerializer.java

@natros
Copy link
Contributor Author

natros commented May 23, 2016

Before doing anything I've come across a problem when using mysql. For some reason mysql uses catalogs to identify schemes.

The sql generator uses "null" for schema.

    public QMaster(String variable) {
        super(Master.class, forVariable(variable), "null", "master");
        addMetadata();
    }
    public QMaster(String variable, String schema, String table) {
        super(Master.class, forVariable(variable), schema, table);
        addMetadata();
    }
    public QMaster(Path<? extends Master> path) {
        super(path.getType(), path.getMetadata(), "null", "master");
        addMetadata();
    }
    public QMaster(PathMetadata metadata) {
        super(Master.class, metadata, "null", "master");
        addMetadata();
    }
    public void addMetadata() {
        addMetadata(masterId, ColumnMetadata.named("master_id").withIndex(1).ofType(Types.INTEGER).withSize(10).notNull());
        addMetadata(name, ColumnMetadata.named("name").withIndex(2).ofType(Types.VARCHAR).withSize(45).notNull());
    }

Another problem is that schemaPatterndoes not work for mysql

<configuration>
    <schemaPattern>master,client_1</schemaPattern>
    <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
    <jdbcUrl>jdbc:mysql://127.0.0.1:3306?useSSL=false</jdbcUrl>
    <jdbcUser>root</jdbcUser>
    <jdbcPassword>root123</jdbcPassword>
    <packageName>com.github.natros.querydsl.entities</packageName>
    <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
    <schemaToPackage>true</schemaToPackage>
    <exportBeans>true</exportBeans>
</configuration>

I'm testing with

  • querydsl-sql 4.1.1
  • mysql 5.7.12
  • mysql-connector-java 5.1.39

@natros
Copy link
Contributor Author

natros commented Jul 3, 2016

Hello,

Is there a way to make schemaToPackage working with mysql?

ntantri pushed a commit to ntantri/querydsl that referenced this issue Jul 14, 2017
This is for the case where each customer has its own database with the same strucure using a single connection.

Fixes querydsl#1877
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants