Skip to content

Commit 6591602

Browse files
author
qwazer
committed
scheme2ddl-28 filter-sequence-values option
1 parent b8d0b4c commit 6591602

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

src/main/java/com/googlecode/scheme2ddl/DDLFormatter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.googlecode.scheme2ddl;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
36
/**
47
* @author A_Reshetnikov
58
* @since Date: 18.10.2012
@@ -41,4 +44,21 @@ public void setStatementOnNewLine(Boolean statementOnNewLine) {
4144
public void setIsMorePrettyFormat(boolean isMorePrettyFormat) {
4245
this.isMorePrettyFormat = isMorePrettyFormat;
4346
}
47+
48+
49+
public String replaceActualSequenceValueWithOne(String res) {
50+
51+
String output;
52+
Pattern p = Pattern.compile("CREATE SEQUENCE (.*) START WITH (\\d+) (.*)");
53+
Matcher m = p.matcher(res);
54+
if (m.find()) {
55+
output = m.replaceFirst("CREATE SEQUENCE " + m.group(1) + " START WITH 1 " + m.group(3) );
56+
if (!"1".equals(m.group(2)))
57+
output = output + newline + "/* -- actual sequence value was replaced by scheme2ddl to 1 */";
58+
}
59+
else {
60+
output = res;
61+
}
62+
return output;
63+
}
4464
}

src/main/java/com/googlecode/scheme2ddl/Main.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Main {
3333
private static boolean justTestConnection = false;
3434
private static boolean skipPublicDbLinks = false;
3535
private static boolean stopOnWarning = false;
36+
private static boolean filterSequenceValues = false;
3637
private static String customConfigLocation = null;
3738
private static String defaultConfigLocation = "scheme2ddl.config.xml";
3839
private static String dbUrl = null;
@@ -120,6 +121,10 @@ private static void modifyContext(ConfigurableApplicationContext context) {
120121
UserObjectProcessor processor = (UserObjectProcessor) context.getBean("processor");
121122
processor.setStopOnWarning(stopOnWarning);
122123
}
124+
if (filterSequenceValues){
125+
UserObjectProcessor processor = (UserObjectProcessor) context.getBean("processor");
126+
processor.setFilterSequenceValues(filterSequenceValues);
127+
}
123128

124129
}
125130

@@ -297,8 +302,10 @@ private static void collectArgs(String[] args) throws Exception {
297302
i++;
298303
} else if (arg.equals("-tc") || arg.equals("--test-connection")) {
299304
justTestConnection = true;
300-
}else if (arg.equals("--stop-on-warning")) {
305+
} else if (arg.equals("--stop-on-warning")) {
301306
stopOnWarning = true;
307+
} else if ((arg.equals("-fsv") || arg.equals("--filter-sequence-values"))) {
308+
filterSequenceValues = true;
302309
} else if (arg.equals("-c") || arg.equals("--config")) {
303310
customConfigLocation = args[i + 1];
304311
i++;

src/main/java/com/googlecode/scheme2ddl/UserObjectProcessor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class UserObjectProcessor implements ItemProcessor<UserObject, UserObject
2626
private Map<String, Set<String>> excludes;
2727
private Map<String, Set<String>> dependencies;
2828
private boolean stopOnWarning;
29+
private boolean filterSequenceValues;
2930

3031
public UserObject process(UserObject userObject) throws Exception {
3132

@@ -75,6 +76,9 @@ private String map2Ddl(UserObject userObject) throws CannotGetDDLException, NonS
7576
return userObjectDao.findRefGroupDDL(userObject.getType(), userObject.getName());
7677
}
7778
String res = userObjectDao.findPrimaryDDL(map2TypeForDBMS(userObject.getType()), userObject.getName());
79+
if (userObject.getType().equals("SEQUENCE") && filterSequenceValues) {
80+
res = ddlFormatter.replaceActualSequenceValueWithOne(res);
81+
}
7882
Set<String> dependedTypes = dependencies.get(userObject.getType());
7983
if (dependedTypes != null) {
8084
for (String dependedType : dependedTypes) {
@@ -112,6 +116,10 @@ public void setFileNameConstructor(FileNameConstructor fileNameConstructor) {
112116
this.fileNameConstructor = fileNameConstructor;
113117
}
114118

119+
public void setFilterSequenceValues(boolean filterSequenceValues) {
120+
this.filterSequenceValues = filterSequenceValues;
121+
}
122+
115123
public void setStopOnWarning(boolean stopOnWarning) {
116124
this.stopOnWarning = stopOnWarning;
117125
}

src/main/resources/applicationContext.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
1010

1111
<context:annotation-config/>
12-
<aop:aspectj-autoproxy/>
12+
<aop:aspectj-autoproxy proxy-target-class="true"/>
1313

1414
<job id="job1" xmlns="http://www.springframework.org/schema/batch">
1515
<step id="step1" parent="simpleStep">
@@ -51,6 +51,7 @@
5151
<property name="excludes" ref="excludes"/>
5252
<property name="dependencies" ref="dependencies"/>
5353
<property name="stopOnWarning" value="false"/>
54+
<property name="filterSequenceValues" value="false"/>
5455
</bean>
5556

5657

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.googlecode.scheme2ddl;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* @author ar
9+
* @since Date: 18.04.2015
10+
*/
11+
public class DDLFormatterTest {
12+
13+
private DDLFormatter ddlFormatter = new DDLFormatter();
14+
15+
@Test
16+
public void testReplaceActualSequenceValueWithOne() throws Exception {
17+
18+
String s = "CREATE SEQUENCE \"TEST01\".\"SEQ_01\" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 2122 CACHE 20 NOORDER NOCYCLE ;\n";
19+
String res = ddlFormatter.replaceActualSequenceValueWithOne(s);
20+
assertEquals(
21+
"CREATE SEQUENCE \"TEST01\".\"SEQ_01\" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE ;\n"
22+
, res);
23+
24+
}
25+
26+
@Test
27+
public void testReplaceActualSequenceValueWithOneOnWrongDDL() throws Exception {
28+
29+
String s = "CREATE TABLE \"TEST01\".\"SEQ_01\" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 2122 CACHE 20 NOORDER NOCYCLE ;\n";
30+
String res = ddlFormatter.replaceActualSequenceValueWithOne(s);
31+
assertNotEquals(
32+
"CREATE TABLE \"TEST01\".\"SEQ_01\" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE ;\n"
33+
, res);
34+
35+
}
36+
}

0 commit comments

Comments
 (0)