You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2007/01/11 18:35:46 UTC

svn commit: r495303 - in /incubator/servicemix/trunk/common/servicemix-components/src: main/java/org/apache/servicemix/components/jdbc/ test/java/org/apache/servicemix/components/jdbc/

Author: gnodet
Date: Thu Jan 11 09:35:41 2007
New Revision: 495303

URL: http://svn.apache.org/viewvc?view=rev&rev=495303
Log:
SM-813: Unique Column Names for JdbcComponent
Patch provided by Kristian Koehler

Added:
    incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/
    incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java   (with props)
Modified:
    incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/jdbc/JdbcComponent.java

Modified: incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/jdbc/JdbcComponent.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/jdbc/JdbcComponent.java?view=diff&rev=495303&r1=495302&r2=495303
==============================================================================
--- incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/jdbc/JdbcComponent.java (original)
+++ incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/jdbc/JdbcComponent.java Thu Jan 11 09:35:41 2007
@@ -16,25 +16,30 @@
  */
 package org.apache.servicemix.components.jdbc;
 
-import org.apache.servicemix.MessageExchangeListener;
-import org.apache.servicemix.components.util.TransformComponentSupport;
-import org.apache.servicemix.jbi.jaxp.SourceTransformer;
-import org.apache.servicemix.jbi.jaxp.StringSource;
-import org.apache.commons.logging.LogFactory;
-import org.apache.commons.logging.Log;
-import org.apache.xpath.CachedXPathAPI;
-import org.w3c.dom.Node;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
 
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
 import javax.sql.DataSource;
 import javax.xml.transform.Source;
-import java.sql.Connection;
-import java.sql.Statement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.ResultSetMetaData;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.MessageExchangeListener;
+import org.apache.servicemix.components.util.TransformComponentSupport;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.xpath.CachedXPathAPI;
+import org.w3c.dom.Node;
 
 public class JdbcComponent extends TransformComponentSupport implements MessageExchangeListener {
     private static final Log log = LogFactory.getLog(JdbcComponent.class);
@@ -176,16 +181,14 @@
         ResultSetMetaData meta = rs.getMetaData();
         int colCount = meta.getColumnCount();
 
+        String[] colNames = getUniqueColumnNames(meta);
+        
         StringBuffer buff = new StringBuffer("");
 
         while (rs.next()) {
             buff.append("<row ");
             for (int i=1; i<=colCount; i++) {
-                String colName = meta.getColumnName(i);
-                if (colName.equals("")) {
-                    colName = "__unknown_column__";
-                }
-                buff.append(colName.toLowerCase() + "='" + rs.getString(i) + "' ");
+                buff.append(colNames[i].toLowerCase() + "='" + rs.getString(i) + "' ");
             }
             buff.append("/>");
         }
@@ -200,6 +203,45 @@
         }
 
         return new StringSource(buff.toString());
+    }
+
+    /**
+     * Returns a String[] containing unique ColNames. 
+     */
+    protected String[] getUniqueColumnNames(ResultSetMetaData metaData) throws SQLException {
+
+        List colNames = new LinkedList();
+        Map chanedNames = new HashMap();
+
+        for (int i = 0; i < metaData.getColumnCount(); i++) {
+            String name = metaData.getColumnName(i);
+
+            if (name.equals("")) {
+                name = "__unknown_column__";
+            }
+
+            if (colNames.contains(name)) {
+
+                int count;
+                if (chanedNames.containsKey(name)) {
+                    Integer integer = (Integer) chanedNames.get(name);
+                    Integer newInteger = new Integer(integer.intValue() + 1);
+                    chanedNames.put(name, newInteger);
+                    count = newInteger.intValue();
+                } else {
+                    chanedNames.put(name, new Integer(1));
+                    count = 1;
+                }
+
+                name = name + "_" + count;
+
+            }
+
+            colNames.add(name);
+
+        }
+
+        return (String[]) colNames.toArray(new String[colNames.size()]);
     }
 
     protected Source toXmlSource(int updateCount) throws Exception {

Added: incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java?view=auto&rev=495303
==============================================================================
--- incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java (added)
+++ incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java Thu Jan 11 09:35:41 2007
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.components.jdbc;
+
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+
+import junit.framework.TestCase;
+
+public class JdbcComponentUniqueNamesTest extends TestCase {
+
+	public void testNames() throws Exception {
+
+		JdbcComponent comp = new JdbcComponent();
+
+		String[] names = new String[]{"name", "name", "name", "id", "test_1", "test_1", "test_1"};
+		
+		ResultSetMetaData metaData = new MetaMock(names);
+		String[] uniqueColumnNames = comp.getUniqueColumnNames(metaData);
+		
+		String[] expected = 
+			new String[]{
+				"name", 
+				"name_1", 
+				"name_2", 
+				"id", 
+				"test_1", 
+				"test_1_1", 
+				"test_1_2"};
+		
+		for (int i = 0; i < uniqueColumnNames.length; i++) {
+			assertEquals(expected[i], uniqueColumnNames[i]);
+		}
+	}
+
+	private static class MetaMock implements ResultSetMetaData {
+
+		private final String[] names;
+		
+		MetaMock(String[] names) {
+			this.names = names;
+		}
+		
+		public int getColumnCount() throws SQLException {
+			return names.length;
+		}
+
+		public String getColumnName(int column) throws SQLException {
+			return names[column];
+		}
+
+		public String getCatalogName(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public String getColumnClassName(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public int getColumnDisplaySize(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public String getColumnLabel(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public int getColumnType(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public String getColumnTypeName(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public int getPrecision(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public int getScale(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public String getSchemaName(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public String getTableName(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isAutoIncrement(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isCaseSensitive(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isCurrency(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isDefinitelyWritable(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public int isNullable(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isReadOnly(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isSearchable(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isSigned(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+		public boolean isWritable(int column) throws SQLException {
+			throw new RuntimeException("not implemented");
+		}
+
+	}
+
+}

Propchange: incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/servicemix/trunk/common/servicemix-components/src/test/java/org/apache/servicemix/components/jdbc/JdbcComponentUniqueNamesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain