Partial fix to 323006: [DB] Various PostgreSQL test failures
https://bugs.eclipse.org/bugs/show_bug.cgi?id=323006
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStoreAccessor.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStoreAccessor.java
index d1bbe8d..0b99215 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStoreAccessor.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStoreAccessor.java
@@ -92,8 +92,6 @@
 import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.Writer;
-import java.sql.Blob;
-import java.sql.Clob;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -396,17 +394,15 @@
       resultSet.next();
 
       long size = resultSet.getLong(1);
-      Blob blob = resultSet.getBlob(2);
+      InputStream inputStream = resultSet.getBinaryStream(2);
       if (resultSet.wasNull())
       {
-        Clob clob = resultSet.getClob(3);
-        Reader in = clob.getCharacterStream();
-        IOUtil.copyCharacter(in, new OutputStreamWriter(out), size);
+        Reader reader = resultSet.getCharacterStream(3);
+        IOUtil.copyCharacter(reader, new OutputStreamWriter(out), size);
       }
       else
       {
-        InputStream in = blob.getBinaryStream();
-        IOUtil.copyBinary(in, out, size);
+        IOUtil.copyBinary(inputStream, out, size);
       }
     }
     catch (SQLException ex)
@@ -432,11 +428,10 @@
       {
         byte[] id = HexUtil.hexToBytes(resultSet.getString(1));
         long size = resultSet.getLong(2);
-        Blob blob = resultSet.getBlob(3);
+        InputStream inputStream = resultSet.getBinaryStream(3);
         if (resultSet.wasNull())
         {
-          Clob clob = resultSet.getClob(4);
-          Reader in = clob.getCharacterStream();
+          Reader in = resultSet.getCharacterStream(4);
           Writer out = handler.handleClob(id, size);
           if (out != null)
           {
@@ -452,13 +447,12 @@
         }
         else
         {
-          InputStream in = blob.getBinaryStream();
           OutputStream out = handler.handleBlob(id, size);
           if (out != null)
           {
             try
             {
-              IOUtil.copyBinary(in, out, size);
+              IOUtil.copyBinary(inputStream, out, size);
             }
             finally
             {
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java
index acc7ab3..4734d38 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java
@@ -632,8 +632,57 @@
   }
 
   /**
-   * @author Eike Stepper
+   * @author Stefan Winkler
    */
+  public static class TMCharacter2Integer extends AbstractTypeMapping
+  {
+    public static final Factory FACTORY = new Factory(TypeMappingUtil.createDescriptor(
+        ID_PREFIX + ".Character2Integer", EcorePackage.eINSTANCE.getEChar(), DBType.INTEGER));
+
+    public static final Factory FACTORY_OBJECT = new Factory(TypeMappingUtil.createDescriptor(ID_PREFIX
+        + ".CharacterObject2Integer", EcorePackage.eINSTANCE.getECharacterObject(), DBType.INTEGER));
+
+    @Override
+    public Object getResultSetValue(ResultSet resultSet) throws SQLException
+    {
+      int intRepresentation = resultSet.getInt(getField().getName());
+      if (resultSet.wasNull())
+      {
+        return getFeature().isUnsettable() ? CDORevisionData.NIL : null;
+      }
+
+      return Character.valueOf((char)intRepresentation);
+    }
+
+    @Override
+    protected void doSetValue(PreparedStatement stmt, int index, Object value) throws SQLException
+    {
+      stmt.setInt(index, /* (int) */((Character)value).charValue());
+    }
+
+    /**
+     * @author Stefan Winkler
+     */
+    public static class Factory extends AbstractTypeMappingFactory
+    {
+      public Factory(Descriptor descriptor)
+      {
+        super(descriptor);
+      }
+
+      @Override
+      public ITypeMapping create(String description) throws ProductCreationException
+      {
+        return new TMCharacter2Integer();
+      }
+    }
+  }
+
+  /**
+  
+  /**
+  * @author Eike Stepper
+  */
   public static class TMByte extends AbstractTypeMapping
   {
     public static final Factory FACTORY = new Factory(TypeMappingUtil.createDescriptor(ID_PREFIX + ".Byte",
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java
index 52d14f8..e217335 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java
@@ -130,6 +130,8 @@
     container.registerFactory(CoreTypeMappings.TMBytesVarbinary.FACTORY);
     container.registerFactory(CoreTypeMappings.TMCharacter.FACTORY);
     container.registerFactory(CoreTypeMappings.TMCharacter.FACTORY_OBJECT);
+    container.registerFactory(CoreTypeMappings.TMCharacter2Integer.FACTORY);
+    container.registerFactory(CoreTypeMappings.TMCharacter2Integer.FACTORY_OBJECT);
     container.registerFactory(CoreTypeMappings.TMCustom.FACTORY_VARCHAR);
     container.registerFactory(CoreTypeMappings.TMCustom.FACTORY_CLOB);
     container.registerFactory(CoreTypeMappings.TMCustom.FACTORY_LONG_VARCHAR);
diff --git a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/Bugzilla_350137_Test.java b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/Bugzilla_350137_Test.java
new file mode 100644
index 0000000..d8ff7e7
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/Bugzilla_350137_Test.java
@@ -0,0 +1,89 @@
+/*

+ * Copyright (c) 2004-2013 Eike Stepper (Berlin, Germany) and others.

+ * All rights reserved. This program and the accompanying materials

+ * are made available under the terms of the Eclipse Public License v1.0

+ * which accompanies this distribution, and is available at

+ * http://www.eclipse.org/legal/epl-v10.html

+ * 

+ * Contributors:

+ *    Eike Stepper - initial API and implementation

+ */

+package org.eclipse.emf.cdo.tests.db;

+

+import org.eclipse.emf.cdo.common.model.EMFUtil;

+import org.eclipse.emf.cdo.eresource.CDOResource;

+import org.eclipse.emf.cdo.session.CDOSession;

+import org.eclipse.emf.cdo.tests.AbstractCDOTest;

+import org.eclipse.emf.cdo.transaction.CDOTransaction;

+import org.eclipse.emf.cdo.util.CDOUtil;

+

+import org.eclipse.emf.ecore.EAttribute;

+import org.eclipse.emf.ecore.EClass;

+import org.eclipse.emf.ecore.EObject;

+import org.eclipse.emf.ecore.EPackage;

+import org.eclipse.emf.ecore.EcorePackage;

+import org.eclipse.emf.ecore.util.EcoreUtil;

+

+/**

+ * @author Stefan Winkler

+ */

+public class Bugzilla_350137_Test extends AbstractCDOTest

+{

+

+  /**

+   * Just check if an EChar attribute can be stored to the database per default. (PostgreSQL has problems with this).

+   */

+  public void testDefault() throws Exception

+  {

+    EPackage pkg = EMFUtil.createEPackage("Test", "t", "http://cdo.eclipse.org/tests/Bugzilla350137_Test1.ecore");

+    EClass cls = EMFUtil.createEClass(pkg, "foo", false, false);

+

+    @SuppressWarnings("unused")

+    EAttribute att = EMFUtil.createEAttribute(cls, "bar", EcorePackage.eINSTANCE.getEChar());

+

+    if (!isConfig(LEGACY))

+    {

+      CDOUtil.prepareDynamicEPackage(pkg);

+    }

+

+    EObject obj = EcoreUtil.create(cls);

+

+    CDOSession session = openSession();

+    CDOTransaction transaction = session.openTransaction();

+    CDOResource resource = transaction.createResource(getResourcePath("/test"));

+

+    resource.getContents().add(obj);

+    transaction.commit();

+    transaction.close();

+    session.close();

+  }

+

+  /**

+   * Check if an EChar attribute with explicit zero value can be stored to the database per default. (PostgreSQL has

+   * problems with this).

+   */

+  public void testExplicitZero() throws Exception

+  {

+    EPackage pkg = EMFUtil.createEPackage("Test", "t", "http://cdo.eclipse.org/tests/Bugzilla350137_Test2.ecore");

+    EClass cls = EMFUtil.createEClass(pkg, "foo2", false, false);

+

+    EAttribute att = EMFUtil.createEAttribute(cls, "bar", EcorePackage.eINSTANCE.getEChar());

+

+    if (!isConfig(LEGACY))

+    {

+      CDOUtil.prepareDynamicEPackage(pkg);

+    }

+

+    EObject obj = EcoreUtil.create(cls);

+    obj.eSet(att, '\u0000');

+

+    CDOSession session = openSession();

+    CDOTransaction transaction = session.openTransaction();

+    CDOResource resource = transaction.createResource(getResourcePath("/test"));

+

+    resource.getContents().add(obj);

+    transaction.commit();

+    transaction.close();

+    session.close();

+  }

+}

diff --git a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/DBConfigs.java b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/DBConfigs.java
index 8f812ff..7a88fda 100644
--- a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/DBConfigs.java
+++ b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/DBConfigs.java
@@ -24,6 +24,7 @@
 import org.eclipse.emf.cdo.tests.config.IScenario;
 import org.eclipse.emf.cdo.tests.config.impl.ConfigTest;
 
+
 import java.util.List;
 
 /**
@@ -40,6 +41,7 @@
     testClasses.add(CustomTypeMappingTest.class);
     testClasses.add(SQLQueryTest.class);
     testClasses.add(Bugzilla_351068_Test.class);
+    testClasses.add(Bugzilla_350137_Test.class);
 
     super.initTestClasses(testClasses, scenario);
     testClasses.remove(MEMStoreQueryTest.class);
diff --git a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/PostgresqlConfig.java b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/PostgresqlConfig.java
index 21fcffd..7cd32b4 100644
--- a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/PostgresqlConfig.java
+++ b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/PostgresqlConfig.java
@@ -31,6 +31,12 @@
  */
 public class PostgresqlConfig extends DBConfig
 {
+  private static final String USERNAME = "sa";
+
+  private static final String PASSWORD = "sa";
+
+  private static final String HOSTNAME = "localhost";
+
   public static final String DB_ADAPTER_NAME = "Postgresql";
 
   private static final long serialVersionUID = 1L;
@@ -65,10 +71,10 @@
     currentRepositoryName = repoName;
 
     dataSource = new PGSimpleDataSource();
-    dataSource.setServerName("localhost");
+    dataSource.setServerName(HOSTNAME);
     dataSource.setDatabaseName(currentRepositoryName);
-    dataSource.setUser("sa");
-    dataSource.setPassword("sa");
+    dataSource.setUser(USERNAME);
+    dataSource.setPassword(PASSWORD);
 
     try
     {
@@ -79,7 +85,10 @@
       OM.LOG.warn(ex.getMessage());
     }
 
-    dropDatabase();
+    if (!isRestarting())
+    {
+      dropDatabase();
+    }
 
     return dataSource;
   }
@@ -114,15 +123,11 @@
 
   private DataSource getSetupDataSource()
   {
-    if (setupDataSource == null)
-    {
-      setupDataSource = new PGSimpleDataSource();
-      setupDataSource.setServerName("localhost");
-      setupDataSource.setDatabaseName(currentRepositoryName);
-      setupDataSource.setUser("sa");
-      setupDataSource.setPassword("sa");
-    }
-
+    setupDataSource = new PGSimpleDataSource();
+    setupDataSource.setServerName(HOSTNAME);
+    setupDataSource.setDatabaseName(currentRepositoryName);
+    setupDataSource.setUser(USERNAME);
+    setupDataSource.setPassword(USERNAME);
     return setupDataSource;
   }
 }
diff --git a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
index d946c92..77634fe 100644
--- a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
+++ b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
@@ -358,8 +358,9 @@
     List<Map<String, Object>> results = query.getResult();
     for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
     {
-      assertEquals("Street " + i, results.get(i).get("STREET"));
-      Object actual = results.get(i).get("CITY");
+      Map<String, Object> result = results.get(i);
+      assertEquals("Street " + i, result.containsKey("STREET") ? result.get("STREET") : result.get("street"));
+      Object actual = result.containsKey("CITY") ? result.get("CITY") : result.get("city");
       if (i == 0)
       {
         assertEquals(null, actual);
@@ -369,7 +370,7 @@
         assertEquals("City " + i, actual);
       }
 
-      assertEquals("" + i, results.get(i).get("NAME"));
+      assertEquals("" + i, result.containsKey("NAME") ? result.get("NAME") : result.get("name"));
     }
   }
 
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java
index 88e68fe..28ee7e3 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java
@@ -26,8 +26,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
-import java.sql.Blob;
-import java.sql.Clob;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -470,7 +468,18 @@
     public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull)
         throws SQLException, IOException
     {
-      Clob value = resultSet.getClob(column);
+      InputStream inputStream = resultSet.getBinaryStream(column);
+      long length = -1;
+      try
+      {
+        length = resultSet.getClob(column).length();
+      }
+      catch (SQLException ex)
+      {
+        // Postgresql does not have support for Blob/Clob
+        // InputStream.available() returns the actual length
+        length = inputStream.available();
+      }
       if (canBeNull)
       {
         if (resultSet.wasNull())
@@ -482,21 +491,18 @@
         out.writeBoolean(true);
       }
 
-      long length = value.length();
-      Reader reader = value.getCharacterStream();
-
       try
       {
         out.writeLong(length);
         while (length-- > 0)
         {
-          int c = reader.read();
+          int c = inputStream.read();
           out.writeChar(c);
         }
       }
       finally
       {
-        IOUtil.close(reader);
+        IOUtil.close(inputStream);
       }
 
       return null;
@@ -844,7 +850,18 @@
     public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull)
         throws SQLException, IOException
     {
-      Blob value = resultSet.getBlob(column);
+      InputStream inputStream = resultSet.getBinaryStream(column);
+      long length = -1;
+      try
+      {
+        length = resultSet.getBlob(column).length();
+      }
+      catch (SQLException ex)
+      {
+        // Postgresql does not have support for Blob/Clob
+        // InputStream.available() returns the actual length
+        length = inputStream.available();
+      }
       if (canBeNull)
       {
         if (resultSet.wasNull())
@@ -856,17 +873,19 @@
         out.writeBoolean(true);
       }
 
-      long length = value.length();
-      InputStream stream = value.getBinaryStream();
-
       try
       {
         out.writeLong(length);
-        IOUtil.copyBinary(stream, new ExtendedDataOutput.Stream(out), length);
+        IOUtil.copyBinary(inputStream, new ExtendedDataOutput.Stream(out), length);
+        // while (length-- > 0)
+        // {
+        // int b = inputStream.read();
+        // out.writeByte(b + Byte.MIN_VALUE);
+        // }
       }
       finally
       {
-        IOUtil.close(stream);
+        IOUtil.close(inputStream);
       }
 
       return null;
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
index 6640520..a0ae362 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
@@ -797,6 +797,7 @@
     {
       statement = connection.createStatement();
       statement.execute(string);
+      connection.commit();
     }
     catch (SQLException ex)
     {