use of com.querydsl.sql.Configuration in project querydsl by querydsl.
the class NativeSQLSerializerTest method path_column2.
@Test
public void path_column2() {
PathBuilder<Entity> entity = new PathBuilder<Entity>(Entity.class, "entity");
Configuration conf = new Configuration(new MySQLTemplates());
NativeSQLSerializer serializer = new NativeSQLSerializer(conf, true);
serializer.handle(entity.get("firstName"));
assertEquals("entity.first_name", serializer.toString());
}
use of com.querydsl.sql.Configuration in project querydsl by querydsl.
the class AbstractMetaDataExportMojo method execute.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (isForTest()) {
project.addTestCompileSourceRoot(targetFolder);
} else {
project.addCompileSourceRoot(targetFolder);
}
if (skip) {
return;
}
try {
Configuration configuration = new Configuration(SQLTemplates.DEFAULT);
NamingStrategy namingStrategy;
if (namingStrategyClass != null) {
namingStrategy = (NamingStrategy) Class.forName(namingStrategyClass).newInstance();
} else {
namingStrategy = new DefaultNamingStrategy();
}
// defaults for Scala
if (createScalaSources) {
if (serializerClass == null) {
serializerClass = "com.querydsl.scala.sql.ScalaMetaDataSerializer";
}
if (exportBeans && beanSerializerClass == null) {
beanSerializerClass = "com.querydsl.scala.ScalaBeanSerializer";
}
}
MetaDataExporter exporter = new MetaDataExporter();
exporter.setNamePrefix(emptyIfSetToBlank(namePrefix));
exporter.setNameSuffix(Strings.nullToEmpty(nameSuffix));
exporter.setBeanPrefix(Strings.nullToEmpty(beanPrefix));
exporter.setBeanSuffix(Strings.nullToEmpty(beanSuffix));
if (beansTargetFolder != null) {
exporter.setBeansTargetFolder(new File(beansTargetFolder));
}
exporter.setCreateScalaSources(createScalaSources);
exporter.setPackageName(packageName);
exporter.setBeanPackageName(beanPackageName);
exporter.setInnerClassesForKeys(innerClassesForKeys);
exporter.setTargetFolder(new File(targetFolder));
exporter.setNamingStrategy(namingStrategy);
exporter.setSchemaPattern(schemaPattern);
exporter.setTableNamePattern(tableNamePattern);
exporter.setColumnAnnotations(columnAnnotations);
exporter.setValidationAnnotations(validationAnnotations);
exporter.setSchemaToPackage(schemaToPackage);
exporter.setLowerCase(lowerCase);
exporter.setExportTables(exportTables);
exporter.setExportViews(exportViews);
exporter.setExportAll(exportAll);
exporter.setTableTypesToExport(tableTypesToExport);
exporter.setExportPrimaryKeys(exportPrimaryKeys);
exporter.setExportForeignKeys(exportForeignKeys);
exporter.setExportDirectForeignKeys(exportDirectForeignKeys);
exporter.setExportInverseForeignKeys(exportInverseForeignKeys);
exporter.setSpatial(spatial);
if (imports != null && imports.length > 0) {
exporter.setImports(imports);
}
if (serializerClass != null) {
try {
exporter.setSerializerClass((Class) Class.forName(serializerClass));
} catch (ClassNotFoundException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
}
}
if (exportBeans) {
if (beanSerializerClass != null) {
exporter.setBeanSerializerClass((Class) Class.forName(beanSerializerClass));
} else {
BeanSerializer serializer = new BeanSerializer();
if (beanInterfaces != null) {
for (String iface : beanInterfaces) {
int sepIndex = iface.lastIndexOf('.');
if (sepIndex < 0) {
serializer.addInterface(new SimpleType(iface));
} else {
String packageName = iface.substring(0, sepIndex);
String simpleName = iface.substring(sepIndex + 1);
serializer.addInterface(new SimpleType(iface, packageName, simpleName));
}
}
}
serializer.setAddFullConstructor(beanAddFullConstructor);
serializer.setAddToString(beanAddToString);
serializer.setPrintSupertype(beanPrintSupertype);
exporter.setBeanSerializer(serializer);
}
}
String sourceEncoding = (String) project.getProperties().get("project.build.sourceEncoding");
if (sourceEncoding != null) {
exporter.setSourceEncoding(sourceEncoding);
}
if (customTypes != null) {
for (String cl : customTypes) {
configuration.register((Type<?>) Class.forName(cl).newInstance());
}
}
if (typeMappings != null) {
for (TypeMapping mapping : typeMappings) {
mapping.apply(configuration);
}
}
if (numericMappings != null) {
for (NumericMapping mapping : numericMappings) {
mapping.apply(configuration);
}
}
if (renameMappings != null) {
for (RenameMapping mapping : renameMappings) {
mapping.apply(configuration);
}
}
if (columnComparatorClass != null) {
try {
exporter.setColumnComparatorClass((Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class));
} catch (ClassNotFoundException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
}
}
exporter.setConfiguration(configuration);
Class.forName(jdbcDriver);
String user;
String password;
if (server == null) {
user = jdbcUser;
password = jdbcPassword;
} else {
AuthenticationInfo info = wagonManager.getAuthenticationInfo(server);
if (info == null) {
throw new MojoExecutionException("No authentication info for server " + server);
}
user = info.getUserName();
if (user == null) {
throw new MojoExecutionException("Missing username from server " + server);
}
password = info.getPassword();
if (password == null) {
throw new MojoExecutionException("Missing password from server " + server);
}
}
Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
try {
exporter.export(conn.getMetaData());
} finally {
if (conn != null) {
conn.close();
}
}
} catch (ClassNotFoundException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (SQLException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (InstantiationException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
use of com.querydsl.sql.Configuration in project querydsl by querydsl.
the class AntMetaDataExporter method execute.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void execute() {
if (targetFolder == null) {
throw new BuildException("targetFolder is a mandatory property");
}
Connection dbConn = null;
try {
Class.forName(jdbcDriver).newInstance();
dbConn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
Configuration configuration = new Configuration(SQLTemplates.DEFAULT);
MetaDataExporter exporter = new MetaDataExporter();
if (namePrefix != null) {
exporter.setNamePrefix(namePrefix);
}
if (nameSuffix != null) {
exporter.setNameSuffix(nameSuffix);
}
if (beanPrefix != null) {
exporter.setBeanPrefix(beanPrefix);
}
if (beanSuffix != null) {
exporter.setBeanSuffix(beanSuffix);
}
if (beansTargetFolder != null) {
exporter.setBeansTargetFolder(new File(beansTargetFolder));
}
exporter.setPackageName(packageName);
exporter.setBeanPackageName(beanPackageName);
exporter.setTargetFolder(new File(targetFolder));
exporter.setNamingStrategy((NamingStrategy) Class.forName(namingStrategyClass).newInstance());
exporter.setInnerClassesForKeys(innerClassesForKeys);
exporter.setSchemaPattern(schemaPattern);
exporter.setTableNamePattern(tableNamePattern);
exporter.setColumnAnnotations(columnAnnotations);
exporter.setValidationAnnotations(validationAnnotations);
exporter.setSchemaToPackage(schemaToPackage);
exporter.setLowerCase(lowerCase);
exporter.setExportTables(exportTables);
exporter.setExportViews(exportViews);
exporter.setExportAll(exportAll);
exporter.setTableTypesToExport(tableTypesToExport);
exporter.setExportPrimaryKeys(exportPrimaryKeys);
exporter.setExportForeignKeys(exportForeignKeys);
exporter.setExportDirectForeignKeys(exportDirectForeignKeys);
exporter.setExportInverseForeignKeys(exportInverseForeignKeys);
exporter.setSpatial(spatial);
if (imports != null && imports.length > 0) {
exporter.setImports(imports);
}
if (exportBeans) {
BeanSerializer serializer = (BeanSerializer) Class.forName(beanSerializerClass).newInstance();
if (beanInterfaces != null) {
for (String iface : beanInterfaces) {
int sepIndex = iface.lastIndexOf('.');
if (sepIndex < 0) {
serializer.addInterface(new SimpleType(iface));
} else {
String packageName = iface.substring(0, sepIndex);
String simpleName = iface.substring(sepIndex + 1);
serializer.addInterface(new SimpleType(iface, packageName, simpleName));
}
}
}
serializer.setAddFullConstructor(beanAddFullConstructor);
serializer.setAddToString(beanAddToString);
serializer.setPrintSupertype(beanPrintSupertype);
exporter.setBeanSerializer(serializer);
}
if (sourceEncoding != null) {
exporter.setSourceEncoding(sourceEncoding);
}
if (customTypes != null) {
for (String cl : customTypes) {
configuration.register((Type<?>) Class.forName(cl).newInstance());
}
}
if (typeMappings != null) {
for (TypeMapping mapping : typeMappings) {
mapping.apply(configuration);
}
}
if (numericMappings != null) {
for (NumericMapping mapping : numericMappings) {
mapping.apply(configuration);
}
}
if (renameMappings != null) {
for (RenameMapping mapping : renameMappings) {
mapping.apply(configuration);
}
}
if (columnComparatorClass != null) {
exporter.setColumnComparatorClass((Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class));
}
exporter.setConfiguration(configuration);
exporter.export(dbConn.getMetaData());
} catch (RuntimeException e) {
throw new BuildException(e);
} catch (InstantiationException e) {
throw new BuildException(e);
} catch (IllegalAccessException e) {
throw new BuildException(e);
} catch (ClassNotFoundException e) {
throw new BuildException(e);
} catch (SQLException e) {
throw new BuildException(e);
} finally {
if (dbConn != null) {
try {
dbConn.close();
} catch (SQLException e2) {
throw new BuildException(e2);
}
}
}
}
use of com.querydsl.sql.Configuration in project querydsl by querydsl.
the class SetQueryBandClauseTest method setUp.
@Before
public void setUp() {
conf = new Configuration(SQLTemplates.DEFAULT);
conf.setUseLiterals(true);
clause = new SetQueryBandClause((Connection) null, conf);
}
use of com.querydsl.sql.Configuration in project querydsl by querydsl.
the class MetaDataSerializerTest method customized_serialization.
@Test
public void customized_serialization() throws SQLException {
String namePrefix = "Q";
Configuration conf = new Configuration(SQLTemplates.DEFAULT);
conf.register("EMPLOYEE", "ID", new AbstractType<CustomNumber>(0) {
@Override
public Class<CustomNumber> getReturnedClass() {
return CustomNumber.class;
}
@Override
public CustomNumber getValue(ResultSet rs, int startIndex) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setValue(PreparedStatement st, int startIndex, CustomNumber value) throws SQLException {
throw new UnsupportedOperationException();
}
});
NamingStrategy namingStrategy = new DefaultNamingStrategy();
// customization of serialization
MetaDataExporter exporter = new MetaDataExporter();
exporter.setBeanSerializerClass(BeanSerializer.class);
exporter.setNamePrefix(namePrefix);
exporter.setPackageName("test");
exporter.setTargetFolder(folder.getRoot());
exporter.setNamingStrategy(namingStrategy);
exporter.setConfiguration(conf);
exporter.export(connection.getMetaData());
compile(exporter);
// validation of output
try {
//
assertMethodsPresent("test/QSurvey.java", // variable + schema constructor
" public QSurvey(String variable, String schema) {\n" + " super(Survey.class, forVariable(variable), schema, \"SURVEY\");\n" + " addMetadata();\n" + " }");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Aggregations