-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Support for generated keys - Fix broken string interpolation
- Loading branch information
Showing
7 changed files
with
144 additions
and
13 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
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 |
---|---|---|
@@ -1,10 +1,38 @@ | ||
package usql | ||
|
||
import usql.Update.SqlResultMissingGenerated | ||
|
||
import java.sql.SQLException | ||
import scala.util.Using | ||
|
||
/** Encapsulates an update statement */ | ||
case class Update(sql: SqlBase) { | ||
|
||
/** Run the update statement */ | ||
def run()(using c: ConnectionProvider): Int = { | ||
sql.withPreparedStatement(_.executeUpdate()) | ||
} | ||
|
||
/** | ||
* Run the update statement and get generated values. See [[java.sql.PreparedStatement.getGeneratedKeys()]] | ||
*/ | ||
def runAndGetGenerated[T]()(using d: ResultRowDecoder[T], c: ConnectionProvider): T = { | ||
given sp: StatementPreparator = StatementPreparator.withGeneratedKeys | ||
sql.withPreparedStatement { statement => | ||
statement.executeUpdate() | ||
Using.resource(statement.getGeneratedKeys) { resultSet => | ||
if (resultSet.next()) { | ||
d.parseRow(resultSet) | ||
} else { | ||
throw new SqlResultMissingGenerated("Missing row for getGeneratedKeys") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
object Update { | ||
|
||
/** Exception thrown if the result set has no generated data. */ | ||
class SqlResultMissingGenerated(msg: String) extends SQLException(msg) | ||
} |
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,53 @@ | ||
package usql | ||
|
||
import usql.dao.{KeyedCrudBase, SqlColumnar, SqlTabular} | ||
import usql.util.TestBaseWithH2 | ||
|
||
class AutoGeneratedUpdateTest extends TestBaseWithH2 { | ||
|
||
override protected def baseSql: String = | ||
""" | ||
|CREATE TABLE tenant ( | ||
| id SERIAL NOT NULL PRIMARY KEY, | ||
| name TEXT | ||
|); | ||
|""".stripMargin | ||
|
||
case class Tenant( | ||
id: Int, | ||
name: Option[String] | ||
) derives SqlTabular | ||
|
||
object Tenant extends KeyedCrudBase[Int, Tenant] { | ||
override val keyColumn: SqlIdentifier = "id" | ||
|
||
override def keyOf(value: Tenant): Int = value.id | ||
|
||
override lazy val tabular: SqlTabular[Tenant] = summon | ||
} | ||
|
||
it should "be possible to insert values" in { | ||
Tenant.findAll() shouldBe empty | ||
val sample = Tenant(1, Some("Hello World")) | ||
Tenant.insert(sample) | ||
Tenant.findAll() shouldBe Seq(sample) | ||
} | ||
|
||
it should "be possible to use auto generated keys" in { | ||
sql"INSERT INTO tenant (name) VALUES (${"Alice"})".update.run() | ||
sql"INSERT INTO tenant (name) VALUES (${"Bob"})".update.run() | ||
Tenant.findAll() should contain theSameElementsAs Seq( | ||
Tenant(1, Some("Alice")), | ||
Tenant(2, Some("Bob")) | ||
) | ||
} | ||
|
||
it should "be possible to return auto generated keys" in { | ||
val id1 = sql"INSERT INTO tenant (name) VALUES (${"Alice"})".update.runAndGetGenerated[Int]() | ||
val id2 = sql"INSERT INTO tenant (name) VALUES (${"Bob"})".update.runAndGetGenerated[Int]() | ||
Tenant.findAll() should contain theSameElementsAs Seq( | ||
Tenant(id1, Some("Alice")), | ||
Tenant(id2, Some("Bob")) | ||
) | ||
} | ||
} |
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