Skip to content
Merged
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
28 changes: 23 additions & 5 deletions src/main/groovy/net/kaleidos/hibernate/usertype/HstoreParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,50 @@ public class HstoreParser extends PGobject implements Iterable<Map.Entry<String,

private int length;

private static final String BACKSLASH = "\\";
private static final String DOUBLE_QUOTE = "\"";

private static final String BACKSLASH_PLACEHOLDER = "!#BS#!";
private static final String DOUBLE_QUOTE_PLACEHOLDER = "!#DQ#!";

public HstoreParser(String rawValue) {
this.type = "hstore";
this.value = rawValue;
this.length = rawValue == null ? 0 : rawValue.length();
setUnescapedValueAndLength(rawValue);
}

// To include a double quote or a backslash in a key or value, escape it with a backslash. (https://www.postgresql.org/docs/current/static/hstore.html)
private void setUnescapedValueAndLength(String rawValue) {
this.value = rawValue
.replace(BACKSLASH + DOUBLE_QUOTE, DOUBLE_QUOTE_PLACEHOLDER)
.replace(BACKSLASH + BACKSLASH, BACKSLASH_PLACEHOLDER);
this.length = this.value == null ? 0 : this.value.length();
}

@Override
public void setValue(String rawValue) {
Assert.state("hstore".equals(type), "HStore database type name should be 'hstore'");
this.value = rawValue;
this.length = rawValue == null ? 0 : rawValue.length();
setUnescapedValueAndLength(rawValue);
}

public Map<String, String> asMap() {
HashMap<String, String> r = new HashMap<String, String>();
try {
for (final HStoreIterator iterator = new HStoreIterator(); iterator.hasNext(); ) {
final HStoreEntry entry = iterator.rawNext();
r.put(entry.key, entry.value);
r.put(replaceEscapePlaceholders(entry.key), replaceEscapePlaceholders(entry.value));
}
} catch (HstoreParseException e) {
throw new IllegalStateException(e);
}
return r;
}

private String replaceEscapePlaceholders(String text) {
return text == null ? null : text
.replace(BACKSLASH_PLACEHOLDER, BACKSLASH)
.replace(DOUBLE_QUOTE_PLACEHOLDER, DOUBLE_QUOTE);
}

private static class HStoreEntry implements Entry<String, String> {
private String key;
private String value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.kaleidos.hibernate.usertype

import spock.lang.Specification
import spock.lang.Unroll

class HstoreParserSpec extends Specification {
@Unroll
void "AsMap with value populated by constructor"() {
expect:
HstoreParser parser = new HstoreParser(input)
def map = parser.asMap()
map[expected_key] == expected_value

where:
example << escapedCharactersExamples()
input = example.input
expected_key = example.expected_key
expected_value = example.expected_value
}

@Unroll
void "AsMap with value populated by setValue"() {
expect:
HstoreParser parser = new HstoreParser('')
parser.setValue(input)
def map = parser.asMap()
map[expected_key] == expected_value

where:
example << escapedCharactersExamples()
input = example.input
expected_key = example.expected_key
expected_value = example.expected_value
}

def escapedCharactersExamples() {
return [
// insert into test(hs) values(hstore(ARRAY['key','"value"']));
[
input : /"key"=>"\"value\""/,
expected_key : /key/,
expected_value: /"value"/
],
// insert into test(hs) values(hstore(ARRAY['\key','value']));
[
input : /"\\key"=>"value"/,
expected_key : /\key/,
expected_value: /value/
],
// insert into test(hs) values(hstore(ARRAY['''key'''','value']))
[
input : /"'key'"=>"value"/,
expected_key : /'key'/,
expected_value: /value/
],
// nested hstore
[
input : /"key"=>"\"nested_key\"=>\"1.1\""/,
expected_key : /key/,
expected_value: /"nested_key"=>"1.1"/
]
]
}
}