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

Support JDBC driver for iSeries DB2 #930

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d4645b0
added jt400 driver and basic DB2 Dialect
May 27, 2020
3eee137
Update exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors…
V3lop5 Mar 7, 2021
9bcd454
Merge remote-tracking branch 'jb/master'
V3lop5 May 1, 2021
2841976
Changed DB2FunctionProvider to internal object
V3lop5 May 2, 2021
b5ef8b9
Added support for db2 LUW
V3lop5 May 2, 2021
3ad3164
Added non working db2 test database
V3lop5 May 2, 2021
fe70789
Applied db2-tests patch from @Tapac
V3lop5 May 13, 2021
6705356
hide output from docker ibm/db2 image
V3lop5 May 30, 2021
657b142
do not specify NULL on column definition for db2
V3lop5 May 30, 2021
fd4aa18
Merge branch 'JetBrains:master' into master
V3lop5 Jun 25, 2021
b8de500
Merge branch 'master' of https://github.com/JetBrains/Exposed into Je…
V3lop5 Mar 14, 2022
c9e318c
Merge branch 'JetBrains-master'
V3lop5 Mar 14, 2022
9d05134
Added db2 test
V3lop5 Mar 14, 2022
024162a
db2 resultset compat
Mar 20, 2022
056c36f
local db2 config
Mar 20, 2022
3db771a
fix limit
Mar 20, 2022
31b63d0
db2 column type and functionProvider
Mar 20, 2022
747349e
make dual sysdummy1 in db2
Mar 20, 2022
e083b8e
fix insert tests,fix table exists in db2,fix seq max number in db2
Mar 20, 2022
2cf2243
fix tableExist
Mar 20, 2022
1d08716
fix tableNames for db2
sola1tmy Mar 20, 2022
c9eb572
ignore some tests which db2 not support
sola1tmy Mar 20, 2022
134b89e
union did't gurantee the order,so I sort the result to compare in tes…
sola1tmy Mar 20, 2022
d6a2ac6
ignore 2 tests
sola1tmy Mar 20, 2022
e1684dc
Merge pull request #19 from sola1tmy/newdb2
V3lop5 Mar 24, 2022
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
22 changes: 22 additions & 0 deletions buildScripts/docker/docker-compose-db2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.3'

services:
db2:
container_name: DB2
image: ibmcom/db2:latest
ports:
- "50000:50000"
# Hide console output of db2 image
logging:
driver: "none"
environment:
LICENSE: accept
DB2INSTANCE: "inst"
DB2INST1_PASSWORD: "yourStrong(!)Password"
DBNAME: "testdb"
volumes:
- db:/database
privileged: true
volumes:
db:
external: false
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ class DBTestingPlugin : Plugin<Project> {
testRuntimeOnly("org.mariadb.jdbc", "mariadb-java-client", Versions.mariaDB)
}

val db2 = register<DBTest>("db2Test", "db2") {
testRuntimeOnly("com.ibm.db2", "jcc", Versions.db2)
}

