2
* $Id: ParserUtils.java 80 2007-09-01 20:25:45Z fordfrog $
4
package cz.startnet.utils.pgdiff.parsers;
6
import cz.startnet.utils.pgdiff.schema.PgDatabase;
8
import java.util.regex.Pattern;
15
* @version $Id: ParserUtils.java 80 2007-09-01 20:25:45Z fordfrog $
17
public class ParserUtils {
19
* Creates a new instance of ParserUtils.
21
private ParserUtils() {
26
* Returns position of last character of single command within
27
* larger command (like CREATE TABLE). Last character is either ',' or
28
* ')'. If no such character is found and method reaches the end of the
29
* command then position after the last character in the command is
32
* @param command command
33
* @param start start position
35
* @return end position of the command
37
public static int getCommandEnd(final String command, final int start) {
39
boolean singleQuoteOn = false;
42
for (; charPos < command.length(); charPos++) {
43
final char chr = command.charAt(charPos);
47
} else if (chr == ')') {
48
if (bracesCount == 0) {
53
} else if (chr == '\'') {
54
singleQuoteOn ^= singleQuoteOn;
55
} else if ((chr == ',') && !singleQuoteOn && (bracesCount == 0)) {
64
* Returns object name from optionally schema qualified name.
66
* @param name optionally schema qualified name
68
* @return name of the object
70
public static String getObjectName(final String name) {
72
final int pos = name.indexOf('.');
77
result = name.substring(pos + 1);
84
* Returns schema name from optionally schema qualified name.
86
* @param name optionally schema qualified name
87
* @param database database
89
* @return name of the schema
91
public static String getSchemaName(
93
final PgDatabase database) {
95
final int pos = name.indexOf('.');
98
result = database.getDefaultSchema().getName();
100
result = name.substring(0, pos);
107
* Removes semicolon from the end of the <code>command</code>, but
108
* only if <code>command</code> ends with semicolon.
110
* @param command command
112
* @return original <code>string</code> without last character and trimmed
114
public static String removeLastSemicolon(final String command) {
117
if (command.endsWith(";")) {
118
result = command.substring(0, command.length() - 1).trim();
127
* Removes substring from <code>string</code> based on
128
* <code>start</code> and <code>end</code> position.
130
* @param string string
131
* @param start start position of substring
132
* @param end offset after the last character of the substring
134
* @return <code>string</code> without given substring
136
public static String removeSubString(
143
result = string.substring(end).trim();
146
string.substring(0, start).trim() + " "
147
+ string.substring(end).trim();
154
* Removes <code>subString</code> from <code>string</code>. The
155
* removal is performed case insensitive.
157
* @param string string
158
* @param subString substring
160
* @return <code>string</code> without <code>subString</code>
162
public static String removeSubString(
164
final String subString) {
165
return Pattern.compile(subString, Pattern.CASE_INSENSITIVE)
166
.matcher(string).replaceAll("");