1

我想注释 SQL 脚本的所有行,除了那些包含字符串 TABLE1 (区分大小写)和 - 如果可能的话 - 只使用一个 ant 任务(replaceregexp?)。理想情况下,注释(以“--”开头)和空行应该被忽略,但如果不是,这不是那么重要。

例如:初始文件

CREATE TABLE TEST (TEST_ID VARCHAR(255) NOT NULL, TEST_NAME VARCHAR(255));
CREATE TABLE LICENSE (ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY, PRIMARY KEY (ID));

delete from TEST2 where
    ID = 'whatever'
;

delete from TEST3 where
    ENV = 'whatelse'
;

UPDATE TEST1 SET VERSION = '1.0';

最终文件

-- CREATE TABLE TEST (TEST_ID VARCHAR(255) NOT NULL, TEST_NAME VARCHAR(255));
-- CREATE TABLE LICENSE (ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY, PRIMARY KEY (ID));

-- delete from TEST2 where
--  ID = 'whatever'
-- ;

-- delete from TEST3 where
--  ENV = 'whatelse'
-- ;

UPDATE TEST1 SET VERSION = '1.0';

我找到的唯一解决方案是: 1. 注释掉脚本的所有行,然后取消注释与字符串 TEST1 匹配的行:

    <replaceregexp file="${sql.file}"
        match="(.*)"
        replace="-- \1"
        byline="true"
    />
    <replaceregexp file="${sql.file}"
        match="^-- (.*TEST1)"
        replace="\1"
        byline="true"
    />
  1. 创建一个仅包含我要保留的行的新文件:

    <copy file="${sql.file}" tofile="${sql.file.bak}">
      <filterchain>
        <linecontains>
          <contains value="TEST1"/>
        </linecontains>
      </filterchain>
    </copy>
    

我对这两种解决方案都不满意:解决方案#1。使用 2 个任务并两次更新同一个文件 解决方案 #2。删除我们希望保留为注释的其他行

如果有人有正确的答案,我会接受。

谢谢,萨布丽娜

4

1 回答 1

1

一种解决方案可能是

<replaceregexp file="${sql.file}"
     match="(^(?!$))(?!.*TEST1)"
     replace="-- \1"
     byline="true" />

关键?!是“负前瞻断言”——它表示当括号中的字符串未找到时,找到了?!匹配

下面是对模式的解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      $                        before an optional \n, and the end of
                               the string
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    TEST1                    'TEST1'
--------------------------------------------------------------------------------
  )                        end of look-ahead
于 2013-11-15T00:14:05.210 回答