cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject cayenne git commit: Fix wrong quoting in expressions when "Quote SQL Identifiers" is on
Date Sat, 14 Jan 2017 13:38:10 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 0bb9a5716 -> e8ef29608


Fix wrong quoting in expressions when "Quote SQL Identifiers" is on


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/e8ef2960
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/e8ef2960
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/e8ef2960

Branch: refs/heads/master
Commit: e8ef29608a83af6bda9a9f611a4ea6c38476dde4
Parents: 0bb9a57
Author: stariy <stariy95@gmail.com>
Authored: Sat Jan 14 16:37:15 2017 +0300
Committer: stariy <stariy95@gmail.com>
Committed: Sat Jan 14 16:37:15 2017 +0300

----------------------------------------------------------------------
 .../cayenne/access/jdbc/ColumnDescriptor.java   | 21 +++++++++
 .../select/DefaultSelectTranslator.java         | 14 ++++--
 .../apache/cayenne/query/ColumnSelectIT.java    | 49 ++++++++++++++++++++
 3 files changed, 80 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/e8ef2960/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/ColumnDescriptor.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/ColumnDescriptor.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/ColumnDescriptor.java
index 3f3f189..7cbaab4 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/ColumnDescriptor.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/ColumnDescriptor.java
@@ -52,6 +52,11 @@ public class ColumnDescriptor {
     protected String javaClass;
 
     /**
+     * @since 4.0
+     */
+    protected boolean isExpression;
+
+    /**
      * Creates a ColumnDescriptor
      */
     public ColumnDescriptor() {
@@ -280,4 +285,20 @@ public class ColumnDescriptor {
     public void setDataRowKey(String dataRowKey) {
         this.dataRowKey = dataRowKey;
     }
+
+    /**
+     * @since 4.0
+     * @param isExpression is this column is an expression
+     */
+    public void setIsExpression(boolean isExpression) {
+        this.isExpression = isExpression;
+    }
+
+    /**
+     * @since 4.0
+     * @return is this column is an expression
+     */
+    public boolean isExpression() {
+        return isExpression;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e8ef2960/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/DefaultSelectTranslator.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/DefaultSelectTranslator.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/DefaultSelectTranslator.java
index d21e643..9adad76 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/DefaultSelectTranslator.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/DefaultSelectTranslator.java
@@ -157,14 +157,19 @@ public class DefaultSelectTranslator extends QueryAssembler implements
SelectTra
 			}
 
 			if (!suppressingDistinct) {
-				queryBuf.append(buildDistinctStatement() + " ");
+				queryBuf.append(buildDistinctStatement()).append(" ");
 			}
 		}
 
 		// convert ColumnDescriptors to column names
 		List<String> selectColumnExpList = new ArrayList<>();
 		for (ColumnDescriptor column : resultColumns) {
-			String fullName = strategy.quotedIdentifier(dataMap, column.getNamePrefix(), column.getName());
+			String fullName;
+			if(column.isExpression()) {
+				fullName = column.getName();
+			} else {
+				fullName = strategy.quotedIdentifier(dataMap, column.getNamePrefix(), column.getName());
+			}
 			selectColumnExpList.add(fullName);
 		}
 
@@ -351,7 +356,7 @@ public class DefaultSelectTranslator extends QueryAssembler implements
SelectTra
 		SelectQuery<?> query = getSelectQuery();
 
 		if(query.getColumns() != null && !query.getColumns().isEmpty()) {
-			appendOverridedColumns(columns, query);
+			appendOverriddenColumns(columns, query);
 		} else if (query.getRoot() instanceof DbEntity) {
 			appendDbEntityColumns(columns, query);
 		} else if (getQueryMetadata().getPageSize() > 0) {
@@ -366,7 +371,7 @@ public class DefaultSelectTranslator extends QueryAssembler implements
SelectTra
 	/**
 	 * If query contains explicit column list, use only them
 	 */
-	<T> List<ColumnDescriptor> appendOverridedColumns(List<ColumnDescriptor>
columns, SelectQuery<T> query) {
+	<T> List<ColumnDescriptor> appendOverriddenColumns(List<ColumnDescriptor>
columns, SelectQuery<T> query) {
 		groupByColumns = new HashMap<>();
 
 		QualifierTranslator qualifierTranslator = adapter.getQualifierTranslator(this);
@@ -385,6 +390,7 @@ public class DefaultSelectTranslator extends QueryAssembler implements
SelectTra
 			}
 			ColumnDescriptor descriptor = new ColumnDescriptor(builder.toString(), type);
 			descriptor.setDataRowKey(alias);
+			descriptor.setIsExpression(true);
 			columns.add(descriptor);
 
 			if(isAggregate(property)) {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e8ef2960/cayenne-server/src/test/java/org/apache/cayenne/query/ColumnSelectIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/query/ColumnSelectIT.java b/cayenne-server/src/test/java/org/apache/cayenne/query/ColumnSelectIT.java
index 025796c..0874763 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/query/ColumnSelectIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/query/ColumnSelectIT.java
@@ -30,6 +30,7 @@ import org.apache.cayenne.exp.Property;
 import org.apache.cayenne.test.jdbc.DBHelper;
 import org.apache.cayenne.test.jdbc.TableHelper;
 import org.apache.cayenne.testdo.testmap.Artist;
+import org.apache.cayenne.unit.PostgresUnitDbAdapter;
 import org.apache.cayenne.unit.UnitDbAdapter;
 import org.apache.cayenne.unit.di.server.CayenneProjects;
 import org.apache.cayenne.unit.di.server.ServerCase;
@@ -228,5 +229,53 @@ public class ColumnSelectIT extends ServerCase {
         assertEquals(5L, result[1]);
     }
 
+    @Test
+    public void testSelectWithQuoting() throws Exception {
+        if(unitDbAdapter instanceof PostgresUnitDbAdapter) {
+            // we need to convert somehow all names to lowercase on postgres, so skip it
for now
+            return;
+        }
+
+        Property<Long> paintingCount = Property.create(countExp(Artist.PAINTING_ARRAY.path()),
Long.class);
+        context.getEntityResolver().getDataMap("testmap").setQuotingSQLIdentifiers(true);
+
+        Object[] result = null;
+        try {
+            result = ColumnSelect.query(Artist.class)
+                    .columns(Artist.ARTIST_NAME, Artist.DATE_OF_BIRTH, paintingCount)
+                    .having(paintingCount.gt(4L))
+                    .selectOne(context);
+        } catch (CayenneRuntimeException ex) {
+            if(unitDbAdapter.supportsExpressionInHaving()) {
+                fail();
+            } else {
+                return;
+            }
+        }
+        assertEquals("artist2", result[0]);
+        assertEquals(5L, result[2]);
 
+        context.getEntityResolver().getDataMap("testmap").setQuotingSQLIdentifiers(false);
+    }
+
+    @Test
+    public void testSelectGroupByWithQuoting() throws Exception {
+        if(unitDbAdapter instanceof PostgresUnitDbAdapter) {
+            // we need to convert somehow all names to lowercase on postgres, so skip it
for now
+            return;
+        }
+
+        Property<Long> count = Property.create(countExp(), Long.class);
+        context.getEntityResolver().getDataMap("testmap").setQuotingSQLIdentifiers(true);
+
+        Object[] result = ColumnSelect.query(Artist.class)
+                .columns(Artist.DATE_OF_BIRTH, count)
+                .orderBy(Artist.DATE_OF_BIRTH.asc())
+                .selectFirst(context);
+
+        assertEquals(dateFormat.parse("1/1/17"), result[0]);
+        assertEquals(4L, result[1]);
+
+        context.getEntityResolver().getDataMap("testmap").setQuotingSQLIdentifiers(false);
+    }
 }


Mime
View raw message