}
public static final List<String> postgresROW2StringList(String value, final int appendStringSize)
throws RowParserException {
if ((value.startsWith("(") && !value.endsWith(")")) || (!value.startsWith("(") && value.endsWith(")"))) {
throw new RowParserException("postgresROW2StringList() ROW must begin with '(' and ends with ')': "
+ value);
}
if (!(value.startsWith("(") || value.endsWith(")"))) {
// in case both elements are missing, we add them for processing:
// this will be the case for enum types:
value = "(" + value + ")";
}
final List<String> result = new ArrayList<String>();
final char[] c = value.toCharArray();
StringBuilder element = new StringBuilder(appendStringSize);
// this processor will fail if value has spaces between ',' and '"' or
// ')'
int i = 1;
while (c[i] != ')') {
if (c[i] == ',') {
final char nextChar = c[i + 1];
if (c[i - 1] == '(') {
// we have an empty first position, that is we have a NULL value
result.add(null);
}
if (nextChar == ',' || nextChar == ')') {
result.add(null);
}
i++;
} else if (c[i] == '\"') {
i++;
boolean insideQuote = true;
while (insideQuote) {
final char nextChar = c[i + 1];
if (c[i] == '\"') {
if (nextChar == ',' || nextChar == ')') {
result.add(element.toString());
element = new StringBuilder(appendStringSize);
insideQuote = false;
} else if (nextChar == '\"') {
i++;
element.append(c[i]);
} else {
throw new RowParserException("postgresROW2StringList() char after \" is not valid");
}
} else if (c[i] == '\\') {
if (nextChar == '\\' || nextChar == '\"') {
i++;
element.append(c[i]);
} else {
throw new RowParserException("postgresROW2StringList() char after \\ is not valid");
}
} else {
element.append(c[i]);
}