Code Generator: Ability to generate a Java package per database schema

Bug #704933 reported by cowwoc
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Querydsl
Invalid
Undecided
Unassigned

Bug Description

I'd like the code generator to translate schema names to package names. Take for example the H2 database that contains two schema names: INFORMATION_SCHEMA and PUBLIC. If "packageName" is set to "foo.queries" then I expect the code generator to generate "foo.queries.information_schema" and "foo.queries.public".

I fear the current strategy of generating all classes into the same package will lead to name conflicts.

Revision history for this message
Timo Westkämper (timo-westkamper) wrote :

I am afraid this would become too complex, since the old use cases should be supported as well. The supported way to avoid name clashes is to use schemaPattern.

If you need contents of two schemas you need two schema generations, which is also not an issue.

Revision history for this message
cowwoc (gili) wrote :

Would it be possible to add a configuration flag that defaults to false?

summary: - Code generator should convert schema to package name
+ Code Generator: Ability to generate a Java package per database schema
Revision history for this message
rrmckinley (rrmckinley) wrote :

I consider this out of scope for the generator. It is very easy to solve your problem at the exporter level. I'll post what I use below. It uses a config file for the schema info. Run it many times for many config files. If you don't want to repeat your package name in the config file, then add a little "SCHEMA_NAME".toLowerCase().replace('_', '.') too my example.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import com.mysema.codegen.CodeWriter;
import com.mysema.query.codegen.BeanSerializer;
import com.mysema.query.codegen.EntityType;
import com.mysema.query.codegen.Property;
import com.mysema.query.codegen.SerializerConfig;
import com.mysema.query.sql.Configuration;
import com.mysema.query.sql.MetaDataExporter;
import com.mysema.query.sql.MetaDataSerializer;
import com.mysema.query.sql.NamingStrategy;

public class SqlExporter {
 Connection conn; // connection of database containing the schema to use
 MetaDataExporter exporter;

 public static void main(String args[]) throws Exception {
  SqlExporter e = new SqlExporter(args[0]);

  e.exporter.export(e.conn.getMetaData());
  System.out.println("Completed");
 }

 public SqlExporter(String conf) throws Exception {
  Properties p = new Properties();
  p.load(new FileInputStream(conf));

  String jdbcClass = p.getProperty("jdbcClass");
  String jdbcURL = p.getProperty("jdbcURL");
  String jdbcUser = p.getProperty("jdbcUser");
  String jdbcPwd = p.getProperty("jdbcPwd");

  System.out.println("Connecting to Database " + jdbcURL + " User "
    + jdbcUser);
  Class.forName(jdbcClass).newInstance();
  conn = DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPwd);
  System.out.println("Connected successfully");

  String namePrefix = p.getProperty("namePrefix");
  String packageName = p.getProperty("packageName");
  String schemaPattern = p.getProperty("schemaPattern");
  String tableNamePattern = p.getProperty("tableNamePattern");
  File targetFolder = new File(p.getProperty("targetFolder"));
  String namingStrategyName = p.getProperty("namingStrategy");

  String databaseName = p.getProperty("databaseName");

  NamingStrategy namingStrategy = (NamingStrategy) Class
    .forName(namingStrategyName).newInstance();

  MetaDataSerializer serializer = new MetaDataSerializer(namePrefix, namingStrategy);
  BeanSerializer beanSerializer = new BeanSerializer();

  exporter = new MetaDataExporter();
  exporter.setSerializer(serializer);
  exporter.setBeanSerializer(beanSerializer);
  exporter.setNamePrefix(namePrefix);
  exporter.setPackageName(packageName);
  exporter.setTargetFolder(targetFolder);
  exporter.setNamingStrategy(namingStrategy);
  exporter.setSchemaPattern(schemaPattern);
  exporter.setTableNamePattern(tableNamePattern);
 }
}

Revision history for this message
cowwoc (gili) wrote :

Works for me. Your help is much appreciated!

Revision history for this message
Timo Westkämper (timo-westkamper) wrote :

Ok, so I guess we are ok with the current state of things.

 This feature request also clashes with different packages for bean types and Q-types. Supporting both would make things quite complicated.

Changed in querydsl:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.