named<Test>("test") {
delegatedTo(
h2,
sqlite,
mysql51,
postgres,
postgresNG
postgresNG,
db2
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object Versions {
const val postgreNG = "0.8.9"
const val sqlLite3 = "3.36.0.3"
const val sqlserver = "8.4.1.jre8"
const val db2 = "11.5.7.0"

/** Spring **/
const val springFramework = "5.3.13"
Expand Down
22 changes: 12 additions & 10 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Column.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.jetbrains.exposed.sql.vendors.DB2Dialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.inProperCase
import java.util.*
Expand Down Expand Up @@ -45,12 +46,13 @@ class Column<T>(

private val isLastColumnInPK: Boolean get() = table.primaryKey?.columns?.last() == this

internal val isPrimaryConstraintWillBeDefined: Boolean get() = when {
currentDialect is SQLiteDialect && columnType.isAutoInc -> false
table.isCustomPKNameDefined() -> isLastColumnInPK
isOneColumnPK() -> false
else -> isLastColumnInPK
}
internal val isPrimaryConstraintWillBeDefined: Boolean
get() = when {
currentDialect is SQLiteDialect && columnType.isAutoInc -> false
table.isCustomPKNameDefined() -> isLastColumnInPK
isOneColumnPK() -> false
else -> isLastColumnInPK
}

override fun createStatement(): List<String> {
val alterTablePrefix = "ALTER TABLE ${TransactionManager.current().identity(table)} ADD"
Expand Down Expand Up @@ -113,11 +115,11 @@ class Column<T>(
}

if (columnType.nullable || (defaultValue != null && defaultValueFun == null && !currentDialect.isAllowedAsColumnDefault(defaultValue))) {
append(" NULL")
} else if (!isPKColumn || (currentDialect is SQLiteDialect && !isSQLiteAutoIncColumn)) {
if (currentDialect !is DB2Dialect) // in db2 columns are null by default - there is no NULL keyword
append(" NULL")
} else if (!isPKColumn || (currentDialect is DB2Dialect && !columnType.isAutoInc)|| (currentDialect is SQLiteDialect && !isSQLiteAutoIncColumn)) {
append(" NOT NULL")
}

if (!modify && isOneColumnPK() && !isPrimaryConstraintWillBeDefined && !isSQLiteAutoIncColumn) {
append(" PRIMARY KEY")
}
Expand All @@ -131,7 +133,7 @@ class Column<T>(
table = this.table,
name = this.name,
columnType = columnType
).also{
).also {
it.foreignKey = this.foreignKey
it.defaultValueFun = this.defaultValueFun
it.dbDefaultValue = this.dbDefaultValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetbrains.exposed.sql

import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.DB2Dialect
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
Expand Down Expand Up @@ -118,10 +119,16 @@ data class ForeignKeyConstraint(
append(" ON DELETE $deleteRule")
}
if (updateRule != ReferenceOption.NO_ACTION) {
if (currentDialect is OracleDialect) {
exposedLogger.warn("Oracle doesn't support FOREIGN KEY with ON UPDATE clause. Please check your $fromTableName table.")
} else {
append(" ON UPDATE $updateRule")
when {
currentDialect is OracleDialect -> {
exposedLogger.warn("Oracle doesn't support FOREIGN KEY with ON UPDATE clause. Please check your $fromTableName table.")
}
currentDialect is DB2Dialect && updateRule == ReferenceOption.CASCADE -> {
exposedLogger.warn("DB2 doesn't support FOREIGN KEY with ON UPDATE clause. Please check your $fromTableName table.")
}
else -> {
append(" ON UPDATE $updateRule")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ class Database private constructor(
"jdbc:mariadb" to "org.mariadb.jdbc.Driver",
"jdbc:oracle" to "oracle.jdbc.OracleDriver",
"jdbc:sqlite" to "org.sqlite.JDBC",
"jdbc:sqlserver" to "com.microsoft.sqlserver.jdbc.SQLServerDriver"
"jdbc:sqlserver" to "com.microsoft.sqlserver.jdbc.SQLServerDriver",
"jdbc:as400" to "com.ibm.as400.access.AS400JDBCDriver",
"jdbc:db2" to "com.ibm.db2.jcc.DB2Driver"
)
private val dialectMapping = mutableMapOf(
"jdbc:h2" to H2Dialect.dialectName,
Expand All @@ -93,7 +95,9 @@ class Database private constructor(
"jdbc:mariadb" to MariaDBDialect.dialectName,
"jdbc:oracle" to OracleDialect.dialectName,
"jdbc:sqlite" to SQLiteDialect.dialectName,
"jdbc:sqlserver" to SQLServerDialect.dialectName
"jdbc:sqlserver" to SQLServerDialect.dialectName,
"jdbc:as400" to DB2Dialect.dialectName,
"jdbc:db2" to DB2Dialect.dialectName
)

init {
Expand All @@ -105,6 +109,7 @@ class Database private constructor(
registerDialect(OracleDialect.dialectName) { OracleDialect() }
registerDialect(SQLServerDialect.dialectName) { SQLServerDialect() }
registerDialect(MariaDBDialect.dialectName) { MariaDBDialect() }
registerDialect(DB2Dialect.dialectName) { DB2Dialect() }
}

fun registerDialect(prefix: String, dialect: () -> DatabaseDialect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.exposed.sql

import org.jetbrains.exposed.sql.statements.Statement
import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import org.jetbrains.exposed.sql.vendors.DB2Dialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import java.sql.ResultSet
import java.util.*
Expand Down Expand Up @@ -119,6 +120,10 @@ open class Query(override var set: FieldSet, where: Op<Boolean>?) : AbstractQuer
set.source.describe(transaction, this)
}

if (set.source == Table.Dual && currentDialect is DB2Dialect) {
append(" FROM SYSIBM.SYSDUMMY1 ")
}

where?.let {
append(" WHERE ")
+it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.exceptions.DuplicateColumnException
import org.jetbrains.exposed.sql.statements.api.ExposedBlob
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.PostgreSQLDialect
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.*
import org.jetbrains.exposed.sql.vendors.currentDialectIfAvailable
import org.jetbrains.exposed.sql.vendors.inProperCase
import java.math.BigDecimal
Expand Down Expand Up @@ -1068,7 +1065,7 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
it,
startWith = 1,
minValue = 1,
maxValue = Long.MAX_VALUE
maxValue = if (currentDialect is DB2Dialect) Int.MAX_VALUE.toLong() else Long.MAX_VALUE
).createStatement()
}.orEmpty()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ open class InsertStatement<Key : Any>(val table: Table, val isIgnore: Boolean =
override fun prepared(transaction: Transaction, sql: String): PreparedStatementApi = when {
// https://github.com/pgjdbc/pgjdbc/issues/1168
// Column names always escaped/quoted in RETURNING clause
autoIncColumns.isNotEmpty() && currentDialect is PostgreSQLDialect ->
autoIncColumns.isNotEmpty() && currentDialect is PostgreSQLDialect ->
transaction.connection.prepareStatement(sql, true)

autoIncColumns.isNotEmpty() ->
// http://viralpatel.net/blogs/oracle-java-jdbc-get-primary-key-insert-sql/
transaction.connection.prepareStatement(sql, autoIncColumns.map { it.name.inProperCase() }.toTypedArray())
transaction.connection.prepareStatement(sql, autoIncColumns.map { transaction.identity(it) }.toTypedArray())

else -> transaction.connection.prepareStatement(sql, false)
}
Expand Down
Loading