First commit
This commit is contained in:
193
src/main/kotlin/db/migration/V1__test_connection.kt
Normal file
193
src/main/kotlin/db/migration/V1__test_connection.kt
Normal file
@@ -0,0 +1,193 @@
|
||||
package db.migration
|
||||
|
||||
import org.flywaydb.core.api.migration.BaseJavaMigration
|
||||
import org.flywaydb.core.api.migration.Context
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import kotlin.collections.*
|
||||
import kotliquery.*
|
||||
import de.m3y.kformat.*
|
||||
import de.m3y.kformat.Table.Hints
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
val format = Json { encodeDefaults = true }
|
||||
|
||||
data class Dictionary(
|
||||
val id: Int,
|
||||
val lang1Id: Int,
|
||||
val lang2Id: Int,
|
||||
val name: String,
|
||||
val fullName: String)
|
||||
|
||||
data class DictType(
|
||||
val id: Int,
|
||||
val shortName: String,
|
||||
val fullName: String
|
||||
)
|
||||
data class Language(
|
||||
val id: Int,
|
||||
val name: String,
|
||||
val shortName: String
|
||||
)
|
||||
data class Translation(
|
||||
val id: Int,
|
||||
val dictionaryId: Int,
|
||||
val lang1Id: Int,
|
||||
val lang2Id: Int,
|
||||
val lang1Name: String,
|
||||
val lang2Name: String,
|
||||
val direction: Int
|
||||
)
|
||||
|
||||
data class Pronunciation(
|
||||
val id: Int,
|
||||
val typeId: Int,
|
||||
val ipa: String?,
|
||||
val filename: String?
|
||||
)
|
||||
|
||||
data class PronunciationType(
|
||||
val id: Int,
|
||||
val name: String?
|
||||
)
|
||||
data class TermsPronunciation(
|
||||
val termId: Int,
|
||||
val pronunciationId: Int,
|
||||
)
|
||||
|
||||
data class Suffix(
|
||||
val id: Int,
|
||||
val text: String
|
||||
)
|
||||
|
||||
data class Term(
|
||||
val id: Int,
|
||||
val dictionaryId: Int,
|
||||
val string1: String,
|
||||
val string2: String,
|
||||
val suffix1Id: Int?,
|
||||
val suffix2Id: Int?,
|
||||
val typeId: Int?,
|
||||
val member: String?,
|
||||
val order2: Int?,
|
||||
val flags: String?
|
||||
)
|
||||
val toDictionary: (Row) -> Dictionary = { row ->
|
||||
Dictionary(
|
||||
row.int("id"),
|
||||
row.int("lang1_id"),
|
||||
row.int("lang2_id"),
|
||||
row.string("name"),
|
||||
row.string("full_name")
|
||||
)
|
||||
}
|
||||
|
||||
val toDictType: (Row) -> DictType = { row ->
|
||||
DictType(
|
||||
row.int("id"),
|
||||
row.string("short_name"),
|
||||
row.string("full_name")
|
||||
)
|
||||
}
|
||||
val toLanguage: (Row) -> Language = { row ->
|
||||
Language(
|
||||
row.int("id"),
|
||||
row.string("name"),
|
||||
row.string("short_name")
|
||||
)
|
||||
}
|
||||
val toTranslation: (Row) -> Translation = { row ->
|
||||
Translation(
|
||||
row.int("id"),
|
||||
row.int("dictionary_id"),
|
||||
row.int("lang1_id"),
|
||||
row.int("lang2_id"),
|
||||
row.string("lang_name1"),
|
||||
row.string("lang_name2"),
|
||||
row.int("direction"),
|
||||
)
|
||||
}
|
||||
|
||||
val toPronunciation: (Row) -> Pronunciation = { row ->
|
||||
Pronunciation(
|
||||
row.int("id"),
|
||||
row.int("type_id"),
|
||||
row.stringOrNull("ipa"),
|
||||
row.stringOrNull("filename")
|
||||
)
|
||||
}
|
||||
|
||||
val toTermsPronunciation: (Row) -> TermsPronunciation = { row ->
|
||||
TermsPronunciation(
|
||||
row.int("term_id"),
|
||||
row.int("pronunciation_id"),
|
||||
)
|
||||
}
|
||||
|
||||
val toSuffix: (Row) -> Suffix = { row ->
|
||||
Suffix(
|
||||
row.int("id"),
|
||||
row.string("text")
|
||||
)
|
||||
}
|
||||
val toTerm: (Row) -> Term = { row ->
|
||||
Term(
|
||||
row.int("id"),
|
||||
row.int("dictionary_id"),
|
||||
row.string("string1"),
|
||||
row.string("string2"),
|
||||
row.intOrNull("suffix1_id"),
|
||||
row.intOrNull("suffix2_id"),
|
||||
row.intOrNull("type_id"),
|
||||
row.stringOrNull("member"),
|
||||
row.intOrNull("order2"),
|
||||
row.stringOrNull("flags")
|
||||
)
|
||||
}
|
||||
|
||||
object SharedData {
|
||||
var hashDict: Map<Int,Dictionary> = mapOf()
|
||||
var hashDictType: Map<Int,DictType> = mapOf()
|
||||
var hashLang: Map<Int,Language> = mapOf()
|
||||
var hashTransMap: Map<Int,Translation> = mapOf()
|
||||
var hashPron: Map<Int,Pronunciation> = mapOf()
|
||||
var hashSuffix: Map<Int,Suffix> = mapOf()
|
||||
var hashTermPron : Map<Int,TermsPronunciation> = mapOf()
|
||||
var listTerm : List<Term> = listOf()
|
||||
var getListofTerms : (Int) -> List<Term> = {
|
||||
listOf()
|
||||
}
|
||||
}
|
||||
|
||||
class V1__test_connection: BaseJavaMigration() {
|
||||
override fun migrate(context: Context?) {
|
||||
val session = sessionOf("jdbc:postgresql://nuc.lan:5432/dict", "dict_user", "PW4dbdict")
|
||||
val allNameQuery = queryOf("select * from dictionary").map(toDictionary).asList
|
||||
val allDict = session.run(allNameQuery)
|
||||
SharedData.hashDict = allDict.associateBy { it.id }
|
||||
val allDictTypeQuery = queryOf("select * from dict_type").map(toDictType).asList
|
||||
val allDictType = session.run(allDictTypeQuery)
|
||||
SharedData.hashDictType = allDictType.associateBy { it.id }
|
||||
val allTranslationQuery = queryOf("select * from translation").map(toTranslation).asList
|
||||
val allTranslation = session.run(allTranslationQuery)
|
||||
SharedData.hashTransMap = allTranslation.associateBy { it.id }
|
||||
val allLangQuery = queryOf("select * from language").map(toLanguage).asList
|
||||
val allLanguages = session.run(allLangQuery)
|
||||
SharedData.hashLang = allLanguages.associateBy { it.id }
|
||||
val allSuffixQuery = queryOf("select * from suffix").map(toSuffix).asList
|
||||
val allSuffix = session.run(allSuffixQuery)
|
||||
SharedData.hashSuffix = allSuffix.associateBy { it.id }
|
||||
val allPronunciationQuery = queryOf("select * from pronunciation").map(toPronunciation).asList
|
||||
val allPronunciation = session.run(allPronunciationQuery)
|
||||
SharedData.hashPron = allPronunciation.associateBy { it.id }
|
||||
val allTermsPronunciationQuery = queryOf("select * from terms_pronunciations").map(toTermsPronunciation).asList
|
||||
val allTermsPronunciation = session.run(allTermsPronunciationQuery)
|
||||
SharedData.hashTermPron = allTermsPronunciation.associateBy { it.termId }
|
||||
SharedData.getListofTerms = {
|
||||
session.run(queryOf("select * from term WHERE dictionary_id=${it}").map(toTerm).asList)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
53
src/main/kotlin/db/migration/V2__crete_settingsdb.kt
Normal file
53
src/main/kotlin/db/migration/V2__crete_settingsdb.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package db.migration
|
||||
|
||||
import dao.DictionaryDao
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import tables.*
|
||||
import org.flywaydb.core.api.migration.BaseJavaMigration
|
||||
import org.flywaydb.core.api.migration.Context
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.jetbrains.exposed.sql.upsert
|
||||
|
||||
class V2__create_settingsdb: BaseJavaMigration() {
|
||||
override fun migrate(context: Context?) {
|
||||
transaction {
|
||||
SchemaUtils.create(Dictionaries, Languages, Translations, Settings)
|
||||
|
||||
for ((di, lang) in SharedData.hashLang) {
|
||||
Languages.insert {
|
||||
it[id] = lang.id
|
||||
it[name] = lang.name
|
||||
it[shortName] = lang.shortName
|
||||
}
|
||||
}
|
||||
|
||||
for ((di, dict) in SharedData.hashDict) {
|
||||
Dictionaries.insert {
|
||||
it[id] = dict.id
|
||||
it[lang1] = dict.lang1Id
|
||||
it[lang2] = dict.lang2Id
|
||||
it[name] = dict.name
|
||||
it[fullName] = dict.fullName
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
for ((di, trans) in SharedData.hashTransMap) {
|
||||
Translations.insert {
|
||||
it[id] = trans.id
|
||||
it[lang1Id] = trans.lang1Id
|
||||
it[lang2Id] = trans.lang2Id
|
||||
it[dictionaryId] = trans.dictionaryId
|
||||
it[langName1] = trans.lang1Name
|
||||
it[langName2] = trans.lang2Name
|
||||
it[direction] = trans.direction
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
117
src/main/kotlin/db/migration/V3__create_dictionaries.kt
Normal file
117
src/main/kotlin/db/migration/V3__create_dictionaries.kt
Normal file
@@ -0,0 +1,117 @@
|
||||
package db.migration
|
||||
|
||||
import dao.DictionaryDao
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import tables.*
|
||||
import org.flywaydb.core.api.migration.BaseJavaMigration
|
||||
import org.flywaydb.core.api.migration.Context
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.jetbrains.exposed.sql.upsert
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
class V3__create_dictionaries: BaseJavaMigration() {
|
||||
override fun migrate(context: Context?) {
|
||||
println(SharedData.hashDict)
|
||||
//exitProcess(0)
|
||||
for ((db_id,dict) in SharedData.hashDict) {
|
||||
|
||||
val db_name = "jdbc:h2:file:/Users/jaro/data/${SharedData.hashLang[dict.lang1Id]?.shortName}-${SharedData.hashLang[dict.lang2Id]?.shortName}"
|
||||
println(db_name)
|
||||
val db = Database.connect(db_name)
|
||||
transaction (db) {
|
||||
SchemaUtils.create(Dictionaries, Languages, Translations, Pronunciations, PronunciationTypes, DictTypes, Suffixes, Terms)
|
||||
|
||||
for ((di, lang) in SharedData.hashLang) {
|
||||
Languages.insert {
|
||||
it[id] = lang.id
|
||||
it[name] = lang.name
|
||||
it[shortName] = lang.shortName
|
||||
}
|
||||
}
|
||||
|
||||
for ((di, d) in SharedData.hashDict) {
|
||||
Dictionaries.insert {
|
||||
it[id] = d.id
|
||||
it[lang1] = d.lang1Id
|
||||
it[lang2] = d.lang2Id
|
||||
it[name] = d.name
|
||||
it[fullName] = d.fullName
|
||||
}
|
||||
}
|
||||
|
||||
for ((di, trans) in SharedData.hashTransMap) {
|
||||
Translations.insert {
|
||||
it[id] = trans.id
|
||||
it[lang1Id] = trans.lang1Id
|
||||
it[lang2Id] = trans.lang2Id
|
||||
it[dictionaryId] = trans.dictionaryId
|
||||
it[langName1] = trans.lang1Name
|
||||
it[langName2] = trans.lang2Name
|
||||
it[direction] = trans.direction
|
||||
}
|
||||
}
|
||||
|
||||
PronunciationTypes.insert {
|
||||
it[id] = 1
|
||||
it[name] = "English"
|
||||
}
|
||||
|
||||
PronunciationTypes.insert {
|
||||
it[id] = 2
|
||||
it[name] = "American english"
|
||||
}
|
||||
|
||||
PronunciationTypes.insert {
|
||||
it[id] = 3
|
||||
it[name] = "Business english"
|
||||
}
|
||||
|
||||
for ((di, pron) in SharedData.hashPron) {
|
||||
Pronunciations.insert {
|
||||
it[id] = pron.id
|
||||
it[typeId] = pron.typeId
|
||||
it[ipa] = pron.ipa
|
||||
it[filename] = pron.filename
|
||||
}
|
||||
}
|
||||
|
||||
for ((di, dt) in SharedData.hashDictType) {
|
||||
DictTypes.insert {
|
||||
it[id] = dt.id
|
||||
it[shortName] = dt.shortName
|
||||
it[fullName] = dt.fullName
|
||||
}
|
||||
}
|
||||
|
||||
for ((di, ss) in SharedData.hashSuffix) {
|
||||
Suffixes.insert {
|
||||
it[id] = ss.id
|
||||
it[text] = ss.text
|
||||
}
|
||||
}
|
||||
|
||||
var dictTerms = SharedData.getListofTerms(db_id)
|
||||
for (t in dictTerms) {
|
||||
Terms.insert {
|
||||
it[id] = t.id
|
||||
it[string1] = t.string1
|
||||
it[string2] = t.string2
|
||||
it[suffix1] = t.suffix1Id
|
||||
it[suffix2] = t.suffix2Id
|
||||
it[type] = t.typeId
|
||||
it[member] = t.member
|
||||
it[order2] = t.order2
|
||||
it[flags] = format.decodeFromString<IntArray?>(t.flags?.toString() ?:"[]")
|
||||
}
|
||||
}
|
||||
dictTerms = listOf()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user