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

fix: EXPOSED-123 ExposedBlob.getBytes() fails on Oracle with IOException #1824

Merged
merged 1 commit into from
Aug 7, 2023
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jetbrains.exposed.sql.statements.api

import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.currentDialectIfAvailable
import java.io.IOException
import java.io.InputStream

class ExposedBlob(inputStream: InputStream) {
Expand All @@ -8,13 +11,20 @@ class ExposedBlob(inputStream: InputStream) {
var inputStream = inputStream
private set

val bytes get() = inputStream.readBytes().also {
if (inputStream.markSupported()) {
inputStream.reset()
} else {
inputStream = it.inputStream()
val bytes: ByteArray
get() = inputStream.readBytes().also {
if (inputStream.markSupported()) {
try {
inputStream.reset()
} catch (_: IOException) {
if (currentDialectIfAvailable is OracleDialect) {
inputStream = it.inputStream()
}
}
} else {
inputStream = it.inputStream()
}
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ class DDLTests : DatabaseTestsBase() {
}
}

@Test fun testBlob() {
@Test
fun testBlob() {
val t = object : Table("t1") {
val id = integer("id").autoIncrement()
val b = blob("blob")
Expand All @@ -455,11 +456,6 @@ class DDLTests : DatabaseTestsBase() {
val longBytes = Random.nextBytes(1024)
val shortBlob = ExposedBlob(shortBytes)
val longBlob = ExposedBlob(longBytes)
// if (currentDialectTest.dataTypeProvider.blobAsStream) {
// SerialBlob(bytes)
// } else connection.createBlob().apply {
// setBytes(1, bytes)
// }
Comment on lines -458 to -462
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither blobAsStream nor createBlob exist in the codebase anymore.


val id1 = t.insert {
it[t.b] = shortBlob
Expand Down Expand Up @@ -497,7 +493,7 @@ class DDLTests : DatabaseTestsBase() {
val defaultBlobStr = "test"
val defaultBlob = ExposedBlob(defaultBlobStr.encodeToByteArray())

val TestTable = object : Table("TestTable") {
val testTable = object : Table("TestTable") {
val number = integer("number")
val blobWithDefault = blob("blobWithDefault").default(defaultBlob)
}
Expand All @@ -506,18 +502,18 @@ class DDLTests : DatabaseTestsBase() {
when (testDb) {
TestDB.MYSQL -> {
expectException<ExposedSQLException> {
SchemaUtils.create(TestTable)
SchemaUtils.create(testTable)
}
}
else -> {
SchemaUtils.create(TestTable)
SchemaUtils.create(testTable)

TestTable.insert {
testTable.insert {
it[number] = 1
}
assertEquals(defaultBlobStr, String(TestTable.selectAll().first()[TestTable.blobWithDefault].bytes))
assertEquals(defaultBlobStr, String(testTable.selectAll().first()[testTable.blobWithDefault].bytes))

SchemaUtils.drop(TestTable)
SchemaUtils.drop(testTable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class EntityTests : DatabaseTestsBase() {
}
}

@Test fun testBlobField() {
@Test
fun testBlobField() {
withTables(EntityTestsData.YTable) {
val y1 = EntityTestsData.YEntity.new {
x = false
Expand Down