Added PByteArray query bean type for byte[] column type.

This commit is contained in:
Ryszard Trojnacki
2021-08-31 11:34:35 +02:00
parent afdfbd6173
commit 847ce5db88
5 changed files with 118 additions and 0 deletions
@@ -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);
}
}
@@ -13,6 +13,7 @@ import javax.persistence.Entity;
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;
@@ -68,6 +69,9 @@ public class Customer extends BaseModel {
@OneToMany(mappedBy = "customer", cascade = CascadeType.PERSIST)
List<Contact> contacts;
@Column @Lob
byte[] photo;
public Status getStatus() {
return status;
}
@@ -156,6 +160,14 @@ public class Customer extends BaseModel {
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.
*/
@@ -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());
}
}
@@ -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"));
@@ -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"));