From a46ae6949814d38394f2159444868015a5be9001 Mon Sep 17 00:00:00 2001 From: jihun Date: Sat, 5 Aug 2023 17:31:59 +0900 Subject: [PATCH 1/3] =?UTF-8?q?sqlite=20=EB=8D=B0=EC=9D=B4=ED=84=B0?= =?UTF-8?q?=EB=B2=A0=EC=9D=B4=EC=8A=A4=20=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bsp/sbt.json | 2 +- build.sbt | 26 +++++---- simple-jh/src/main/scala/DBSampleApp.scala | 61 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 simple-jh/src/main/scala/DBSampleApp.scala diff --git a/.bsp/sbt.json b/.bsp/sbt.json index 655098c..338800f 100644 --- a/.bsp/sbt.json +++ b/.bsp/sbt.json @@ -1 +1 @@ -{"name":"sbt","version":"1.8.3","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["C:\\Program Files\\Java\\jdk-20/bin/java","-Xms100m","-Xmx100m","-classpath","C:/Users/admin/AppData/Roaming/JetBrains/IdeaIC2023.1/plugins/Scala/launcher/sbt-launch.jar","-Dsbt.script=C:\\Users\\admin\\AppData\\Local\\Coursier\\data\\bin\\sbt.bat","xsbt.boot.Boot","-bsp"]} \ No newline at end of file +{"name":"sbt","version":"1.8.3","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["C:\\Users\\jihun\\.jdks\\openjdk-20.0.2/bin/java","-Xms100m","-Xmx100m","-classpath","C:/Users/jihun/AppData/Roaming/JetBrains/IdeaIC2023.2/plugins/Scala/launcher/sbt-launch.jar","xsbt.boot.Boot","-bsp","--sbt-launch-jar=C:/Users/jihun/AppData/Roaming/JetBrains/IdeaIC2023.2/plugins/Scala/launcher/sbt-launch.jar"]} \ No newline at end of file diff --git a/build.sbt b/build.sbt index 4d32508..f9b19c3 100644 --- a/build.sbt +++ b/build.sbt @@ -120,20 +120,28 @@ lazy val `forecast-cheese` = project lazy val `simple-jh` = project .settings(sharedSettings) - -lazy val `forecast` = project.dependsOn(`read-file`) - .settings(sharedSettings) .settings( libraryDependencies ++= Seq( - "dev.zio" %% "zio" % zioVersion, - "com.lihaoyi" %% "ujson" % "3.0.0", - "com.softwaremill.sttp.client3" %% "core" % "3.8.16", - "com.softwaremill.sttp.client3" %% "zio-json" % "3.3.9", - "dev.zio" %% "zio-http" % "3.0.0-RC2", - "org.scalameta" %% "scalafmt-core" % "2.7.5" + "org.tpolecat" %% "doobie-core" % "1.0.0-RC2", + "io.github.gaelrenoux" %% "tranzactio" % "4.1.0", + "org.xerial" % "sqlite-jdbc" % "3.40.1.0" ) ) + +//lazy val `forecast` = project.dependsOn(`read-file`) +// .settings(sharedSettings) +// .settings( +// libraryDependencies ++= Seq( +// "dev.zio" %% "zio" % zioVersion, +// "com.lihaoyi" %% "ujson" % "3.0.0", +// "com.softwaremill.sttp.client3" %% "core" % "3.8.16", +// "com.softwaremill.sttp.client3" %% "zio-json" % "3.3.9", +// "dev.zio" %% "zio-http" % "3.0.0-RC2", +// "org.scalameta" %% "scalafmt-core" % "2.7.5" +// ) +// ) + lazy val `forecast-subway` = project .settings(sharedSettings) .settings( diff --git a/simple-jh/src/main/scala/DBSampleApp.scala b/simple-jh/src/main/scala/DBSampleApp.scala new file mode 100644 index 0000000..20c9d28 --- /dev/null +++ b/simple-jh/src/main/scala/DBSampleApp.scala @@ -0,0 +1,61 @@ + 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 test_table""".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 = "C:/Users/jihun/coding/ZIOPROJ/ZIODB/ZIO_TEST_DB.db" + 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 + ) + ) + ) + } \ No newline at end of file From 82da06ba873b39acdb658851118cb44da1e2e78d Mon Sep 17 00:00:00 2001 From: jihun Date: Sun, 6 Aug 2023 13:56:27 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=EC=A2=80=EB=B9=84=20=EA=B2=8C=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-jh/src/main/scala/ZombieGame.scala | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 simple-jh/src/main/scala/ZombieGame.scala diff --git a/simple-jh/src/main/scala/ZombieGame.scala b/simple-jh/src/main/scala/ZombieGame.scala new file mode 100644 index 0000000..2ed1bd5 --- /dev/null +++ b/simple-jh/src/main/scala/ZombieGame.scala @@ -0,0 +1,67 @@ +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 { + 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 = "C:/Users/jihun/coding/ZIOPROJ/ZIODB/ZIO_TEST_DB.db" + s"jdbc:sqlite:$path" + } + + private val conn = ZLayer( + ZIO.attempt( + java.sql.DriverManager.getConnection( + sqlite + ) + ) + ) +} From 4ca5ca2cf893e1a29ad1bfa2816c946b725f722c Mon Sep 17 00:00:00 2001 From: jihun Date: Tue, 8 Aug 2023 23:51:08 +0900 Subject: [PATCH 3/3] =?UTF-8?q?sbt.json=20=EC=82=AD=EC=A0=9C=20+=20SQLite?= =?UTF-8?q?=20=EA=B2=BD=EB=A1=9C=20=ED=95=98=EB=93=9C=EC=BD=94=EB=94=A9=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bsp/sbt.json | 1 - simple-jh/src/main/scala/DBSampleApp.scala | 5 +++-- simple-jh/src/main/scala/SimpleUtil.scala | 3 +++ simple-jh/src/main/scala/ZombieGame.scala | 4 +++- 4 files changed, 9 insertions(+), 4 deletions(-) delete mode 100644 .bsp/sbt.json create mode 100644 simple-jh/src/main/scala/SimpleUtil.scala diff --git a/.bsp/sbt.json b/.bsp/sbt.json deleted file mode 100644 index 338800f..0000000 --- a/.bsp/sbt.json +++ /dev/null @@ -1 +0,0 @@ -{"name":"sbt","version":"1.8.3","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["C:\\Users\\jihun\\.jdks\\openjdk-20.0.2/bin/java","-Xms100m","-Xmx100m","-classpath","C:/Users/jihun/AppData/Roaming/JetBrains/IdeaIC2023.2/plugins/Scala/launcher/sbt-launch.jar","xsbt.boot.Boot","-bsp","--sbt-launch-jar=C:/Users/jihun/AppData/Roaming/JetBrains/IdeaIC2023.2/plugins/Scala/launcher/sbt-launch.jar"]} \ No newline at end of file diff --git a/simple-jh/src/main/scala/DBSampleApp.scala b/simple-jh/src/main/scala/DBSampleApp.scala index 20c9d28..7c9d939 100644 --- a/simple-jh/src/main/scala/DBSampleApp.scala +++ b/simple-jh/src/main/scala/DBSampleApp.scala @@ -26,7 +26,7 @@ .transactionOrWiden(for { res <- tzio { sql"""|select name, hobby - |from test_table""".stripMargin + |from zombie""".stripMargin .query[TestTableRow] .to[List] } @@ -40,9 +40,10 @@ ) val sqlite = locally { - val path = "C:/Users/jihun/coding/ZIOPROJ/ZIODB/ZIO_TEST_DB.db" + val path = SimpleUtil.path s"jdbc:sqlite:$path" } + val postgres = locally { val path = "localhost:5432" val name = "postgres" diff --git a/simple-jh/src/main/scala/SimpleUtil.scala b/simple-jh/src/main/scala/SimpleUtil.scala new file mode 100644 index 0000000..dc70787 --- /dev/null +++ b/simple-jh/src/main/scala/SimpleUtil.scala @@ -0,0 +1,3 @@ +object SimpleUtil { + +} diff --git a/simple-jh/src/main/scala/ZombieGame.scala b/simple-jh/src/main/scala/ZombieGame.scala index 2ed1bd5..dce13d9 100644 --- a/simple-jh/src/main/scala/ZombieGame.scala +++ b/simple-jh/src/main/scala/ZombieGame.scala @@ -7,6 +7,8 @@ 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("") @@ -53,7 +55,7 @@ object ZombieGame extends ZIOAppDefault { ) val sqlite = locally { - val path = "C:/Users/jihun/coding/ZIOPROJ/ZIODB/ZIO_TEST_DB.db" + val path = SimpleUtil.path s"jdbc:sqlite:$path" }