我是Liquibase的新手,并已成功为给定的更改日志生成了ddl脚本.
我将更改集用作xml,并使用maven目标liquibase:updateSQl生成了ddl脚本.我还使用liquibase.properies指定了url,driver,dialect等
这对我来说很好用,我使用了liquibase version 3.5.5.
我试图使用添加了liquibase 3.5.5作为我的maven依赖项的Java代码执行相同的操作.但是我无法使用Java代码实现相同的目的.有人可以照亮我的路吗?
如何通过Java执行updateSQL?
解决方案我在liquibase Main类中进行了一些研究,并提出了解决方案. 对于下面的代码,input是databaseChangeLog,而output是ddl脚本刷新.
public class DDLScriptGenerator { protected ClassLoader classLoader; protected String driver; protected String username; protected String password; protected String url; protected String databaseClass; protected String defaultSchemaName; protected String outputDefaultSchema; protected String outputDefaultCatalog; protected String liquibaseCatalogName; protected String liquibaseSchemaName; protected String databaseChangeLogTableName; protected String databaseChangeLogLockTableName; protected String defaultCatalogName; protected String changeLogFile; protected String classpath; protected String contexts; protected String labels; protected String driverPropertiesFile; protected String propertyProviderClass = null; protected Boolean promptForNonLocalDatabase = null; protected Boolean includeSystemClasspath; protected Boolean strict = Boolean.TRUE; protected String defaultsFile = "liquibase.properties"; protected String diffTypes; protected String changeSetAuthor; protected String changeSetContext; protected String dataOutputDirectory; protected String referenceDriver; protected String referenceUrl; protected String referenceUsername; protected String referencePassword; protected String referenceDefaultCatalogName; protected String referenceDefaultSchemaName; protected String currentDateTimeFunction; protected String command; protected Set<String> commandParams = new LinkedHashSet<String>(); protected String logLevel; protected String logFile; protected Map<String, Object> changeLogParameters = new HashMap<String, Object>(); protected String outputFile; /** * @param d * @throws DatabaseException * @throws LiquibaseException * @throws UnsupportedEncodingException * @throws IOException */ public void toSQL(DatabaseChangeLog d,String url,String user,String password) throws DatabaseException, LiquibaseException, UnsupportedEncodingException, IOException { this.url=url; this.username=user; this.password=password; this.driver=""; //your driver this.outputFile=""; // The path in which the script have to be flushed. FileSystemResourceAccessor fsOpener = new FileSystemResourceAccessor(); CommandLineResourceAccessor clOpener = new CommandLineResourceAccessor(this.getClass().getClassLoader()); CompositeResourceAccessor fileOpener = new CompositeResourceAccessor(new ResourceAccessor[] { fsOpener, clOpener }); Database database = CommandLineUtils.createDatabaseObject(fileOpener, this.url, this.username, this.password, this.driver, this.defaultCatalogName, this.defaultSchemaName, Boolean.parseBoolean(this.outputDefaultCatalog), Boolean.parseBoolean(this.outputDefaultSchema), this.databaseClass, this.driverPropertiesFile, this.propertyProviderClass, this.liquibaseCatalogName, this.liquibaseSchemaName, this.databaseChangeLogTableName, this.databaseChangeLogLockTableName); Liquibase liquibase=new Liquibase(d, null, database); liquibase.update(new Contexts(this.contexts), new LabelExpression(this.labels), getOutputWriter()); } private Writer getOutputWriter() throws UnsupportedEncodingException, IOException { String charsetName = ((GlobalConfiguration)LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class)).getOutputEncoding(); if (this.outputFile != null) { try { FileOutputStream fileOut = new FileOutputStream(this.outputFile, false); return new OutputStreamWriter(fileOut, charsetName); } catch (IOException e) { System.err.printf("Could not create output file %s\n", new Object[] { this.outputFile }); throw e; } } return new OutputStreamWriter(System.out, charsetName); } }I am new to Liquibase and have successfully generated the ddl scripts for the given change log.
I used the change set as an xml and generated the ddl scripts using the maven goal liquibase:updateSQl.I also used the liquibase.properies for specifying the url,driver,dialect etc
This worked fine for me and I used the liquibase version 3.5.5.
I was trying to do the same using the java code with the liquibase 3.5.5 added as my maven dependency. But I can't achieve the same using java code. Can someone put light in my path ?
How can I do updateSQL through java?
解决方案I did a little research in the liquibase Main class and I came up with a solution . For the below code input is databaseChangeLog and output is ddl script flush.
public class DDLScriptGenerator { protected ClassLoader classLoader; protected String driver; protected String username; protected String password; protected String url; protected String databaseClass; protected String defaultSchemaName; protected String outputDefaultSchema; protected String outputDefaultCatalog; protected String liquibaseCatalogName; protected String liquibaseSchemaName; protected String databaseChangeLogTableName; protected String databaseChangeLogLockTableName; protected String defaultCatalogName; protected String changeLogFile; protected String classpath; protected String contexts; protected String labels; protected String driverPropertiesFile; protected String propertyProviderClass = null; protected Boolean promptForNonLocalDatabase = null; protected Boolean includeSystemClasspath; protected Boolean strict = Boolean.TRUE; protected String defaultsFile = "liquibase.properties"; protected String diffTypes; protected String changeSetAuthor; protected String changeSetContext; protected String dataOutputDirectory; protected String referenceDriver; protected String referenceUrl; protected String referenceUsername; protected String referencePassword; protected String referenceDefaultCatalogName; protected String referenceDefaultSchemaName; protected String currentDateTimeFunction; protected String command; protected Set<String> commandParams = new LinkedHashSet<String>(); protected String logLevel; protected String logFile; protected Map<String, Object> changeLogParameters = new HashMap<String, Object>(); protected String outputFile; /** * @param d * @throws DatabaseException * @throws LiquibaseException * @throws UnsupportedEncodingException * @throws IOException */ public void toSQL(DatabaseChangeLog d,String url,String user,String password) throws DatabaseException, LiquibaseException, UnsupportedEncodingException, IOException { this.url=url; this.username=user; this.password=password; this.driver=""; //your driver this.outputFile=""; // The path in which the script have to be flushed. FileSystemResourceAccessor fsOpener = new FileSystemResourceAccessor(); CommandLineResourceAccessor clOpener = new CommandLineResourceAccessor(this.getClass().getClassLoader()); CompositeResourceAccessor fileOpener = new CompositeResourceAccessor(new ResourceAccessor[] { fsOpener, clOpener }); Database database = CommandLineUtils.createDatabaseObject(fileOpener, this.url, this.username, this.password, this.driver, this.defaultCatalogName, this.defaultSchemaName, Boolean.parseBoolean(this.outputDefaultCatalog), Boolean.parseBoolean(this.outputDefaultSchema), this.databaseClass, this.driverPropertiesFile, this.propertyProviderClass, this.liquibaseCatalogName, this.liquibaseSchemaName, this.databaseChangeLogTableName, this.databaseChangeLogLockTableName); Liquibase liquibase=new Liquibase(d, null, database); liquibase.update(new Contexts(this.contexts), new LabelExpression(this.labels), getOutputWriter()); } private Writer getOutputWriter() throws UnsupportedEncodingException, IOException { String charsetName = ((GlobalConfiguration)LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class)).getOutputEncoding(); if (this.outputFile != null) { try { FileOutputStream fileOut = new FileOutputStream(this.outputFile, false); return new OutputStreamWriter(fileOut, charsetName); } catch (IOException e) { System.err.printf("Could not create output file %s\n", new Object[] { this.outputFile }); throw e; } } return new OutputStreamWriter(System.out, charsetName); } }
更多推荐
如何在Java中以编程方式使用Liquibase?
发布评论