1

我正在尝试将 XML 文件转换为 Eclipse 中的 SQL 语句,以节省转换大文件的手动工作。

一行如:

<TABLE_NAME COL_1="value1" COL_2="value2"/>

应转换为:

insert into TABLE_NAME (COL_1, COL_2) values ("value1", "value2");

到目前为止,我已经成功地匹配并捕获了表名和第一列/值对:

<(\w+)( \w+)=(".+?").*/>

.*近端只是用来测试模式的第一部分,一旦完成就应该移除。

以下替换模式产生以下结果:

insert into $1 ($2) values ($3);
insert into TABLE_NAME ( COL_1) values ("value1");

我遇到的问题是不同表的列数不同,所以我想要一个匹配n列/值对并在替换模式中重复使用捕获的组的通用模式。\G尽管似乎是一个不错的候选人,但我还没有设法理解如何做到这一点。

理想情况下,这将在单个正则表达式语句中解决,尽管我也不会反对必须按顺序运行的多个语句(尽管我真的不想强迫开发人员为每一列执行一次/值对)。

有人有想法么?

4

1 回答 1

0

I did not in the end solve this with regular expressions as originally envisaged. Taking alternative solutions in mind, it was solved in the end by re-factoring test code to make it re-usable and then co-opting dbunit to use tests to write data to the different stages of the development databases.

I know that this is a misuse of tests, but it does make the whole thing much simpler since the user gets a green/red feedback as to whether the data is inserted or not and the same test data can be inserted for manual acceptance tests as is used for the automated component/integration tests (which used dbunit with an in-memory database). As long as these fake test classes are not added to the test suite, then they are only executed by hand.

The resulting fake tests ended up being extremely simple and lightweight, for instance:

public class MyComponentTestDataInserter extends DataInserter {
  @Override
  protected String getDeleteSql() {
    return "./input/component/delete.sql";
  }

  @Override
  protected String getInsertXml() {
    return "./input/component/TestData.xml";
  }

  @Test
  public void insertIntoDevStage() {
    insertData(Environment.DEV);
  }

  @Test
  public void insertIntoTestStage() {
    insertData(Environment.TEST);
  }
}

This also had the additional advantage that a developer can insert the data in to a single environment by simply executing a single test method via the context menu in the IDE. At the same time, by running the whole test class, the exact same data can be deployed to all environments simultaneously.

Also, a failure at any point during data cleanup or insertion causes the whole transaction to be rolled back, preventing an inconsistent state.

于 2012-06-19T07:17:40.263 回答