Skip to content

Commit

Permalink
revise blob example
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Jan 29, 2025
1 parent 4651be7 commit 8a20795
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
13 changes: 13 additions & 0 deletions modules/clients/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,16 @@ tasks.register('runSqlClient', JavaExec) {
systemProperty 'org.slf4j.simpleLogger.showShortLogName', 'true'
systemProperty 'org.slf4j.simpleLogger.levelInBrackets', 'true'
}

tasks.register('runLob', JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'com.tsurugidb.tsubakuro.examples.blob.Main'

systemProperty 'tsurugi.dbname', findProperty('tsurugi.dbname') ?: 'ipc:tsurugi'

systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', findProperty('loglevel') ?: 'info'
systemProperty 'org.slf4j.simpleLogger.showThreadName', 'false'
systemProperty 'org.slf4j.simpleLogger.showShortLogName', 'true'
systemProperty 'org.slf4j.simpleLogger.levelInBrackets', 'true'
systemProperty 'tsurugi.dbname', 'ipc:mock'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tsurugidb.tsubakuro.examples.blob;

import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -71,9 +72,9 @@ public static void main(String[] args) {
preparedStatement = sqlClient.prepare("SELECT FROM testtable WHERE key = 1").await();

var resultSet = transaction.executeQuery(preparedStatement).await();
var metadata = resultSet.getMetadata().getColumns();
while (resultSet.nextRow()) {
int columnIndex = 0;
var metadata = resultSet.getMetadata().getColumns();
while (resultSet.nextColumn()) {
if (!resultSet.isNull()) {
switch (metadata.get(columnIndex).getAtomType()) {
Expand All @@ -94,13 +95,27 @@ public static void main(String[] args) {
break;
case BLOB:
var is = sqlClient.openInputStream(resultSet.fetchBlob()).await();
System.out.print("column name = " + metadata.get(columnIndex).getName() + ": content = '");
while (true) {
var c = is.read();
if (c < 0) {
var i = is.read();
if (i < 0) {
break;
}
System.out.print(is.read() + " ");
char c = (char) i;
if (c != '\n') {
System.out.print(c);
}
}
System.out.println("'");
break;
case CLOB:
var lr = new LineNumberReader(sqlClient.openReader(resultSet.fetchClob()).await());
System.out.print("column name = " + metadata.get(columnIndex).getName() + ": content = '");
String s;
while ((s = lr.readLine()) != null) {
System.out.print(s);
}
System.out.println("'");
break;
default:
throw new IOException("the column type is invalid");
Expand All @@ -113,17 +128,18 @@ public static void main(String[] args) {
}
resultSet.close();
} else {
// var placeholder = SqlRequest.Placeholder.newBuilder()
// .setName("blob")
// .setAtomType(SqlCommon.AtomType.BLOB)
// .build();
preparedStatement = sqlClient.prepare("INSERT INTO testTable (key, blob) VALUES (1, :blob)",
Placeholders.of("blob", SqlCommon.Blob.class)).await();
Placeholders.of("blob1", SqlCommon.Blob.class),
Placeholders.of("blob2", SqlCommon.Blob.class),
Placeholders.of("clob", SqlCommon.Clob.class)).await();

transaction.executeStatement(preparedStatement,
Parameters.blobOf("blob", Paths.get("/tmp/testChannelUp.data"))).await();
Parameters.blobOf("blob1", Paths.get("/tmp/testChannelBlob1Up.data")),
Parameters.blobOf("blob2", Paths.get("/tmp/testChannelBlob2Up.data")),
Parameters.clobOf("clob", Paths.get("/tmp/testChannelClobUp.data"))).await();
}
} catch (Exception e) {
e.printStackTrace();
System.err.println(e);
}
}
Expand Down

0 comments on commit 8a20795

Please sign in to comment.