如何在Java中以编程方式使用Liquibase?

编程入门 行业动态 更新时间:2024-10-24 10:26:05
本文介绍了如何在Java中以编程方式使用Liquibase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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?

本文发布于:2023-11-28 13:20:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   方式   如何在   Liquibase   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!