Skip to content

Commit cbe67db

Browse files
lolgabkeynmol
authored andcommitted
Update scala-native-jdbc and use insertReturning
1 parent 516bad2 commit cbe67db

File tree

2 files changed

+8
-35
lines changed

2 files changed

+8
-35
lines changed

project.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//> using nativeVersion 0.5.8
44

55
//> using dep com.github.lolgab::magnum::1.3.1
6-
//> using dep com.github.lolgab::scala-native-jdbc-sqlite::0.0.3
6+
//> using dep com.github.lolgab::scala-native-jdbc-sqlite::0.0.4
77
//> using dep com.indoorvivants::decline-derive::0.3.1
88
//> using dep tech.neander::cue4s::0.0.9
99
//> using dep com.indoorvivants::toml::0.3.0

src/db.scala

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,27 @@ package sniper
33
import com.augustnagro.magnum.*
44

55
case class SnippetAttributes(
6-
description: String,
7-
uid: Option[String] = None
6+
description: String
87
)
98

109
@Table(SqliteDbType, SqlNameMapper.CamelToSnakeCase)
1110
case class Snippet(
1211
@Id id: Long,
13-
description: String,
14-
uid: Option[String]
12+
description: String
1513
) derives DbCodec
1614

1715
case class SnippetFileAttributes(
1816
snippetId: Long,
1917
filename: String,
20-
code: String,
21-
uid: Option[String] = None
18+
code: String
2219
)
2320

2421
@Table(SqliteDbType, SqlNameMapper.CamelToSnakeCase)
2522
case class SnippetFile(
2623
@Id id: Long,
2724
snippetId: Long,
2825
filename: String,
29-
code: String,
30-
uid: Option[String]
26+
code: String
3127
) derives DbCodec
3228

3329
object SnippetFile:
@@ -47,18 +43,7 @@ class SnippetsDB private (using DbCon):
4743

4844
def add(attrs: SnippetAttributes): Snippet =
4945
val rand = randomString()
50-
snippetsRepo.insert(
51-
attrs.copy(uid = Some(rand))
52-
)
53-
// TODO: when sqlite-jdbc-native supports prepared statements with returning,
54-
// remove uid and all logic surrounding it
55-
val id = sql"select id from snippet where uid = $rand"
56-
.query[Long]
57-
.run()
58-
.headOption
59-
.getOrElse(sys.error("Snippet not found"))
60-
61-
snippetsRepo.findById(id).getOrElse(sys.error("Snippet not found"))
46+
snippetsRepo.insertReturning(attrs)
6247
end add
6348

6449
def delete(id: Long) =
@@ -75,23 +60,11 @@ class SnippetsDB private (using DbCon):
7560
snippetId: Long,
7661
files: Map[String, String]
7762
): Vector[SnippetFile] =
78-
val sorted = files.toList.sortBy(_._1)
79-
val uids = Vector.newBuilder[String]
80-
63+
val sorted = files.toVector.sortBy(_._1)
8164
sorted.map { case (filename, code) =>
8265
val attrs = SnippetFileAttributes(snippetId, filename, code)
83-
val uid = randomString()
84-
uids += uid
85-
snippetFilesRepo.insert(attrs.copy(uid = Some(uid)))
66+
snippetFilesRepo.insertReturning(attrs)
8667
}
87-
88-
// TODO: figure out how to do IN(...) queries
89-
uids.result.map: uid =>
90-
sql"select ${SnippetFile.Table.all} from ${SnippetFile.Table} where uid = $uid"
91-
.query[SnippetFile]
92-
.run()
93-
.headOption
94-
.getOrElse(sys.error("Snippet file not found"))
9568
end addFilesToSnippet
9669
end SnippetsDB
9770

0 commit comments

Comments
 (0)