Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

3 changed files with 41 additions and 66 deletions

View File

@ -6,7 +6,7 @@ plugins {
} }
group = "me.blzr" group = "me.blzr"
version = "1.1" version = "1.0-SNAPSHOT"
repositories { repositories {
mavenCentral() mavenCentral()
@ -14,13 +14,12 @@ repositories {
dependencies { dependencies {
implementation("me.alllex.parsus:parsus-jvm:0.6.1") implementation("me.alllex.parsus:parsus-jvm:0.6.1")
implementation("info.picocli:picocli:4.7.6")
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
} }
application { application {
mainClass.set("me.blzr.apex.Main") mainClass.set("me.blzr.apex.MainKt")
} }
tasks.test { tasks.test {

View File

@ -1,9 +1,6 @@
package me.blzr.apex package me.blzr.apex
import me.alllex.parsus.parser.getOrElse import me.alllex.parsus.parser.getOrElse
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
@ -13,78 +10,57 @@ import kotlin.io.path.deleteIfExists
import kotlin.io.path.listDirectoryEntries import kotlin.io.path.listDirectoryEntries
import kotlin.system.exitProcess import kotlin.system.exitProcess
@Command(name = "apex-ddl-splitter", version = ["1.1"], mixinStandardHelpOptions = true) fun main(vararg args: String) {
class Main : Runnable { val (input, output) = when (args.size) {
@Option(names = ["-i", "--input"], description = ["Input SQL file"]) 1 -> args[0] to "${args[0].let { if (it.endsWith(".sql")) it.dropLast(4) else it }}/out/"
lateinit var input: String 2 -> args[0] to args[1]
else -> {
@Option(names = ["-o", "--output"], description = ["Output folder. By default, './out'"]) println("apex-ddl-splitter input.sql [out folder]")
var output: String? = null exitProcess(0)
@Option(names = ["-s", "--split"], description = ["Split. By default, '\\s*;$'", "Previous Oracle used '^/$'"])
var split: String = "\\s*;$"
companion object {
@JvmStatic
fun main(args: Array<String>) {
val exitCode: Int = CommandLine(Main()).execute(*args)
exitProcess(exitCode)
} }
} }
override fun run() { val ins = FileInputStream(input)
output = output ?: ((if (input.endsWith(".sql")) input.dropLast(4) else input) + "/out/")
val ins = FileInputStream(input) val text = ins.bufferedReader().readText()
val text = ins.bufferedReader().readText() val nodes = text.split(Regex("^/$", RegexOption.MULTILINE)).filter { it.isNotBlank() }
println("We have ${nodes.size} nodes")
val nodes = text.split(Regex(split, RegexOption.MULTILINE)).filter { it.isNotBlank() } val parser = OraDumpGrammar()
println("We have ${nodes.size} nodes")
val parser = OraDumpGrammar() val parsed: Map<String, Ora> = nodes.associateWith { node ->
parser.parse(node).getOrElse {
println(node)
throw IllegalArgumentException(it.toString())
}
}
val parsed: Map<String, Ora> = nodes.associateWith { node -> val outputPath = Path(output).createDirectories()
parser.parse(node).getOrElse { println("Create $outputPath")
println(node)
throw IllegalArgumentException(it.toString()) outputPath.listDirectoryEntries("*.sql").forEach { entry ->
println("Delete $entry")
entry.deleteIfExists()
}
FileOutputStream(File(output, "dict.sql")).bufferedWriter().use { dict ->
parsed.entries.forEachIndexed { index, (text, type) ->
println("Writing ${type.fileName}")
val outputFile = File(output, type.fileName)
if (outputFile.exists()) {
throw IllegalArgumentException("File already exists $outputFile")
} }
}
val outputPath = Path(output!!).createDirectories() dict.write("-- ${type.fileName}\n")
println("Create $outputPath")
outputPath.listDirectoryEntries("*.sql").forEach { entry -> FileOutputStream(outputFile).bufferedWriter().use {
println("Delete $entry") it.write(text.trim())
entry.deleteIfExists()
}
FileOutputStream(File(output, "dict.sql")).bufferedWriter().use { dict ->
parsed.entries.forEachIndexed { index, (text, type) ->
println("Writing ${type.fileName}")
var outputFile = File(output, type.fileName)
if (outputFile.exists()) {
for (i in 1..10) {
val anotherName = File(output, type.fileName.dropLast(".sql".length) + "-duplicate-$i.sql")
if (i == 10) {
throw IllegalArgumentException("File already exists $outputFile")
} else if (!anotherName.exists()) {
outputFile = anotherName
break
}
}
}
dict.write("-- ${type.fileName}\n")
FileOutputStream(outputFile).bufferedWriter().use {
it.write(text.trim())
}
} }
} }
} }
} }

View File

@ -46,7 +46,7 @@ class OraDumpGrammar() : Grammar<Ora>(ignoreCase = true, debugMode = true) {
val sequence = literalToken("sequence") val sequence = literalToken("sequence")
val quote = literalToken("\"") val quote = literalToken("\"")
val name = regexToken("[\\w$]+") val name = regexToken("\\w+")
val remaining = regexToken(Regex(".*", RegexOption.DOT_MATCHES_ALL)) val remaining = regexToken(Regex(".*", RegexOption.DOT_MATCHES_ALL))
val quoted by -quote * ref(::name) * -quote map { it.text } val quoted by -quote * ref(::name) * -quote map { it.text }