cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r545601 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/query/ test/java/org/apache/cayenne/access/ test/java/org/apache/cayenne/query/
Date Fri, 08 Jun 2007 19:29:07 GMT
Author: aadamchik
Date: Fri Jun  8 12:29:06 2007
New Revision: 545601

URL: http://svn.apache.org/viewvc?view=rev&rev=545601
Log:
CAY-800: SQLTemplate improvement
runtime support for the capitalization control api. 
still need to do modeler and xml loading support

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/SQLTemplateTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptor.java?view=diff&rev=545601&r1=545600&r2=545601
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/RowDescriptor.java
Fri Jun  8 12:29:06 2007
@@ -77,6 +77,28 @@
 
         indexTypes(types);
     }
+    
+    /**
+     * Converts result column labels to uppercase using the default Locale.
+     * 
+     * @since 3.0 
+     */
+    public void forceUpperCaseColumnNames() {
+        for(int i = 0; i < columns.length; i++) {
+            columns[i].setLabel(columns[i].getLabel().toUpperCase());
+        }
+    }
+    
+    /**
+     * Converts result column labels to lowercase using the default Locale.
+     * 
+     * @since 3.0 
+     */
+    public void forceLowerCaseColumnNames() {
+        for(int i = 0; i < columns.length; i++) {
+            columns[i].setLabel(columns[i].getLabel().toLowerCase());
+        }
+    }
 
     /**
      * Initializes converters for columns.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java?view=diff&rev=545601&r1=545600&r2=545601
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
Fri Jun  8 12:29:06 2007
@@ -200,6 +200,17 @@
         RowDescriptor descriptor = (compiled.getResultColumns().length > 0)
                 ? new RowDescriptor(compiled.getResultColumns(), types)
                 : new RowDescriptor(resultSet, types);
+                
+           if (query.getColumnNamesCapitalization() != null) {
+            if (SQLTemplate.LOWERCASE_COLUMN_NAMES.equals(query
+                    .getColumnNamesCapitalization())) {
+                descriptor.forceLowerCaseColumnNames();
+            }
+            else if (SQLTemplate.UPPERCASE_COLUMN_NAMES.equals(query
+                    .getColumnNamesCapitalization())) {
+                descriptor.forceUpperCaseColumnNames();
+            }
+        }
 
         JDBCResultIterator result = new JDBCResultIterator(
                 connection,

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java?view=diff&rev=545601&r1=545600&r2=545601
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
Fri Jun  8 12:29:06 2007
@@ -48,7 +48,7 @@
  * </p>
  * 
  * <pre>
- *                    SELECT ID, NAME FROM SOME_TABLE WHERE NAME LIKE $a
+ *  SELECT ID, NAME FROM SOME_TABLE WHERE NAME LIKE $a
  * </pre>
  * 
  * <p>
@@ -75,6 +75,16 @@
 public class SQLTemplate extends AbstractQuery implements ParameterizedQuery,
         XMLSerializable {
 
+    /**
+     * @since 3.0
+     */
+    public static final String UPPERCASE_COLUMN_NAMES = "upper";
+
+    /**
+     * @since 3.0
+     */
+    public static final String LOWERCASE_COLUMN_NAMES = "lower";
+
     private static final Transformer nullMapTransformer = new Transformer() {
 
         public Object transform(Object input) {
@@ -85,6 +95,7 @@
     protected String defaultTemplate;
     protected Map templates;
     protected Map[] parameters;
+    protected String columnNamesCapitalization;
 
     SQLTemplateMetadata selectInfo = new SQLTemplateMetadata();
 
@@ -335,7 +346,7 @@
     public void setCachePolicy(String policy) {
         this.selectInfo.setCachePolicy(policy);
     }
-    
+
     /**
      * @since 3.0
      */
@@ -532,5 +543,36 @@
      */
     public void clearPrefetches() {
         selectInfo.clearPrefetches();
+    }
+
+    /**
+     * Returns a column name capitalization policy applied to selecting queries. This is
+     * used to simplify mapping of the queries like "SELECT * FROM ...", ensuring that a
+     * chosen Cayenne column mapping strategy (e.g. all column names in uppercase) is
+     * portable across database engines that can have varying default capitalization.
+     * Default (null) value indicates that column names provided in result set are used
+     * unchanged.
+     * 
+     * @since 3.0
+     */
+    public String getColumnNamesCapitalization() {
+        return columnNamesCapitalization;
+    }
+
+    /**
+     * Sets a column name capitalization policy applied to selecting queries. This is used
+     * to simplify mapping of the queries like "SELECT * FROM ...", ensuring that a chosen
+     * Cayenne column mapping strategy (e.g. all column names in uppercase) is portable
+     * across database engines that can have varying default capitalization. Default
+     * (null) value indicates that column names provided in result set are used unchanged.
+     * <p/> Note that while a non-default setting is useful for queries that do not
rely
+     * on a #result directive to describe columns, it works for all SQLTemplates the same
way.
+     * 
+     * @param columnNameCapitalization Can be null of one of
+     *            {@link #LOWERCASE_COLUMN_NAMES} or {@link #UPPERCASE_COLUMN_NAMES}.
+     * @since 3.0
+     */
+    public void setColumnNamesCapitalization(String columnNameCapitalization) {
+        this.columnNamesCapitalization = columnNameCapitalization;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java?view=diff&rev=545601&r1=545600&r2=545601
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
Fri Jun  8 12:29:06 2007
@@ -40,6 +40,37 @@
         context = createDataContext();
     }
 
+    public void testColumnNamesCapitalization() throws Exception {
+        getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
+
+        String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
+        SQLTemplate query = new SQLTemplate(Artist.class, template);
+        query.setColumnNamesCapitalization(SQLTemplate.LOWERCASE_COLUMN_NAMES);
+        query.setFetchingDataRows(true);
+
+        List rows = context.performQuery(query);
+
+        DataRow row1 = (DataRow) rows.get(0);
+        assertFalse(row1.containsKey("ARTIST_ID"));
+        assertTrue(row1.containsKey("artist_id"));
+
+        DataRow row2 = (DataRow) rows.get(1);
+        assertFalse(row2.containsKey("ARTIST_ID"));
+        assertTrue(row2.containsKey("artist_id"));
+    
+        query.setColumnNamesCapitalization(SQLTemplate.UPPERCASE_COLUMN_NAMES);
+        
+        List rowsUpper = context.performQuery(query);
+
+        DataRow row3 = (DataRow) rowsUpper.get(0);
+        assertFalse(row3.containsKey("artist_id"));
+        assertTrue(row3.containsKey("ARTIST_ID"));
+
+        DataRow row4 = (DataRow) rowsUpper.get(1);
+        assertFalse(row4.containsKey("artist_id"));
+        assertTrue(row4.containsKey("ARTIST_ID"));
+    }
+
     public void testFetchDataRows() throws Exception {
         getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/SQLTemplateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/SQLTemplateTest.java?view=diff&rev=545601&r1=545600&r2=545601
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/SQLTemplateTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/SQLTemplateTest.java
Fri Jun  8 12:29:06 2007
@@ -34,6 +34,13 @@
  * @author Andrus Adamchik
  */
 public class SQLTemplateTest extends TestCase {
+    
+    public void testColumnNameCapitalization() {
+        SQLTemplate q1 = new SQLTemplate("E1", "SELECT");
+        assertNull(q1.getColumnNamesCapitalization());
+        q1.setColumnNamesCapitalization(SQLTemplate.UPPERCASE_COLUMN_NAMES);
+        assertEquals(SQLTemplate.UPPERCASE_COLUMN_NAMES, q1.getColumnNamesCapitalization());
+    }
 
     public void testQueryWithParameters() {
         SQLTemplate q1 = new SQLTemplate("E1", "SELECT");



Mime
View raw message