PATCH: Ingres limit query support
---------------------------------
Key: CAY-1659
URL: https://issues.apache.org/jira/browse/CAY-1659
Project: Cayenne
Issue Type: Improvement
Components: Database integration
Affects Versions: Short term future
Environment: Developed for Ingres 10 (open source edition)
Reporter: Lucas Holt
Priority: Minor
Fix For: 3.1M4
Adds support for Ingres's FIRST keyword to limit select query results. Code is
based on existing implementation for Microsoft SQLServer. The behavior is
similar although the name of the keyword is FIRST rather than TOP. Note that
unlike SQL Server, DISTINCT must come after the limit.
I also updated the URL to Ingres as it's no longer a CA product.
Index: src/main/java/org/apache/cayenne/dba/ingres/IngresSelectTranslator.java
===================================================================
--- src/main/java/org/apache/cayenne/dba/ingres/IngresSelectTranslator.java
(revision 0)
+++ src/main/java/org/apache/cayenne/dba/ingres/IngresSelectTranslator.java
(working copy)
@@ -0,0 +1,46 @@
+/*****************************************************************
+ * 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.cayenne.dba.ingres;
+
+import org.apache.cayenne.access.trans.SelectTranslator;
+import org.apache.cayenne.query.QueryMetadata;
+
+public class IngresSelectTranslator extends SelectTranslator {
+
+ @Override
+ protected void appendLimitAndOffsetClauses(StringBuilder buffer) {
+ QueryMetadata metadata = getQuery().getMetaData(getEntityResolver());
+
+ int limit = metadata.getFetchLimit();
+ int offset = metadata.getFetchOffset();
+
+ if (limit > 0) {
+ String sql = buffer.toString();
+
+ // If contains distinct insert first limit before
+ if (sql.startsWith("SELECT DISTINCT ")) {
+ buffer.replace(0, 15, "SELECT FIRST " + (offset +
limit) + " DISTINCT ");
+
+ } else {
+ buffer.replace(0, 6, "SELECT FIRST " + (offset +
limit));
+ }
+ }
+ }
+
+}
\ No newline at end of file
Index: src/main/java/org/apache/cayenne/dba/ingres/IngresSelectAction.java
===================================================================
--- src/main/java/org/apache/cayenne/dba/ingres/IngresSelectAction.java
(revision 0)
+++ src/main/java/org/apache/cayenne/dba/ingres/IngresSelectAction.java
(working copy)
@@ -0,0 +1,47 @@
+/*****************************************************************
+ * 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.cayenne.dba.ingres;
+
+import java.sql.Connection;
+
+import org.apache.cayenne.access.jdbc.SelectAction;
+import org.apache.cayenne.access.trans.SelectTranslator;
+import org.apache.cayenne.dba.JdbcAdapter;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.query.SelectQuery;
+
+public class IngresSelectAction extends SelectAction {
+
+ public IngresSelectAction(SelectQuery query, JdbcAdapter adapter,
+ EntityResolver entityResolver) {
+ super(query, adapter, entityResolver);
+ }
+
+ @Override
+ protected SelectTranslator createTranslator(Connection connection) {
+ SelectTranslator translator = new IngresSelectTranslator();
+ translator.setQuery(query);
+ translator.setAdapter(adapter);
+ translator.setEntityResolver(getEntityResolver());
+ translator.setConnection(connection);
+ translator.setJdbcEventLogger(adapter.getJdbcEventLogger());
+ return translator;
+ }
+
+}
Index: src/main/java/org/apache/cayenne/dba/ingres/IngresAdapter.java
===================================================================
--- src/main/java/org/apache/cayenne/dba/ingres/IngresAdapter.java
(revision 1240211)
+++ src/main/java/org/apache/cayenne/dba/ingres/IngresAdapter.java
(working copy)
@@ -29,6 +29,7 @@
import org.apache.cayenne.access.types.ExtendedType;
import org.apache.cayenne.access.types.ExtendedTypeFactory;
import org.apache.cayenne.access.types.ExtendedTypeMap;
+import org.apache.cayenne.access.DataNode;
import org.apache.cayenne.configuration.RuntimeProperties;
import org.apache.cayenne.dba.JdbcAdapter;
import org.apache.cayenne.dba.PkGenerator;
@@ -37,9 +38,11 @@
import org.apache.cayenne.di.Inject;
import org.apache.cayenne.map.DbAttribute;
import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.SQLAction;
/**
- * DbAdapter implementation for <a
href="http://opensource.ca.com/projects/ingres/">Ingres</a>.
+ * DbAdapter implementation for <a
href="http://www.actian.com/products/ingres">Ingres</a>.
* Sample connection settings to use with Ingres are shown below:
*
* <pre>
@@ -59,6 +62,17 @@
@Inject(EXTENDED_TYPE_FACTORY_LIST) List<ExtendedTypeFactory>
extendedTypeFactories) {
super(runtimeProperties, defaultExtendedTypes, userExtendedTypes,
extendedTypeFactories);
}
+
+ /**
+ * Uses IngresActionBuilder to create the right action.
+ *
+ * @since 1.2
+ */
+ @Override
+ public SQLAction getAction(Query query, DataNode node) {
+ return query
+ .createSQLAction(new IngresActionBuilder(this,
node.getEntityResolver()));
+ }
@Override
public QualifierTranslator getQualifierTranslator(QueryAssembler
queryAssembler) {
Index: src/main/java/org/apache/cayenne/dba/ingres/IngresActionBuilder.java
===================================================================
--- src/main/java/org/apache/cayenne/dba/ingres/IngresActionBuilder.java
(revision 0)
+++ src/main/java/org/apache/cayenne/dba/ingres/IngresActionBuilder.java
(working copy)
@@ -0,0 +1,45 @@
+/*****************************************************************
+ * 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.cayenne.dba.ingres;
+
+import org.apache.cayenne.access.jdbc.BatchAction;
+import org.apache.cayenne.dba.JdbcActionBuilder;
+import org.apache.cayenne.dba.JdbcAdapter;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.query.BatchQuery;
+import org.apache.cayenne.query.ProcedureQuery;
+import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.query.SQLAction;
+
+
+/**
+ * @since 1.2
+ */
+public class IngresActionBuilder extends JdbcActionBuilder {
+
+ public IngresActionBuilder(JdbcAdapter adapter, EntityResolver resolver) {
+ super(adapter, resolver);
+ }
+
+ @Override
+ public SQLAction objectSelectAction(SelectQuery query) {
+ return new IngresSelectAction(query, adapter, entityResolver);
+ }
+}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira