Skip to content

Commit

Permalink
IGNITE-6141 JDBC: add basic support for BLOB and CLOB types (#11492)
Browse files Browse the repository at this point in the history
Co-authored-by: rkondakov <rkondakov@gridgain.com>
  • Loading branch information
skorotkov and rkondakov authored Sep 11, 2024
1 parent 2280dea commit 95afef0
Show file tree
Hide file tree
Showing 26 changed files with 1,242 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.cache.query.annotations.QuerySqlField;
Expand Down Expand Up @@ -53,7 +55,7 @@ public abstract class JdbcAbstractDmlStatementSelfTest extends GridCommonAbstrac
CFG_URL_PREFIX + "cache=" + DEFAULT_CACHE_NAME + "@modules/clients/src/test/config/jdbc-bin-config.xml";

/** SQL SELECT query for verification. */
static final String SQL_SELECT = "select _key, id, firstName, lastName, age, data from Person";
static final String SQL_SELECT = "select _key, id, firstName, lastName, age, data, text from Person";

/** Alias for _key */
private static final String KEY_ALIAS = "key";
Expand Down Expand Up @@ -112,6 +114,7 @@ final CacheConfiguration binaryCacheConfig() {
e.addQueryField("firstName", String.class.getName(), null);
e.addQueryField("lastName", String.class.getName(), null);
e.addQueryField("data", byte[].class.getName(), null);
e.addQueryField("text", String.class.getName(), null);

cache.setQueryEntities(Collections.singletonList(e));

Expand Down Expand Up @@ -189,6 +192,18 @@ static String str(byte[] arr) {
}
}

/**
* @param clob Clob.
*/
static String str(Clob clob) {
try {
return clob.getSubString(1, (int)clob.length());
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}

/**
* Person.
*/
Expand All @@ -213,6 +228,10 @@ static class Person implements Serializable {
@QuerySqlField
private final byte[] data;

/** CLOB data. */
@QuerySqlField
private final String text;

/**
* @param id ID.
* @param firstName First name.
Expand All @@ -229,6 +248,7 @@ static class Person implements Serializable {
this.lastName = lastName;
this.age = age;
this.data = getBytes(lastName);
this.text = firstName + " " + lastName;
}

/** {@inheritDoc} */
Expand All @@ -241,6 +261,8 @@ static class Person implements Serializable {
if (id != person.id) return false;
if (age != person.age) return false;
if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) return false;
if (data != null ? !Arrays.equals(data, person.data) : person.data != null) return false;
if (text != null ? !text.equals(person.text) : person.text != null) return false;
return lastName != null ? lastName.equals(person.lastName) : person.lastName == null;

}
Expand All @@ -251,6 +273,8 @@ static class Person implements Serializable {
result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + age;
result = 31 * result + (data != null ? Arrays.hashCode(data) : 0);
result = 31 * result + (text != null ? text.hashCode() : 0);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
*/
public abstract class JdbcAbstractUpdateStatementSelfTest extends JdbcAbstractDmlStatementSelfTest {
/** SQL query to populate cache. */
private static final String ITEMS_SQL = "insert into Person(_key, id, firstName, lastName, age, data) values " +
"('p1', 1, 'John', 'White', 25, RAWTOHEX('White')), " +
"('p2', 2, 'Joe', 'Black', 35, RAWTOHEX('Black')), " +
"('p3', 3, 'Mike', 'Green', 40, RAWTOHEX('Green'))";
private static final String ITEMS_SQL = "insert into Person(_key, id, firstName, lastName, age, data, text) values " +
"('p1', 1, 'John', 'White', 25, RAWTOHEX('White'), 'John White'), " +
"('p2', 2, 'Joe', 'Black', 35, RAWTOHEX('Black'), 'Joe Black'), " +
"('p3', 3, 'Mike', 'Green', 40, RAWTOHEX('Green'), 'Mike Green')";

/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public void testGetBytes() throws Exception {
res = blob.getBytes(1, 0);
assertEquals(0, res.length);

blob = new JdbcBlob(new byte[0]);
assertEquals(0, blob.getBytes(1, 0).length);

blob.free();

try {
Expand Down
Loading

0 comments on commit 95afef0

Please sign in to comment.