Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Bug Fixes:

CAY-3071 DataNodeSQLAction could lose update count info
CAY-3073 AI plugin / MCP db import schema description inconsistencies
CAY-3079 IN qualifier binds each byte of a binary value as a separate parameter

----------------------------------
Release: 5.0-RC1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
import org.apache.cayenne.query.QueryMetadata;
import org.apache.cayenne.reflect.ClassDescriptor;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Processes a number of DataRow sets corresponding to a given prefetch tree, resolving
Expand Down Expand Up @@ -253,7 +255,7 @@ private void createDisjointByIdPrefetchQualifier(CayennePath pathPrefix, Prefetc
if (joins.size() == 1 && values.size() > 1) {
currentQuery.and( ExpressionFactory.inDbExp(
pathPrefix.dot(joins.get(0).getTargetName()).value(),
values.stream().flatMap(List::stream).collect(Collectors.toSet())
distinctValues(values)
));
} else { // Handle a single value or compound prefetch ID predicates
// SQL: ... (field1=? and field2=? ...) OR (field1=? and field2=? ...) etc
Expand All @@ -278,6 +280,19 @@ private void createDisjointByIdPrefetchQualifier(CayennePath pathPrefix, Prefetc
}
}

/**
* Returns the distinct values of single-column keys. A byte[] (e.g. a binary PK) has identity equals, so it
* is compared by content.
*/
private static Collection<Object> distinctValues(Set<List<Object>> values) {
Map<Object, Object> distinct = new LinkedHashMap<>();
for (List<Object> joinValues : values) {
Object value = joinValues.get(0);
distinct.putIfAbsent(value instanceof byte[] bytes ? ByteBuffer.wrap(bytes) : value, value);
}
return distinct.values();
}

@Override
public boolean startPhantomPrefetch(PrefetchTreeNode node) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ private void appendValue(Object val, SQLAppendable buffer, SQLGenerationContext
case double[] doubles -> appendValue(doubles, buffer, context);
case boolean[] booleans -> appendValue(booleans, buffer, context);
case Object[] objects -> appendValue(objects, buffer, context);
// append byte[] array as a single object
// append byte[] array as a single object for a binary column (e.g. a binary PK in an IN list)
case byte[] bytes when attribute != null && TypesMapping.isBinary(attribute.getType()) ->
appendObjectValue(buffer, context, bytes);
// otherwise treat byte[] as a list of byte values
case byte[] bytes -> appendValue(bytes, buffer, context);
default -> throw new CayenneRuntimeException("Unsupported array type %s", val.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*****************************************************************
* 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
*
* https://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.access;

import org.apache.cayenne.ObjectId;
import org.apache.cayenne.PersistenceState;
import org.apache.cayenne.Persistent;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.query.ObjectSelect;
import org.apache.cayenne.test.jdbc.TableHelper;
import org.apache.cayenne.testdo.binary_pk.BinaryPKTest1;
import org.apache.cayenne.testdo.binary_pk.BinaryPKTest2;
import org.apache.cayenne.unit.CayenneProjects;
import org.apache.cayenne.unit.CayenneTestsEnv;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

/**
* IN qualifiers with binary values, either explicit or built by "disjoint by id" prefetches.
*/
public class DataContextBinaryPKInListIT {

private static final byte[] KEY1 = {1, 2, 3};
private static final byte[] KEY2 = {4, 5, 6};
private static final byte[] KEY3 = {7, 8, 9};

@RegisterExtension
static final CayenneTestsEnv env = CayenneTestsEnv.forProject(CayenneProjects.BINARY_PK_PROJECT);

@BeforeEach
public void createTestData() throws SQLException {
if (!env.testDbAdapter().supportsBinaryPK()) {
return;
}

TableHelper tMaster = env.table("BINARY_PK_TEST1").setColumns("BIN_ID", "NAME")
.setColumnTypes(Types.VARBINARY, Types.VARCHAR);
tMaster.insert(KEY1, "master1");
tMaster.insert(KEY2, "master2");
tMaster.insert(KEY3, "master3");

TableHelper tDetail = env.table("BINARY_PK_TEST2").setColumns("ID", "FK_ID", "DETAIL_NAME")
.setColumnTypes(Types.INTEGER, Types.VARBINARY, Types.VARCHAR);
tDetail.insert(1, KEY1, "detail1");
tDetail.insert(2, KEY2, "detail2");
tDetail.insert(3, KEY2, "detail3");
}

@Test
public void inDbExp() {
if (!env.testDbAdapter().supportsBinaryPK()) {
return;
}

List<BinaryPKTest1> masters = ObjectSelect.query(BinaryPKTest1.class)
.where(ExpressionFactory.inDbExp(BinaryPKTest1.BIN_ID_PK_COLUMN, KEY1, KEY3))
.orderBy(BinaryPKTest1.NAME.asc())
.select(env.context());

assertEquals(2, masters.size());
assertEquals("master1", masters.get(0).getName());
assertEquals("master3", masters.get(1).getName());
}

@Test
public void disjointByIdPrefetchToMany() {
if (!env.testDbAdapter().supportsBinaryPK()) {
return;
}

List<BinaryPKTest1> masters = ObjectSelect.query(BinaryPKTest1.class)
.prefetch(BinaryPKTest1.BINARY_PKDETAILS.disjointById())
.orderBy(BinaryPKTest1.NAME.asc())
.select(env.context());
assertEquals(3, masters.size());

env.runWithQueriesBlocked(() -> {
assertEquals(1, masters.get(0).getBinaryPKDetails().size());
assertEquals(2, masters.get(1).getBinaryPKDetails().size());
assertEquals(0, masters.get(2).getBinaryPKDetails().size());
});
}

@Test
public void disjointByIdPrefetchToOne() {
if (!env.testDbAdapter().supportsBinaryPK()) {
return;
}

DataContext context = env.context();
List<BinaryPKTest2> details = ObjectSelect.query(BinaryPKTest2.class)
.prefetch(BinaryPKTest2.TO_BINARY_PKMASTER.disjointById())
.select(context);
assertEquals(3, details.size());

// the prefetch query must have found both masters by their keys
assertFetchedMaster(context, KEY1, "master1");
assertFetchedMaster(context, KEY2, "master2");
}

private static void assertFetchedMaster(DataContext context, byte[] key, String expectedName) {
Persistent object = context.getObjectStore().getObject(ObjectId.of("BinaryPKTest1", "BIN_ID", key));
BinaryPKTest1 master = assertInstanceOf(BinaryPKTest1.class, object);
assertEquals(PersistenceState.COMMITTED, master.getPersistenceState());
assertEquals(expectedName, master.readPropertyDirectly("name"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*****************************************************************
* 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
*
* https://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.access.sqlbuilder.sqltree;

import org.apache.cayenne.access.jdbc.PSParameter;
import org.apache.cayenne.access.sqlbuilder.DefaultSQLAppendable;
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
import org.apache.cayenne.access.sqlbuilder.SQLGenerationContext;
import org.apache.cayenne.access.types.ExtendedTypeMap;
import org.apache.cayenne.dba.DbAdapter;
import org.apache.cayenne.map.DbAttribute;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ValueNodeTest {

private SQLGenerationContext context;
private List<PSParameter<?>> bindings;

@BeforeEach
public void createContext() {
DbAdapter adapter = mock(DbAdapter.class);
when(adapter.getExtendedTypes()).thenReturn(new ExtendedTypeMap());

bindings = new ArrayList<>();
context = mock(SQLGenerationContext.class);
when(context.getAdapter()).thenReturn(adapter);
when(context.getBindings()).thenReturn(bindings);
}

@Test
public void binaryValuesInList() {
for (int type : new int[]{Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY, Types.BLOB}) {
bindings.clear();
byte[] key1 = {1, 2, 3};
byte[] key2 = {4, 5, 6};

ValueNode node = new ValueNode(new Object[]{key1, key2}, true, attribute(type), true);
String sql = append(node);

assertEquals(2, sql.chars().filter(c -> c == '?').count(), sql);
assertEquals(2, bindings.size());
assertSame(key1, bindings.get(0).value());
assertSame(key2, bindings.get(1).value());
}
}

@Test
public void byteArrayForNonBinaryAttributeIsExpanded() {
ValueNode node = new ValueNode(new byte[]{1, 2, 3}, true, attribute(Types.TINYINT), true);
String sql = append(node);

assertEquals(3, sql.chars().filter(c -> c == '?').count(), sql);
assertEquals(3, bindings.size());
assertEquals((byte) 1, bindings.get(0).value());
}

@Test
public void byteArrayWithoutAttributeIsExpanded() {
ValueNode node = new ValueNode(new byte[]{1, 2, 3}, true, null, true);
append(node);

assertEquals(3, bindings.size());
}

private String append(ValueNode node) {
SQLAppendable buffer = new DefaultSQLAppendable(null);
node.append(buffer, context);
return buffer.toString();
}

private static DbAttribute attribute(int type) {
DbAttribute attribute = new DbAttribute("KEY");
attribute.setType(type);
return attribute;
}
}
Loading