Skip to content

Commit

Permalink
Fix sql generation in quoted mode
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanovicz committed Nov 5, 2022
1 parent c0e298e commit a52a5df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/commonMain/kotlin/com/republicate/kddl/SQLFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ abstract class SQLFormatter(val quoted: Boolean, val uppercase: Boolean): Format
it.fields.values
}.filter {
it.type.startsWith("enum(")
}.distinctBy {
it.name
}.map {
defineEnum(it)
}.joinToString(separator = EOL))
Expand Down Expand Up @@ -110,7 +112,7 @@ abstract class SQLFormatter(val quoted: Boolean, val uppercase: Boolean): Format

if (asm.parent != null) {
if (!supportsInheritance) throw Error("inheritance not supported")
tableName = "base_$tableName"
tableName = transform("base_${asm.name}")
}

ret.append("CREATE TABLE $tableName (")
Expand Down Expand Up @@ -209,16 +211,24 @@ abstract class SQLFormatter(val quoted: Boolean, val uppercase: Boolean): Format
else "$Q${transform(src.name).removeSurrounding(Q)}_${transform(asm.fields.first().name.removeSuffix(suffix)).removeSurrounding(Q)}_fk$Q"
ret.append("ALTER TABLE $srcName")
ret.append(" ADD CONSTRAINT $fkName")
ret.append(" FOREIGN KEY (${transform(asm.fields.map{it.name}.joinToString(","))})")
ret.append(" FOREIGN KEY (${
asm.fields.map {
transform(it.name)
}.joinToString(",")
})")
ret.append(" REFERENCES ")

if (asm.towards.schema.name != src.schema.name) {
ret.append("$Q${transform(asm.towards.schema.name)}$Q.")
ret.append("${transform(asm.towards.schema.name)}.")
}
val dstName =
if (asm.towards.parent == null) transform(asm.towards.name)
else "${Q}base_${transform(asm.towards.name).removeSurrounding(Q)}$Q"
ret.append("$dstName (${transform(asm.towards.getOrCreatePrimaryKey().map{it.name}.joinToString(","))})")
ret.append("$dstName (${
asm.towards.getOrCreatePrimaryKey().map {
transform(it.name)
}.joinToString(",")
})")
if (asm.cascade) {
ret.append(" ON DELETE CASCADE")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PostgreSQLFormatter(quoted: Boolean, uppercase: Boolean): SQLFormatter(quo
ret.append(" $childFields")
}
ret.append(EOL)
ret.append(" FROM $tableName JOIN $qualifiedParentName ON ")
ret.append(" FROM ${transform(tableName)} JOIN $qualifiedParentName ON ")
val join = parent.getPrimaryKey().map {
"$parentName.${it.name} = $tableName.${it.name}"
}.joinToString(" AND ")
Expand All @@ -63,7 +63,7 @@ class PostgreSQLFormatter(quoted: Boolean, uppercase: Boolean): SQLFormatter(quo

if (pk.type == "serial") {

var seqName = "${transform(parent.name)}_${pkName.removeSurrounding(Q)}_seq"
var seqName = "${parent.name}_${pkName.removeSurrounding(Q)}_seq"
if (table.schema != parent.schema) seqName = "${transform(parent.schema.name)}.$seqName"

ret.append("CREATE RULE insert_${baseName} AS ON INSERT TO $viewName DO INSTEAD (${EOL}")
Expand Down

0 comments on commit a52a5df

Please sign in to comment.