-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from jihun315016/develop
DB CRUD(Zombie Game)
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import io.github.gaelrenoux.tranzactio.ConnectionSource | ||
import io.github.gaelrenoux.tranzactio.doobie.{Database, tzio} | ||
import zio._ | ||
import doobie._ | ||
import doobie.implicits._ | ||
|
||
case class TestTableRow(name: String, hobby: String) | ||
|
||
object DBSampleApp extends ZIOAppDefault { | ||
|
||
def insertTableRow(row: TestTableRow): ZIO[Database, Throwable, Int] = { | ||
val insertQuery = tzio { | ||
sql"""|insert into test_table (name, hobby) | ||
|values (${row.name}, ${row.hobby})""".stripMargin.update.run | ||
} | ||
|
||
val db = ZIO.service[Database] | ||
db.flatMap(database => database.transactionOrWiden(insertQuery)) | ||
} | ||
|
||
val prog = for { | ||
_ <- ZIO.unit | ||
database <- ZIO.service[Database] | ||
_ <- insertTableRow(TestTableRow("John", "Skiing")) | ||
rows <- database | ||
.transactionOrWiden(for { | ||
res <- tzio { | ||
sql"""|select name, hobby | ||
|from zombie""".stripMargin | ||
.query[TestTableRow] | ||
.to[List] | ||
} | ||
} yield res) | ||
|
||
_ <- zio.Console.printLine(rows) | ||
|
||
} yield () | ||
override def run = prog.provide( | ||
conn >>> ConnectionSource.fromConnection >>> Database.fromConnectionSource | ||
) | ||
|
||
val sqlite = locally { | ||
val path = SimpleUtil.path | ||
s"jdbc:sqlite:$path" | ||
} | ||
|
||
val postgres = locally { | ||
val path = "localhost:5432" | ||
val name = "postgres" | ||
val user = "postgres" | ||
val password = "1q2w3e4r" | ||
s"jdbc:postgresql://$path/$name?user=$user&password=$password" | ||
} | ||
|
||
private val conn = ZLayer( | ||
ZIO.attempt( | ||
java.sql.DriverManager.getConnection( | ||
sqlite | ||
) | ||
) | ||
) | ||
} |
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,3 @@ | ||
object SimpleUtil { | ||
|
||
} |
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,69 @@ | ||
import io.github.gaelrenoux.tranzactio.ConnectionSource | ||
import io.github.gaelrenoux.tranzactio.doobie.{Database, tzio} | ||
import zio._ | ||
import doobie._ | ||
import doobie.implicits._ | ||
|
||
case class Zombie(name: String) | ||
|
||
object ZombieGame extends ZIOAppDefault { | ||
//val config = ScalafmtConfig.fromHoconFile("./scalafmt.conf") | ||
|
||
def getTargetName(rows: List[Zombie], choice: Integer) = for { | ||
result <- ZIO.attempt(rows(choice).name) | ||
.catchAll(cause => ZIO.succeed("") | ||
) | ||
} yield result | ||
|
||
val prog = for { | ||
_ <- ZIO.unit | ||
database <- ZIO.service[Database] | ||
rows <- database | ||
.transactionOrWiden(for { | ||
res <- tzio { | ||
sql"""|select name | ||
|from zombie""".stripMargin | ||
.query[Zombie] | ||
.to[List] | ||
} | ||
} yield res) | ||
|
||
_ <- zio.Console.printLine(rows) | ||
|
||
input <- zio.Console.readLine(s"좀비 번호 입력(1~${rows.size}) : ") | ||
nInput = Integer.parseInt(input) - 1 | ||
|
||
target <- getTargetName(rows, nInput) | ||
_ <- target match { | ||
case "" => zio.Console.printLine("좀비 무서워서 도망갔다!") | ||
case _ => for { | ||
_ <- database | ||
.transactionOrWiden(for { | ||
res <- tzio { | ||
sql"""|delete from zombie where name = ${target}""".stripMargin | ||
.update | ||
.run | ||
} | ||
} yield res) | ||
_ <- zio.Console.printLine(s"${target}을 손봐줬다!") | ||
} yield () | ||
} | ||
} yield () | ||
|
||
override def run = prog.provide( | ||
conn >>> ConnectionSource.fromConnection >>> Database.fromConnectionSource | ||
) | ||
|
||
val sqlite = locally { | ||
val path = SimpleUtil.path | ||
s"jdbc:sqlite:$path" | ||
} | ||
|
||
private val conn = ZLayer( | ||
ZIO.attempt( | ||
java.sql.DriverManager.getConnection( | ||
sqlite | ||
) | ||
) | ||
) | ||
} |