org.oewntk.sql.out.Utils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tosql Show documentation
Show all versions of tosql Show documentation
Export Wordnet model to SQL files.
The newest version!
/*
* Copyright (c) 2024. Bernard Bou.
*/
package org.oewntk.sql.out
import org.oewntk.sql.out.Printers.printInsert
import java.io.PrintStream
/**
* Utilities
*/
object Utils {
/**
* Generate table
*
* @param ps print stream
* @param table table name
* @param columns column name
* @param byNid nid-to-value map, values mapped by nid
* @param toRow nid-to-value pair stringifier
* @param T type of values
*/
fun generateTable(
ps: PrintStream,
table: String,
columns: String,
byNid: Map,
toRow: (Pair) -> String,
) {
// make object-to-nid map
val seq = byNid.entries
.asSequence()
.map { Pair(it.key, it.value) }
.sortedBy { it.first }
// insert map
printInsert(ps, table, columns, seq, toRow, false)
}
// escape
/**
* Escape string for it to be handled by SQL
*
* @param str string
* @return SQL escaped string
*/
fun escape(str: String): String {
return str.replace("'", "''")
}
}