Skip to content

Modify table scripts #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.googlecode</groupId>
<artifactId>scheme2ddl</artifactId>
<version>2.4.3-SNAPSHOT</version>
<version>2.4.2</version>
<packaging>jar</packaging>

<parent>
Expand All @@ -19,7 +19,7 @@
<connection>scm:git:[email protected]:qwazer/scheme2ddl.git</connection>
<url>scm:git:[email protected]:qwazer/scheme2ddl.git</url>
<developerConnection>scm:git:[email protected]:qwazer/scheme2ddl.git</developerConnection>
<tag>HEAD</tag>
<tag>v2.4.2</tag>
</scm>


Expand Down Expand Up @@ -76,10 +76,7 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down Expand Up @@ -169,11 +166,16 @@
mvn install:install-file -Dfile=ojdbc5.jar -DgroupId=com.oracle -DartifactId=ojdbc5 -Dversion=11.2.0.3.0 -Dpackaging=jar -DgeneratePom=true
-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc5</artifactId>
<version>11.2.0.3.0</version>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>

<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>orai18n</artifactId>
<version>19.3.0.0</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
Expand All @@ -190,17 +192,6 @@
<id>release</id>
<build>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Expand Down
205 changes: 129 additions & 76 deletions src/main/java/com/googlecode/scheme2ddl/DDLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,91 +12,144 @@
*/
public class DDLFormatter {

private boolean noFormat;
private boolean sortCreateIndexStatements = true;
private boolean statementOnNewLine;
private boolean isMorePrettyFormat = false;

static String newline = "\r\n"; //may be use "\n"

public String formatDDL(String ddl) {
if (noFormat)
return ddl;

ddl = ddl.trim() + "\n";

// replace unix line endings with windows
ddl = ddl.replaceAll("\r\n", "\n");
ddl = ddl.replaceAll("\n", "\r\n");

if (!isMorePrettyFormat)
return ddl;

/* smart formatting */
ddl = ddl.replaceAll(newline + "GRANT ", newline + newline + " GRANT ");
ddl = ddl.replaceAll(newline + "COMMENT ", newline + newline + " COMMENT ");
ddl = ddl.replaceAll(newline + " CREATE ", newline + "CREATE ");
ddl = ddl.replaceAll(newline + " ALTER ", newline + "ALTER ");
return ddl;
}
static String newline = "\r\n"; //may be use "\n"
private boolean noFormat;
private boolean sortCreateIndexStatements = true;
private boolean statementOnNewLine;
private boolean isMorePrettyFormat = true;

public void setNoFormat(Boolean noFormat) {
this.noFormat = noFormat;
public String formatDDL(String ddl) {
if (noFormat) {
return ddl;
}

@Deprecated
public void setStatementOnNewLine(Boolean statementOnNewLine) {
this.statementOnNewLine = statementOnNewLine;
}
ddl = ddl.trim() + "\n";

// replace unix line endings with windows
ddl = ddl.replaceAll("\r\n", "\n");
ddl = ddl.replaceAll("\n", "\r\n");

public void setIsMorePrettyFormat(boolean isMorePrettyFormat) {
this.isMorePrettyFormat = isMorePrettyFormat;
if (!isMorePrettyFormat) {
return ddl;
}

public void setSortCreateIndexStatements(boolean sortCreateIndexStatements) {
this.sortCreateIndexStatements = sortCreateIndexStatements;
// replace "
ddl = ddl.replaceAll("\"", "");

// replace owner
ddl = ddl.replaceAll(Main.schemaList.get(0).toUpperCase() + "\\.", "");

/* smart formatting */
// ddl = ddl.replaceAll(newline + "GRANT ", newline + newline + "GRANT ");
// ddl = ddl.replaceAll(newline + "COMMENT ", newline + newline + "COMMENT ");
ddl = ddl.replaceAll(newline + " CREATE ", newline + "CREATE ");
ddl = ddl.replaceAll(newline + " ALTER ", newline + "ALTER ");

// replace tab
ddl = ddl.replaceAll("\\t", " ");

return ddl;
}

public String formatTableDDL(String ddl) {
// for table
ddl = ddl.replaceAll(newline + "\\s*\\(", newline + "(");
ddl = ddl.replaceAll(newline + "\\s*\\)", newline + ")");
ddl = ddl.replaceAll(" ENABLE,", ",");
ddl = ddl.replaceAll(",0\\)", ")");
ddl = ddl.replaceAll(newline + "\\s*COMMENT ", newline + "COMMENT ");
// ddl = ddl.replaceAll(newline + "" + newline + "\\s*;", newline + "COMMENT ");

String output;

// find "alter table CONSTRAINT"
Pattern p = Pattern.compile(newline + "ALTER TABLE (.*)");
Matcher m = p.matcher(ddl);
if (m.find()) {
output = m.replaceFirst("");
output = output.replaceAll(newline + "\\s*USING .*", "");
output = output + newline + "alter table " + m.group(1) + " using index;";
ddl = output;
}

public String replaceActualSequenceValueWithOne(String res) {

String output;
Pattern p = Pattern.compile("CREATE SEQUENCE (.*) START WITH (\\d+) (.*)");
Matcher m = p.matcher(res);
if (m.find()) {
output = m.replaceFirst("CREATE SEQUENCE " + m.group(1) + " START WITH 1 " + m.group(3) );
if (!"1".equals(m.group(2)))
output = output + newline + "/* -- actual sequence value was replaced by scheme2ddl to 1 */";
}
else {
output = res;
}
return output;
// find "alter table CONSTRAINT"
do {
p = Pattern.compile(newline + "CREATE (.*)INDEX (.*)" + newline + "\\s*;");
m = p.matcher(ddl);

if (m.find()) {
output = m.replaceFirst(newline + "CREATE " + m.group(1) + "INDEX " + m.group(2).trim() + ";");
ddl = output;
} else {
break;
}
} while (true);

// remove SUPPLEMENTAL LOG
ddl = ddl.replaceAll(newline + ".*SUPPLEMENTAL LOG.*", "");

return ddl;
}

public void setNoFormat(Boolean noFormat) {
this.noFormat = noFormat;
}

@Deprecated
public void setStatementOnNewLine(Boolean statementOnNewLine) {
this.statementOnNewLine = statementOnNewLine;
}

public void setIsMorePrettyFormat(boolean isMorePrettyFormat) {
this.isMorePrettyFormat = isMorePrettyFormat;
}

public void setSortCreateIndexStatements(boolean sortCreateIndexStatements) {
this.sortCreateIndexStatements = sortCreateIndexStatements;
}

public String replaceActualSequenceValueWithOne(String res) {

String output;
Pattern p = Pattern.compile("CREATE SEQUENCE (.*) MINVALUE 1 .*");
// Pattern p = Pattern.compile("CREATE SEQUENCE (.*) MINVALUE 1 START WITH (\\d+) (.*)");
Matcher m = p.matcher(res);
if (m.find()) {
output = m.replaceFirst("CREATE SEQUENCE " + m.group(1) + ";");
// output = m.replaceFirst("CREATE SEQUENCE " + m.group(1) + " START WITH 1 " + m.group(3));
// if (!"1".equals(m.group(2)))
// output = output + newline + "/* -- actual sequence value was replaced by scheme2ddl to 1 */";
} else {
output = res;
}
return output;
}

/**
* Read input string with list of 'create index' statements and, tokenize and sort alphabetically.
* @param dependentDLL -string with list of 'create index' statements
* @return string with sorted alphabetically 'create index' statements
*/
public String sortIndexesInDDL(String dependentDLL) {
if (noFormat || !sortCreateIndexStatements){
return dependentDLL;
}
String[] parts = dependentDLL.split("(?=CREATE INDEX)|(?=CREATE UNIQUE INDEX)|(?=CREATE BITMAP INDEX)");
List<String> list = new ArrayList<String>();
for (String part : parts) {
if (part != null && !part.trim().isEmpty()) {
list.add(part.trim());
}
}
Collections.sort(list);
StringBuilder s = new StringBuilder();
String prefix = "\n "; //to preserve formatting of dbms_metadata.get_depended_ddl output
for (String statement:list){
s.append(prefix);
prefix = "\n";
s.append(statement);
}
return s.toString();
/**
* Read input string with list of 'create index' statements and, tokenize and sort alphabetically.
*
* @param dependentDLL -string with list of 'create index' statements
* @return string with sorted alphabetically 'create index' statements
*/
public String sortIndexesInDDL(String dependentDLL) {
if (noFormat || !sortCreateIndexStatements) {
return dependentDLL;
}
String[] parts = dependentDLL.split("(?=CREATE INDEX)|(?=CREATE UNIQUE INDEX)|(?=CREATE BITMAP INDEX)");
List<String> list = new ArrayList<String>();
for (String part : parts) {
if (part != null && !part.trim().isEmpty()) {
list.add(part.trim());
}
}
Collections.sort(list);
StringBuilder s = new StringBuilder();
String prefix = "\n "; //to preserve formatting of dbms_metadata.get_depended_ddl output
for (String statement : list) {
s.append(prefix);
prefix = "\n";
s.append(statement);
}
return s.toString();
}
}
Loading