kaleidos / grails-postgresql-extensions

Grails plugin to use postgresql native elements such as arrays, hstores,...
Apache License 2.0
78 stars 62 forks source link

Exception when hstore key or value contains escaped characters #30

Closed jglapa closed 5 years ago

jglapa commented 10 years ago

Hi, it looks like "manually" inserted data(via psql) cannot be "parsed" by the extension. Hstore allows for the keys and values to contain double quote character but upon parsing we'll get an error as the escape character is not properly handled.

Caused by IllegalStateException: net.kaleidos.hibernate.usertype.HstoreParseException: Error @8 : Cannot find comma as an end of the value

Executing this:

insert into test(hs) values(hstore(ARRAY['key','"value"']));
insert into test(hs) values(hstore(ARRAY['\key','value']));
insert into test(hs) values(hstore(ARRAY['''key'''','value']))

will result with the following in db:

"key"=>"\"value\""
"\\key"=>"value" 
"'key'"=>"value" 

Those \", ', \ sequences should be treated in a special way.

I guess this should be somehow handled in the HstoreParser.advanceQuoted or HstoreParser.advanceWord. Are there any bigger limitations or gotchas around this issue? I might try to face this bug.

Just to mention: currently the extension "corrupts" the data. If we insert a key with double quote character inside it will be convert to a single quote. This means if this string was a hstore key we wouldn't be able to look up the value having the original key.

Thanks! PS. Good job guys with the extension! Keep up the good work!

ilopmar commented 10 years ago

Hi @jglapa,

Good catch! We're replacing all double-quotes with single-quotes here: https://github.com/kaleidos/grails-postgresql-extensions/blob/master/src/java/net/kaleidos/hibernate/usertype/HstoreHelper.java#L19

It would be great if you can fix the bug. I'll be more than happy accepting your pull request and generating a new version.

Regards, Iván.