-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the columnType field in TableColumn to use an ADT (ColumnType).
- Loading branch information
1 parent
9e1864a
commit 6864ff8
Showing
6 changed files
with
54 additions
and
9 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
spra-play-server/src/main/scala/net/wiringbits/spra/admin/models/ColumnType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package net.wiringbits.spra.admin.models | ||
|
||
sealed trait ColumnType { | ||
val value: String | ||
} | ||
|
||
object ColumnType { | ||
case class Date(value: String) extends ColumnType | ||
case class Text(value: String) extends ColumnType | ||
case class Bytea(value: String) extends ColumnType | ||
case class Int(value: String) extends ColumnType | ||
case class Decimal(value: String) extends ColumnType | ||
case class UUID(value: String) extends ColumnType | ||
|
||
def parseColumnType(columnType: String): ColumnType = { | ||
// 'contains' is used because PostgreSQL types may include additional details like precision or scale | ||
// https://www.postgresql.org/docs/8.1/datatype.html | ||
val isInt = List("int", "serial").exists(columnType.contains) | ||
val isDecimal = List("float", "decimal").exists(columnType.contains) | ||
val isBytea = columnType == "bytea" | ||
val isUUID = columnType == "uuid" | ||
val isDate = List("date", "time").exists(columnType.contains) | ||
|
||
if (isDate) | ||
Date(columnType) | ||
else if (isDecimal) | ||
Decimal(columnType) | ||
else if (isBytea) | ||
Bytea(columnType) | ||
else if (isInt) | ||
Int(columnType) | ||
else if (isUUID) | ||
UUID(columnType) | ||
else | ||
Text(columnType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...lay-server/src/main/scala/net/wiringbits/spra/admin/repositories/models/TableColumn.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package net.wiringbits.spra.admin.repositories.models | ||
|
||
import net.wiringbits.spra.admin.models.ColumnType | ||
|
||
case class TableColumn( | ||
name: String, | ||
`type`: String | ||
`type`: ColumnType | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters