Skip to content

Commit

Permalink
rename method, ignore NoticeResponse, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rolang committed Jan 28, 2024
1 parent e4c4ee1 commit ca56bc8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
12 changes: 6 additions & 6 deletions modules/core/shared/src/main/scala/Session.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ trait Session[F[_]] {
def execute[A](command: Command[A], args: A)(implicit ev: DummyImplicit): F[Completion] = execute(command)(args)

/**
* Execute any non-parameterized statement containing single or multi-query statements.
* Discard returned completitions and rows.
* Execute any non-parameterized statement containing single or multi-query statements,
* discarding returned completions and rows.
*/
def execute_(statement: Statement[Void]): F[Unit]
def executeDiscard(statement: Statement[Void]): F[Unit]

/**
* Prepares then caches a query, yielding a `PreparedQuery` which can be executed multiple
Expand Down Expand Up @@ -629,8 +629,8 @@ object Session {
override def execute(command: Command[Void]): F[Completion] =
proto.execute(command)

override def execute_(statement: Statement[Void]): F[Unit] =
proto.execute_(statement)
override def executeDiscard(statement: Statement[Void]): F[Unit] =
proto.executeDiscard(statement)

override def channel(name: Identifier): Channel[F, String, String] =
Channel.fromNameAndProtocol(name, proto)
Expand Down Expand Up @@ -710,7 +710,7 @@ object Session {

override def execute(command: Command[Void]): G[Completion] = fk(outer.execute(command))

override def execute_(statement: Statement[Void]): G[Unit] = fk(outer.execute_(statement))
override def executeDiscard(statement: Statement[Void]): G[Unit] = fk(outer.executeDiscard(statement))

override def execute[A](query: Query[Void,A]): G[List[A]] = fk(outer.execute(query))

Expand Down
8 changes: 4 additions & 4 deletions modules/core/shared/src/main/scala/net/Protocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ trait Protocol[F[_]] {
def execute[A](query: Query[Void, A], ty: Typer): F[List[A]]

/**
* Execute any non-parameterized statement containing single or multi-query statements.
* Discard returned completitions and rows.
* Execute any non-parameterized statement containing single or multi-query statements,
* discarding returned completions and rows.
*/
def execute_(statement: Statement[Void]): F[Unit]
def executeDiscard(statement: Statement[Void]): F[Unit]

/**
* Initiate the session. This must be the first thing you do. This is very basic at the moment.
Expand Down Expand Up @@ -252,7 +252,7 @@ object Protocol {
override def execute[B](query: Query[Void, B], ty: Typer): F[List[B]] =
protocol.Query[F](redactionStrategy).apply(query, ty)

override def execute_(statement: Statement[Void]): F[Unit] = protocol.Query[F](redactionStrategy).apply_(statement)
override def executeDiscard(statement: Statement[Void]): F[Unit] = protocol.Query[F](redactionStrategy).applyDiscard(statement)

override def startup(user: String, database: String, password: Option[String], parameters: Map[String, String]): F[Unit] =
protocol.Startup[F].apply(user, database, password, parameters)
Expand Down
17 changes: 3 additions & 14 deletions modules/core/shared/src/main/scala/net/protocol/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import skunk.exception.EmptyStatementException
trait Query[F[_]] {
def apply(command: Command[Void]): F[Completion]
def apply[B](query: skunk.Query[Void, B], ty: Typer): F[List[B]]
def apply_(statement: Statement[Void]): F[Unit]
def applyDiscard(statement: Statement[Void]): F[Unit]
}

object Query {
Expand Down Expand Up @@ -222,20 +222,9 @@ object Query {
case Some(e) => e.raiseError[F, Unit]
}

case RowDescription(_) | RowData(_) | CommandComplete(_) | EmptyQueryResponse =>
case RowDescription(_) | RowData(_) | CommandComplete(_) | EmptyQueryResponse | NoticeResponse(_) =>
finishUpDiscard(stmt, error)

case NoticeResponse(info) =>
error match {
case None =>
for {
hi <- history(Int.MaxValue)
err = new PostgresErrorException(stmt.sql, Some(stmt.origin), info, hi)
c <- finishUpDiscard(stmt, Some(err))
} yield c
case _ => finishUpDiscard(stmt, error)
}

case ErrorResponse(info) =>
error match {
case None =>
Expand All @@ -258,7 +247,7 @@ object Query {
finishCopyOut *> finishUpDiscard(stmt, error.orElse(new CopyNotSupportedException(stmt).some))
}

override def apply_(statement: Statement[Void]): F[Unit] =
override def applyDiscard(statement: Statement[Void]): F[Unit] =
exchange("query") { (span: Span[F]) =>
span.addAttribute(
Attribute("command.sql", statement.sql)
Expand Down
16 changes: 11 additions & 5 deletions modules/tests/shared/src/test/scala/MultipleStatementsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ class MultipleStatementsTest extends SkunkTest {
statements.foreach { case (q, c, any) =>
sessionTest(s"query: ${q.sql}") { s => s.execute(q).assertFailsWith[SkunkException] *> s.assertHealthy }
sessionTest(s"command: ${c.sql}") { s => s.execute(c).assertFailsWith[SkunkException] *> s.assertHealthy }
sessionTest(s"any discarded: ${any.sql}") { s => s.execute_(any).assertFailsWith[CopyNotSupportedException] *> s.assertHealthy }
sessionTest(s"any discarded: ${any.sql}") { s => s.executeDiscard(any).assertFailsWith[CopyNotSupportedException] *> s.assertHealthy }
}

// statements with no errors
List("select 1","commit","/* empty */")
List(
"""CREATE FUNCTION do_something() RETURNS integer AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;
SELECT do_something();
DROP FUNCTION do_something""", // query + command
"select 1", // query
"commit", // command
"/* empty */")
.permutations
.toList
.map(s => sql"#${s.intercalate(";")}".command)
.foreach { stmt =>
sessionTest(s"discarded no errors: ${stmt.sql}") { s =>
s.execute_(stmt) *> s.assertHealthy
s.executeDiscard(stmt) *> s.assertHealthy
}
}

Expand All @@ -50,11 +56,11 @@ class MultipleStatementsTest extends SkunkTest {

if (statements.indexOf(conflict) < statements.indexOf(copy))
sessionTest(s"discarded with postgres error: ${stmt.sql}")(s =>
s.execute_(stmt).assertFailsWith[PostgresErrorException] *> s.assertHealthy
s.executeDiscard(stmt).assertFailsWith[PostgresErrorException] *> s.assertHealthy
)
else
sessionTest(s"discarded with unsupported error: ${stmt.sql}")(s =>
s.execute_(stmt).assertFailsWith[CopyNotSupportedException] *> s.assertHealthy
s.executeDiscard(stmt).assertFailsWith[CopyNotSupportedException] *> s.assertHealthy
)
}
}
Expand Down

0 comments on commit ca56bc8

Please sign in to comment.