From 1e191327c211a6a5055f51259adb3da0a43afdd3 Mon Sep 17 00:00:00 2001 From: Dario Foresti Date: Tue, 28 Nov 2023 17:57:46 +0100 Subject: [PATCH 1/5] Fix for chain failure after read operation - patch for maven setup --- .mvn/jvm.config | 1 + .../kotlin/com/smeup/dbnative/DBMManager.kt | 16 +- .../com/smeup/dbnative/DBManagerBaseImpl.kt | 32 ++- .../smeup/dbnative/DBNativeAccessConfig.kt | 10 +- .../kotlin/com/smeup/dbnative/file/DBFile.kt | 15 +- .../kotlin/com/smeup/dbnative/file/Record.kt | 16 +- .../kotlin/com/smeup/dbnative/file/Result.kt | 2 +- .../kotlin/com/smeup/dbnative/log/Logging.kt | 100 ++++--- .../dbnative/metadata/MetadataRegister.kt | 15 +- .../metadata/file/FSMetadataRegisterImpl.kt | 20 +- .../metadata/file/PropertiesSerializer.kt | 40 +-- .../kotlin/com/smeup/dbnative/model/Field.kt | 2 +- .../com/smeup/dbnative/model/FileMetadata.kt | 2 +- .../com/smeup/dbnative/utils/Comparison.kt | 2 +- .../com/smeup/dbnative/utils/FileUtilities.kt | 12 +- .../dbnative/PropertiesSerializationTest.kt | 3 +- .../com/smeup/dbnative/utils/FieldType.kt | 251 ++++++++---------- .../kotlin/com/smeup/dbnative/utils/Types.kt | 94 ++++--- pom.xml | 14 + 19 files changed, 358 insertions(+), 289 deletions(-) create mode 100644 .mvn/jvm.config diff --git a/.mvn/jvm.config b/.mvn/jvm.config new file mode 100644 index 0000000..45d7a1d --- /dev/null +++ b/.mvn/jvm.config @@ -0,0 +1 @@ +--add-opens java.base/java.lang=ALL-UNNAMED \ No newline at end of file diff --git a/base/src/main/kotlin/com/smeup/dbnative/DBMManager.kt b/base/src/main/kotlin/com/smeup/dbnative/DBMManager.kt index e8b2dc7..31e24a0 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/DBMManager.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/DBMManager.kt @@ -26,16 +26,24 @@ import com.smeup.dbnative.model.FileMetadata * A datasource can contains either only tables(views) or only documents. * File is an abstraction of table, view or document. * */ -interface DBMManager : AutoCloseable{ - val connectionConfig : ConnectionConfig - +interface DBMManager : AutoCloseable { + val connectionConfig: ConnectionConfig fun existFile(name: String): Boolean - fun registerMetadata(metadata: FileMetadata, overwrite: Boolean) + + fun registerMetadata( + metadata: FileMetadata, + overwrite: Boolean, + ) + fun metadataOf(name: String): FileMetadata + fun openFile(name: String): DBFile + fun closeFile(name: String) + fun unregisterMetadata(name: String) + /** * Validate connectionConfig. If validation fails, implementation has to throw an IllegalArgumentException * */ diff --git a/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt b/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt index 395d642..82fc982 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt @@ -15,7 +15,6 @@ * */ - package com.smeup.dbnative import com.smeup.dbnative.log.Logger @@ -37,11 +36,17 @@ abstract class DBManagerBaseImpl : DBMManager { return getMetadataRegister().getMetadata(name.toUpperCase()) } - override fun registerMetadata(metadata: FileMetadata, overwrite: Boolean) { + override fun registerMetadata( + metadata: FileMetadata, + overwrite: Boolean, + ) { if (getMetadataRegister().contains(metadata.name)) { - if (overwrite) getMetadataRegister().remove(metadata.name) - else return - //TODO: send exception (existent metadata and no overwrite) + if (overwrite) { + getMetadataRegister().remove(metadata.name) + } else { + return + } + // TODO: send exception (existent metadata and no overwrite) } getMetadataRegister().registerMetadata(metadata, overwrite) @@ -58,7 +63,6 @@ abstract class DBManagerBaseImpl : DBMManager { } companion object { - val register: MetadataRegister get() { return FSMetadataRegisterImpl @@ -68,11 +72,17 @@ abstract class DBManagerBaseImpl : DBMManager { return register } - fun staticRegisterMetadata(metadata: FileMetadata, overwrite: Boolean) { + fun staticRegisterMetadata( + metadata: FileMetadata, + overwrite: Boolean, + ) { if (getMetadataRegister().contains(metadata.name)) { - if (overwrite) getMetadataRegister().remove(metadata.name) - else return - //TODO: send exception (existent metadata and no overwrite) + if (overwrite) { + getMetadataRegister().remove(metadata.name) + } else { + return + } + // TODO: send exception (existent metadata and no overwrite) } getMetadataRegister().registerMetadata(metadata, overwrite) @@ -85,7 +95,7 @@ abstract class DBManagerBaseImpl : DBMManager { } fun staticGetMetadata(name: String): FileMetadata { - return getMetadataRegister().getMetadata(name); + return getMetadataRegister().getMetadata(name) } } } diff --git a/base/src/main/kotlin/com/smeup/dbnative/DBNativeAccessConfig.kt b/base/src/main/kotlin/com/smeup/dbnative/DBNativeAccessConfig.kt index beec4c5..387d5d7 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/DBNativeAccessConfig.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/DBNativeAccessConfig.kt @@ -19,8 +19,8 @@ package com.smeup.dbnative import com.smeup.dbnative.log.Logger -data class DBNativeAccessConfig (val connectionsConfig: List, val logger: Logger? = null){ - constructor(connectionsConfig: List):this(connectionsConfig, null) +data class DBNativeAccessConfig(val connectionsConfig: List, val logger: Logger? = null) { + constructor(connectionsConfig: List) : this(connectionsConfig, null) } /** @@ -34,12 +34,12 @@ data class DBNativeAccessConfig (val connectionsConfig: List, * @param impl DBMManager implementation. If doesn't specified is assumed by url * @param properties Others connection properties * */ -data class ConnectionConfig ( +data class ConnectionConfig( val fileName: String, val url: String, val user: String, val password: String, val driver: String? = null, val impl: String? = null, - val properties : Map = mutableMapOf()) - + val properties: Map = mutableMapOf(), +) diff --git a/base/src/main/kotlin/com/smeup/dbnative/file/DBFile.kt b/base/src/main/kotlin/com/smeup/dbnative/file/DBFile.kt index 0ccba2a..722051c 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/file/DBFile.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/file/DBFile.kt @@ -23,35 +23,44 @@ import com.smeup.dbnative.model.FileMetadata /** * DBFile is an abstraction of table or view (in sql database) or document (in nosql database). * */ -interface DBFile: AutoCloseable { +interface DBFile : AutoCloseable { var name: String var fileMetadata: FileMetadata var logger: Logger? // Control functions fun eof(): Boolean + fun equal(): Boolean // Pointing functions fun setll(key: String): Boolean + fun setll(keys: List): Boolean + fun setgt(key: String): Boolean - fun setgt(keys: List): Boolean + fun setgt(keys: List): Boolean // Read functions fun chain(key: String): Result + fun chain(keys: List): Result fun read(): Result + fun readPrevious(): Result fun readEqual(): Result + fun readEqual(key: String): Result + fun readEqual(keys: List): Result fun readPreviousEqual(): Result + fun readPreviousEqual(key: String): Result + fun readPreviousEqual(keys: List): Result // Write functions @@ -64,4 +73,4 @@ interface DBFile: AutoCloseable { fun delete(record: Record): Result override fun close() {} -} \ No newline at end of file +} diff --git a/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt b/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt index bf99ace..7be4df9 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt @@ -24,20 +24,20 @@ class Record(vararg fields: RecordField) : LinkedHashMap() { } } - fun matches(keyFields: List) = keyFields.all { + fun matches(keyFields: List) = + keyFields.all { + var value1 = this[it.name]?.trim() + var value2 = it.value.trim() - var value1 = this[it.name]?.trim() - var value2 = it.value.trim(); - - value1.equals(value2.trim()) - } + value1.equals(value2.trim()) + } fun add(field: RecordField) { put(field.name, field.value) } - fun duplicate(): Record{ + fun duplicate(): Record { val thisMap = this return Record().apply { this.putAll(thisMap) } } -} \ No newline at end of file +} diff --git a/base/src/main/kotlin/com/smeup/dbnative/file/Result.kt b/base/src/main/kotlin/com/smeup/dbnative/file/Result.kt index d6f2e84..f41dc00 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/file/Result.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/file/Result.kt @@ -17,4 +17,4 @@ package com.smeup.dbnative.file -data class Result(var record: Record = Record(), var indicatorHI: Boolean = false, var indicatorLO: Boolean = false, var indicatorEQ: Boolean = false, var errorMsg: String = "") \ No newline at end of file +data class Result(var record: Record = Record(), var indicatorHI: Boolean = false, var indicatorLO: Boolean = false, var indicatorEQ: Boolean = false, var errorMsg: String = "") diff --git a/base/src/main/kotlin/com/smeup/dbnative/log/Logging.kt b/base/src/main/kotlin/com/smeup/dbnative/log/Logging.kt index 528de58..f1485e1 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/log/Logging.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/log/Logging.kt @@ -3,71 +3,95 @@ package com.smeup.dbnative.log import java.text.SimpleDateFormat import java.util.* -enum class LoggingLevel{ - OFF, ERROR, WARN, INFO, DEBUG, TRACE, ALL +enum class LoggingLevel { + OFF, + ERROR, + WARN, + INFO, + DEBUG, + TRACE, + ALL, } -enum class LoggingKey(val level: LoggingLevel){ +enum class LoggingKey(val level: LoggingLevel) { native_access_method(LoggingLevel.TRACE), read_data(LoggingLevel.TRACE), execute_inquiry(LoggingLevel.DEBUG), search_data(LoggingLevel.DEBUG), - connection(LoggingLevel.DEBUG) + connection(LoggingLevel.DEBUG), } -enum class NativeMethod(){ - equal, setll, setgt, chain, read, readPrevious, readEqual, readPreviousEqual, write, update, delete; +enum class NativeMethod() { + equal, + setll, + setgt, + chain, + read, + readPrevious, + readEqual, + readPreviousEqual, + write, + update, + delete, } -data class LoggingEvent(val eventKey: LoggingKey, - val message: String, - val callerMethod: String? = null, - val elapsedTime: Long? = null, - val nativeMethodCall: NativeMethod? = null, - val fileName: String? = null){ +data class LoggingEvent( + val eventKey: LoggingKey, + val message: String, + val callerMethod: String? = null, + val elapsedTime: Long? = null, + val nativeMethodCall: NativeMethod? = null, + val fileName: String? = null, +) { val issueTime: Date = Date() + fun isMeasuredEvent() = elapsedTime != null } -open class Logger(val level:LoggingLevel = LoggingLevel.OFF, - var loggingFunction: ((LoggingEvent) -> Unit)?) -{ - companion object{ +open class Logger( + val level: LoggingLevel = LoggingLevel.OFF, + var loggingFunction: ((LoggingEvent) -> Unit)?, +) { + companion object { @JvmStatic - fun getSimpleInstance(level:LoggingLevel = LoggingLevel.DEBUG): Logger{ + fun getSimpleInstance(level: LoggingLevel = LoggingLevel.DEBUG): Logger { return Logger(level, loggingFunction = { val logged = "[%s][%s][%s][%s][%s] * %s %s" - println(logged.format(SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(it.issueTime), - it.eventKey.level, - it.eventKey.name, - it.nativeMethodCall?:"", - it.fileName?:"", - it.message, - if(it.isMeasuredEvent()) "(${it.elapsedTime} ms)" else "") - ) + println( + logged.format( + SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(it.issueTime), + it.eventKey.level, + it.eventKey.name, + it.nativeMethodCall ?: "", + it.fileName ?: "", + it.message, + if (it.isMeasuredEvent()) "(${it.elapsedTime} ms)" else "", + ), + ) }) } } - fun logEvent(eventKey: LoggingKey, - message: String, - elapsedTime: Long? = null, - nativeMethodCall: NativeMethod? = null, - fileName: String? = null): LoggingEvent?{ - return if(eventKey.level.ordinal <= level.ordinal) { - val caller = Thread.currentThread().getStackTrace().getOrNull(2)?.let { - "${it.className} ${it.methodName}:${it.lineNumber}" - }?:"" + fun logEvent( + eventKey: LoggingKey, + message: String, + elapsedTime: Long? = null, + nativeMethodCall: NativeMethod? = null, + fileName: String? = null, + ): LoggingEvent? { + return if (eventKey.level.ordinal <= level.ordinal) { + val caller = + Thread.currentThread().getStackTrace().getOrNull(2)?.let { + "${it.className} ${it.methodName}:${it.lineNumber}" + } ?: "" logEvent(LoggingEvent(eventKey, message, caller, elapsedTime, nativeMethodCall, fileName)) - } - else{ + } else { null } } - internal fun logEvent(ev: LoggingEvent): LoggingEvent{ + internal fun logEvent(ev: LoggingEvent): LoggingEvent { loggingFunction?.invoke(ev) return ev } } - diff --git a/base/src/main/kotlin/com/smeup/dbnative/metadata/MetadataRegister.kt b/base/src/main/kotlin/com/smeup/dbnative/metadata/MetadataRegister.kt index af52dd8..2ea8769 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/metadata/MetadataRegister.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/metadata/MetadataRegister.kt @@ -20,9 +20,14 @@ package com.smeup.dbnative.metadata import com.smeup.dbnative.model.FileMetadata interface MetadataRegister { + fun registerMetadata( + metadata: FileMetadata, + overwrite: Boolean, + ) - fun registerMetadata(metadata: FileMetadata, overwrite: Boolean) - fun getMetadata(filename:String): FileMetadata - fun contains(fileName:String): Boolean - fun remove(fileName:String) -} \ No newline at end of file + fun getMetadata(filename: String): FileMetadata + + fun contains(fileName: String): Boolean + + fun remove(fileName: String) +} diff --git a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt index e3884dc..ad8a280 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt @@ -21,22 +21,24 @@ import com.smeup.dbnative.metadata.MetadataRegister import com.smeup.dbnative.model.FileMetadata import java.io.File -object FSMetadataRegisterImpl: MetadataRegister{ - +object FSMetadataRegisterImpl : MetadataRegister { var propertiesDirPath: String init { - propertiesDirPath = System.getenv("DBNATIVE_DDS_DIR") ?:"${System.getProperty("user.home")}${File.separatorChar}" + - "etc${File.separatorChar}" + - "dbnativeaccess${File.separatorChar}dds" + propertiesDirPath = System.getenv("DBNATIVE_DDS_DIR") ?: "${System.getProperty("user.home")}${File.separatorChar}" + + "etc${File.separatorChar}" + + "dbnativeaccess${File.separatorChar}dds" if (File(propertiesDirPath).exists() == false) { File(propertiesDirPath).mkdirs() } } - override fun registerMetadata(metadata: FileMetadata, overwrite: Boolean) { + override fun registerMetadata( + metadata: FileMetadata, + overwrite: Boolean, + ) { PropertiesSerializer.metadataToProperties(propertiesDirPath, metadata, true) } @@ -45,11 +47,11 @@ object FSMetadataRegisterImpl: MetadataRegister{ } override fun contains(fileName: String): Boolean { - return File("${propertiesDirPath}${File.separatorChar}${fileName}.properties").exists() + return File("${propertiesDirPath}${File.separatorChar}$fileName.properties").exists() } override fun remove(fileName: String) { - var propertiesFile = File("${propertiesDirPath}${File.separatorChar}${fileName}.properties") + var propertiesFile = File("${propertiesDirPath}${File.separatorChar}$fileName.properties") if (propertiesFile.exists()) propertiesFile.delete() } -} \ No newline at end of file +} diff --git a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt index 2218ca3..8df8111 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt @@ -27,18 +27,22 @@ import java.nio.charset.Charset import java.util.* import kotlin.collections.ArrayList - object PropertiesSerializer { - - fun propertiesToMetadata(propertiesDirPath: String, fileName: String): FileMetadata{ + fun propertiesToMetadata( + propertiesDirPath: String, + fileName: String, + ): FileMetadata { val propertiesFile = FileInputStream(File("$propertiesDirPath${File.separatorChar}${fileName.toUpperCase()}.properties")) - //val properties = Properties() - //properties.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) + // val properties = Properties() + // properties.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) val mp: MutableMap = LinkedHashMap() object : Properties() { @Synchronized - override fun put(key: Any, value: Any): Any? { + override fun put( + key: Any, + value: Any, + ): Any? { return mp.put(key as String, value as String) } }.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) @@ -63,7 +67,7 @@ object PropertiesSerializer { // FieldKeys val fieldsKeys: MutableList = ArrayList() - if(!(mp["filekeys"]).isNullOrEmpty()){ + if (!(mp["filekeys"]).isNullOrEmpty()) { fieldsKeys.addAll((mp["filekeys"]?.split(",")!!)) } @@ -71,27 +75,30 @@ object PropertiesSerializer { return FileMetadata(fileName, tableName, fields, fieldsKeys) } - - fun metadataToProperties(propertiesDirPath: String, fileMetadata: FileMetadata, overwrite: Boolean){ + fun metadataToProperties( + propertiesDirPath: String, + fileMetadata: FileMetadata, + overwrite: Boolean, + ) { metadataToPropertiesImpl(propertiesDirPath, fileMetadata, fileMetadata.fieldsToProperties(), overwrite) } - - private fun metadataToPropertiesImpl(propertiesDirPath: String, - fileMetadata: FileMetadata, - properties: MutableList>, - overwrite: Boolean){ + private fun metadataToPropertiesImpl( + propertiesDirPath: String, + fileMetadata: FileMetadata, + properties: MutableList>, + overwrite: Boolean, + ) { properties.add(Pair("tablename", fileMetadata.tableName)) val keys = fileMetadata.fileKeys.joinToString(",") properties.add(Pair("filekeys", keys)) - val propertiesFilePath = "${propertiesDirPath}${File.separatorChar}${fileMetadata.name.toUpperCase()}.properties" val propertiesFile = File(propertiesFilePath) - if (overwrite && propertiesFile.exists()) { + if (overwrite && propertiesFile.exists()) { propertiesFile.delete() } @@ -103,6 +110,5 @@ object PropertiesSerializer { } writer.flush() writer.close() - } } diff --git a/base/src/main/kotlin/com/smeup/dbnative/model/Field.kt b/base/src/main/kotlin/com/smeup/dbnative/model/Field.kt index f266536..94e803d 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/model/Field.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/model/Field.kt @@ -17,4 +17,4 @@ package com.smeup.dbnative.model -data class Field(val name: String, val text:String = "") \ No newline at end of file +data class Field(val name: String, val text: String = "") diff --git a/base/src/main/kotlin/com/smeup/dbnative/model/FileMetadata.kt b/base/src/main/kotlin/com/smeup/dbnative/model/FileMetadata.kt index 9aa1f00..3ae9f33 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/model/FileMetadata.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/model/FileMetadata.kt @@ -17,4 +17,4 @@ package com.smeup.dbnative.model -data class FileMetadata(var name: String, var tableName:String, var fields: List, var fileKeys:List) \ No newline at end of file +data class FileMetadata(var name: String, var tableName: String, var fields: List, var fileKeys: List) diff --git a/base/src/main/kotlin/com/smeup/dbnative/utils/Comparison.kt b/base/src/main/kotlin/com/smeup/dbnative/utils/Comparison.kt index 7995e1d..52563d9 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/utils/Comparison.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/utils/Comparison.kt @@ -23,5 +23,5 @@ enum class Comparison(val symbol: String) { GT(">"), GE(">="), LT("<"), - LE("<="); + LE("<="), } diff --git a/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt b/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt index 8176280..1dedf4b 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt @@ -21,12 +21,10 @@ import com.smeup.dbnative.file.RecordField import com.smeup.dbnative.model.Field import com.smeup.dbnative.model.FileMetadata - /* Return true if passed keys are all primary fields in metadata - */ + */ fun FileMetadata.matchFileKeys(keys: List): Boolean { - val keysAsString = mutableListOf() keys.forEach { @@ -51,16 +49,16 @@ fun FileMetadata.getField(name: String): Field? { } } -fun FileMetadata.fieldsToProperties(): MutableList>{ +fun FileMetadata.fieldsToProperties(): MutableList> { val properties = mutableListOf>() for (field in fields.iterator()) { properties.add( Pair( "field.${field.name}", - "${field.text}" - ) + "${field.text}", + ), ) } - return properties; + return properties } diff --git a/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt b/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt index c3a44e9..735a9e6 100644 --- a/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt +++ b/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt @@ -22,7 +22,6 @@ import org.junit.Test import java.io.File class DBFileFactoryTest { - @Test fun loadAndSaveTest() { // Delete tmp file @@ -43,4 +42,4 @@ class DBFileFactoryTest { // Compare metadatas class assert(metadata2.equals(metadata1)) } -} \ No newline at end of file +} diff --git a/base/src/test/kotlin/com/smeup/dbnative/utils/FieldType.kt b/base/src/test/kotlin/com/smeup/dbnative/utils/FieldType.kt index b11b610..fe099f3 100644 --- a/base/src/test/kotlin/com/smeup/dbnative/utils/FieldType.kt +++ b/base/src/test/kotlin/com/smeup/dbnative/utils/FieldType.kt @@ -31,190 +31,173 @@ enum class Type { TIME, DATE, BINARY, - VARBINARY + VARBINARY, } sealed class FieldType { - - abstract val type: Type - abstract val size: Int - abstract val digits: Int - } + abstract val type: Type + abstract val size: Int + abstract val digits: Int +} // Fixed length string - data class CharacterType(val length: Int) : FieldType() { +data class CharacterType(val length: Int) : FieldType() { + override val type: Type + get() = Type.CHARACTER - override val type: Type - get() = Type.CHARACTER + override val size: Int + get() = length - override val size: Int - get() = length - - override val digits: Int - get() = 0 - } + override val digits: Int + get() = 0 +} // Varying length string (with max length) - data class VarcharType(val length: Int) : FieldType() { - - override val type: Type - get() = Type.VARCHAR - - override val size: Int - get() = length - - override val digits: Int - get() = 0 - } - - object IntegerType : FieldType() { +data class VarcharType(val length: Int) : FieldType() { + override val type: Type + get() = Type.VARCHAR - override val type: Type - get() = Type.INTEGER + override val size: Int + get() = length - override val size: Int - get() = 10 - - override val digits: Int - get() = 0 - } - - object SmallintType : FieldType() { + override val digits: Int + get() = 0 +} - override val type: Type - get() = Type.SMALLINT +object IntegerType : FieldType() { + override val type: Type + get() = Type.INTEGER - override val size: Int - get() = 5 + override val size: Int + get() = 10 - override val digits: Int - get() = 0 - } + override val digits: Int + get() = 0 +} - object BigintType : FieldType() { +object SmallintType : FieldType() { + override val type: Type + get() = Type.SMALLINT - override val type: Type - get() = Type.BIGINT + override val size: Int + get() = 5 - override val size: Int - get() = 19 + override val digits: Int + get() = 0 +} - override val digits: Int - get() = 0 - } +object BigintType : FieldType() { + override val type: Type + get() = Type.BIGINT - object BooleanType : FieldType() { + override val size: Int + get() = 19 - override val type: Type - get() = Type.BOOLEAN + override val digits: Int + get() = 0 +} - override val size: Int - get() = 1 +object BooleanType : FieldType() { + override val type: Type + get() = Type.BOOLEAN - override val digits: Int - get() = 0 - } + override val size: Int + get() = 1 + override val digits: Int + get() = 0 +} // Numeric with total length and number of digits (a.k.a. NUMERIC) - data class DecimalType(val length: Int, val precision: Int) : FieldType() { - - override val type: Type - get() = Type.DECIMAL +data class DecimalType(val length: Int, val precision: Int) : FieldType() { + override val type: Type + get() = Type.DECIMAL - override val size: Int - get() = length + override val size: Int + get() = length - override val digits: Int - get() = precision - } - - object FloatType : FieldType() { - - override val type: Type - get() = Type.FLOAT + override val digits: Int + get() = precision +} - override val size: Int - get() = 19 +object FloatType : FieldType() { + override val type: Type + get() = Type.FLOAT - override val digits: Int - get() = 0 - } + override val size: Int + get() = 19 - object DoubleType : FieldType() { + override val digits: Int + get() = 0 +} - override val type: Type - get() = Type.DOUBLE +object DoubleType : FieldType() { + override val type: Type + get() = Type.DOUBLE - override val size: Int - get() = 19 + override val size: Int + get() = 19 - override val digits: Int - get() = 0 - } + override val digits: Int + get() = 0 +} // Year, month, day, hour, minutes, seconds - object TimeStampType : FieldType() { - - override val type: Type - get() = Type.TIMESTAMP +object TimeStampType : FieldType() { + override val type: Type + get() = Type.TIMESTAMP - override val size: Int - get() = 14 + override val size: Int + get() = 14 - override val digits: Int - get() = 0 - } + override val digits: Int + get() = 0 +} // Year, month, day - object DateType : FieldType() { - - override val type: Type - get() = Type.DATE +object DateType : FieldType() { + override val type: Type + get() = Type.DATE - override val size: Int - get() = 8 + override val size: Int + get() = 8 - override val digits: Int - get() = 0 - - } + override val digits: Int + get() = 0 +} // hour, minutes, seconds - object TimeType : FieldType() { +object TimeType : FieldType() { + override val type: Type + get() = Type.TIME - override val type: Type - get() = Type.TIME + override val size: Int + get() = 6 - override val size: Int - get() = 6 - - override val digits: Int - get() = 0 - } + override val digits: Int + get() = 0 +} // Binary with fixed length - data class BinaryType(val length: Int) : FieldType() { +data class BinaryType(val length: Int) : FieldType() { + override val type: Type + get() = Type.BINARY - override val type: Type - get() = Type.BINARY + override val size: Int + get() = length - override val size: Int - get() = length - - override val digits: Int - get() = 0 - } + override val digits: Int + get() = 0 +} // Binary with varying length - data class VarbinaryType(val length: Int) : FieldType() { +data class VarbinaryType(val length: Int) : FieldType() { + override val type: Type + get() = Type.VARBINARY - override val type: Type - get() = Type.VARBINARY + override val size: Int + get() = length - override val size: Int - get() = length - - override val digits: Int - get() = 0 - } + override val digits: Int + get() = 0 +} diff --git a/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt b/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt index e1ca8b8..d1aee14 100644 --- a/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt +++ b/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt @@ -8,35 +8,36 @@ import java.nio.charset.Charset import java.util.* import kotlin.collections.ArrayList -data class TypedField(val field: Field, val type: FieldType){ -} +data class TypedField(val field: Field, val type: FieldType) -fun Collection.fieldList():List{ - return map({tf -> tf.field}) +fun Collection.fieldList(): List { + return map({ tf -> tf.field }) } -fun Collection.fieldTypeList():List{ - return map({tf -> tf.type}) +fun Collection.fieldTypeList(): List { + return map({ tf -> tf.type }) } -data class TypedMetadata(var name: String, - var tableName: String, - var fields: List, - var fileKeys:List){ - fun fileMetadata(): FileMetadata = FileMetadata(name, tableName, fields.fieldList(), fileKeys); +data class TypedMetadata( + var name: String, + var tableName: String, + var fields: List, + var fileKeys: List, +) { + fun fileMetadata(): FileMetadata = FileMetadata(name, tableName, fields.fieldList(), fileKeys) - fun fieldsToProperties(): MutableList>{ + fun fieldsToProperties(): MutableList> { val properties = mutableListOf>() for ((index, field) in fields.iterator().withIndex()) { properties.add( Pair( "field.${field.field.name}", - "${field.field.text},${fields[index].type.type},${fields[index].type.size},${fields[index].type.digits}" - ) + "${field.field.text},${fields[index].type.type},${fields[index].type.size},${fields[index].type.digits}", + ), ) } - return properties; + return properties } fun getField(name: String): TypedField? { @@ -50,38 +51,47 @@ data class TypedMetadata(var name: String, infix fun String.fieldByType(type: FieldType): TypedField = TypedField(Field(this), type) -fun String.getFieldTypeInstance(columnSize: Int, decimalDigits: Int): FieldType { - - val fieldTypeObject = when (this.toUpperCase()) { - "CHAR","CHARACTER" -> CharacterType(columnSize) - "VARCHAR" -> VarcharType(columnSize) - "INT", "INTEGER" -> IntegerType - "SMALLINT" -> SmallintType - "BIGINT" -> BigintType - "BOOLEAN", "BOOL" -> BooleanType - "DECIMAL" -> DecimalType(columnSize, decimalDigits) - "DOUBLE" -> DoubleType - "FLOAT" -> FloatType - "TIMESTAMP" -> TimeStampType - "TIME" -> TimeType - "DATE" -> DateType - "BINARY" -> BinaryType(columnSize) - "VARBINARY" -> VarbinaryType(columnSize) - else -> throw IllegalArgumentException("Wrong type of FieldType") - } +fun String.getFieldTypeInstance( + columnSize: Int, + decimalDigits: Int, +): FieldType { + val fieldTypeObject = + when (this.toUpperCase()) { + "CHAR", "CHARACTER" -> CharacterType(columnSize) + "VARCHAR" -> VarcharType(columnSize) + "INT", "INTEGER" -> IntegerType + "SMALLINT" -> SmallintType + "BIGINT" -> BigintType + "BOOLEAN", "BOOL" -> BooleanType + "DECIMAL" -> DecimalType(columnSize, decimalDigits) + "DOUBLE" -> DoubleType + "FLOAT" -> FloatType + "TIMESTAMP" -> TimeStampType + "TIME" -> TimeType + "DATE" -> DateType + "BINARY" -> BinaryType(columnSize) + "VARBINARY" -> VarbinaryType(columnSize) + else -> throw IllegalArgumentException("Wrong type of FieldType") + } return fieldTypeObject } -fun propertiesToTypedMetadata(propertiesDirPath: String, fileName: String): TypedMetadata { +fun propertiesToTypedMetadata( + propertiesDirPath: String, + fileName: String, +): TypedMetadata { val propertiesFile = FileInputStream(File("$propertiesDirPath${File.separatorChar}${fileName.toUpperCase()}.properties")) - //val properties = Properties() - //properties.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) + // val properties = Properties() + // properties.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) val mp: MutableMap = LinkedHashMap() object : Properties() { @Synchronized - override fun put(key: Any, value: Any): Any? { + override fun put( + key: Any, + value: Any, + ): Any? { return mp.put(key as String, value as String) } }.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) @@ -99,17 +109,17 @@ fun propertiesToTypedMetadata(propertiesDirPath: String, fileName: String): Type val datatype = fldAttributes[1].trim() val fieldType = datatype.getFieldTypeInstance(length, decimal) - fields.add(TypedField(Field(name, description), fieldType)) + fields.add(TypedField(Field(name, description), fieldType)) } // FormatName - val tablename = mp.get("tablename")?:"" + val tablename = mp.get("tablename") ?: "" // FieldKeys val fieldsKeys: MutableList = ArrayList() - if(!(mp.get("filekeys")).isNullOrEmpty()){ + if (!(mp.get("filekeys")).isNullOrEmpty()) { fieldsKeys.addAll((mp.get("filekeys")?.split(",")!!)) } return TypedMetadata(fileName, tablename, fields, fieldsKeys) -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index 139fa96..9e95b8c 100644 --- a/pom.xml +++ b/pom.xml @@ -181,6 +181,20 @@ + + com.github.gantsign.maven + ktlint-maven-plugin + 3.0.0 + + + format-and-check + + format + check + + + + From 70b58a48180fc1176a68d5888a48d2f37757bbc0 Mon Sep 17 00:00:00 2001 From: Dario Foresti Date: Tue, 28 Nov 2023 18:00:59 +0100 Subject: [PATCH 2/5] Fix for chain failure after read operation - patch for maven setup --- base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt b/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt index d1aee14..163e3a7 100644 --- a/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt +++ b/base/src/test/kotlin/com/smeup/dbnative/utils/Types.kt @@ -56,7 +56,7 @@ fun String.getFieldTypeInstance( decimalDigits: Int, ): FieldType { val fieldTypeObject = - when (this.toUpperCase()) { + when (this.uppercase()) { "CHAR", "CHARACTER" -> CharacterType(columnSize) "VARCHAR" -> VarcharType(columnSize) "INT", "INTEGER" -> IntegerType @@ -81,7 +81,7 @@ fun propertiesToTypedMetadata( propertiesDirPath: String, fileName: String, ): TypedMetadata { - val propertiesFile = FileInputStream(File("$propertiesDirPath${File.separatorChar}${fileName.toUpperCase()}.properties")) + val propertiesFile = FileInputStream(File("$propertiesDirPath${File.separatorChar}${fileName.uppercase()}.properties")) // val properties = Properties() // properties.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) From 92c0e93624163f46e0418c3fafd674f93125d6c5 Mon Sep 17 00:00:00 2001 From: Dario Foresti Date: Tue, 28 Nov 2023 18:04:43 +0100 Subject: [PATCH 3/5] Fix for chain failure after read operation - patch for maven setup --- base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt | 2 +- .../com/smeup/dbnative/metadata/file/PropertiesSerializer.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt b/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt index 82fc982..a9b8e40 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt @@ -59,7 +59,7 @@ abstract class DBManagerBaseImpl : DBMManager { } override fun existFile(name: String): Boolean { - return getMetadataRegister().contains(name.toUpperCase()) + return getMetadataRegister().contains(name.uppercase()) } companion object { diff --git a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt index 8df8111..4c3d254 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/PropertiesSerializer.kt @@ -32,7 +32,7 @@ object PropertiesSerializer { propertiesDirPath: String, fileName: String, ): FileMetadata { - val propertiesFile = FileInputStream(File("$propertiesDirPath${File.separatorChar}${fileName.toUpperCase()}.properties")) + val propertiesFile = FileInputStream(File("$propertiesDirPath${File.separatorChar}${fileName.uppercase()}.properties")) // val properties = Properties() // properties.load(InputStreamReader(propertiesFile, Charset.forName("UTF-8"))) @@ -94,7 +94,7 @@ object PropertiesSerializer { val keys = fileMetadata.fileKeys.joinToString(",") properties.add(Pair("filekeys", keys)) - val propertiesFilePath = "${propertiesDirPath}${File.separatorChar}${fileMetadata.name.toUpperCase()}.properties" + val propertiesFilePath = "${propertiesDirPath}${File.separatorChar}${fileMetadata.name.uppercase(Locale.getDefault())}.properties" val propertiesFile = File(propertiesFilePath) From ee27f3375072f7a6d3dbbed4c74c995c1294316b Mon Sep 17 00:00:00 2001 From: Dario Foresti Date: Tue, 28 Nov 2023 18:13:57 +0100 Subject: [PATCH 4/5] Fix for chain failure after read operation - patch for maven setup --- .../kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt | 3 ++- .../src/main/kotlin/com/smeup/dbnative/file/Record.kt | 4 ++-- .../dbnative/metadata/file/FSMetadataRegisterImpl.kt | 11 +++++++---- .../kotlin/com/smeup/dbnative/utils/FileUtilities.kt | 2 +- .../com/smeup/dbnative/PropertiesSerializationTest.kt | 6 +++--- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt b/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt index a9b8e40..93d4e1a 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/DBManagerBaseImpl.kt @@ -21,6 +21,7 @@ import com.smeup.dbnative.log.Logger import com.smeup.dbnative.metadata.MetadataRegister import com.smeup.dbnative.metadata.file.FSMetadataRegisterImpl import com.smeup.dbnative.model.FileMetadata +import java.util.* abstract class DBManagerBaseImpl : DBMManager { var logger: Logger? = null @@ -33,7 +34,7 @@ abstract class DBManagerBaseImpl : DBMManager { */ override fun metadataOf(name: String): FileMetadata { - return getMetadataRegister().getMetadata(name.toUpperCase()) + return getMetadataRegister().getMetadata(name.uppercase(Locale.getDefault())) } override fun registerMetadata( diff --git a/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt b/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt index 7be4df9..33aa86d 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/file/Record.kt @@ -26,8 +26,8 @@ class Record(vararg fields: RecordField) : LinkedHashMap() { fun matches(keyFields: List) = keyFields.all { - var value1 = this[it.name]?.trim() - var value2 = it.value.trim() + val value1 = this[it.name]?.trim() + val value2 = it.value.trim() value1.equals(value2.trim()) } diff --git a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt index ad8a280..be99aea 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/metadata/file/FSMetadataRegisterImpl.kt @@ -26,9 +26,12 @@ object FSMetadataRegisterImpl : MetadataRegister { init { - propertiesDirPath = System.getenv("DBNATIVE_DDS_DIR") ?: "${System.getProperty("user.home")}${File.separatorChar}" + - "etc${File.separatorChar}" + - "dbnativeaccess${File.separatorChar}dds" + propertiesDirPath = + System.getenv("DBNATIVE_DDS_DIR") ?: ( + "${System.getProperty("user.home")}${File.separatorChar}" + + "etc${File.separatorChar}" + + "dbnativeaccess${File.separatorChar}dds" + ) if (File(propertiesDirPath).exists() == false) { File(propertiesDirPath).mkdirs() @@ -51,7 +54,7 @@ object FSMetadataRegisterImpl : MetadataRegister { } override fun remove(fileName: String) { - var propertiesFile = File("${propertiesDirPath}${File.separatorChar}$fileName.properties") + val propertiesFile = File("${propertiesDirPath}${File.separatorChar}$fileName.properties") if (propertiesFile.exists()) propertiesFile.delete() } } diff --git a/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt b/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt index 1dedf4b..97d546b 100644 --- a/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt +++ b/base/src/main/kotlin/com/smeup/dbnative/utils/FileUtilities.kt @@ -56,7 +56,7 @@ fun FileMetadata.fieldsToProperties(): MutableList> { properties.add( Pair( "field.${field.name}", - "${field.text}", + field.text, ), ) } diff --git a/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt b/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt index 735a9e6..ecc0baf 100644 --- a/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt +++ b/base/src/test/kotlin/com/smeup/dbnative/PropertiesSerializationTest.kt @@ -25,19 +25,19 @@ class DBFileFactoryTest { @Test fun loadAndSaveTest() { // Delete tmp file - var tmpFile = File("src/test/resources/dds/properties/out/BRARTI0F.properties") + val tmpFile = File("src/test/resources/dds/properties/out/BRARTI0F.properties") if (tmpFile.exists()) tmpFile.delete() tmpFile.parentFile.mkdirs() // Read metadata1 from properties - var metadata1 = PropertiesSerializer.propertiesToMetadata("src/test/resources/dds/properties/", "BRARTI0F") + val metadata1 = PropertiesSerializer.propertiesToMetadata("src/test/resources/dds/properties/", "BRARTI0F") println(metadata1) // Save metadata1 to tmp properties file PropertiesSerializer.metadataToProperties("src/test/resources/dds/properties/out", metadata1, true) // Read metadata2 from tmp properties file - var metadata2 = PropertiesSerializer.propertiesToMetadata("src/test/resources/dds/properties/out/", "BRARTI0F") + val metadata2 = PropertiesSerializer.propertiesToMetadata("src/test/resources/dds/properties/out/", "BRARTI0F") // Compare metadatas class assert(metadata2.equals(metadata1)) From 6c8b74a27a20cbc998bc9e1ed97fffc2d362132d Mon Sep 17 00:00:00 2001 From: Dario Foresti Date: Wed, 29 Nov 2023 15:03:46 +0100 Subject: [PATCH 5/5] Fix source formattation and klint integration --- .../com/smeup/dbnative/jt400/JT400DBFile.kt | 91 +++--- .../smeup/dbnative/jt400/JT400DBMManager.kt | 23 +- .../dbnative/jt400/JT400Chain1KeyTest.kt | 3 - .../dbnative/jt400/JT400Chain2KeysTest.kt | 22 +- .../dbnative/jt400/JT400MunicipalityTest.kt | 11 +- .../dbnative/jt400/JT400OperationsOnFile.kt | 48 ++- .../dbnative/jt400/JT400ReadPreviousTest.kt | 3 - .../dbnative/jt400/utils/JT400DBTestUtils.kt | 181 ++++++------ .../smeup/dbnative/manager/DBFileFactory.kt | 54 ++-- .../smeup/dbnative/manager/DBFileWrapper.kt | 9 +- .../dbnative/manager/DBFileFactoryTest.kt | 92 +++--- .../com/smeup/dbnative/nosql/NoSQLDBFile.kt | 208 +++++++------ .../smeup/dbnative/nosql/NoSQLDBMManager.kt | 21 +- .../dbnative/nosql/utils/MongoDbUtils.kt | 23 +- .../smeup/dbnative/nosql/NoSQLDBFileTest.kt | 6 +- .../dbnative/nosql/NoSQLMunicipalityTest.kt | 12 +- .../dbnative/nosql/utils/NoSQLDBTestUtils.kt | 87 +++--- .../com/smeup/dbnative/sql/JDBCUtils.kt | 21 +- .../smeup/dbnative/sql/Native2SQLAdapter.kt | 200 ++++++++----- .../com/smeup/dbnative/sql/SQLDBFile.kt | 74 ++--- .../com/smeup/dbnative/sql/SQLDBFileNoPerf.kt | 133 +++++---- .../com/smeup/dbnative/sql/SQLDBMManager.kt | 34 +-- .../kotlin/com/smeup/dbnative/sql/SQLUtils.kt | 94 +++--- .../dbnative/sql/DB2400OperationsOnFile.kt | 49 ++-- .../sql/DB2400OperationsOnFilePerfTest.kt | 48 +-- .../com/smeup/dbnative/sql/JDBCUtilsTest.kt | 11 +- .../smeup/dbnative/sql/SQLChain1KeyTest.kt | 4 - .../smeup/dbnative/sql/SQLChain2KeysTest.kt | 24 +- .../dbnative/sql/SQLMunicipalityPerfTest.kt | 28 +- .../smeup/dbnative/sql/SQLMunicipalityTest.kt | 10 +- .../smeup/dbnative/sql/SQLReadEqualTest.kt | 17 +- .../smeup/dbnative/sql/SQLReadPreviousTest.kt | 5 +- .../com/smeup/dbnative/sql/SQLUtilsTest.kt | 60 ++-- .../dbnative/sql/utils/SQLDBTestUtils.kt | 276 ++++++++++-------- 34 files changed, 1032 insertions(+), 950 deletions(-) diff --git a/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBFile.kt b/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBFile.kt index aa8f24b..1ac67c3 100644 --- a/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBFile.kt +++ b/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBFile.kt @@ -29,14 +29,17 @@ import com.smeup.dbnative.model.FileMetadata import java.math.BigDecimal private enum class CursorAction { - NONE, SETLL, SETGT + NONE, + SETLL, + SETGT, } -class JT400DBFile(override var name: String, - override var fileMetadata: FileMetadata, - var file: KeyedFile, - override var logger: Logger? = null) : DBFile { - +class JT400DBFile( + override var name: String, + override var fileMetadata: FileMetadata, + var file: KeyedFile, + override var logger: Logger? = null, +) : DBFile { private var equalFlag: Boolean = false private var eofReached: Boolean = false private var previousAction: CursorAction = CursorAction.NONE @@ -96,9 +99,9 @@ class JT400DBFile(override var name: String, } catch (e: AS400Exception) { } try { - //file.positionCursorBefore(keys2Array(keys)) + // file.positionCursorBefore(keys2Array(keys)) file.positionCursor(keys2Array(keys), KeyedFile.KEY_LT) - //file.positionCursor(keys2Array(keys), KeyedFile.KEY_LT) + // file.positionCursor(keys2Array(keys), KeyedFile.KEY_LT) return true } catch (e: AS400Exception) { handleAS400Error(e) @@ -142,14 +145,14 @@ class JT400DBFile(override var name: String, override fun chain(keys: List): Result { this.previousAction = CursorAction.NONE resetStatus() - //TODO("Attenzione alla gestione del lock") + // TODO("Attenzione alla gestione del lock") try { /* file.positionCursor(keys2Array(keys), KeyedFile.KEY_EQ) var r : Result? = Result(as400RecordToSmeUPRecord(file.read())) //file.positionCursorToNext(); return r ?: fail("Read failed"); - */ + */ return Result(as400RecordToSmeUPRecord(file.read(keys2Array(keys)))) } catch (e: AS400Exception) { handleAS400Error(e) @@ -161,13 +164,13 @@ class JT400DBFile(override var name: String, * The READ operation reads the record, currently pointed to, from a full procedural file. */ override fun read(): Result { - //https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzasd/zzread.htm - var r : Result + // https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzasd/zzread.htm + var r: Result try { - if (this.previousAction==CursorAction.SETLL) { + if (this.previousAction == CursorAction.SETLL) { file.positionCursorToNext() } - r = Result(as400RecordToSmeUPRecord(file.read())) + r = Result(as400RecordToSmeUPRecord(file.read())) } catch (e: AS400Exception) { handleAS400Error(e) r = Result(Record()) @@ -186,12 +189,12 @@ class JT400DBFile(override var name: String, */ override fun readPrevious(): Result { resetStatus() - var r : Result + var r: Result try { - if (this.previousAction!=CursorAction.SETLL) { + if (this.previousAction != CursorAction.SETLL) { file.positionCursorToPrevious() } - r = Result(as400RecordToSmeUPRecord(file.read())) + r = Result(as400RecordToSmeUPRecord(file.read())) } catch (e: AS400Exception) { handleAS400Error(e) r = Result(Record()) @@ -223,10 +226,10 @@ class JT400DBFile(override var name: String, override fun readEqual(keys: List): Result { resetStatus() - //https://code400.com/forum/forum/iseries-programming-languages/java/8386-noobie-question + // https://code400.com/forum/forum/iseries-programming-languages/java/8386-noobie-question return try { - //val r = if (this.previousAction==CursorAction.SETGT) file.read(keys2Array(keys)) else file.readNextEqual(keys2Array(keys)) - if (this.previousAction==CursorAction.SETGT) { + // val r = if (this.previousAction==CursorAction.SETGT) file.read(keys2Array(keys)) else file.readNextEqual(keys2Array(keys)) + if (this.previousAction == CursorAction.SETGT) { file.positionCursorToPrevious() } val r = file.readNextEqual(keys2Array(keys)) @@ -274,7 +277,7 @@ class JT400DBFile(override var name: String, override fun readPreviousEqual(keys: List): Result { resetStatus() return try { - if (this.previousAction==CursorAction.SETLL) { + if (this.previousAction == CursorAction.SETLL) { file.positionCursorToNext() } val r = file.readPreviousEqual(keys2Array(keys)) @@ -324,8 +327,8 @@ class JT400DBFile(override var name: String, * After a conversion mapping message on a read operation, the file is positioned to the record containing the data that caused the message. */ private fun handleAS400Error(e: AS400Exception) { - //CPF5001 End of file reached - //CPF5006 Record not found + // CPF5001 End of file reached + // CPF5006 Record not found val eid = as400ErrorID(e).toUpperCase() if (eid.startsWith("CPF5001")) { this.eofReached = true @@ -335,20 +338,22 @@ class JT400DBFile(override var name: String, } throw RuntimeException() } - private fun as400ErrorID(e: AS400Exception) : String { - //CPF5001 End of file reached - //CPF5006 Record not found - if (e.aS400Message != null - && e.aS400Message.id != null) { + + private fun as400ErrorID(e: AS400Exception): String { + // CPF5001 End of file reached + // CPF5006 Record not found + if (e.aS400Message != null && + e.aS400Message.id != null + ) { return e.aS400Message.id } return "" } private fun keys2Array(keys: List): Array { - //return keys.map { it.value }.toTypedArray() + // return keys.map { it.value }.toTypedArray() val keysValues = mutableListOf() - //for (key in fileMetadata.fileKeys) { + // for (key in fileMetadata.fileKeys) { for (i in keys.indices) { val keyName = fileMetadata.fileKeys[i] val keyValue = keys.get(i) @@ -373,7 +378,7 @@ class JT400DBFile(override var name: String, private fun as400RecordToSmeUPRecord(r: com.ibm.as400.access.Record?): Record { // TODO create a unit test for the isAfterLast condition - if (r == null) { //TODO || this.isAfterLast + if (r == null) { // TODO || this.isAfterLast return Record() } val result = Record() @@ -385,10 +390,10 @@ class JT400DBFile(override var name: String, return result } - //fun com.ibm.as400.access.Record?.currentRecordToValues(): Record { + // fun com.ibm.as400.access.Record?.currentRecordToValues(): Record { private fun smeUPRecordToAS400Record(r: Record?): com.ibm.as400.access.Record? { if (r == null) { - return null //com.ibm.as400.access.Record() + return null // com.ibm.as400.access.Record() } val result = com.ibm.as400.access.Record() result.recordFormat = file.recordFormat @@ -396,20 +401,20 @@ class JT400DBFile(override var name: String, if (numericField(name)) { result.setField(name, BigDecimal(value)) } else { - //try { - result.setField(name, value.trimEnd()) - //} catch (ex : Exception) { + // try { + result.setField(name, value.trimEnd()) + // } catch (ex : Exception) { // println(ex.message) - //} + // } } } return result } - private fun numericField(name : String) : Boolean { + private fun numericField(name: String): Boolean { val dataType = file.recordFormat.getFieldDescription(name).dataType.instanceType - //val field : Field? = this.fileMetadata.getField(name) - //val type : FieldType? = field?.type + // val field : Field? = this.fileMetadata.getField(name) + // val type : FieldType? = field?.type return when (dataType) { AS400DataType.TYPE_ZONED, AS400DataType.TYPE_PACKED, @@ -423,11 +428,11 @@ class JT400DBFile(override var name: String, AS400DataType.TYPE_UBIN4, AS400DataType.TYPE_UBIN8, AS400DataType.TYPE_FLOAT4, - AS400DataType.TYPE_FLOAT8 -> + AS400DataType.TYPE_FLOAT8, + -> true else -> false } } - -} \ No newline at end of file +} diff --git a/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBMManager.kt b/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBMManager.kt index ca53bae..98da145 100644 --- a/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBMManager.kt +++ b/jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBMManager.kt @@ -25,29 +25,28 @@ import com.smeup.dbnative.ConnectionConfig import com.smeup.dbnative.DBManagerBaseImpl import com.smeup.dbnative.file.DBFile -open class JT400DBMManager(final override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() { - +open class JT400DBMManager(final override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() { private var openedFile = mutableMapOf() // as400://SRVLAB01.SMEUP.COM/W_SCAARM private val match = Regex("as400://((?:\\w|\\.)+)/(\\w+)").find(connectionConfig.url) - private val host : String by lazy { + private val host: String by lazy { match!!.destructured.component1() } - private val library : String by lazy { + private val library: String by lazy { match!!.destructured.component2() } - private val connection : AS400 by lazy { + private val connection: AS400 by lazy { val as400 = AS400(host, connectionConfig.user, connectionConfig.password) as400.isGuiAvailable = false - //as400.addConnectionListener + // as400.addConnectionListener as400.connectService(AS400.RECORDACCESS) as400 } - override fun openFile(name: String) : DBFile { + override fun openFile(name: String): DBFile { require(existFile(name)) { "Cannot open unregistered file $name" } @@ -58,16 +57,15 @@ open class JT400DBMManager(final override val connectionConfig: ConnectionConfig // val fileName = QSYSObjectPathName(library, name, "*FILE", "MBR") val path = fileName.path - //println("Path: $path") + // println("Path: $path") val file = KeyedFile(connection, path) - //val rf = AS400FileRecordDescription(system, path).retrieveRecordFormat() - //file.recordFormat = rf[0] + // val rf = AS400FileRecordDescription(system, path).retrieveRecordFormat() + // file.recordFormat = rf[0] file.setRecordFormat() // Loads the record format directly from the server. file.open(AS400File.READ_WRITE, 0, AS400File.COMMIT_LOCK_LEVEL_NONE) val jt400File = JT400DBFile(name, metadataOf(name), file, logger) openedFile.putIfAbsent(name, jt400File) return jt400File - } override fun closeFile(name: String) { @@ -84,5 +82,4 @@ open class JT400DBMManager(final override val connectionConfig: ConnectionConfig override fun close() { connection.disconnectAllServices() } - -} \ No newline at end of file +} diff --git a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain1KeyTest.kt b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain1KeyTest.kt index cc11c72..73bf549 100644 --- a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain1KeyTest.kt +++ b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain1KeyTest.kt @@ -28,7 +28,6 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class JT400Chain1KeyTest { - private lateinit var dbManager: JT400DBMManager @Before @@ -60,6 +59,4 @@ class JT400Chain1KeyTest { assertTrue(dbFile.chain("XYZ").record.isEmpty()) dbManager.closeFile(TSTTAB_TABLE_NAME) } - } - diff --git a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain2KeysTest.kt b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain2KeysTest.kt index 1b5972d..002611a 100644 --- a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain2KeysTest.kt +++ b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400Chain2KeysTest.kt @@ -28,7 +28,6 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class JT400Chain2KeysTest { - private lateinit var dbManager: JT400DBMManager @Before @@ -48,10 +47,11 @@ class JT400Chain2KeysTest { @Test fun findRecordsIfChainWithExistingKey() { val dbFile = dbManager.openFile(TST2TAB_TABLE_NAME) - val key2 = listOf( - "ABC", - "12.00" - ) + val key2 = + listOf( + "ABC", + "12.00", + ) val chainResult = dbFile.chain(key2) assertEquals("ABC", chainResult.record["TSTFLDCHR"]) assertEquals("12.00", chainResult.record["TSTFLDNBR"]) @@ -62,14 +62,12 @@ class JT400Chain2KeysTest { @Test fun doesNotFindRecordsIfChainWithNotExistingKey() { val dbFile = dbManager.openFile(TST2TAB_TABLE_NAME) - val key2 = listOf( - "ZZZ", - "12" - ) + val key2 = + listOf( + "ZZZ", + "12", + ) assertTrue(dbFile.chain(key2).record.isEmpty()) dbManager.closeFile(TST2TAB_TABLE_NAME) } - - } - diff --git a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400MunicipalityTest.kt b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400MunicipalityTest.kt index ab27a90..c1a95ae 100644 --- a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400MunicipalityTest.kt +++ b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400MunicipalityTest.kt @@ -25,9 +25,7 @@ import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertTrue - class JT400MunicipalityTest { - private lateinit var dbManager: JT400DBMManager @Before @@ -80,11 +78,11 @@ class JT400MunicipalityTest { assertTrue(dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ERBUSCO"))) assertEquals( "EDOLO", - getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record) + getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record), ) assertEquals( "DESENZANO DEL GARDA", - getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record) + getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record), ) dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } @@ -404,7 +402,7 @@ class JT400MunicipalityTest { fun t15_eof() { val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME) val key3A = buildMunicipalityKey("IT", "BAS", "MT") - //val key3A = buildMunicipalityKey("IT", "LOM", "PV", "PALESTRO") + // val key3A = buildMunicipalityKey("IT", "LOM", "PV", "PALESTRO") assertTrue(dbFile.setll(key3A)) var count = 0 while (!dbFile.eof()) { @@ -414,7 +412,4 @@ class JT400MunicipalityTest { assertEquals(32, count) dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } - - } - diff --git a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400OperationsOnFile.kt b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400OperationsOnFile.kt index 5b50436..600477d 100644 --- a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400OperationsOnFile.kt +++ b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400OperationsOnFile.kt @@ -32,7 +32,6 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class JT400OperationsOnFile { - private lateinit var dbManager: JT400DBMManager @Before @@ -194,7 +193,7 @@ class JT400OperationsOnFile { var chainResult = dbFile.chain(keyList) assertEquals(0, chainResult.record.size) - //Set field values and write record + // Set field values and write record chainResult.record["A§ARTI"] = key chainResult.record["A§DEAR"] = "Kotlin DBNativeAccess Write " chainResult.record["A§TIAR"] = "ART " @@ -202,17 +201,17 @@ class JT400OperationsOnFile { dbFile.write(chainResult.record) - //Must exists correct write + // Must exists correct write chainResult = dbFile.chain(keyList) assertEquals(key, chainResult.record["A§ARTI"]) assertEquals("ART ", chainResult.record["A§TIAR"]) assertEquals("Kotlin DBNativeAccess Write ", chainResult.record["A§DEAR"]) assertEquals("0 ", chainResult.record["A§TPAR"]) - //Delete record + // Delete record dbFile.delete(chainResult.record) - //Check delete success + // Check delete success chainResult = dbFile.chain(keyList) assertEquals(0, chainResult.record.size) @@ -220,7 +219,7 @@ class JT400OperationsOnFile { } @Test - fun multipleUpdateOnReadE(){ + fun multipleUpdateOnReadE() { // TEST FLOW // Step1: write 100 records with "currentTimeMillis" as unique key // Step2: read above written records and update A§DEA2 field @@ -235,7 +234,7 @@ class JT400OperationsOnFile { // Create list of items to write into A§ARTI field val items = mutableListOf() - repeat(numberOfRecordsToHandle){ + repeat(numberOfRecordsToHandle) { items.add(System.currentTimeMillis().toString() + " ") Thread.sleep(5) } @@ -246,19 +245,21 @@ class JT400OperationsOnFile { val dea2Key = "Kotlin DBNativeAccess TEST-UPDATED " // WRITE - repeat(numberOfRecordsToHandle){ + repeat(numberOfRecordsToHandle) { val record = Record() - repeat(fieldsNumber){ index -> + repeat(fieldsNumber) { index -> val name: String = dbFile.fileMetadata.fields[index].name - //print(dbFile.fileMetadata.getField(name)?.type) - val value = when(name){ - "A§ARTI" -> items[it] - "A§DEAR" -> dearKey - else -> when(tMetadata.getField(name)?.type){ - is DecimalType -> "0" - else -> "" + // print(dbFile.fileMetadata.getField(name)?.type) + val value = + when (name) { + "A§ARTI" -> items[it] + "A§DEAR" -> dearKey + else -> + when (tMetadata.getField(name)?.type) { + is DecimalType -> "0" + else -> "" + } } - } val recordField = RecordField(name, value) record.add(recordField) @@ -270,7 +271,7 @@ class JT400OperationsOnFile { // Read records with same description (A§DEAR) and update field named 'secondary description' (A§DEA2) val keyList = listOf(dearKey) assertTrue(dbFile.setll(keyList)) - //dbFile.positionCursorBefore(keyList) //TODO rivedere + // dbFile.positionCursorBefore(keyList) //TODO rivedere // Update repeat(numberOfRecordsToHandle) { val readEResult = dbFile.readEqual(keyList) @@ -283,7 +284,7 @@ class JT400OperationsOnFile { // READ AND CHECK // Check all records are updated as expected assertTrue(dbFile.setll(keyList)) - //dbFile.positionCursorBefore(keyList) //TODO rivedere + // dbFile.positionCursorBefore(keyList) //TODO rivedere repeat(numberOfRecordsToHandle) { val readEResult = dbFile.readEqual(keyList) println("[READ AND CHECK]: " + readEResult.record["A§ARTI"]) @@ -292,14 +293,13 @@ class JT400OperationsOnFile { // DELETE assertTrue(dbFile.setll(keyList)) - //dbFile.positionCursorBefore(keyList) //TODO rivedere + // dbFile.positionCursorBefore(keyList) //TODO rivedere repeat(numberOfRecordsToHandle) { val readEResult = dbFile.readEqual(keyList) assertEquals(dea2Key, readEResult.record["A§DEA2"]) - //Delete record + // Delete record dbFile.delete(readEResult.record) } - } @Test @@ -345,7 +345,7 @@ class JT400OperationsOnFile { dbManager.registerMetadata(fileMetadata, false) val dbFile = dbManager.openFile("VERAPG1L") - //keys: V£DATA, V£NOME, V£IDOJ + // keys: V£DATA, V£NOME, V£IDOJ val data = "20200901" val nome = "BNUNCA " val idoj = "0002003070" @@ -449,6 +449,4 @@ class JT400OperationsOnFile { dbManager.closeFile("BRARTI0L") } */ - } - diff --git a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400ReadPreviousTest.kt b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400ReadPreviousTest.kt index ad69ad5..a1a8953 100644 --- a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400ReadPreviousTest.kt +++ b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/JT400ReadPreviousTest.kt @@ -25,7 +25,6 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class JT400ReadPreviousTest { - private lateinit var dbManager: JT400DBMManager @Before @@ -80,6 +79,4 @@ class JT400ReadPreviousTest { assertEquals("SALLY KWAN", getEmployeeName(dbFile.read().record)) dbManager.closeFile(EMPLOYEE_TABLE_NAME) } - - } diff --git a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/utils/JT400DBTestUtils.kt b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/utils/JT400DBTestUtils.kt index 96de888..1ae3ad1 100644 --- a/jt400/src/test/kotlin/com/smeup/dbnative/jt400/utils/JT400DBTestUtils.kt +++ b/jt400/src/test/kotlin/com/smeup/dbnative/jt400/utils/JT400DBTestUtils.kt @@ -35,8 +35,9 @@ const val TSTTAB_TABLE_NAME = "TSTTAB" const val TST2TAB_TABLE_NAME = "TST2TAB" const val MUNICIPALITY_TABLE_NAME = "MUNIC0000B" const val TEST_LOG = false -//do not change defaultValue -//if you want to create sqlconnection against another db use function: dbManagerForTest(testSQLDBType: TestSQLDBType) + +// do not change defaultValue +// if you want to create sqlconnection against another db use function: dbManagerForTest(testSQLDBType: TestSQLDBType) private var defaultDbType = TestSQLDBType.DB2_400 const val DB2_400_HOST = "SRVLAB01.SMEUP.COM" const val DB2_400_LIBRARY_NAME = "W_PARFRA" @@ -46,32 +47,34 @@ const val CONVENTIONAL_INDEX_SUFFIX = "_INDEX" enum class TestSQLDBType( val connectionConfig: ConnectionConfig, val dbaConnectionConfig: ConnectionConfig? = connectionConfig, - val createDatabase : (dbaConnection: AS400) -> Unit = {}, - val destroyDatabase: (dbaConnection: AS400) -> Unit = {}) { - DB2_400(ConnectionConfig( - fileName= "*", + val createDatabase: (dbaConnection: AS400) -> Unit = {}, + val destroyDatabase: (dbaConnection: AS400) -> Unit = {}, +) { + DB2_400( + ConnectionConfig( + fileName = "*", driver = "com.ibm.as400.access.AS400JDBCDriver", url = "jdbc:as400://$DB2_400_HOST/$DB2_400_LIBRARY_NAME;", user = "USER", - password = "*********"), - //force no create connection for dba operations - dbaConnectionConfig = null - ) - + password = "*********", + ), + // force no create connection for dba operations + dbaConnectionConfig = null, + ), } fun dbManagerForTest() = dbManagerForTest(defaultDbType) -fun dbManagerForTest(testSQLDBType: TestSQLDBType) : JT400DBMManager { +fun dbManagerForTest(testSQLDBType: TestSQLDBType): JT400DBMManager { testLog("Creating SQLDBManager with db type = $testSQLDBType") val dbManager = JT400DBMManager(testSQLDBType.connectionConfig) if (testSQLDBType.dbaConnectionConfig != null) { - //JT400DBMMAnager(testSQLDBType.dbaConnectionConfig).connection.use { + // JT400DBMMAnager(testSQLDBType.dbaConnectionConfig).connection.use { // testSQLDBType.createDatabase(it) - //} + // } } - //dbManager.setSQLLog(TEST_LOG) + // dbManager.setSQLLog(TEST_LOG) return dbManager } @@ -81,117 +84,121 @@ fun destroyDatabase() { fun destroyDatabase(testSQLDBType: TestSQLDBType) { if (testSQLDBType.dbaConnectionConfig != null) { - //JT400DBMMAnager(testSQLDBType.dbaConnectionConfig).connection.use { + // JT400DBMMAnager(testSQLDBType.dbaConnectionConfig).connection.use { // testSQLDBType.destroyDatabase(it) - //} + // } } } fun createAndPopulateMunicipalityTable(dbManager: JT400DBMManager?) { - val fields = listOf( - "NAZ" fieldByType CharacterType(2), - "REG" fieldByType CharacterType(3), - "PROV" fieldByType CharacterType(2), - "CITTA" fieldByType VarcharType(35), - "CAP" fieldByType CharacterType(5), - "PREF" fieldByType CharacterType(4), - "COMUNE" fieldByType CharacterType(4), - "ISTAT" fieldByType CharacterType(6) - ) - - val keys = listOf( - "NAZ", - "REG", - "PROV", - "CITTA" - ) - - - //createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ + val fields = + listOf( + "NAZ" fieldByType CharacterType(2), + "REG" fieldByType CharacterType(3), + "PROV" fieldByType CharacterType(2), + "CITTA" fieldByType VarcharType(35), + "CAP" fieldByType CharacterType(5), + "PREF" fieldByType CharacterType(4), + "COMUNE" fieldByType CharacterType(4), + "ISTAT" fieldByType CharacterType(6), + ) + + val keys = + listOf( + "NAZ", + "REG", + "PROV", + "CITTA", + ) + + // createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ registerTable( dbManager, MUNICIPALITY_TABLE_NAME, "TSTREC", fields, keys, - "src/test/resources/csv/Municipality.csv" + "src/test/resources/csv/Municipality.csv", ) - /**/ + // } fun createAndPopulateTestTable(dbManager: JT400DBMManager?) { - val fields = listOf( - "TSTFLDCHR" fieldByType CharacterType(3), - "TSTFLDNBR" fieldByType DecimalType(5, 2) - ) - - val keys = listOf( - "TSTFLDCHR", - "TSTFLDNBR" - ) - - - //createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ + val fields = + listOf( + "TSTFLDCHR" fieldByType CharacterType(3), + "TSTFLDNBR" fieldByType DecimalType(5, 2), + ) + + val keys = + listOf( + "TSTFLDCHR", + "TSTFLDNBR", + ) + + // createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ registerTable( dbManager, TSTTAB_TABLE_NAME, "TSTREC", fields, keys, - "src/test/resources/csv/TstTab.csv" + "src/test/resources/csv/TstTab.csv", ) - /**/ + // } fun createAndPopulateTest2Table(dbManager: JT400DBMManager?) { - val fields = listOf( - "TSTFLDCHR" fieldByType CharacterType(3), - "TSTFLDNBR" fieldByType DecimalType(5, 2), - "DESTST" fieldByType CharacterType(10), - ) - - val keys = listOf( - "TSTFLDCHR", - "TSTFLDNBR" - ) - - - //createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ + val fields = + listOf( + "TSTFLDCHR" fieldByType CharacterType(3), + "TSTFLDNBR" fieldByType DecimalType(5, 2), + "DESTST" fieldByType CharacterType(10), + ) + + val keys = + listOf( + "TSTFLDCHR", + "TSTFLDNBR", + ) + + // createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ registerTable( dbManager, TST2TAB_TABLE_NAME, "TSTREC", fields, keys, - "src/test/resources/csv/TstTab.csv" + "src/test/resources/csv/TstTab.csv", ) - /**/ + // } fun createAndPopulateEmployeeTable(dbManager: JT400DBMManager?) { - val fields = listOf( - "EMPNO" fieldByType CharacterType(6), - "FIRSTNME" fieldByType CharacterType(20), - "MIDINIT" fieldByType CharacterType(1), - "LASTNAME" fieldByType CharacterType(20), - "WORKDEPT" fieldByType CharacterType(3), - ) - - val keys = listOf( - "EMPNO" - ) - - - //createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ + val fields = + listOf( + "EMPNO" fieldByType CharacterType(6), + "FIRSTNME" fieldByType CharacterType(20), + "MIDINIT" fieldByType CharacterType(1), + "LASTNAME" fieldByType CharacterType(20), + "WORKDEPT" fieldByType CharacterType(3), + ) + + val keys = + listOf( + "EMPNO", + ) + + // createAndPopulateTable( /* ci mette tantissimo >20min, la teniamo fissa già creata */ registerTable( dbManager, EMPLOYEE_TABLE_NAME, "TSTREC", fields, keys, - "src/test/resources/csv/Employee.csv" + "src/test/resources/csv/Employee.csv", ) - /**/ + // } fun getEmployeeName(record: Record): String { @@ -214,7 +221,7 @@ private fun registerTable( formatName: String, fields: List, keys: List, - dataFilePath: String + dataFilePath: String, ) { val metadata = TypedMetadata(tableName, formatName, fields, keys).fileMetadata() dbManager!!.registerMetadata(metadata, true) @@ -261,7 +268,7 @@ fun buildMunicipalityKey(vararg values: String): List { val recordFields = mutableListOf() val keys = arrayOf("NAZ", "REG", "PROV", "CITTA") for ((index, value) in values.withIndex()) { - if (keys.size> index) { + if (keys.size > index) { recordFields.add(value) } } @@ -290,5 +297,3 @@ fun connectJDBC() : Connection { return DriverManager.getConnection(connectionConfig.url, connectionConfig.user, connectionConfig.password) } */ - - diff --git a/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileFactory.kt b/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileFactory.kt index 6b17deb..a909a28 100644 --- a/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileFactory.kt +++ b/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileFactory.kt @@ -40,10 +40,9 @@ import com.smeup.dbnative.model.FileMetadata * */ class DBFileFactory( private val config: DBNativeAccessConfig, - private val fileNameNormalizer: (String) -> String = {it} + private val fileNameNormalizer: (String) -> String = { it }, ) : AutoCloseable { - - private val managers = mutableMapOf () + private val managers = mutableMapOf() /** * Open the file named fileName. A file can only be opened after registration of its metadata. @@ -54,10 +53,13 @@ class DBFileFactory( * @param fileName file to open * @param fileMetadata metadata to register. If passed, file metadata is registered before file opening * */ - fun open(fileName: String, fileMetadata: FileMetadata?) : DBFile { + fun open( + fileName: String, + fileMetadata: FileMetadata?, + ): DBFile { val fileNameNormalized = fileNameNormalizer(fileName) val configMatch = findConnectionConfigFor(fileNameNormalized, config.connectionsConfig) - val dbmManager = managers.getOrPut(configMatch) {createDBManager(configMatch, config.logger).apply { validateConfig() }} + val dbmManager = managers.getOrPut(configMatch) { createDBManager(configMatch, config.logger).apply { validateConfig() } } if (fileMetadata != null) { dbmManager.registerMetadata(fileMetadata, true) @@ -87,7 +89,7 @@ class DBFileFactory( } override fun close() { - managers.values.forEach {it.close()} + managers.values.forEach { it.close() } } } @@ -96,34 +98,42 @@ class DBFileFactory( * @param fileName file name * @param connectionsConfig ConnectionConfig entries * */ -fun findConnectionConfigFor(fileName: String, connectionsConfig: List) : ConnectionConfig { - val configList = connectionsConfig.filter { - it.fileName.toUpperCase() == fileName.toUpperCase() || it.fileName == "*" || +fun findConnectionConfigFor( + fileName: String, + connectionsConfig: List, +): ConnectionConfig { + val configList = + connectionsConfig.filter { + it.fileName.toUpperCase() == fileName.toUpperCase() || it.fileName == "*" || fileName.toUpperCase().matches(Regex(it.fileName.toUpperCase().replace("*", ".*"))) - } + } require(configList.isNotEmpty()) { "Wrong configuration. Not found a ConnectionConfig entry matching name: $fileName" } - //At the top of the list we have ConnectionConfig whose property file does not have wildcards + // At the top of the list we have ConnectionConfig whose property file does not have wildcards return configList.sortedWith(DBFileFactory.COMPARATOR)[0] } -private fun createDBManager(config: ConnectionConfig, logger: Logger? = null): DBMManager { +private fun createDBManager( + config: ConnectionConfig, + logger: Logger? = null, +): DBMManager { val impl = getImplByUrl(config) - val clazz :Class? = Class.forName(impl) as Class? + val clazz: Class? = Class.forName(impl) as Class? return clazz?.let { val constructor = it.getConstructor(ConnectionConfig::class.java) val dbmManager = constructor.newInstance(config) - if(dbmManager is DBManagerBaseImpl){ - dbmManager.logger = logger - } + if (dbmManager is DBManagerBaseImpl) + { + dbmManager.logger = logger + } return dbmManager }!! } -private fun getImplByUrl(config: ConnectionConfig) : String { +private fun getImplByUrl(config: ConnectionConfig): String { return when { config.impl != null && config.impl!!.trim().isEmpty() -> config.impl!! config.url.startsWith("jdbc:") -> "com.smeup.dbnative.sql.SQLDBMManager" @@ -133,10 +143,12 @@ private fun getImplByUrl(config: ConnectionConfig) : String { } } -//ConnectionConfig.file with wildcards at the bottom +// ConnectionConfig.file with wildcards at the bottom class ConnectionConfigComparator : Comparator { - - override fun compare(o1: ConnectionConfig?, o2: ConnectionConfig?): Int { + override fun compare( + o1: ConnectionConfig?, + o2: ConnectionConfig?, + ): Int { require(o1 != null) require(o2 != null) return when { @@ -148,4 +160,4 @@ class ConnectionConfigComparator : Comparator { else -> o1.fileName.compareTo(o2.fileName) } } -} \ No newline at end of file +} diff --git a/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileWrapper.kt b/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileWrapper.kt index 8b78eb4..0fd69ff 100644 --- a/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileWrapper.kt +++ b/manager/src/main/kotlin/com/smeup/dbnative/manager/DBFileWrapper.kt @@ -24,8 +24,7 @@ import com.smeup.dbnative.file.Result import com.smeup.dbnative.log.Logger import com.smeup.dbnative.model.FileMetadata -class DBFileWrapper (private val dbFile: DBFile, private val dbmManager: DBMManager): DBFile { - +class DBFileWrapper(private val dbFile: DBFile, private val dbmManager: DBMManager) : DBFile { private var closed = false override var name: String @@ -37,7 +36,9 @@ class DBFileWrapper (private val dbFile: DBFile, private val dbmManager: DBMMana set(value) {} override var logger: Logger? get() = dbFile.logger - set(value) {dbFile.logger = value} + set(value) { + dbFile.logger = value + } override fun eof(): Boolean { return dbFile.eof() @@ -142,4 +143,4 @@ class DBFileWrapper (private val dbFile: DBFile, private val dbmManager: DBMMana closed = true dbmManager.closeFile(fileMetadata.tableName) } -} \ No newline at end of file +} diff --git a/manager/src/test/kotlin/com/smeup/dbnative/manager/DBFileFactoryTest.kt b/manager/src/test/kotlin/com/smeup/dbnative/manager/DBFileFactoryTest.kt index acbb3a4..4c03040 100644 --- a/manager/src/test/kotlin/com/smeup/dbnative/manager/DBFileFactoryTest.kt +++ b/manager/src/test/kotlin/com/smeup/dbnative/manager/DBFileFactoryTest.kt @@ -32,51 +32,57 @@ import org.junit.Before import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertTrue + private val LOGGING_LEVEL = LoggingLevel.OFF private enum class TestConnectionConfig( - val connectionConfig: ConnectionConfig + val connectionConfig: ConnectionConfig, ) { DEFAULT( - connectionConfig = ConnectionConfig( - fileName= "*", - url = "jdbc:hsqldb:mem:TEST", - user = "", - password = "", - driver = "org.hsqldb.jdbcDriver" - ) + connectionConfig = + ConnectionConfig( + fileName = "*", + url = "jdbc:hsqldb:mem:TEST", + user = "", + password = "", + driver = "org.hsqldb.jdbcDriver", + ), ), STARTS_WITH_TEST( - connectionConfig = ConnectionConfig( - fileName= "TEST*", - url = "jdbc:hsqldb:mem:TEST", - user = "", - password = "", - driver = "org.hsqldb.jdbcDriver" - ) + connectionConfig = + ConnectionConfig( + fileName = "TEST*", + url = "jdbc:hsqldb:mem:TEST", + user = "", + password = "", + driver = "org.hsqldb.jdbcDriver", + ), ), MUNICIPALITY( ConnectionConfig( - fileName= "MUNICIPALITY", + fileName = "MUNICIPALITY", url = "mongodb://localhost:27017/W_TEST", user = "", - password = "") - ) + password = "", + ), + ), } class DBFileFactoryTest { - - private lateinit var config : DBNativeAccessConfig + private lateinit var config: DBNativeAccessConfig private lateinit var manager: SQLDBMManager @Before fun setUp() { - config = DBNativeAccessConfig(mutableListOf( - TestConnectionConfig.DEFAULT.connectionConfig, - TestConnectionConfig.STARTS_WITH_TEST.connectionConfig, - TestConnectionConfig.MUNICIPALITY.connectionConfig - ), - Logger.getSimpleInstance(LOGGING_LEVEL)) + config = + DBNativeAccessConfig( + mutableListOf( + TestConnectionConfig.DEFAULT.connectionConfig, + TestConnectionConfig.STARTS_WITH_TEST.connectionConfig, + TestConnectionConfig.MUNICIPALITY.connectionConfig, + ), + Logger.getSimpleInstance(LOGGING_LEVEL), + ) manager = SQLDBMManager(TestConnectionConfig.STARTS_WITH_TEST.connectionConfig) manager.connection.createStatement().use { it.executeUpdate("CREATE TABLE TEST1F (NAME CHAR(20))") @@ -94,31 +100,36 @@ class DBFileFactoryTest { testFields.add("NAME" fieldByType CharacterType(20)) val testTableMetadata = FileMetadata("TEST1L", "TEST1F", testFields.fieldList(), listOf("NAME")) manager.registerMetadata(testTableMetadata, true) - } @Test fun findConnectionForPIPPO() { - assertEquals(TestConnectionConfig.DEFAULT.connectionConfig, - findConnectionConfigFor("PIPPO/PLUTO", config.connectionsConfig)) + assertEquals( + TestConnectionConfig.DEFAULT.connectionConfig, + findConnectionConfigFor("PIPPO/PLUTO", config.connectionsConfig), + ) } @Test fun findConnectionForTESTXXX() { - assertEquals(TestConnectionConfig.STARTS_WITH_TEST.connectionConfig, - findConnectionConfigFor("TEST3L", config.connectionsConfig)) + assertEquals( + TestConnectionConfig.STARTS_WITH_TEST.connectionConfig, + findConnectionConfigFor("TEST3L", config.connectionsConfig), + ) } @Test fun findConnectionForMUNICIPALITY() { - assertEquals(TestConnectionConfig.MUNICIPALITY.connectionConfig, - findConnectionConfigFor("MUNICIPALITY", config.connectionsConfig)) + assertEquals( + TestConnectionConfig.MUNICIPALITY.connectionConfig, + findConnectionConfigFor("MUNICIPALITY", config.connectionsConfig), + ) } - //test ok if no throw exception + // test ok if no throw exception @Test fun openExistingTables() { - DBFileFactory(config).use {dbFileFactory -> + DBFileFactory(config).use { dbFileFactory -> // Open a file already registered dbFileFactory.open("TEST1L", null) @@ -143,9 +154,9 @@ class DBFileFactoryTest { DBFileFactory(config).use { dbFileFactory -> val dbFile = dbFileFactory.open("TEST1L", null) var result = dbFile.chain("MARCO") - assertTrue (result.record["NAME"]?.trim().equals("MARCO")) + assertTrue(result.record["NAME"]?.trim().equals("MARCO")) result = dbFile.chain("DARIO") - assertTrue (result.record["NAME"]?.trim().equals("DARIO")) + assertTrue(result.record["NAME"]?.trim().equals("DARIO")) dbFile.close() } @@ -160,12 +171,12 @@ class DBFileFactoryTest { @Test fun reopenClosedFile() { - DBFileFactory(config).use {dbFileFactory -> + DBFileFactory(config).use { dbFileFactory -> val dbFile = dbFileFactory.open("TEST1L", null) dbFile.setll("DARIO") dbFile.read() dbFile.close() - assertTrue (dbFile.runCatching { read() }.isFailure) + assertTrue(dbFile.runCatching { read() }.isFailure) } } @@ -184,5 +195,4 @@ class DBFileFactoryTest { manager.unregisterMetadata("TEST2L") manager.unregisterMetadata("TEST3L") } - -} \ No newline at end of file +} diff --git a/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBFile.kt b/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBFile.kt index be0bb2e..7e0e98e 100644 --- a/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBFile.kt +++ b/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBFile.kt @@ -35,11 +35,12 @@ import com.smeup.dbnative.utils.matchFileKeys import org.bson.Document import kotlin.system.measureTimeMillis -class NoSQLDBFile(override var name: String, - override var fileMetadata: FileMetadata, - private val database: MongoDatabase, - override var logger: Logger? = null): DBFile { - +class NoSQLDBFile( + override var name: String, + override var fileMetadata: FileMetadata, + private val database: MongoDatabase, + override var logger: Logger? = null, +) : DBFile { private var globalCursor: MongoCursor? = null private var up_direction: Boolean = true private var last_set_keys: List = emptyList() @@ -48,8 +49,11 @@ class NoSQLDBFile(override var name: String, private var eof: Boolean = false private var lastNativeMethod: NativeMethod? = null - private fun logEvent(loggingKey: LoggingKey, message: String, elapsedTime: Long? = null) = - logger?.logEvent(loggingKey, message, elapsedTime, lastNativeMethod, fileMetadata.name) + private fun logEvent( + loggingKey: LoggingKey, + message: String, + elapsedTime: Long? = null, + ) = logger?.logEvent(loggingKey, message, elapsedTime, lastNativeMethod, fileMetadata.name) override fun eof(): Boolean { return eof @@ -67,7 +71,6 @@ class NoSQLDBFile(override var name: String, } } - override fun setll(key: String): Boolean { return setll(mutableListOf(key)) } @@ -82,10 +85,11 @@ class NoSQLDBFile(override var name: String, measureTimeMillis { eof = false - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = fileMetadata.fileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = fileMetadata.fileKeys.get(index) + RecordField(keyname, value) + } /* Passed keys are not primary key for DBFile @@ -94,7 +98,6 @@ class NoSQLDBFile(override var name: String, return false } - /* Find syntax @@ -104,7 +107,7 @@ class NoSQLDBFile(override var name: String, {$and:[{ NAZ: { $eq: "IT" } }, { REG: { $eq: "LOM" } }, { PROV: { $eq: "BS" } }, { CITTA: { $gt: "ERBUSCO" } } ] } - */ + */ /* val filter = StringBuilder() @@ -152,7 +155,7 @@ class NoSQLDBFile(override var name: String, up_direction = true last_keys = keys - */ + */ val cursor = calculateCursor(keyAsRecordField, true, true) @@ -185,10 +188,11 @@ class NoSQLDBFile(override var name: String, measureTimeMillis { eof = false - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = fileMetadata.fileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = fileMetadata.fileKeys.get(index) + RecordField(keyname, value) + } /* Passed keys are not primary key for DBFile @@ -206,19 +210,19 @@ class NoSQLDBFile(override var name: String, {$and:[{ NAZ: { $gte: "IT" } }, { REG: { $gte: "LOM" } }, { PROV: { $gte: "BS" } }, { CITTA: { $gte: "ERBUSCO" } } ] } - *** SETGT forward: + *** SETGT forward: {$and:[{ NAZ: { $eq: "IT" } }, { REG: { $eq: "LOM" } }, { PROV: { $eq: "BS" } }, { CITTA: { $gt: "ERBUSCO" } } ] } with order {NAZ: 1, REG: 1, PROV: 1, CITTA: 1} - ***SETGT backward: + ***SETGT backward: {$and:[{ NAZ: { $eq: "IT" } }, { REG: { $eq: "LOM" } }, { PROV: { $eq: "BS" } }, { CITTA: { $gt: "ERBUSCO" } } ] } with order {NAZ: 1, REG: 1, PROV: 1, CITTA: -1} - */ + */ /* val filter = StringBuilder() @@ -231,8 +235,7 @@ class NoSQLDBFile(override var name: String, filter.append("{ ${dbField.name}: {\$gt: \"${keys.get(keys.indexOfFirst { recordField -> recordField.name == dbField.name }).value}\" } }") } } - */ - + */ /* Sort sintax: @@ -273,14 +276,13 @@ class NoSQLDBFile(override var name: String, last_set_keys = keyAsRecordField IncludeFirst = false }.apply { - logEvent(LoggingKey.native_access_method, "setgt executed", this) + logEvent(LoggingKey.native_access_method, "setgt executed", this) } lastNativeMethod = null return result } override fun chain(key: String): Result { - return chain(mutableListOf(key)) } @@ -291,37 +293,37 @@ class NoSQLDBFile(override var name: String, measureTimeMillis { eof = false - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = fileMetadata.fileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = fileMetadata.fileKeys.get(index) + RecordField(keyname, value) + } /* Passed keys are not primary key for DBFile */ if (fileMetadata.matchFileKeys(keyAsRecordField) == false) { - result = Result(record = Record(), indicatorLO = true) - } - else { - + result = Result(record = Record(), indicatorLO = true) + } else { val cursor = calculateCursor(keyAsRecordField, true, true) globalCursor = cursor.iterator() up_direction = true last_set_keys = keyAsRecordField - //when globalCursor is empty return result empty + // when globalCursor is empty return result empty val document = globalCursor!!.tryNext() - if(document == null){ - result = Result(Record()) - } - else - if (matchKeys(document, keyAsRecordField)) { - val record = documentToRecord(document) - updateLastKeys(record) - result = Result(record) - } else { - result = Result(Record()) + if (document == null) + { + result = Result(Record()) + } else { + if (matchKeys(document, keyAsRecordField)) { + val record = documentToRecord(document) + updateLastKeys(record) + result = Result(record) + } else { + result = Result(Record()) + } } } }.apply { @@ -341,7 +343,7 @@ class NoSQLDBFile(override var name: String, if (globalCursor == null) { return Result( indicatorLO = true, - errorMsg = "Cursor not defined. Call SETLL or SETGT before invoke READ command" + errorMsg = "Cursor not defined. Call SETLL or SETGT before invoke READ command", ) } @@ -356,7 +358,6 @@ class NoSQLDBFile(override var name: String, IncludeFirst = true if (globalCursor != null) { - if (globalCursor!!.hasNext()) { val record = documentToRecord(globalCursor!!.next()) if (globalCursor!!.hasNext()) { @@ -398,10 +399,11 @@ class NoSQLDBFile(override var name: String, lastNativeMethod = NativeMethod.readEqual logEvent(LoggingKey.native_access_method, "Executing readEqual on keys $keys") measureTimeMillis { - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = fileMetadata.fileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = fileMetadata.fileKeys.get(index) + RecordField(keyname, value) + } if (globalCursor == null) { globalCursor = calculateCursor(keyAsRecordField, true, true).iterator() @@ -426,7 +428,6 @@ class NoSQLDBFile(override var name: String, while (true) { if (globalCursor!!.hasNext()) { - val document = globalCursor!!.next() val record = documentToRecord(document) @@ -457,7 +458,7 @@ class NoSQLDBFile(override var name: String, if (globalCursor == null) { return Result( indicatorLO = true, - errorMsg = "Cursor not defined. Call SETLL or SETGT before invoke READ command" + errorMsg = "Cursor not defined. Call SETLL or SETGT before invoke READ command", ) } @@ -472,7 +473,6 @@ class NoSQLDBFile(override var name: String, IncludeFirst = true if (globalCursor != null) { - if (globalCursor!!.hasNext()) { val record = documentToRecord(globalCursor!!.next()) if (globalCursor!!.hasNext()) { @@ -506,15 +506,16 @@ class NoSQLDBFile(override var name: String, lastNativeMethod = NativeMethod.readPreviousEqual logEvent(LoggingKey.native_access_method, "Executing readPreviousEqual on keys $keys") measureTimeMillis { - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = fileMetadata.fileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = fileMetadata.fileKeys.get(index) + RecordField(keyname, value) + } if (globalCursor == null) { return Result( indicatorLO = true, - errorMsg = "Cursor not defined. Call SETLL or SETGT before invoke READ command" + errorMsg = "Cursor not defined. Call SETLL or SETGT before invoke READ command", ) } @@ -536,10 +537,8 @@ class NoSQLDBFile(override var name: String, IncludeFirst = true - while (true) { if (globalCursor!!.hasNext()) { - val document = globalCursor!!.next() val record = documentToRecord(document) @@ -563,7 +562,6 @@ class NoSQLDBFile(override var name: String, lastNativeMethod = null } - override fun write(record: Record): Result { lastNativeMethod = NativeMethod.write logEvent(LoggingKey.native_access_method, "Executing write for record $record") @@ -579,7 +577,7 @@ class NoSQLDBFile(override var name: String, } override fun update(record: Record): Result { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + TODO("not implemented") // To change body of created functions use File | Settings | File Templates. } override fun delete(record: Record): Result { @@ -591,8 +589,10 @@ class NoSQLDBFile(override var name: String, /* Evaluate matching between values of passed keys and relative values in document */ - private fun matchKeys(document: Document, keys: List): Boolean { - + private fun matchKeys( + document: Document, + keys: List, + ): Boolean { var match = true keys.forEach({ @@ -602,10 +602,8 @@ class NoSQLDBFile(override var name: String, }) return match - } - private fun documentToRecord(document: Document?): Record { val result = Record() @@ -617,7 +615,11 @@ class NoSQLDBFile(override var name: String, return result } - private fun calculateCursor(keys: List, up_direction: Boolean, includeFirst: Boolean): FindIterable { + private fun calculateCursor( + keys: List, + up_direction: Boolean, + includeFirst: Boolean, + ): FindIterable { /* Complete examples for filters in file filter_syntax_examples.txt @@ -650,7 +652,7 @@ class NoSQLDBFile(override var name: String, ] } - */ + */ var operator1: MongoOperator = MongoOperator.EQ var operator2: MongoOperator = MongoOperator.EQ @@ -692,25 +694,36 @@ class NoSQLDBFile(override var name: String, } } - val filter = StringBuilder() filter.append("{\$or:[") val orContent = StringBuilder() - // Add first line val line = StringBuilder() line.append("{\$and:[") - keyFields.forEachIndexed{index: Int, dbField: Field -> - if (index != keyFields.size-1) { - line.append("{ \"${dbField.name}\": {${operator3.symbol} \"${keys.get(keys.indexOfFirst { recordField -> recordField.name == dbField.name }).value}\" } }, ") - } - else { - line.append("{ \"${dbField.name}\": {${operator1.symbol} \"${keys.get(keys.indexOfFirst { recordField -> recordField.name == dbField.name }).value}\" } }") + keyFields.forEachIndexed { index: Int, dbField: Field -> + if (index != keyFields.size - 1) { + line.append( + "{ \"${dbField.name}\": {${operator3.symbol} \"${keys.get( + keys.indexOfFirst { + recordField -> + recordField.name == dbField.name + }, + ).value}\" } }, ", + ) + } else { + line.append( + "{ \"${dbField.name}\": {${operator1.symbol} \"${keys.get( + keys.indexOfFirst { + recordField -> + recordField.name == dbField.name + }, + ).value}\" } }", + ) } } @@ -720,23 +733,34 @@ class NoSQLDBFile(override var name: String, // Add other lines if (keyFields.size > 1) { - var while_index = keyFields.size while (while_index > 1) { - val tempLine = StringBuilder() tempLine.append(", {\$and:[") - val subList = keyFields.subList(0, while_index-1) - - subList.forEachIndexed{index: Int, dbField: Field -> - if (index != subList.size-1) { - tempLine.append("{ \"${dbField.name}\": {${operator3.symbol} \"${keys.get(keys.indexOfFirst { recordField -> recordField.name == dbField.name }).value}\" } }, ") - } - else { - tempLine.append("{ \"${dbField.name}\": {${operator2.symbol} \"${keys.get(keys.indexOfFirst { recordField -> recordField.name == dbField.name }).value}\" } }") + val subList = keyFields.subList(0, while_index - 1) + + subList.forEachIndexed { index: Int, dbField: Field -> + if (index != subList.size - 1) { + tempLine.append( + "{ \"${dbField.name}\": {${operator3.symbol} \"${keys.get( + keys.indexOfFirst { + recordField -> + recordField.name == dbField.name + }, + ).value}\" } }, ", + ) + } else { + tempLine.append( + "{ \"${dbField.name}\": {${operator2.symbol} \"${keys.get( + keys.indexOfFirst { + recordField -> + recordField.name == dbField.name + }, + ).value}\" } }", + ) } } @@ -752,7 +776,6 @@ class NoSQLDBFile(override var name: String, filter.append("] }") - /* Sort sintax: @@ -762,7 +785,7 @@ class NoSQLDBFile(override var name: String, val sort = StringBuilder() - keyFields.joinTo(sort, separator= ",", prefix= "{", postfix = "}") { + keyFields.joinTo(sort, separator = ",", prefix = "{", postfix = "}") { if (up_direction) { "\"${it.name}\": 1" } else { @@ -771,7 +794,7 @@ class NoSQLDBFile(override var name: String, } sort.append("}") logEvent(LoggingKey.execute_inquiry, "Building filter command $filter with sort $sort") - //println("$filter - $sort") + // println("$filter - $sort") val cursor = database.getCollection(fileMetadata.tableName).find(Document.parse(filter.toString())) @@ -779,7 +802,6 @@ class NoSQLDBFile(override var name: String, } private fun updateLastKeys(record: Record) { - val lastKeys = mutableListOf() fileMetadata.fileKeys.forEach { lastKeys.add(RecordField(it, record.getValue(it))) @@ -793,8 +815,6 @@ class NoSQLDBFile(override var name: String, GT("\$gt:"), GE("\$gte:"), LT("\$lt:"), - LE("\$lte:") + LE("\$lte:"), } - } - diff --git a/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBMManager.kt b/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBMManager.kt index 9d195cd..c84f690 100644 --- a/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBMManager.kt +++ b/nosql/src/main/kotlin/com/smeup/dbnative/nosql/NoSQLDBMManager.kt @@ -17,14 +17,11 @@ package com.smeup.dbnative.nosql -import com.mongodb.BasicDBObject import com.mongodb.MongoClient -import com.mongodb.client.MongoCollection import com.mongodb.client.MongoDatabase import com.smeup.dbnative.ConnectionConfig import com.smeup.dbnative.DBManagerBaseImpl import com.smeup.dbnative.file.DBFile -import org.bson.Document /** * Assign table: @@ -34,24 +31,23 @@ import org.bson.Document * Record --> Object in collection */ -class NoSQLDBMManager (override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() { - +class NoSQLDBMManager(override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() { private val match = Regex("mongodb://((?:\\w|\\.)+):(\\d+)/(\\w+)").find(connectionConfig.url) - private val host : String by lazy { + private val host: String by lazy { match!!.destructured.component1() } - private val port : Int by lazy { + private val port: Int by lazy { match!!.destructured.component2().toInt() } - private val dataBase : String by lazy { + private val dataBase: String by lazy { match!!.destructured.component3() } - private val mongoClient : MongoClient by lazy { + private val mongoClient: MongoClient by lazy { MongoClient(host, port) } - val mongoDatabase : MongoDatabase by lazy { + val mongoDatabase: MongoDatabase by lazy { mongoClient.getDatabase(dataBase) } @@ -64,13 +60,12 @@ class NoSQLDBMManager (override val connectionConfig: ConnectionConfig) : DBMana } override fun close() { - openedFile.values.forEach { it.close()} + openedFile.values.forEach { it.close() } openedFile.clear() mongoClient.close() } override fun openFile(name: String): DBFile { - require(existFile(name)) { "Cannot open unregistered file $name" } @@ -93,4 +88,4 @@ class NoSQLDBMManager (override val connectionConfig: ConnectionConfig) : DBMana override fun closeFile(name: String) { openedFile.remove(name)!!.close() } -} \ No newline at end of file +} diff --git a/nosql/src/main/kotlin/com/smeup/dbnative/nosql/utils/MongoDbUtils.kt b/nosql/src/main/kotlin/com/smeup/dbnative/nosql/utils/MongoDbUtils.kt index ac0ed3a..3ab6b17 100644 --- a/nosql/src/main/kotlin/com/smeup/dbnative/nosql/utils/MongoDbUtils.kt +++ b/nosql/src/main/kotlin/com/smeup/dbnative/nosql/utils/MongoDbUtils.kt @@ -20,14 +20,16 @@ package com.smeup.dbnative.nosql.utils import com.smeup.dbnative.file.Record import com.smeup.dbnative.model.FileMetadata - -fun FileMetadata.buildInsertCommand(filename: String, record: Record): String { - //TODO: insert controls beetwen metadata and record format - println("Build insert command from ${filename} metadata") +fun FileMetadata.buildInsertCommand( + filename: String, + record: Record, +): String { + // TODO: insert controls beetwen metadata and record format + println("Build insert command from $filename metadata") val documents = StringBuilder() - record.toList().joinTo(documents, separator=",", prefix="{", postfix="}") { + record.toList().joinTo(documents, separator = ",", prefix = "{", postfix = "}") { "\"${it.first}\": \"${it.second}\"" } @@ -41,10 +43,8 @@ fun FileMetadata.buildInsertCommand(filename: String, record: Record): String { println(result) return result - } - /* Expected command format: @@ -74,11 +74,10 @@ Expected command format: */ -fun FileMetadata.buildIndexCommand(): String{ - +fun FileMetadata.buildIndexCommand(): String { val keys = StringBuilder() - this.fileKeys.joinTo(keys, separator=",", prefix="{", postfix="}") { + this.fileKeys.joinTo(keys, separator = ",", prefix = "{", postfix = "}") { "\"${it}\": 1" } @@ -87,7 +86,7 @@ fun FileMetadata.buildIndexCommand(): String{ createIndexes: "${this.tableName.toUpperCase()}", indexes: [ { - key: ${keys}, + key: $keys, name: "${this.tableName.toUpperCase()}_index", unique: false } @@ -98,6 +97,4 @@ fun FileMetadata.buildIndexCommand(): String{ println(result) return result - } - diff --git a/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLDBFileTest.kt b/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLDBFileTest.kt index 09a152d..7b42d4a 100644 --- a/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLDBFileTest.kt +++ b/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLDBFileTest.kt @@ -27,11 +27,10 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse class NoSQLDBFileTest { - private val tableName = TSTAB_TABLE_NAME companion object { - private lateinit var dbManager : NoSQLDBMManager + private lateinit var dbManager: NoSQLDBMManager @BeforeClass @JvmStatic @@ -84,8 +83,5 @@ class NoSQLDBFileTest { @After fun destroyEnv() { - } - } - diff --git a/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLMunicipalityTest.kt b/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLMunicipalityTest.kt index 4275e2d..a5fd3db 100644 --- a/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLMunicipalityTest.kt +++ b/nosql/src/test/kotlin/com/smeup/dbnative/nosql/NoSQLMunicipalityTest.kt @@ -29,7 +29,6 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class NoSQLMunicipalityTest { - private lateinit var dbManager: NoSQLDBMManager @Before @@ -74,11 +73,11 @@ class NoSQLMunicipalityTest { assertTrue(dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ERBUSCO"))) assertEquals( "EDOLO", - getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record) + getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record), ) assertEquals( "DESENZANO DEL GARDA", - getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record) + getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record), ) dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } @@ -92,7 +91,6 @@ class NoSQLMunicipalityTest { dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } - @Test fun t04_findLastOfBergamoWithSetll4AndReadPE2() { val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME) @@ -402,20 +400,16 @@ class NoSQLMunicipalityTest { @After fun destroyEnv() { - } - private fun buildMunicipalityKey(vararg values: String): List { val keyValues = mutableListOf() val keys = arrayOf("£NAZ", "§REG", "PROV", "CITTA") for ((index, value) in values.withIndex()) { - if (keys.size> index) { + if (keys.size > index) { keyValues.add(value) } } return keyValues } - } - diff --git a/nosql/src/test/kotlin/com/smeup/dbnative/nosql/utils/NoSQLDBTestUtils.kt b/nosql/src/test/kotlin/com/smeup/dbnative/nosql/utils/NoSQLDBTestUtils.kt index a307638..b121e6f 100644 --- a/nosql/src/test/kotlin/com/smeup/dbnative/nosql/utils/NoSQLDBTestUtils.kt +++ b/nosql/src/test/kotlin/com/smeup/dbnative/nosql/utils/NoSQLDBTestUtils.kt @@ -18,7 +18,6 @@ package com.smeup.dbnative.nosql.utils import com.github.doyaaaaaken.kotlincsv.dsl.csvReader -import com.mongodb.BasicDBObject import com.smeup.dbnative.ConnectionConfig import com.smeup.dbnative.file.DBFile import com.smeup.dbnative.file.Record @@ -30,7 +29,6 @@ import com.smeup.dbnative.nosql.NoSQLDBMManager import com.smeup.dbnative.utils.TypedField import com.smeup.dbnative.utils.TypedMetadata import com.smeup.dbnative.utils.fieldByType -import com.smeup.dbnative.utils.getFieldTypeInstance import org.bson.Document import org.junit.Assert import java.io.File @@ -42,19 +40,23 @@ private val LOGGING_LEVEL = LoggingLevel.OFF fun dbManagerForTest(): NoSQLDBMManager { testLog("Creating NOSQLDBManager with db type MONGO") - return NoSQLDBMManager(ConnectionConfig("*", "mongodb://localhost:27017/W_TEST", "", "")).apply { logger = Logger.getSimpleInstance(LOGGING_LEVEL) } + return NoSQLDBMManager(ConnectionConfig("*", "mongodb://localhost:27017/W_TEST", "", "")).apply { + logger = Logger.getSimpleInstance(LOGGING_LEVEL) + } } fun createAndPopulateTestTable(dbManager: NoSQLDBMManager) { // Create file - val fields = listOf( - "TSTFLDCHR" fieldByType CharacterType(3), - "TSTFLDNBR" fieldByType DecimalType(5, 2) - ) - - val keys = listOf( - "TSTFLDCHR" - ) + val fields = + listOf( + "TSTFLDCHR" fieldByType CharacterType(3), + "TSTFLDNBR" fieldByType DecimalType(5, 2), + ) + + val keys = + listOf( + "TSTFLDCHR", + ) val tMetadata = TypedMetadata(TSTAB_TABLE_NAME, "TSTREC", fields, keys) createFile(tMetadata, dbManager) @@ -67,33 +69,32 @@ fun createAndPopulateTestTable(dbManager: NoSQLDBMManager) { } fun createAndPopulateMunicipalityTable(dbManager: NoSQLDBMManager) { - if (!dbManager.existFile(MUNICIPALITY_TABLE_NAME)) { - - val fields = listOf( - "£NAZ" fieldByType CharacterType(2), - "§REG" fieldByType CharacterType(3), - "PROV" fieldByType CharacterType(2), - "CITTA" fieldByType VarcharType(35), - "CAP" fieldByType CharacterType(5), - "PREF" fieldByType CharacterType(4), - "COMUNE" fieldByType CharacterType(4), - "ISTAT" fieldByType CharacterType(6) - ) - - val keys = listOf( - "£NAZ", - "§REG", - "PROV", - "CITTA" - ) + val fields = + listOf( + "£NAZ" fieldByType CharacterType(2), + "§REG" fieldByType CharacterType(3), + "PROV" fieldByType CharacterType(2), + "CITTA" fieldByType VarcharType(35), + "CAP" fieldByType CharacterType(5), + "PREF" fieldByType CharacterType(4), + "COMUNE" fieldByType CharacterType(4), + "ISTAT" fieldByType CharacterType(6), + ) + + val keys = + listOf( + "£NAZ", + "§REG", + "PROV", + "CITTA", + ) createAndPopulateTable( - dbManager, MUNICIPALITY_TABLE_NAME, fields, keys, - "src/test/resources/csv/Municipality.csv" + "src/test/resources/csv/Municipality.csv", ) } } @@ -109,13 +110,17 @@ fun testLog(message: String) { } } -private fun createAndPopulateTable(dbManager: NoSQLDBMManager, tableName: String, fields: List, keys:List, dataFilePath: String) { - +private fun createAndPopulateTable( + dbManager: NoSQLDBMManager, + tableName: String, + fields: List, + keys: List, + dataFilePath: String, +) { val tMetadata = TypedMetadata(tableName, tableName, fields, keys) - //if not exist file on mongodb create and populate with data + // if not exist file on mongodb create and populate with data if (dbManager.existFile(tableName) == false) { - createFile(tMetadata, dbManager) Assert.assertTrue(dbManager.existFile(tableName)) var dbFile = dbManager.openFile(tableName) @@ -135,18 +140,18 @@ private fun createAndPopulateTable(dbManager: NoSQLDBMManager, tableName: String } dbManager.closeFile(tableName) - } -fun createFile(tMetadata: TypedMetadata, dbManager: NoSQLDBMManager) { +fun createFile( + tMetadata: TypedMetadata, + dbManager: NoSQLDBMManager, +) { // Find table registration in library metadata file - val metadata: FileMetadata = tMetadata.fileMetadata(); + val metadata: FileMetadata = tMetadata.fileMetadata() if (dbManager.existFile(metadata.name) == false) { - // Create file index dbManager.mongoDatabase.runCommand(Document.parse(metadata.buildIndexCommand())) } dbManager.registerMetadata(metadata, true) - } diff --git a/sql/src/main/kotlin/com/smeup/dbnative/sql/JDBCUtils.kt b/sql/src/main/kotlin/com/smeup/dbnative/sql/JDBCUtils.kt index 72c7821..9b256d9 100644 --- a/sql/src/main/kotlin/com/smeup/dbnative/sql/JDBCUtils.kt +++ b/sql/src/main/kotlin/com/smeup/dbnative/sql/JDBCUtils.kt @@ -40,7 +40,8 @@ fun ResultSet.joinToString(separator: String = " - "): String { fun PreparedStatement.bind(values: List) { values.forEachIndexed { - i, value -> this.setObject(i + 1, value) + i, value -> + this.setObject(i + 1, value) } } @@ -55,7 +56,8 @@ fun Connection.recordFormatName(tableName: String): String? = return@use tableName } -private fun ResultSet.indexId() = "${this.getString("TABLE_CAT")}." + +private fun ResultSet.indexId() = + "${this.getString("TABLE_CAT")}." + "${this.getString("TABLE_SCHEM")}.${this.getString("INDEX_NAME")}" private fun ResultSet.isUnique() = this.getInt("NON_UNIQUE") == 0 @@ -67,10 +69,10 @@ fun Connection.primaryKeys(tableName: String): List { result.add(it.getString("COLUMN_NAME")) } } - //if primary key is not defined i will get it by first unique index + // if primary key is not defined i will get it by first unique index if (result.isEmpty()) { var indexId: String? = null - //a row for every field in the indexes + // a row for every field in the indexes this.metaData.getIndexInfo(null, null, tableName, true, false).use { while (it.next()) { if (indexId == null) { @@ -78,8 +80,7 @@ fun Connection.primaryKeys(tableName: String): List { } if (it.indexId() != indexId) { break - } - else { + } else { result.add(it.getString("COLUMN_NAME")) } } @@ -98,7 +99,7 @@ fun Connection.orderingFields(tableName: String): List { if (it.next()) { // TODO handle DESC and ASC keywords val fields = it.getString(field).toUpperCase().substringAfter("ORDER BY").split(",") - result.addAll(fields.map { fl: String -> fl.substring(fl.lastIndexOf('.') + 1).trim('`', ' ') }) + result.addAll(fields.map { fl: String -> fl.substring(fl.lastIndexOf('.') + 1).trim('`', ' ') }) } } } @@ -110,7 +111,8 @@ fun ResultSet?.closeIfOpen() { if (this != null) { try { this.close() - } catch (t: Throwable) {} + } catch (t: Throwable) { + } } } @@ -136,6 +138,3 @@ fun ResultSet?.currentRecordToValues(): Record { } return result } - - - diff --git a/sql/src/main/kotlin/com/smeup/dbnative/sql/Native2SQLAdapter.kt b/sql/src/main/kotlin/com/smeup/dbnative/sql/Native2SQLAdapter.kt index 1254fe5..5ab8b7c 100644 --- a/sql/src/main/kotlin/com/smeup/dbnative/sql/Native2SQLAdapter.kt +++ b/sql/src/main/kotlin/com/smeup/dbnative/sql/Native2SQLAdapter.kt @@ -1,33 +1,39 @@ package com.smeup.dbnative.sql import com.smeup.dbnative.file.Record -import com.smeup.dbnative.model.FileMetadata import com.smeup.dbnative.model.Field +import com.smeup.dbnative.model.FileMetadata import java.lang.Exception enum class PositioningMethod { - SETLL, SETGT; + SETLL, + SETGT, } -enum class ReadMethod(val forward: Boolean){ - READE(true), READPE(false), READP(false), READ(true), CHAIN(true); +enum class ReadMethod(val forward: Boolean) { + READE(true), + READPE(false), + READP(false), + READ(true), + CHAIN(true), } -enum class SortOrder(val symbol: String){ - ASCEDING("ASC"), DESCENDING("DESC"); +enum class SortOrder(val symbol: String) { + ASCEDING("ASC"), + DESCENDING("DESC"), } -class PositioningInstruction(val method: PositioningMethod, val keys: List){ +class PositioningInstruction(val method: PositioningMethod, val keys: List) { init { - require(keys.isNotEmpty()){ + require(keys.isNotEmpty()) { "No keys specified for positioning instruction $method" } } } -class ReadInstruction(var method: ReadMethod, var keys: List){ - constructor(method: ReadMethod): this(method, emptyList()){ - require(admitEmptyKeys()){ +class ReadInstruction(var method: ReadMethod, var keys: List) { + constructor(method: ReadMethod) : this(method, emptyList()) { + require(admitEmptyKeys()) { "Keys are mandatory for read instruction $method " } } @@ -39,40 +45,43 @@ class Native2SQL(val fileMetadata: FileMetadata) { private var lastPositioningInstruction: PositioningInstruction? = null private var lastReadInstruction: ReadInstruction? = null - private fun checkPositioning(){ - requireNotNull(lastPositioningInstruction){ + private fun checkPositioning() { + requireNotNull(lastPositioningInstruction) { "No positioning instruction found" } } - private fun checkKeys(keys: List){ - require(fileMetadata.fileKeys.size > 0){ + private fun checkKeys(keys: List) { + require(fileMetadata.fileKeys.size > 0) { "No keys specified in metadata" } - require(keys.size <= fileMetadata.fileKeys.size){ + require(keys.size <= fileMetadata.fileKeys.size) { "Number of metadata keys $fileMetadata.fileKeys less than number of positioning/read keys $keys" } } - private fun checkRead(){ - requireNotNull(lastReadInstruction){ + private fun checkRead() { + requireNotNull(lastReadInstruction) { "No read instruction found" } } - private fun checkReadKeys(){ + private fun checkReadKeys() { checkRead() - require(!lastReadInstruction!!.keys.isNullOrEmpty()){ + require(!lastReadInstruction!!.keys.isNullOrEmpty()) { "No keys specified for read instruction ${lastReadInstruction!!.method}" } } - private fun checkInstructions(){ + private fun checkInstructions() { checkPositioning() checkRead() } - fun setPositioning(method: PositioningMethod, keys: List){ + fun setPositioning( + method: PositioningMethod, + keys: List, + ) { checkKeys(keys) lastPositioningInstruction = PositioningInstruction(method, keys) lastReadInstruction = null @@ -81,14 +90,17 @@ class Native2SQL(val fileMetadata: FileMetadata) { /* * @return true if read method need new query execution */ - fun setRead(method: ReadMethod, keys: List? = null): Boolean{ + fun setRead( + method: ReadMethod, + keys: List? = null, + ): Boolean { checkKeys(keys ?: emptyList()) var executeQuery = false - when(method){ - ReadMethod.READPE, ReadMethod.READE ->{ - val coherent = keys?.let{isCoherent(keys)} ?: true - //Test to remove on fully supported operations - require(coherent){ + when (method) { + ReadMethod.READPE, ReadMethod.READE -> { + val coherent = keys?.let { isCoherent(keys) } ?: true + // Test to remove on fully supported operations + require(coherent) { "Uncoherent read not yet managed" } } @@ -101,90 +113,105 @@ class Native2SQL(val fileMetadata: FileMetadata) { require(lastReadInstruction == null || method == ReadMethod.CHAIN || method == lastReadInstruction!!.method) { "read operation " + method + " is allowed immediatly after positioning or after a same method read instruction" } - if(lastReadInstruction == null){ + if (lastReadInstruction == null) { executeQuery = true } - lastReadInstruction = ReadInstruction(method, keys ?:emptyList()) + lastReadInstruction = ReadInstruction(method, keys ?: emptyList()) return executeQuery } - fun clear(){ + fun clear() { lastReadInstruction = null lastPositioningInstruction = null } - fun getLastKeys() = lastReadInstruction?.keys ?:lastPositioningInstruction?.keys ?: throw Exception("Keys not yet set") + fun getLastKeys() = lastReadInstruction?.keys ?: lastPositioningInstruction?.keys ?: throw Exception("Keys not yet set") - fun isLastOperationSet()=lastReadInstruction == null + fun isLastOperationSet() = lastReadInstruction == null fun lastReadMatchRecord(record: Record): Boolean { - if(lastReadInstruction!!.keys.isEmpty()){ + if (lastReadInstruction!!.keys.isEmpty()) { return true } lastReadInstruction!!.keys.mapIndexed { index, value -> val keyname = fileMetadata.fileKeys.get(index) - if(record[keyname]?.trim() != value.trim()){ + if (record[keyname]?.trim() != value.trim()) { return false } } return true } - - fun isCoherent(newKeys: List): Boolean{ + fun isCoherent(newKeys: List): Boolean { checkPositioning() return newKeys.isEmpty() || if (newKeys.size <= lastPositioningInstruction!!.keys.size && - newKeys.size <= lastReadInstruction?.keys?.size?:newKeys.size) { - newKeys.forEachIndexed() { index, value -> - if(lastPositioningInstruction!!.keys.get(index) != value){ - return false + newKeys.size <= lastReadInstruction?.keys?.size ?: newKeys.size + ) { + newKeys.forEachIndexed { index, value -> + if (lastPositioningInstruction!!.keys.get(index) != value) { + return false + } } + return true + } else { + false } - return true - } - else false } - private fun getSortOrder(): SortOrder { checkInstructions() - return if(lastReadInstruction!!.method.forward) SortOrder.ASCEDING else SortOrder.DESCENDING + return if (lastReadInstruction!!.method.forward) SortOrder.ASCEDING else SortOrder.DESCENDING } - private fun getComparison(): Pair{ + private fun getComparison(): Pair { checkInstructions() - return if(lastReadInstruction!!.method.forward){ - when(lastPositioningInstruction!!.method){ - PositioningMethod.SETLL -> return Pair(Comparison.GE, Comparison.GT) - PositioningMethod.SETGT -> return Pair(Comparison.GT, Comparison.GT) + return if (lastReadInstruction!!.method.forward) { + when (lastPositioningInstruction!!.method) { + PositioningMethod.SETLL -> return Pair(Comparison.GE, Comparison.GT) + PositioningMethod.SETGT -> return Pair(Comparison.GT, Comparison.GT) } - } - else{ - when(lastPositioningInstruction!!.method){ + } else { + when (lastPositioningInstruction!!.method) { PositioningMethod.SETLL -> return Pair(Comparison.LT, Comparison.LT) PositioningMethod.SETGT -> return Pair(Comparison.LE, Comparison.LT) } } } - private fun getSQLOrderByClause(): String{ + private fun getSQLOrderByClause(): String { val sortOrder = getSortOrder() - return " ORDER BY " + fileMetadata.fileKeys.joinToString(separator = " " + sortOrder.symbol + ", ", postfix = " " + sortOrder.symbol ) + return " ORDER BY " + fileMetadata.fileKeys.joinToString(separator = " " + sortOrder.symbol + ", ", postfix = " " + sortOrder.symbol) } - fun getReadSqlStatement(): Pair>{ + fun getReadSqlStatement(): Pair> { checkPositioning() - return Pair(getSQL(fileMetadata.fields, fileMetadata.fileKeys.subList(0, lastPositioningInstruction!!.keys.size), Comparison.EQ, fileMetadata.tableName), lastPositioningInstruction!!.keys) + return Pair( + getSQL( + fileMetadata.fields, + fileMetadata.fileKeys.subList(0, lastPositioningInstruction!!.keys.size), + Comparison.EQ, + fileMetadata.tableName, + ), + lastPositioningInstruction!!.keys, + ) } - fun getSQLSatement(): Pair>{ - when(lastReadInstruction!!.method){ - ReadMethod.CHAIN ->{ + fun getSQLSatement(): Pair> { + when (lastReadInstruction!!.method) { + ReadMethod.CHAIN -> { checkReadKeys() - return Pair(getSQL(fileMetadata.fields, fileMetadata.fileKeys.subList(0, lastReadInstruction!!.keys.size), Comparison.EQ, fileMetadata.tableName), lastReadInstruction!!.keys) + return Pair( + getSQL( + fileMetadata.fields, + fileMetadata.fileKeys.subList(0, lastReadInstruction!!.keys.size), + Comparison.EQ, + fileMetadata.tableName, + ), + lastReadInstruction!!.keys, + ) } - ReadMethod.READ,ReadMethod.READP -> { + ReadMethod.READ, ReadMethod.READP -> { checkRead() return getCoherentSql(true) } @@ -195,23 +222,28 @@ class Native2SQL(val fileMetadata: FileMetadata) { } } - - private fun getCoherentSql(fullUnion: Boolean = false):Pair>{ + private fun getCoherentSql(fullUnion: Boolean = false): Pair> { checkPositioning() val queries = mutableListOf() val replacements = mutableListOf() val comparison = getComparison() - require(!lastPositioningInstruction!!.keys.isEmpty()){ + require(!lastPositioningInstruction!!.keys.isEmpty()) { "Empty positioning keys" } - if(lastPositioningInstruction!!.keys.size == 1){ + if (lastPositioningInstruction!!.keys.size == 1) { queries.add(getSQL(fileMetadata.fields, fileMetadata.fileKeys.subList(0, 1), comparison.first, fileMetadata.tableName)) replacements.addAll(lastPositioningInstruction!!.keys) - } - else { - val limit = if(fullUnion)1 else 2 + } else { + val limit = if (fullUnion)1 else 2 for (i in lastPositioningInstruction!!.keys.size downTo limit) { - queries.add(getSQL(fileMetadata.fields, fileMetadata.fileKeys.subList(0, i), if(i == lastPositioningInstruction!!.keys.size) comparison.first else comparison.second, fileMetadata.tableName)) + queries.add( + getSQL( + fileMetadata.fields, + fileMetadata.fileKeys.subList(0, i), + if (i == lastPositioningInstruction!!.keys.size) comparison.first else comparison.second, + fileMetadata.tableName, + ), + ) replacements.addAll(lastPositioningInstruction!!.keys.subList(0, i)) } } @@ -219,7 +251,12 @@ class Native2SQL(val fileMetadata: FileMetadata) { } } -private fun getSQL(fields: List, keys: List, comparison: Comparison, tableName: String): String{ +private fun getSQL( + fields: List, + keys: List, + comparison: Comparison, + tableName: String, +): String { var columns = "" fields.forEachIndexed { index, k -> run { @@ -230,22 +267,23 @@ private fun getSQL(fields: List, keys: List, comparison: Comparis var value = "" keys.forEachIndexed { index, k -> run { - value += "\"" + k + "\" " + (if (index < keys.size - 1) Comparison.EQ.symbol else comparison.symbol) + " ? AND " + value += "\"" + k + "\" " + (if (index < keys.size - 1) Comparison.EQ.symbol else comparison.symbol) + " ? AND " } } - return "(SELECT " + columns.removeSuffix(", ")+ " FROM $tableName WHERE " + value.removeSuffix("AND ") + ")" + return "(SELECT " + columns.removeSuffix(", ") + " FROM $tableName WHERE " + value.removeSuffix("AND ") + ")" } -fun main(){ +fun main() { var fields = listOf(Field("Regione", ""), Field("Provincia", ""), Field("Comune", "")) var fieldsKeys = listOf("Regione", "Provincia", "Comune") var metadata = FileMetadata("test", "rld_comuni", fields, fieldsKeys) - val adapter = Native2SQL(metadata).apply{ - setPositioning(PositioningMethod.SETLL, listOf("Lombardia", "Brescia", "Erbusco")) - println(isCoherent(listOf("Lombardia", "Brescia", "Erbusco"))) - setRead(ReadMethod.READE, listOf("Lombardia", "Brescia", "Erbusco")) - } - adapter.getSQLSatement().let{ + val adapter = + Native2SQL(metadata).apply { + setPositioning(PositioningMethod.SETLL, listOf("Lombardia", "Brescia", "Erbusco")) + println(isCoherent(listOf("Lombardia", "Brescia", "Erbusco"))) + setRead(ReadMethod.READE, listOf("Lombardia", "Brescia", "Erbusco")) + } + adapter.getSQLSatement().let { println(it.first) println(it.second) } diff --git a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFile.kt b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFile.kt index 45d48ae..903797e 100644 --- a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFile.kt +++ b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFile.kt @@ -17,7 +17,6 @@ package com.smeup.dbnative.sql - import com.smeup.dbnative.file.DBFile import com.smeup.dbnative.file.Record import com.smeup.dbnative.file.Result @@ -30,14 +29,17 @@ import java.sql.PreparedStatement import java.sql.ResultSet import kotlin.system.measureTimeMillis -class SQLDBFile(override var name: String, override var fileMetadata: FileMetadata, - var connection: Connection, - override var logger: Logger? = null) : DBFile { - +class SQLDBFile( + override var name: String, + override var fileMetadata: FileMetadata, + var connection: Connection, + override var logger: Logger? = null, +) : DBFile { constructor( name: String, fileMetadata: FileMetadata, - connection: Connection): this(name, fileMetadata, connection, null) + connection: Connection, + ) : this(name, fileMetadata, connection, null) private var preparedStatements: MutableMap = mutableMapOf() private var resultSet: ResultSet? = null @@ -45,8 +47,8 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada private var lastNativeMethod: NativeMethod? = null - //Search from: metadata, primary key, unique index, view ordering fields - //private val thisFileKeys: List by lazy { + // Search from: metadata, primary key, unique index, view ordering fields + // private val thisFileKeys: List by lazy { // // TODO: think about a right way (local file maybe?) to retrieve keylist // var indexes = this.fileMetadata.fileKeys // if(indexes.isEmpty()){ @@ -54,14 +56,16 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada // } // } // if (indexes.isEmpty()) connection.orderingFields(fileMetadata.name) else indexes - //} + // } private var adapter: Native2SQL = Native2SQL(this.fileMetadata) - private var eof:Boolean = false - - private fun logEvent(loggingKey: LoggingKey, message: String, elapsedTime: Long? = null) = - logger?.logEvent(loggingKey, message, elapsedTime, lastNativeMethod, fileMetadata.name) + private var eof: Boolean = false + private fun logEvent( + loggingKey: LoggingKey, + message: String, + elapsedTime: Long? = null, + ) = logger?.logEvent(loggingKey, message, elapsedTime, lastNativeMethod, fileMetadata.name) override fun setll(key: String): Boolean { return setll(mutableListOf(key)) @@ -85,7 +89,6 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada adapter.setPositioning(PositioningMethod.SETGT, keys) return true - } override fun chain(key: String): Result { @@ -98,7 +101,6 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada adapter.setRead(ReadMethod.CHAIN, keys) val read: Result measureTimeMillis { - executeQuery(adapter.getSQLSatement()) read = readNextFromResultSet() }.apply { @@ -113,7 +115,7 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada logEvent(LoggingKey.native_access_method, "Executing read") val read: Result measureTimeMillis { - if (adapter.setRead(ReadMethod.READ) ) { + if (adapter.setRead(ReadMethod.READ)) { executeQuery(adapter.getSQLSatement()) } read = readNextFromResultSet() @@ -153,7 +155,6 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada logEvent(LoggingKey.native_access_method, "Executing readEqual on keys $keys") val read: Result measureTimeMillis { - if (adapter.setRead(ReadMethod.READE, keys)) { executeQuery(adapter.getSQLSatement()) } @@ -165,7 +166,6 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada return read } - override fun readPreviousEqual(): Result { return readPreviousEqual(adapter.getLastKeys()) } @@ -179,7 +179,6 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada logEvent(LoggingKey.native_access_method, "Executing readPreviousEqual on keys $keys") val read: Result measureTimeMillis { - if (adapter.setRead(ReadMethod.READPE, keys)) { executeQuery(adapter.getSQLSatement()) } @@ -210,19 +209,22 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada override fun update(record: Record): Result { lastNativeMethod = NativeMethod.update - logEvent(LoggingKey.native_access_method, "Executing update record $actualRecord to $record with autocommit=${connection.autoCommit}") + logEvent( + LoggingKey.native_access_method, + "Executing update record $actualRecord to $record with autocommit=${connection.autoCommit}", + ) measureTimeMillis { // record before update is "actualRecord" // record post update will be "record" var atLeastOneFieldChanged = false actualRecord?.forEach { val fieldValue = record.getValue(it.key) - if(fieldValue != it.value){ + if (fieldValue != it.value) { atLeastOneFieldChanged = true this.getResultSet()?.updateObject(it.key, fieldValue) } - }?:logEvent(LoggingKey.native_access_method, "No previous read executed, nothing to update") - if(atLeastOneFieldChanged){ + } ?: logEvent(LoggingKey.native_access_method, "No previous read executed, nothing to update") + if (atLeastOneFieldChanged) { this.getResultSet()?.updateRow() } }.apply { @@ -234,12 +236,14 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada override fun delete(record: Record): Result { lastNativeMethod = NativeMethod.delete - logEvent(LoggingKey.native_access_method, "Executing delete for current record $actualRecord with autocommit=${connection.autoCommit}") + logEvent( + LoggingKey.native_access_method, + "Executing delete for current record $actualRecord with autocommit=${connection.autoCommit}", + ) measureTimeMillis { - if(actualRecord != null) { + if (actualRecord != null) { this.getResultSet()?.deleteRow() - } - else{ + } else { logEvent(LoggingKey.native_access_method, "No previous read executed, nothing to delete") } }.apply { @@ -253,14 +257,17 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada executeQuery(sqlAndValues.first, sqlAndValues.second) } - private fun executeQuery(sql: String, values: List) { + private fun executeQuery( + sql: String, + values: List, + ) { eof = false resultSet.closeIfOpen() logEvent(LoggingKey.execute_inquiry, "Preparing statement for query: $sql with bingings: $values") val stm: PreparedStatement measureTimeMillis { - stm = preparedStatements.get(sql)?:connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE) - preparedStatements.putIfAbsent(sql, stm); + stm = preparedStatements.get(sql) ?: connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE) + preparedStatements.putIfAbsent(sql, stm) stm.bind(values) }.apply { logEvent(LoggingKey.execute_inquiry, "Statement prepared, executing query for statement", this) @@ -272,7 +279,6 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada } } - private fun readNextFromResultSet(): Result { val result = Result(resultSet.toValues()) if (!eof() && adapter.lastReadMatchRecord(result.record)) { @@ -288,14 +294,13 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada } } - private fun closeResultSet(){ + private fun closeResultSet() { resultSet.closeIfOpen() resultSet = null } override fun eof() = eof - override fun equal(): Boolean { logEvent(LoggingKey.read_data, "Read current record for equal") lastNativeMethod = NativeMethod.equal @@ -303,7 +308,6 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada if (!adapter.isLastOperationSet()) { result = false } else { - measureTimeMillis { executeQuery(adapter.getReadSqlStatement()) result = resultSet?.next() ?: false @@ -323,4 +327,4 @@ class SQLDBFile(override var name: String, override var fileMetadata: FileMetada resultSet.closeIfOpen() preparedStatements.values.forEach { it.close() } } -} \ No newline at end of file +} diff --git a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFileNoPerf.kt b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFileNoPerf.kt index 5618b85..5491987 100644 --- a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFileNoPerf.kt +++ b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBFileNoPerf.kt @@ -17,7 +17,6 @@ package com.smeup.dbnative.sql - import com.smeup.dbnative.file.DBFile import com.smeup.dbnative.file.Record import com.smeup.dbnative.file.RecordField @@ -31,15 +30,17 @@ import java.sql.PreparedStatement import java.sql.ResultSet import kotlin.system.measureTimeMillis -class SQLDBFileNoPerf(override var name: String, - override var fileMetadata: FileMetadata, - var connection: Connection, - override var logger: Logger? = null) : DBFile { - +class SQLDBFileNoPerf( + override var name: String, + override var fileMetadata: FileMetadata, + var connection: Connection, + override var logger: Logger? = null, +) : DBFile { constructor( name: String, fileMetadata: FileMetadata, - connection: Connection): this(name, fileMetadata, connection, null) + connection: Connection, + ) : this(name, fileMetadata, connection, null) private var resultSet: ResultSet? = null private var movingForward = true @@ -48,21 +49,22 @@ class SQLDBFileNoPerf(override var name: String, private var actualRecordToPop: Record? = null private var eof: Boolean = false private var lastOperationSet: Boolean = false - private var lastNativeMethod: NativeMethod? = null; - - + private var lastNativeMethod: NativeMethod? = null private val thisFileKeys: List by lazy { // TODO: think about a right way (local file maybe?) to retrieve keylist var indexes = this.fileMetadata.fileKeys - if(indexes.isEmpty()){ + if (indexes.isEmpty()) { indexes = connection.primaryKeys(fileMetadata.name) } if (indexes.isEmpty()) connection.orderingFields(fileMetadata.name) else indexes } - private fun logEvent(loggingKey: LoggingKey, message: String, elapsedTime: Long? = null) = - logger?.logEvent(loggingKey, message, elapsedTime, lastNativeMethod, fileMetadata.tableName) + private fun logEvent( + loggingKey: LoggingKey, + message: String, + elapsedTime: Long? = null, + ) = logger?.logEvent(loggingKey, message, elapsedTime, lastNativeMethod, fileMetadata.tableName) override fun setll(key: String): Boolean { return setll(mutableListOf(key)) @@ -75,14 +77,15 @@ class SQLDBFileNoPerf(override var name: String, measureTimeMillis { lastOperationSet = true - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = thisFileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = thisFileKeys.get(index) + RecordField(keyname, value) + } checkAndStoreLastKeys(keyAsRecordField) movingForward = true - point = point(keyAsRecordField); + point = point(keyAsRecordField) }.apply { logEvent(LoggingKey.native_access_method, "setll executed", this) } @@ -91,7 +94,6 @@ class SQLDBFileNoPerf(override var name: String, } override fun setgt(key: String): Boolean { - return setgt(mutableListOf(key)) } @@ -102,10 +104,11 @@ class SQLDBFileNoPerf(override var name: String, measureTimeMillis { lastOperationSet = true - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = thisFileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = thisFileKeys.get(index) + RecordField(keyname, value) + } checkAndStoreLastKeys(keyAsRecordField) movingForward = false @@ -128,10 +131,11 @@ class SQLDBFileNoPerf(override var name: String, measureTimeMillis { lastOperationSet = false - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = thisFileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = thisFileKeys.get(index) + RecordField(keyname, value) + } checkAndStoreLastKeys(keyAsRecordField) movingForward = true @@ -151,7 +155,6 @@ class SQLDBFileNoPerf(override var name: String, measureTimeMillis { lastOperationSet = false - if (resultSet == null) { pointAtUpperLL() } @@ -190,15 +193,14 @@ class SQLDBFileNoPerf(override var name: String, } override fun readEqual(): Result { - val lastKeysAsList = lastKeys.map { - it.value - } + val lastKeysAsList = + lastKeys.map { + it.value + } return readEqual(lastKeysAsList) } override fun readEqual(key: String): Result { - - return readEqual(mutableListOf(key)) } @@ -209,10 +211,11 @@ class SQLDBFileNoPerf(override var name: String, measureTimeMillis { lastOperationSet = false - var keysAsRecordField = keys.mapIndexed { index, value -> - val keyname = thisFileKeys.get(index) - RecordField(keyname, value) - } + var keysAsRecordField = + keys.mapIndexed { index, value -> + val keyname = thisFileKeys.get(index) + RecordField(keyname, value) + } checkAndStoreLastKeys(keysAsRecordField) if (resultSet == null) { @@ -231,10 +234,10 @@ class SQLDBFileNoPerf(override var name: String, } override fun readPreviousEqual(): Result { - - val lastKeysAsList = lastKeys.map { - it.value - } + val lastKeysAsList = + lastKeys.map { + it.value + } return readPreviousEqual(lastKeysAsList) } @@ -245,15 +248,16 @@ class SQLDBFileNoPerf(override var name: String, override fun readPreviousEqual(keys: List): Result { lastNativeMethod = NativeMethod.readPreviousEqual - logEvent(LoggingKey.native_access_method, "Executing readPreviousEqual on keys ${keys}") + logEvent(LoggingKey.native_access_method, "Executing readPreviousEqual on keys $keys") val read: Result measureTimeMillis { lastOperationSet = false - var keyAsRecordField = keys.mapIndexed { index, value -> - val keyname = thisFileKeys.get(index) - RecordField(keyname, value) - } + var keyAsRecordField = + keys.mapIndexed { index, value -> + val keyname = thisFileKeys.get(index) + RecordField(keyname, value) + } checkAndStoreLastKeys(keyAsRecordField) if (resultSet == null) { @@ -276,7 +280,7 @@ class SQLDBFileNoPerf(override var name: String, override fun write(record: Record): Result { lastNativeMethod = NativeMethod.write - logEvent(LoggingKey.native_access_method, "Executing write for record ${record}") + logEvent(LoggingKey.native_access_method, "Executing write for record $record") lastOperationSet = false measureTimeMillis { // TODO: manage errors @@ -294,7 +298,7 @@ class SQLDBFileNoPerf(override var name: String, override fun update(record: Record): Result { lastNativeMethod = NativeMethod.update - logEvent(LoggingKey.native_access_method, "Executing update for record ${record}") + logEvent(LoggingKey.native_access_method, "Executing update for record $record") lastOperationSet = false measureTimeMillis { // record before update is "actualRecord" @@ -302,12 +306,12 @@ class SQLDBFileNoPerf(override var name: String, var atLeastOneFieldChanged = false actualRecord?.forEach { var fieldValue = record.getValue(it.key) - if(fieldValue != it.value){ + if (fieldValue != it.value) { atLeastOneFieldChanged = true this.getResultSet()?.updateObject(it.key, fieldValue) } } - if(atLeastOneFieldChanged){ + if (atLeastOneFieldChanged) { this.getResultSet()?.updateRow() } }.apply { @@ -319,7 +323,7 @@ class SQLDBFileNoPerf(override var name: String, override fun delete(record: Record): Result { lastNativeMethod = NativeMethod.delete - logEvent(LoggingKey.native_access_method, "Executing delete for record ${record}") + logEvent(LoggingKey.native_access_method, "Executing delete for record $record") lastOperationSet = false measureTimeMillis { this.getResultSet()?.deleteRow() @@ -330,8 +334,10 @@ class SQLDBFileNoPerf(override var name: String, return Result(record) } - - private fun executeQuery(sql: String, values: List) { + private fun executeQuery( + sql: String, + values: List, + ) { resultSet.closeIfOpen() logEvent(LoggingKey.execute_inquiry, "Preparing statement for query: $sql with bingings: $values") val stm: PreparedStatement @@ -370,7 +376,10 @@ class SQLDBFileNoPerf(override var name: String, } // calculate the upper or the lower part of the ordered table given the input keys using an sql query (composed of selects in union if primary keys size > 1) - private fun calculateResultSet(keys: List, withEquals: Boolean = true) { + private fun calculateResultSet( + keys: List, + withEquals: Boolean = true, + ) { actualRecordToPop = null val sqlAndValues = filePartSQLAndValues(fileMetadata.tableName, movingForward, thisFileKeys, keys, withEquals) val values = sqlAndValues.first @@ -382,7 +391,10 @@ class SQLDBFileNoPerf(override var name: String, calculateResultSet(calculateRecordKeys(actualRecord, thisFileKeys), false) } - private fun calculateRecordKeys(record: Record?, keysNames: List): List { + private fun calculateRecordKeys( + record: Record?, + keysNames: List, + ): List { val result = mutableListOf() keysNames.forEach { result.add(RecordField(it, record!![it].toString())) @@ -410,7 +422,7 @@ class SQLDBFileNoPerf(override var name: String, } private fun readFromResultSetFilteringBy(keys: List): Result { - logEvent(LoggingKey.search_data, "Searching record for keys: ${keys}") + logEvent(LoggingKey.search_data, "Searching record for keys: $keys") var result: Result var counter = 0 measureTimeMillis { @@ -419,7 +431,11 @@ class SQLDBFileNoPerf(override var name: String, counter++ } while (!result.record.matches(keys) && resultSet.hasRecords() && !eof()) }.apply { - logEvent(LoggingKey.search_data, "Search stops after $counter ResultSet iterations. Is eof: ${eof()}. Current row number is ${resultSet?.row?:" undefined"}}", this) + logEvent( + LoggingKey.search_data, + "Search stops after $counter ResultSet iterations. Is eof: ${eof()}. Current row number is ${resultSet?.row ?: " undefined"}}", + this, + ) } return result } @@ -431,7 +447,6 @@ class SQLDBFileNoPerf(override var name: String, override fun eof(): Boolean = resultSet?.isAfterLast ?: true - override fun equal(): Boolean { lastNativeMethod = NativeMethod.equal if (lastOperationSet == false) { @@ -457,4 +472,4 @@ class SQLDBFileNoPerf(override var name: String, fun getResultSet(): ResultSet? { return this.resultSet } -} \ No newline at end of file +} diff --git a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBMManager.kt b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBMManager.kt index 9ac0a1a..ccbfc0a 100644 --- a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBMManager.kt +++ b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLDBMManager.kt @@ -25,13 +25,12 @@ import java.sql.DriverManager import java.util.* import kotlin.system.measureTimeMillis -open class SQLDBMManager(override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() { - +open class SQLDBMManager(override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() { private var sqlLog: Boolean = false private var openedFile = mutableMapOf() - val connection : Connection by lazy { + val connection: Connection by lazy { logger?.logEvent(LoggingKey.connection, "Opening SQL connection on url ${connectionConfig.url}") val conn: Connection measureTimeMillis { @@ -39,13 +38,13 @@ open class SQLDBMManager(override val connectionConfig: ConnectionConfig) : DBMa Class.forName(connectionConfig.driver) } - val connectionProps = Properties(); - connectionProps.put("user", connectionConfig.user); - connectionProps.put("password", connectionConfig.password); + val connectionProps = Properties() + connectionProps.put("user", connectionConfig.user) + connectionProps.put("password", connectionConfig.password) - connectionConfig.properties.forEach() { - if (!it.key.equals("user") && !it.key.equals("password")) { - connectionProps.put(it.key, it.value); + connectionConfig.properties.forEach { + if (!it.key.equals("user") && !it.key.equals("password")) { + connectionProps.put(it.key, it.value) } } conn = DriverManager.getConnection(connectionConfig.url, connectionProps) @@ -59,18 +58,18 @@ open class SQLDBMManager(override val connectionConfig: ConnectionConfig) : DBMa } override fun close() { - openedFile.values.forEach { it.close()} + openedFile.values.forEach { it.close() } openedFile.clear() connection.close() } - override fun openFile(name: String) = openedFile.getOrPut(name) { - require(existFile(name)) { - "Cannot open a unregistered file $name" + override fun openFile(name: String) = + openedFile.getOrPut(name) { + require(existFile(name)) { + "Cannot open a unregistered file $name" + } + SQLDBFile(name = name, fileMetadata = metadataOf(name), connection = connection, logger) } - SQLDBFile(name = name, fileMetadata = metadataOf(name), connection = connection, logger) - } - override fun closeFile(name: String) { openedFile.remove(name)?.close() @@ -90,5 +89,4 @@ open class SQLDBMManager(override val connectionConfig: ConnectionConfig) : DBMa fun setSQLLog(on: Boolean) { sqlLog = on } - -} \ No newline at end of file +} diff --git a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLUtils.kt b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLUtils.kt index 49f22c5..16217a1 100644 --- a/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLUtils.kt +++ b/sql/src/main/kotlin/com/smeup/dbnative/sql/SQLUtils.kt @@ -20,7 +20,6 @@ package com.smeup.dbnative.sql import com.smeup.dbnative.file.Record import com.smeup.dbnative.file.RecordField - fun String.insertSQL(record: Record): String { val names = record.keys.joinToString { "\"" + it + "\"" } val questionMarks = record.keys.joinToString { "?" } @@ -40,18 +39,24 @@ fun String.deleteSQL(record: Record): String { return "DELETE FROM $this ${whereSQL(wheres, comparations)}" } -fun orderBySQL(keysNames: List, reverse: Boolean = false): String = +fun orderBySQL( + keysNames: List, + reverse: Boolean = false, +): String = if (keysNames.isEmpty()) { "" } else { if (reverse) { - "ORDER BY " + keysNames.joinToString(separator = " DESC, ", postfix = " DESC") {"\"$it\""} + "ORDER BY " + keysNames.joinToString(separator = " DESC, ", postfix = " DESC") { "\"$it\"" } } else { - "ORDER BY " + keysNames.joinToString {"\"$it\""} + "ORDER BY " + keysNames.joinToString { "\"$it\"" } } } -fun whereSQL(wheres: List, comparations: List): String { +fun whereSQL( + wheres: List, + comparations: List, +): String { return if (wheres.isEmpty()) { "" } else { @@ -66,9 +71,8 @@ fun filePartSQLAndValues( movingForward: Boolean, fileKeys: List, keys: List, - withEquals: Boolean + withEquals: Boolean, ): Pair, String> { - val queries = mutableListOf() val values = mutableListOf() @@ -76,12 +80,12 @@ fun filePartSQLAndValues( val keys2Size = keys2.size do { - - var lastComparison = if (movingForward) { - Comparison.GT - } else { - Comparison.LT - } + var lastComparison = + if (movingForward) { + Comparison.GT + } else { + Comparison.LT + } val comparisons = mutableListOf() keys2.forEachIndexed { index, _ -> @@ -101,19 +105,20 @@ fun filePartSQLAndValues( val sql = "(SELECT * FROM $tableName ${whereSQL( keys2.map { it.name }, - comparisons + comparisons, )})" values.addAll(keys2.map { it.value.toString() }) queries.add(sql) keys2 = keys2.subList(0, keys2.size - 1) - } while (keys2.isNotEmpty()) - val sql = queries.joinToString(" UNION ") + " " + orderBySQL( - fileKeys, - reverse = !movingForward - ) + val sql = + queries.joinToString(" UNION ") + " " + + orderBySQL( + fileKeys, + reverse = !movingForward, + ) return Pair(values, sql) } @@ -124,7 +129,7 @@ enum class Comparison(val symbol: String) { GT(">"), GE(">="), LT("<"), - LE("<="); + LE("<="), } fun createMarkerSQL(keysNames: List): String = @@ -133,50 +138,47 @@ fun createMarkerSQL(keysNames: List): String = } else { // in HSQLDB CONCAT needs at least two params! if (keysNames.size == 1) { - "CONCAT( " + keysNames.joinToString{"\"$it\""} + ", '') AS NATIVE_ACCESS_MARKER" + "CONCAT( " + keysNames.joinToString { "\"$it\"" } + ", '') AS NATIVE_ACCESS_MARKER" } else { - "CONCAT( " + keysNames.joinToString{"\"$it\""} + ") AS NATIVE_ACCESS_MARKER" + "CONCAT( " + keysNames.joinToString { "\"$it\"" } + ") AS NATIVE_ACCESS_MARKER" } - } fun calculateMarkerValue( keys: List, movingForward: Boolean = true, - withEquals: Boolean = true + withEquals: Boolean = true, ): String = if (keys.isEmpty()) { "" } else { - val padChar = if (movingForward) { - ' ' - } else { - 'Z' - } + val padChar = + if (movingForward) { + ' ' + } else { + 'Z' + } // NOTE: calculate max length of marker using primary fields max length (temp 100 but incorrect) if (withEquals) { keys.joinToString("") { it.value }.padEnd(100, padChar) } else { keys.joinToString("") { it.value } - } } -fun markerWhereSQL(movingForward: Boolean, withEquals: Boolean): String { - val comparison = if (movingForward && !withEquals) { - Comparison.GT - } else if (movingForward && withEquals) { - Comparison.GE - } else if (!movingForward && !withEquals) { - Comparison.LT - } else { - Comparison.LE - } +fun markerWhereSQL( + movingForward: Boolean, + withEquals: Boolean, +): String { + val comparison = + if (movingForward && !withEquals) { + Comparison.GT + } else if (movingForward && withEquals) { + Comparison.GE + } else if (!movingForward && !withEquals) { + Comparison.LT + } else { + Comparison.LE + } return " WHERE NATIVE_ACCESS_MARKER ${comparison.symbol} ? " } - - - - - - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFile.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFile.kt index 2001976..5c8588c 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFile.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFile.kt @@ -31,11 +31,9 @@ import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertTrue -//@Ignore +// @Ignore class DB2400OperationsOnFile { - companion object { - private var dbManager: SQLDBMManager? = null @BeforeClass @@ -49,7 +47,7 @@ class DB2400OperationsOnFile { } } - //Ignored fields in metadata not necessary + // Ignored fields in metadata not necessary @Ignore @Test fun open() { @@ -203,7 +201,7 @@ class DB2400OperationsOnFile { var chainResult = dbFile.chain(key) assertEquals(0, chainResult.record.size) - //Set field values and write record + // Set field values and write record chainResult.record["A§ARTI"] = key chainResult.record["A§DEAR"] = "Kotlin DBNativeAccess Write " chainResult.record["A§TIAR"] = "ART " @@ -211,17 +209,17 @@ class DB2400OperationsOnFile { dbFile.write(chainResult.record) - //Must exists correct write + // Must exists correct write chainResult = dbFile.chain(key) assertEquals(key, chainResult.record["A§ARTI"]) assertEquals("ART ", chainResult.record["A§TIAR"]) assertEquals("Kotlin DBNativeAccess Write ", chainResult.record["A§DEAR"]) assertEquals("0 ", chainResult.record["A§TPAR"]) - //Delete record + // Delete record dbFile.delete(chainResult.record) - //Check delete success + // Check delete success chainResult = dbFile.chain(key) assertEquals(0, chainResult.record.size) @@ -231,7 +229,7 @@ class DB2400OperationsOnFile { // Ignore, fields in metadata non necessary @Ignore @Test - fun multipleUpdateOnReadE(){ + fun multipleUpdateOnReadE() { // TEST FLOW // Step1: write 100 records with "currentTimeMillis" as unique key // Step2: read above written records and update A§DEA2 field @@ -246,7 +244,7 @@ class DB2400OperationsOnFile { // Create list of items to write into A§ARTI field val items = mutableListOf() - repeat(numberOfRecordsToHandle){ + repeat(numberOfRecordsToHandle) { items.add(System.currentTimeMillis().toString() + " ") Thread.sleep(5) } @@ -257,19 +255,21 @@ class DB2400OperationsOnFile { val dea2Key = "Kotlin DBNativeAccess TEST-UPDATED " // WRITE - repeat(numberOfRecordsToHandle){ + repeat(numberOfRecordsToHandle) { var record = Record() - repeat(fieldsNumber){ index -> + repeat(fieldsNumber) { index -> var name: String = dbFile.fileMetadata.fields[index].name print(tMetadata.getField(name)?.type) - var value = when(name){ - "A§ARTI" -> items[it] - "A§DEAR" -> dearKey - else -> when(tMetadata.getField(name)?.type){ - is DecimalType -> "0" - else -> "" + var value = + when (name) { + "A§ARTI" -> items[it] + "A§DEAR" -> dearKey + else -> + when (tMetadata.getField(name)?.type) { + is DecimalType -> "0" + else -> "" + } } - } var recordField: RecordField = RecordField(name, value) record.add(recordField) @@ -303,10 +303,9 @@ class DB2400OperationsOnFile { repeat(numberOfRecordsToHandle) { var readEResult = dbFile.readEqual(dearKey) assertEquals(dea2Key, readEResult.record["A§DEA2"]) - //Delete record + // Delete record dbFile.delete(readEResult.record) } - } @Test @@ -352,7 +351,7 @@ class DB2400OperationsOnFile { dbManager!!.registerMetadata(fileMetadata, false) var dbFile = dbManager!!.openFile("VERAPG1L") - //keys: V£DATA, V£NOME, V£IDOJ + // keys: V£DATA, V£NOME, V£IDOJ val data = "20200901" val nome = "BNUNCA " val idoj = "0002003070" @@ -378,7 +377,7 @@ class DB2400OperationsOnFile { } @Test - fun resultSetCursorMovements(){ + fun resultSetCursorMovements() { val fileMetadata = PropertiesSerializer.propertiesToMetadata("src/test/resources/dds/properties/", "BRARTI0L") dbManager!!.registerMetadata(fileMetadata, false) var dbFile = dbManager!!.openFile("BRARTI0L") @@ -402,7 +401,7 @@ class DB2400OperationsOnFile { } @Test - fun resultSetCursorUpdate(){ + fun resultSetCursorUpdate() { val fileMetadata = PropertiesSerializer.propertiesToMetadata("src/test/resources/dds/properties/", "BRARTI0L") dbManager!!.registerMetadata(fileMetadata, false) var dbFile = dbManager!!.openFile("BRARTI0L") @@ -439,6 +438,4 @@ class DB2400OperationsOnFile { dbManager!!.closeFile("BRARTI0L") } - } - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFilePerfTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFilePerfTest.kt index 6bbe4fc..13c2861 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFilePerfTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/DB2400OperationsOnFilePerfTest.kt @@ -22,21 +22,18 @@ import com.smeup.dbnative.file.Record import com.smeup.dbnative.file.RecordField import com.smeup.dbnative.metadata.file.PropertiesSerializer import com.smeup.dbnative.sql.utils.* -import org.junit.AfterClass -import org.junit.BeforeClass import org.junit.Ignore import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertFalse -import kotlin.test.assertTrue - class DB2400OperationsOnFilePerfTest { - - private var dbManager: SQLDBMManager? = null - fun initDbManager(host: String = DB2_400_HOST, library: String = DB2_400_LIBRARY_NAME) { + fun initDbManager( + host: String = DB2_400_HOST, + library: String = DB2_400_LIBRARY_NAME, + ) { dbManager = dbManagerDB2400ForTest(host, library) } @@ -48,12 +45,15 @@ class DB2400OperationsOnFilePerfTest { PropertiesSerializer.propertiesToMetadata("src/test/resources/dds/properties/", "VERAPG0L") it!!.registerMetadata(fileMetadata, false) val dbFile = it!!.openFile("VERAPG0L") - for(i in 1..10) { - var record = Record(RecordField("V£IDOJ", "A3L00000X1"), - RecordField("V£DATA", "20210117"), - RecordField("V£NOME", "BUSFIO2 "), - RecordField("V£CDC", "SMEGL.001 "), - RecordField("V£COD1", "ERB ")) + for (i in 1..10) { + var record = + Record( + RecordField("V£IDOJ", "A3L00000X1"), + RecordField("V£DATA", "20210117"), + RecordField("V£NOME", "BUSFIO2 "), + RecordField("V£CDC", "SMEGL.001 "), + RecordField("V£COD1", "ERB "), + ) dbFile.write(record) record = dbFile.chain(arrayListOf("A3L00000X1")).record if (!dbFile.eof()) { @@ -93,11 +93,14 @@ class DB2400OperationsOnFilePerfTest { var record = dbFile.chain(arrayListOf("A3L0000001")).record if (dbFile.eof()) { - record = Record(RecordField("V£IDOJ", "A3L0000001"), - RecordField("V£DATA", "20210117"), - RecordField("V£NOME", "BUSFIO "), - RecordField("V£CDC", "SMEGL.001 "), - RecordField("V£COD1", "ERB ")) + record = + Record( + RecordField("V£IDOJ", "A3L0000001"), + RecordField("V£DATA", "20210117"), + RecordField("V£NOME", "BUSFIO "), + RecordField("V£CDC", "SMEGL.001 "), + RecordField("V£COD1", "ERB "), + ) dbFile.write(record) record = dbFile.chain(arrayListOf("A3L0000001")).record assertFalse { dbFile.eof() } @@ -110,7 +113,6 @@ class DB2400OperationsOnFilePerfTest { record = dbFile.chain(arrayListOf("A3L0000001")).record assertEquals(record["V£ATV0"], "2") dbFile.delete(record) - } } @@ -155,11 +157,11 @@ class DB2400OperationsOnFilePerfTest { } } - private fun doChain(keys: List, dbFile: DBFile){ + private fun doChain( + keys: List, + dbFile: DBFile, + ) { val chainResult = dbFile.chain(keys) assertEquals(keys[0], chainResult.record["A§ARTI"]?.trim()) } - - } - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/JDBCUtilsTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/JDBCUtilsTest.kt index e6b3ca3..6d2e022 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/JDBCUtilsTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/JDBCUtilsTest.kt @@ -25,7 +25,6 @@ import org.junit.Test import kotlin.test.assertEquals class JDBCUtilsTest { - private lateinit var dbManager: SQLDBMManager @Before @@ -37,7 +36,9 @@ class JDBCUtilsTest { fun primaryKeysTest() { dbManager.connection.use { it.createStatement() - .execute("CREATE TABLE TSTTAB00 (TSTFLDCHR CHAR (5) NOT NULL, TSTFLDNBR DECIMAL (7, 2) NOT NULL, TSTFLDNB2 DECIMAL (2, 0) NOT NULL, PRIMARY KEY(TSTFLDCHR, TSTFLDNBR))") + .execute( + "CREATE TABLE TSTTAB00 (TSTFLDCHR CHAR (5) NOT NULL, TSTFLDNBR DECIMAL (7, 2) NOT NULL, TSTFLDNB2 DECIMAL (2, 0) NOT NULL, PRIMARY KEY(TSTFLDCHR, TSTFLDNBR))", + ) assertEquals(listOf("TSTFLDCHR", "TSTFLDNBR"), it.primaryKeys("TSTTAB00")) } } @@ -46,7 +47,9 @@ class JDBCUtilsTest { fun orderingFieldsTest() { dbManager.connection.use { it.createStatement() - .execute("CREATE TABLE TSTTAB00 (TSTFLDCHR CHAR (5) NOT NULL, TSTFLDNBR DECIMAL (7, 2) NOT NULL, TSTFLDNB2 DECIMAL (2, 0) NOT NULL, PRIMARY KEY(TSTFLDCHR, TSTFLDNBR))") + .execute( + "CREATE TABLE TSTTAB00 (TSTFLDCHR CHAR (5) NOT NULL, TSTFLDNBR DECIMAL (7, 2) NOT NULL, TSTFLDNB2 DECIMAL (2, 0) NOT NULL, PRIMARY KEY(TSTFLDCHR, TSTFLDNBR))", + ) it.createStatement().execute("CREATE VIEW TSTVIEW AS SELECT * FROM TSTTAB00 ORDER BY TSTFLDNB2, TSTFLDNBR") it.createStatement() .execute("CREATE INDEX TSTVIEW$CONVENTIONAL_INDEX_SUFFIX ON TSTTAB00 (TSTFLDNB2, TSTFLDNBR)") @@ -58,4 +61,4 @@ class JDBCUtilsTest { fun tearDownEach() { destroyDatabase() } -} \ No newline at end of file +} diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain1KeyTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain1KeyTest.kt index 1fb9bcd..7229cd4 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain1KeyTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain1KeyTest.kt @@ -28,9 +28,7 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class SQLChain1KeyTest { - companion object { - private var dbManager: SQLDBMManager? = null private var libName: String? = null @@ -63,6 +61,4 @@ class SQLChain1KeyTest { assertTrue(dbFile.chain("XYZ").record.isEmpty()) dbManager!!.closeFile(TSTTAB_TABLE_NAME) } - } - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain2KeysTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain2KeysTest.kt index bcbeaf9..6daae14 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain2KeysTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLChain2KeysTest.kt @@ -28,12 +28,9 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class SQLChain2KeysTest { - companion object { - private lateinit var dbManager: SQLDBMManager - @BeforeClass @JvmStatic fun setUp() { @@ -51,10 +48,11 @@ class SQLChain2KeysTest { @Test fun findRecordsIfChainWithExistingKey() { val dbFile = dbManager.openFile(TST2TAB_TABLE_NAME) - val key2 = listOf( - "ABC", - "12.00" - ) + val key2 = + listOf( + "ABC", + "12.00", + ) val chainResult = dbFile.chain(key2) assertEquals("ABC", chainResult.record["TSTFLDCHR"]) assertEquals("12.00", chainResult.record["TSTFLDNBR"]) @@ -65,14 +63,12 @@ class SQLChain2KeysTest { @Test fun doesNotFindRecordsIfChainWithNotExistingKey() { val dbFile = dbManager.openFile(TST2TAB_TABLE_NAME) - val key2 = listOf( - "ZZZ", - "12" - ) + val key2 = + listOf( + "ZZZ", + "12", + ) assertTrue(dbFile.chain(key2).record.isEmpty()) dbManager.closeFile(TST2TAB_TABLE_NAME) } - - } - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityPerfTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityPerfTest.kt index be563c1..1ad5b89 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityPerfTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityPerfTest.kt @@ -27,11 +27,8 @@ import kotlin.test.assertEquals import kotlin.test.assertFails import kotlin.test.assertTrue - class SQLMunicipalityPerfTest { - companion object { - private lateinit var dbManager: SQLDBMManager @BeforeClass @@ -52,11 +49,11 @@ class SQLMunicipalityPerfTest { fun read() { val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME) assertTrue(dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ERBUSCO"))) - for(index in 0..138){ - var result = dbFile.read(); - assertTrue{!dbFile.eof()} - if(index == 138) { - assertEquals("CO" , getMunicipalityProv(result.record)) + for (index in 0..138) { + var result = dbFile.read() + assertTrue { !dbFile.eof() } + if (index == 138) { + assertEquals("CO", getMunicipalityProv(result.record)) } } dbManager.closeFile(MUNICIPALITY_TABLE_NAME) @@ -160,7 +157,7 @@ class SQLMunicipalityPerfTest { recordFields.add(RecordField("PREF", "1234")) recordFields.add(RecordField("COMUNE", "A99")) recordFields.add(RecordField("ISTAT", "999999")) - dbFile.write(Record(*recordFields.toTypedArray())); + dbFile.write(Record(*recordFields.toTypedArray())) // Read new record for control var chainResult = dbFile.chain(buildMunicipalityKey("IT", "LOM", "BG", "TOPOLINIA")) @@ -216,7 +213,7 @@ class SQLMunicipalityPerfTest { recordFields.add(RecordField("PREF", "4321")) recordFields.add(RecordField("COMUNE", "A99")) recordFields.add(RecordField("ISTAT", "999999")) - dbFile.write(Record(*recordFields.toTypedArray())); + dbFile.write(Record(*recordFields.toTypedArray())) // Read new record dbFile.setll(buildMunicipalityKey("IT", "LOM", "BG", "PAPEROPOLI")) @@ -224,7 +221,7 @@ class SQLMunicipalityPerfTest { assertEquals("PAPEROPOLI", getMunicipalityName(readResult.record)) // Delete added record - var chainResult= dbFile.chain(buildMunicipalityKey("IT", "LOM", "BG", "PAPEROPOLI")) + var chainResult = dbFile.chain(buildMunicipalityKey("IT", "LOM", "BG", "PAPEROPOLI")) dbFile.delete(chainResult.record) chainResult = dbFile.chain(buildMunicipalityKey("IT", "LOM", "BG", "PAPEROPOLI")) assertTrue(dbFile.eof()) @@ -246,7 +243,7 @@ class SQLMunicipalityPerfTest { fun usupportedUncoherentKeys() { val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME) dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ZONE")) - assertFails {dbFile.readEqual(buildMunicipalityKey("IT", "LOM", "CO"))} + assertFails { dbFile.readEqual(buildMunicipalityKey("IT", "LOM", "CO")) } dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } @@ -255,14 +252,14 @@ class SQLMunicipalityPerfTest { val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME) dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ZONE")) dbFile.readEqual(buildMunicipalityKey("IT", "LOM", "BS")) - assertFails {dbFile.read()} + assertFails { dbFile.read() } dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } @Test fun usupportedUnpositioning() { val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME) - assertFails {dbFile.readEqual(buildMunicipalityKey("IT", "LOM", "BS"))} + assertFails { dbFile.readEqual(buildMunicipalityKey("IT", "LOM", "BS")) } dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } @@ -271,7 +268,7 @@ class SQLMunicipalityPerfTest { val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME) dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ZONE")) dbFile.readEqual(buildMunicipalityKey("IT", "LOM", "BS")) - assertFails {dbFile.readPrevious()} + assertFails { dbFile.readPrevious() } dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } @@ -283,4 +280,3 @@ class SQLMunicipalityPerfTest { usupportedReadChangeDirection() } } - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityTest.kt index 9e99211..88c42c9 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLMunicipalityTest.kt @@ -25,11 +25,8 @@ import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertTrue - class SQLMunicipalityTest { - companion object { - private lateinit var dbManager: SQLDBMManager @BeforeClass @@ -82,11 +79,11 @@ class SQLMunicipalityTest { assertTrue(dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ERBUSCO"))) assertEquals( "EDOLO", - getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record) + getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record), ) assertEquals( "DESENZANO DEL GARDA", - getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record) + getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record), ) dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } @@ -415,7 +412,4 @@ class SQLMunicipalityTest { assertEquals(32, count) dbManager.closeFile(MUNICIPALITY_TABLE_NAME) } - - } - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadEqualTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadEqualTest.kt index 52ce186..4d32ea9 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadEqualTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadEqualTest.kt @@ -26,11 +26,9 @@ import kotlin.test.assertFailsWith import kotlin.test.assertTrue class SQLReadEqualTest { - companion object { - private lateinit var dbManager: SQLDBMManager - + @BeforeClass @JvmStatic fun setUp() { @@ -67,7 +65,7 @@ class SQLReadEqualTest { @Test fun findRecordsIfChainAndReadEExistingKey() { val dbFile = dbManager.openFile(XEMP2_VIEW_NAME) - val chainResult = dbFile.chain( "C01") + val chainResult = dbFile.chain("C01") assertEquals("SALLY KWAN", getEmployeeName(chainResult.record)) assertEquals("DELORES QUINTANA", getEmployeeName(dbFile.readEqual().record)) assertEquals("HEATHER NICHOLLS", getEmployeeName(dbFile.readEqual().record)) @@ -79,8 +77,8 @@ class SQLReadEqualTest { @Test fun readUntilEof() { val dbFile = dbManager.openFile(XEMP2_VIEW_NAME) - val chainResult = dbFile.chain( "C01") - var readed = 0; + val chainResult = dbFile.chain("C01") + var readed = 0 while (dbFile.eof() == false) { var readResult = dbFile.readEqual("C01") readed++ @@ -92,12 +90,11 @@ class SQLReadEqualTest { @Test fun equals() { val dbFile = dbManager.openFile(XEMP2_VIEW_NAME) - val chainResult = dbFile.setll( "C01") - assertTrue (dbFile.equal()) + val chainResult = dbFile.setll("C01") + assertTrue(dbFile.equal()) dbManager.closeFile(XEMP2_VIEW_NAME) } - @Test fun findRecordsIfReadEWithKeyExistingKey() { val dbFile = dbManager.openFile(XEMP2_VIEW_NAME) @@ -128,6 +125,4 @@ class SQLReadEqualTest { assertEquals(0, dbFile.readEqual("C01").record.size) dbManager.closeFile(XEMP2_VIEW_NAME) } - } - diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadPreviousTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadPreviousTest.kt index fca6e4c..a5a4a5d 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadPreviousTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLReadPreviousTest.kt @@ -25,11 +25,9 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class SQLReadPreviousTest { - companion object { - private lateinit var dbManager: SQLDBMManager - + @BeforeClass @JvmStatic fun setUp() { @@ -62,5 +60,4 @@ class SQLReadPreviousTest { assertEquals("MICHELLE SPRINGER", getEmployeeName(dbFile.readPrevious().record)) dbManager.closeFile(EMPLOYEE_TABLE_NAME) } - } diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLUtilsTest.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLUtilsTest.kt index 98da867..20d26c5 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLUtilsTest.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/SQLUtilsTest.kt @@ -28,52 +28,54 @@ import org.junit.Test import kotlin.test.assertEquals class SQLUtilsTest { - @Test fun sqlForCreateTableTestWithPrimaryKeys() { - val fileMetadata = TypedMetadata( - "TSTTAB", - "TSTTAB", - listOf( - "TSTFLDCHR" fieldByType CharacterType(5), - "TSTFLDNBR" fieldByType DecimalType(7, 2), - "TSTFLDNB2" fieldByType DecimalType(2, 0) - ), - listOf( - "TSTFLDCHR", - "TSTFLDNBR" + val fileMetadata = + TypedMetadata( + "TSTTAB", + "TSTTAB", + listOf( + "TSTFLDCHR" fieldByType CharacterType(5), + "TSTFLDNBR" fieldByType DecimalType(7, 2), + "TSTFLDNB2" fieldByType DecimalType(2, 0), + ), + listOf( + "TSTFLDCHR", + "TSTFLDNBR", + ), ) - ) assertEquals( "CREATE TABLE TSTTAB (TSTFLDCHR CHAR(5) DEFAULT '' NOT NULL, TSTFLDNBR DECIMAL(7,2) DEFAULT 0 NOT NULL, TSTFLDNB2 DECIMAL(2,0) DEFAULT 0 NOT NULL, PRIMARY KEY(TSTFLDCHR, TSTFLDNBR))", - fileMetadata.toSQL() + fileMetadata.toSQL(), ) } @Test fun sqlForCreateTableTestWithoutPrimaryKeys() { - val fileMetadata = TypedMetadata( - "TSTTAB", - "TSTTAB", - listOf( - "TSTFLDCHR" fieldByType CharacterType(5), - "TSTFLDNBR" fieldByType DecimalType(7, 2), - "TSTFLDNB2" fieldByType DecimalType(2, 0) - ), - listOf() - ) + val fileMetadata = + TypedMetadata( + "TSTTAB", + "TSTTAB", + listOf( + "TSTFLDCHR" fieldByType CharacterType(5), + "TSTFLDNBR" fieldByType DecimalType(7, 2), + "TSTFLDNB2" fieldByType DecimalType(2, 0), + ), + listOf(), + ) assertEquals( "CREATE TABLE TSTTAB (TSTFLDCHR CHAR(5) DEFAULT '' NOT NULL, TSTFLDNBR DECIMAL(7,2) DEFAULT 0 NOT NULL, TSTFLDNB2 DECIMAL(2,0) DEFAULT 0 NOT NULL)", - fileMetadata.toSQL() + fileMetadata.toSQL(), ) } @Test fun sqlForInsertTest() { - val record = Record( - RecordField("TSTFLDCHR", "XXX"), - RecordField("TSTFLDNBR", "123.45") - ) + val record = + Record( + RecordField("TSTFLDCHR", "XXX"), + RecordField("TSTFLDNBR", "123.45"), + ) assertEquals("INSERT INTO TSTTAB (TSTFLDCHR, TSTFLDNBR) VALUES(?, ?)", "TSTTAB".insertSQL(record)) } diff --git a/sql/src/test/kotlin/com/smeup/dbnative/sql/utils/SQLDBTestUtils.kt b/sql/src/test/kotlin/com/smeup/dbnative/sql/utils/SQLDBTestUtils.kt index e728a05..f0fd00f 100644 --- a/sql/src/test/kotlin/com/smeup/dbnative/sql/utils/SQLDBTestUtils.kt +++ b/sql/src/test/kotlin/com/smeup/dbnative/sql/utils/SQLDBTestUtils.kt @@ -43,11 +43,13 @@ const val TST2TAB_TABLE_NAME = "TSTTAB" const val MUNICIPALITY_TABLE_NAME = "MUNICIPALITY" const val TEST_LOG = false private val LOGGING_LEVEL = LoggingLevel.ALL -//do not change defaultValue -//if you want to create sqlconnection against another db use function: dbManagerForTest(testSQLDBType: TestSQLDBType) + +// do not change defaultValue +// if you want to create sqlconnection against another db use function: dbManagerForTest(testSQLDBType: TestSQLDBType) private var defaultDbType = TestSQLDBType.HSQLDB -//private var defaultDbType = TestSQLDBType.MY_SQL -//private var defaultDbType = TestSQLDBType.DB2_400 + +// private var defaultDbType = TestSQLDBType.MY_SQL +// private var defaultDbType = TestSQLDBType.DB2_400 const val DATABASE_NAME = "TEST" const val DB2_400_HOST = "SRVLAB01.SMEUP.COM" const val DB2_400_LIBRARY_NAME = "W_PARFRA" @@ -55,71 +57,85 @@ const val DB2_400_LIBRARY_NAME = "W_PARFRA" enum class TestSQLDBType( val connectionConfig: ConnectionConfig, val dbaConnectionConfig: ConnectionConfig? = connectionConfig, - val createDatabase : (dbaConnection: Connection) -> Unit = {}, - val destroyDatabase: (dbaConnection: Connection) -> Unit = {}) { + val createDatabase: (dbaConnection: Connection) -> Unit = {}, + val destroyDatabase: (dbaConnection: Connection) -> Unit = {}, +) { MY_SQL( - connectionConfig = ConnectionConfig( - fileName= "*", - url = "jdbc:mysql://localhost:3306/$DATABASE_NAME", - user = "root", - password = "root"), - dbaConnectionConfig = ConnectionConfig( - fileName= "*", - url = "jdbc:mysql://localhost:3306/", - user = "root", - password = "root"), - createDatabase = { dbaConnection -> dbaConnection.prepareStatement("CREATE DATABASE $DATABASE_NAME").use { it.execute() } }, - destroyDatabase = { dbaConnection -> dbaConnection.prepareStatement("DROP DATABASE $DATABASE_NAME").use { it.execute() } } + connectionConfig = + ConnectionConfig( + fileName = "*", + url = "jdbc:mysql://localhost:3306/$DATABASE_NAME", + user = "root", + password = "root", + ), + dbaConnectionConfig = + ConnectionConfig( + fileName = "*", + url = "jdbc:mysql://localhost:3306/", + user = "root", + password = "root", + ), + createDatabase = { dbaConnection -> dbaConnection.prepareStatement("CREATE DATABASE $DATABASE_NAME").use { it.execute() } }, + destroyDatabase = { dbaConnection -> dbaConnection.prepareStatement("DROP DATABASE $DATABASE_NAME").use { it.execute() } }, ), - HSQLDB(ConnectionConfig( - fileName= "*", - url = "jdbc:hsqldb:mem:$DATABASE_NAME", - user = "sa", - password = "root"), - destroyDatabase = { dbaConnection -> dbaConnection.prepareStatement("DROP SCHEMA PUBLIC CASCADE").use { it.execute() } } + HSQLDB( + ConnectionConfig( + fileName = "*", + url = "jdbc:hsqldb:mem:$DATABASE_NAME", + user = "sa", + password = "root", + ), + destroyDatabase = { dbaConnection -> dbaConnection.prepareStatement("DROP SCHEMA PUBLIC CASCADE").use { it.execute() } }, ), - DB2_400(ConnectionConfig( - fileName= "*", + DB2_400( + ConnectionConfig( + fileName = "*", driver = "com.ibm.as400.access.AS400JDBCDriver", url = "jdbc:as400://$DB2_400_HOST/$DB2_400_LIBRARY_NAME;", user = "USER", - password = "**********"), - - //force no create connection for dba operations - dbaConnectionConfig = null + password = "**********", + ), + // force no create connection for dba operations + dbaConnectionConfig = null, + ), + DB2_400_DAT( + ConnectionConfig( + fileName = "*", + driver = "com.ibm.as400.access.AS400JDBCDriver", + url = "jdbc:as400://$DB2_400_HOST/SMEUP_DAT", + user = "USER", + password = "**********", + ), + // force no create connection for dba operations + dbaConnectionConfig = null, ), - DB2_400_DAT(ConnectionConfig( - fileName= "*", - driver = "com.ibm.as400.access.AS400JDBCDriver", - url = "jdbc:as400://$DB2_400_HOST/SMEUP_DAT", - user = "USER", - password = "**********"), - - //force no create connection for dba operations - dbaConnectionConfig = null - ) - } object DatabaseNameFactory { var COUNTER = AtomicInteger() } -fun dbManagerDB2400ForTest(host: String, library:String): SQLDBMManager{ - val dbManager = SQLDBMManager(ConnectionConfig( - fileName= "*", - driver = "com.ibm.as400.access.AS400JDBCDriver", - url = "jdbc:as400://$host/$library;", - user = "USER", - password = "**********"), - ) +fun dbManagerDB2400ForTest( + host: String, + library: String, +): SQLDBMManager { + val dbManager = + SQLDBMManager( + ConnectionConfig( + fileName = "*", + driver = "com.ibm.as400.access.AS400JDBCDriver", + url = "jdbc:as400://$host/$library;", + user = "USER", + password = "**********", + ), + ) dbManager.logger = Logger.getSimpleInstance(LOGGING_LEVEL) return dbManager } fun dbManagerForTest() = dbManagerForTest(defaultDbType) -fun dbManagerForTest(testSQLDBType: TestSQLDBType) : SQLDBMManager { +fun dbManagerForTest(testSQLDBType: TestSQLDBType): SQLDBMManager { testLog("Creating SQLDBManager with db type = $testSQLDBType") val dbManager = SQLDBMManager(testSQLDBType.connectionConfig) @@ -146,68 +162,74 @@ fun destroyDatabase(testSQLDBType: TestSQLDBType) { } fun createAndPopulateTstTable(dbManager: SQLDBMManager?) { - val fields = listOf( - "TSTFLDCHR" fieldByType CharacterType(3), - "TSTFLDNBR" fieldByType DecimalType(5, 2) - ) - - val keys = listOf( - "TSTFLDCHR" - ) + val fields = + listOf( + "TSTFLDCHR" fieldByType CharacterType(3), + "TSTFLDNBR" fieldByType DecimalType(5, 2), + ) + val keys = + listOf( + "TSTFLDCHR", + ) createAndPopulateTable(dbManager, TSTTAB_TABLE_NAME, TSTTAB_TABLE_NAME, fields, keys, "src/test/resources/csv/TstTab.csv") } fun createAndPopulateTst2Table(dbManager: SQLDBMManager?) { - val fields = listOf( - "TSTFLDCHR" fieldByType VarcharType(3), - "TSTFLDNBR" fieldByType DecimalType(5, 2), - "DESTST" fieldByType VarcharType(40) - ) - - val keys = listOf( - "TSTFLDCHR", - "TSTFLDNBR" - ) - - createAndPopulateTable(dbManager, TST2TAB_TABLE_NAME, TST2TAB_TABLE_NAME, fields, keys,"src/test/resources/csv/Tst2Tab.csv") + val fields = + listOf( + "TSTFLDCHR" fieldByType VarcharType(3), + "TSTFLDNBR" fieldByType DecimalType(5, 2), + "DESTST" fieldByType VarcharType(40), + ) + + val keys = + listOf( + "TSTFLDCHR", + "TSTFLDNBR", + ) + + createAndPopulateTable(dbManager, TST2TAB_TABLE_NAME, TST2TAB_TABLE_NAME, fields, keys, "src/test/resources/csv/Tst2Tab.csv") } fun createAndPopulateEmployeeTable(dbManager: SQLDBMManager?) { - val fields = listOf( - "EMPNO" fieldByType CharacterType(6), - "FIRSTNME" fieldByType VarcharType(12), - "MIDINIT" fieldByType VarcharType(1), - "LASTNAME" fieldByType VarcharType(15), - "WORKDEPT" fieldByType CharacterType(3) - ) - - val keys = listOf( - "EMPNO" - ) - - createAndPopulateTable(dbManager, EMPLOYEE_TABLE_NAME, EMPLOYEE_TABLE_NAME, fields, keys,"src/test/resources/csv/Employee.csv") + val fields = + listOf( + "EMPNO" fieldByType CharacterType(6), + "FIRSTNME" fieldByType VarcharType(12), + "MIDINIT" fieldByType VarcharType(1), + "LASTNAME" fieldByType VarcharType(15), + "WORKDEPT" fieldByType CharacterType(3), + ) + + val keys = + listOf( + "EMPNO", + ) + + createAndPopulateTable(dbManager, EMPLOYEE_TABLE_NAME, EMPLOYEE_TABLE_NAME, fields, keys, "src/test/resources/csv/Employee.csv") } fun createAndPopulateXemp2View(dbManager: SQLDBMManager?) { // create view executing sql -> TODO: insert a createView method in DBMManager and use it fun createXEMP2() = "CREATE VIEW $XEMP2_VIEW_NAME AS SELECT * FROM EMPLOYEE ORDER BY WORKDEPT, EMPNO" - fun createXEMP2Index() = - "CREATE INDEX $XEMP2_VIEW_NAME$CONVENTIONAL_INDEX_SUFFIX ON EMPLOYEE (WORKDEPT ASC, EMPNO ASC)" + fun createXEMP2Index() = "CREATE INDEX $XEMP2_VIEW_NAME$CONVENTIONAL_INDEX_SUFFIX ON EMPLOYEE (WORKDEPT ASC, EMPNO ASC)" - val fields = listOf( - "EMPNO" fieldByType CharacterType(6), - "FIRSTNME" fieldByType VarcharType(12), - "MIDINIT" fieldByType VarcharType(1), - "LASTNAME" fieldByType VarcharType(15), - "WORKDEPT" fieldByType CharacterType(3) - ) + val fields = + listOf( + "EMPNO" fieldByType CharacterType(6), + "FIRSTNME" fieldByType VarcharType(12), + "MIDINIT" fieldByType VarcharType(1), + "LASTNAME" fieldByType VarcharType(15), + "WORKDEPT" fieldByType CharacterType(3), + ) - val keys = listOf( - "WORKDEPT" - ) + val keys = + listOf( + "WORKDEPT", + ) val metadata = FileMetadata("$XEMP2_VIEW_NAME", "EMPLOYEE", fields.fieldList(), keys) dbManager!!.registerMetadata(metadata, true) @@ -215,24 +237,25 @@ fun createAndPopulateXemp2View(dbManager: SQLDBMManager?) { } fun createAndPopulateMunicipalityTable(dbManager: SQLDBMManager?) { - val fields = listOf( - "NAZ" fieldByType CharacterType(2), - "REG" fieldByType CharacterType(3), - "PROV" fieldByType CharacterType(2), - "CITTA" fieldByType VarcharType(35), - "CAP" fieldByType CharacterType(5), - "PREF" fieldByType CharacterType(4), - "COMUNE" fieldByType CharacterType(4), - "ISTAT" fieldByType CharacterType(6) - ) - - val keys = listOf( - "NAZ", - "REG", - "PROV", - "CITTA" - ) - + val fields = + listOf( + "NAZ" fieldByType CharacterType(2), + "REG" fieldByType CharacterType(3), + "PROV" fieldByType CharacterType(2), + "CITTA" fieldByType VarcharType(35), + "CAP" fieldByType CharacterType(5), + "PREF" fieldByType CharacterType(4), + "COMUNE" fieldByType CharacterType(4), + "ISTAT" fieldByType CharacterType(6), + ) + + val keys = + listOf( + "NAZ", + "REG", + "PROV", + "CITTA", + ) createAndPopulateTable( dbManager, @@ -240,7 +263,7 @@ fun createAndPopulateMunicipalityTable(dbManager: SQLDBMManager?) { MUNICIPALITY_TABLE_NAME, fields, keys, - "src/test/resources/csv/Municipality.csv" + "src/test/resources/csv/Municipality.csv", ) } @@ -268,9 +291,8 @@ private fun createAndPopulateTable( tableName: String, fields: List, keys: List, - dataFilePath: String + dataFilePath: String, ) { - val tMetadata = TypedMetadata(name, tableName, fields, keys) createFile(tMetadata, dbManager!!) Assert.assertTrue(dbManager.existFile(tableName)) @@ -294,15 +316,18 @@ fun buildMunicipalityKey(vararg values: String): List { val keyValues = mutableListOf() val keys = arrayOf("NAZ", "REG", "PROV", "CITTA") for ((index, value) in values.withIndex()) { - if (keys.size> index) { + if (keys.size > index) { keyValues.add(value) } } return keyValues } -fun createFile(tMetadata: TypedMetadata, dbManager: SQLDBMManager) { - val metadata: FileMetadata = tMetadata.fileMetadata(); +fun createFile( + tMetadata: TypedMetadata, + dbManager: SQLDBMManager, +) { + val metadata: FileMetadata = tMetadata.fileMetadata() dbManager.connection.createStatement().use { it.execute(tMetadata.toSQL()) } @@ -311,7 +336,6 @@ fun createFile(tMetadata: TypedMetadata, dbManager: SQLDBMManager) { fun TypedMetadata.toSQL(): String = "CREATE TABLE ${this.tableName} (${this.fields.toSQL(this)})" - fun Collection.toSQL(tMetadata: TypedMetadata): String { val primaryKeys = tMetadata.fileKeys.joinToString { it } @@ -344,14 +368,16 @@ fun sql2Type(metadataResultSet: ResultSet): FieldType { return sql2Type(sqlType, columnSize, decimalDigits) } - - /** * Convert SQL type in FieldType */ -fun sql2Type(sqlType: String, columnSize: Int, decimalDigits: Int): FieldType = +fun sql2Type( + sqlType: String, + columnSize: Int, + decimalDigits: Int, +): FieldType = when (sqlType) { - "CHAR","CHARACTER","NCHAR" -> CharacterType(columnSize) + "CHAR", "CHARACTER", "NCHAR" -> CharacterType(columnSize) "VARCHAR" -> VarcharType(columnSize) "INT", "INTEGER" -> IntegerType "SMALLINT" -> SmallintType @@ -366,4 +392,4 @@ fun sql2Type(sqlType: String, columnSize: Int, decimalDigits: Int): FieldType = "BINARY" -> BinaryType(columnSize) "VARBINARY" -> VarbinaryType(columnSize) else -> TODO("Conversion from SQL Type not yet implemented: $sqlType") - } \ No newline at end of file + }