Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 350137

Summary: [DB] [PostgreSQL] Exception when persisting EChar attribute with default '\u0000' value
Product: [Modeling] EMF Reporter: Josh M <public.web.josh>
Component: cdo.dbAssignee: Eike Stepper <stepper>
Status: CLOSED FIXED QA Contact: Eike Stepper <stepper>
Severity: normal    
Priority: P3 CC: public.web.josh, stefan, stepper
Version: 4.2Flags: stepper: review+
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 323006, 408479    
Bug Blocks:    
Attachments:
Description Flags
Original exception and stack trace
none
patch-v1
none
Patch v2 none

Description Josh M CLA 2011-06-23 08:47:00 EDT
Build Identifier: 20110218-0911

It seems that if the default value on an EChar attribute is not set, or if it is set to '\u0000' that CDO cannot persist the object due to an "invalid byte sequence for encoding 'UTF8': 0x00" error.  This may be specific to only a PostgreSQL datastore, but I am unable to test with other datastore implementations at this time. 

Details of this bug have been discussed in the forums: http://www.eclipse.org/forums/index.php/t/215023/

Reproducible: Always

Steps to Reproduce:
1. Configure PostgreSQL as the CDO datastore
2. Generate an ECore model with an object consisting of an EChar attribute.  Do not set its default attribute.
3. Instantiate the object with the EChar attribute, but do not change the EChar attribute's value.
4. Commit the object via CDO.
Comment 1 Josh M CLA 2011-06-23 08:51:05 EDT
Created attachment 198465 [details]
Original exception and stack trace
Comment 2 Stefan Winkler CLA 2011-06-23 12:56:31 EDT
The explanation seems to be easy:

Since the attribute is not unsettable, its default value is zero (0), because EChar reflects a base type (char).
Since the default type mapping represents a EChar or ECharacter as a String of length 1, it writes this string to the database.

PostgreSQL, however does not support NUL-characters in Strings: (e.g., see http://archives.postgresql.org/pgsql-jdbc/2007-02/msg00106.php)


The solution is more difficult.
In my opinion, the easiest way is to introduce a new type mapping, which maps EChar (and ECharacter) to a number field (instead a string field). I don't want to change the behavior of the existing type mapping, because of backwards-compatibility and because the string-representation is more 'direct'.

I'll have a look if the new TypeMapping can be configured transparently. (If not, a model annotation to use the correct type mapping would be needed).
Comment 3 Stefan Winkler CLA 2011-06-25 12:36:51 EDT
Created attachment 198599 [details]
patch-v1

I have created a test case and a patch.
Introducing a char to integer mapping and setting the database type for char to integer for PostgreSQL fixes the problem. See attached patch.

Eike, please review.
Comment 4 Eike Stepper CLA 2011-07-06 01:51:44 EDT
Created attachment 199160 [details]
Patch v2

Adjusted manifest version to 4.0.100.

Note that I'm going to move this bug/fix to 4.1!!!
Comment 6 Victor Roldan Betancort CLA 2012-03-08 07:23:19 EST
Guys, can we commit this fix? Fixes the issue nicely.
Comment 7 Eike Stepper CLA 2012-08-14 22:56:36 EDT
Moving all open issues to 4.2. Open bugs can be ported to 4.1 maintenance after they've been fixed in master.
Comment 8 Eike Stepper CLA 2012-10-31 06:20:29 EDT
Nothing in this patch applies to master anymore. Please reopen this bug if you feel a need.
Comment 9 Victor Roldan Betancort CLA 2013-05-10 12:24:38 EDT
I managed to merge this old bug with master. Reopening for fixation
Comment 10 Eike Stepper CLA 2013-05-20 06:08:39 EDT
I've provided a ColumnTypeModifier concept in bug 408479.
Comment 11 Eike Stepper CLA 2013-05-20 06:15:26 EDT
I've taken the new EChar -> DBType.INTEGER type mappings and added a ColumnTypeModifier for Postgres. The attached test passes adn is committed:
commit aa8bfe023cad0c187cc2d88518456b1e8abf7828
Comment 12 Eike Stepper CLA 2013-06-27 03:31:52 EDT
Available in R20130613-1157 (4.2)