Reputation: 11
I would like to comment all lines of a SQL script except those containing the string TABLE1 (case sensitive) and - if possible - using only a single ant task (replaceregexp?). Ideally, the comment (starting with "--") and blank lines should be ignored but if not this is not so important.
Ex: Initial file
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';
Final file
-- 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';
The only solutions I have found are those ones: 1. Comment out all lines of the script and then uncomment the lines that matches the string TEST1:
<replaceregexp file="${sql.file}"
match="(.*)"
replace="-- \1"
byline="true"
/>
<replaceregexp file="${sql.file}"
match="^-- (.*TEST1)"
replace="\1"
byline="true"
/>
Create a new file that only contains the line I want to keep:
<copy file="${sql.file}" tofile="${sql.file.bak}">
<filterchain>
<linecontains>
<contains value="TEST1"/>
</linecontains>
</filterchain>
</copy>
I am not so happy with both solutions as: Solution #1. uses 2 tasks and updates the same file twice Solution #2. removes the other lines we would like to keep as comment
If anybody has the right answer I take it.
Thanks, Sabrina
Upvotes: 1
Views: 872
Reputation: 78155
One solution might be
<replaceregexp file="${sql.file}"
match="(^(?!$))(?!.*TEST1)"
replace="-- \1"
byline="true" />
The key to this is that ?!
is the "negative look-ahead assertion" - it says a match is found when the string in brackets after the ?!
is not found.
Here's an explanation of the pattern:
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
Upvotes: 1