Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query bean PByteArray type for byte[] #2337

Merged
merged 3 commits into from
Sep 2, 2021
Merged
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
27 changes: 27 additions & 0 deletions ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.ebean.typequery;

/**
* Byte array property (<code>byte[]</code>).
*
* @param <R> the root query bean type
*/
public class PByteArray<R> extends PBaseValueEqual<R, byte[]> {
/**
* Construct with a property name and root instance.
*
* @param name property name
* @param root the root query bean instance
*/
public PByteArray(String name, R root) {
super(name, root);
}

/**
* Construct with additional path prefix.
*/
public PByteArray(String name, R root, String prefix) {
super(name, root, prefix);
}


}
12 changes: 12 additions & 0 deletions ebean-querybean/src/test/java/org/example/domain/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Lob;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -68,6 +69,9 @@ public enum Status {
@OneToMany(mappedBy = "customer", cascade = CascadeType.PERSIST)
List<Contact> contacts;

@Column @Lob
byte[] photo;

public Status getStatus() {
return status;
}
Expand Down Expand Up @@ -156,6 +160,14 @@ public void setEmail(final ValidEmail email) {
this.email = email;
}

public byte[] getPhoto() {
return photo;
}

public void setPhoto(byte[] photo) {
this.photo = photo;
}

/**
* Helper method to add a contact to the customer.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.querytest;

import io.ebean.DB;
import io.ebean.Database;
import org.example.domain.Customer;
import org.example.domain.query.QCustomer;
import org.junit.Test;
import org.junit.Assert;

import java.util.List;

public class QCustomerByteArrayTest {
@Test
public void testIsNull() {
final byte[] photo1= new byte[] { 0x05, 0x10, 0x20, 0x30 };
final byte[] photo2=new byte[] { 0x50, 0x55, 0x60, 0x70, 0x10 };
final Database db=DB.getDefault();
final String noPhoto="no_photo_client";
final String wPhoto1="with_photo1_client";
final String wPhoto2="with_photo2_client";

Customer c;

c=new Customer();
c.setName(noPhoto);
c.save();

c=new Customer();
c.setName(wPhoto1);
c.setPhoto(photo1);
c.save();

c=new Customer();
c.setName(wPhoto2);
c.setPhoto(photo2);
c.save();

Assert.assertNotNull(new QCustomer(db).name.eq(noPhoto).photo.isNull().findOne());
// Assert.assertNotNull(new QCustomer(db).name.eq(noPhoto).photo.ne(photo1).findOne()); // Fails
Assert.assertNull(new QCustomer(db).name.eq(noPhoto).photo.isNotNull().findOne());
Assert.assertNull(new QCustomer(db).name.eq(noPhoto).photo.eq(photo1).findOne());


Assert.assertNotNull(new QCustomer(db).name.eq(wPhoto1).photo.isNotNull().findOne());
Assert.assertNotNull(new QCustomer(db).name.eq(wPhoto1).photo.eq(photo1).findOne());
Assert.assertNotNull(new QCustomer(db).photo.eq(photo1).findOne());
Assert.assertNotNull(new QCustomer(db).name.eq(wPhoto1).photo.ne(photo2).findOne());

Assert.assertNull(new QCustomer(db).name.eq(wPhoto1).photo.isNull().findOne());
Assert.assertNull(new QCustomer(db).name.eq(wPhoto1).photo.eq(photo2).findOne());
Assert.assertNull(new QCustomer(db).name.eq(wPhoto1).photo.ne(photo1).findOne());

Assert.assertNotNull(new QCustomer(db).name.eq(wPhoto2).photo.isNotNull().findOne());
Assert.assertNotNull(new QCustomer(db).name.eq(wPhoto2).photo.eq(photo2).findOne());
Assert.assertNotNull(new QCustomer(db).photo.eq(photo2).findOne());
Assert.assertNotNull(new QCustomer(db).name.eq(wPhoto2).photo.ne(photo1).findOne());

Assert.assertNull(new QCustomer(db).name.eq(wPhoto2).photo.isNull().findOne());
Assert.assertNull(new QCustomer(db).name.eq(wPhoto2).photo.eq(photo1).findOne());
Assert.assertNull(new QCustomer(db).name.eq(wPhoto2).photo.ne(photo2).findOne());

Assert.assertEquals(2, new QCustomer(db).photo.in(photo1, photo2).findList().size());


List<Customer> ordered = new QCustomer(db).name.in(wPhoto1, wPhoto2).photo.desc().findList();
Assert.assertEquals(wPhoto2, ordered.get(0).getName());
Assert.assertArrayEquals(photo2, ordered.get(0).getPhoto());
Assert.assertEquals(wPhoto1, ordered.get(1).getName());
Assert.assertArrayEquals(photo1, ordered.get(1).getPhoto());

ordered = new QCustomer(db).name.in(wPhoto1, wPhoto2).photo.asc().findList();
Assert.assertEquals(wPhoto2, ordered.get(1).getName());
Assert.assertArrayEquals(photo2, ordered.get(1).getPhoto());
Assert.assertEquals(wPhoto1, ordered.get(0).getName());
Assert.assertArrayEquals(photo1, ordered.get(0).getPhoto());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PropertyTypeMap {
map.put("java.lang.Class<?>", new PropertyType("PClass"));
addType(Locale.class);
addType(File.class);
map.put("byte[]", new PropertyType("PByteArray"));
addType(InetAddress.class);
map.put(URI.class.getName(), new PropertyType("PUri"));
map.put(URL.class.getName(), new PropertyType("PUrl"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PropertyTypeMap {
addType(Locale.class);
map.put("java.lang.Class<?>", new PropertyType("PClass"));
addType(File.class);
map.put("byte[]", new PropertyType("PByteArray"));
addType(InetAddress.class);
map.put(URI.class.getName(), new PropertyType("PUri"));
map.put(URL.class.getName(), new PropertyType("PUrl"));
Expand Down