Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Add support to foreign key constraint actions #523

Merged
merged 2 commits into from
Mar 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions anko/library/static/sqlite/src/sqlTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,28 @@ val REAL: SqlType = SqlTypeImpl("REAL")
val TEXT: SqlType = SqlTypeImpl("TEXT")
val BLOB: SqlType = SqlTypeImpl("BLOB")

fun FOREIGN_KEY(columnName: String, referenceTable: String, referenceColumn: String): Pair<String, SqlType> {
return "" to SqlTypeImpl("FOREIGN KEY($columnName) REFERENCES $referenceTable($referenceColumn)")
enum class ConstraintActions {
SET_NULL,
SET_DEFAULT,
SET_RESTRICT,
CASCADE,
NO_ACTION;

override fun toString(): String {
return super.toString().replace("_", " ")
}
}

fun ON_UPDATE(constraintActions: ConstraintActions): SqlTypeModifier {
return SqlTypeModifierImpl("ON UPDATE $constraintActions")
}

fun ON_DELETE(constraintActions: ConstraintActions): SqlTypeModifier {
return SqlTypeModifierImpl("ON DELETE $constraintActions")
}

fun FOREIGN_KEY(columnName: String, referenceTable: String, referenceColumn: String, vararg actions: SqlTypeModifier): Pair<String, SqlType> {
return "" to SqlTypeImpl("FOREIGN KEY($columnName) REFERENCES $referenceTable($referenceColumn)${actions.map { it.modifier }.joinToString("") { " $it" }}")
}

val PRIMARY_KEY: SqlTypeModifier = SqlTypeModifierImpl("PRIMARY KEY")
Expand Down