001: /*
002: * DelimiterDefinition.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql;
013:
014: import java.util.regex.Pattern;
015: import workbench.util.StringUtil;
016:
017: /**
018: * Encapsulate the alternate delimiter
019: * @author support@sql-workbench.net
020: */
021: public class DelimiterDefinition {
022: /**
023: * The default delimiter for ANSI SQL: a semicolon
024: */
025: public static final DelimiterDefinition STANDARD_DELIMITER = new DelimiterDefinition(
026: ";", false);
027:
028: /**
029: * A default alternate delimiter. This is Oracle's slash on a single line
030: */
031: public static final DelimiterDefinition DEFAULT_ALTERNATE_DELIMITER = new DelimiterDefinition(
032: "/", true);
033:
034: /**
035: * A default alternate delimiter that matches SQL Server's GO command
036: */
037: public static final DelimiterDefinition DEFAULT_MS_DELIMITER = new DelimiterDefinition(
038: "GO", true);
039:
040: private String delimiter;
041: private boolean singleLineDelimiter;
042: private boolean changed = false;
043:
044: public DelimiterDefinition() {
045: this .delimiter = "";
046: this .singleLineDelimiter = false;
047: this .changed = false;
048: }
049:
050: public DelimiterDefinition(String delim, boolean single) {
051: setDelimiter(delim);
052: this .singleLineDelimiter = single;
053: this .changed = false;
054: }
055:
056: public DelimiterDefinition createCopy() {
057: DelimiterDefinition copy = new DelimiterDefinition(
058: this .delimiter, singleLineDelimiter);
059: copy.changed = false;
060: return copy;
061: }
062:
063: public boolean isEmpty() {
064: return (this .delimiter == null || this .delimiter.trim()
065: .length() == 0);
066: }
067:
068: public boolean isStandard() {
069: return this .delimiter.equals(";");
070: }
071:
072: public void resetChanged() {
073: this .changed = false;
074: }
075:
076: public static DelimiterDefinition parseCmdLineArgument(String arg) {
077: if (StringUtil.isEmptyString(arg))
078: return null;
079:
080: String[] elements = arg.split(";");
081:
082: // as the argument is never empty, we always have at least one element
083: String delim = elements[0];
084: boolean single = false;
085:
086: if (elements.length > 1) {
087: single = "nl".equalsIgnoreCase(elements[1]);
088: }
089: return new DelimiterDefinition(delim, single);
090: }
091:
092: public String getDelimiter() {
093: return this .delimiter;
094: }
095:
096: public void setDelimiter(String d) {
097: if (d == null)
098: return;
099: if (!StringUtil.equalString(this .delimiter, d)) {
100: this .delimiter = d.trim();
101: this .changed = true;
102: }
103: }
104:
105: public boolean isChanged() {
106: return this .changed;
107: }
108:
109: public boolean isSingleLine() {
110: return this .singleLineDelimiter;
111: }
112:
113: public void setSingleLine(boolean flag) {
114: if (flag != this .singleLineDelimiter) {
115: this .singleLineDelimiter = flag;
116: this .changed = true;
117: }
118: }
119:
120: /**
121: * Return true if the given SQL script ends
122: * with this delimiter
123: * @param sql
124: */
125: public boolean terminatesScript(String sql) {
126: if (StringUtil.isEmptyString(sql))
127: return false;
128: if (this .isSingleLine()) {
129: Pattern p = Pattern.compile("(?i)[\\r\\n|\\n]+[ \t]*"
130: + StringUtil.quoteRegexMeta(this .delimiter)
131: + "[ \t]*[\\r\\n|\\n]*$");
132: return p.matcher(sql.trim()).find();
133: } else {
134: return sql.trim().endsWith(this .delimiter);
135: }
136:
137: }
138:
139: public boolean equals(Object other) {
140: if (other == null)
141: return false;
142:
143: if (other instanceof DelimiterDefinition) {
144: DelimiterDefinition od = (DelimiterDefinition) other;
145: if (this .singleLineDelimiter == od.singleLineDelimiter) {
146: return StringUtil.equalStringIgnoreCase(this .delimiter,
147: od.delimiter);
148: }
149: return false;
150: } else if (other instanceof String) {
151: return StringUtil.equalStringIgnoreCase(this .delimiter,
152: (String) other);
153: }
154: return false;
155: }
156:
157: public int hashCode() {
158: return (this.delimiter + Boolean
159: .toString(this.singleLineDelimiter)).hashCode();
160: }
161:
162: }
|