Skip to content

Commit

Permalink
inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
tpolecat committed Jul 9, 2020
1 parent 61fc612 commit 3885565
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
35 changes: 14 additions & 21 deletions modules/core/src/main/scala/net/protocol/Bind.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,25 @@ object Bind {
_ <- send(Flush)
_ <- flatExpect {
case BindComplete => ().pure[F]
case ErrorResponse(info) => syncAndFail(statement, args, argsOrigin, info)
case ErrorResponse(info) =>
for {
hi <- history(Int.MaxValue)
_ <- send(Sync)
_ <- expect { case ReadyForQuery(_) => }
a <- PostgresErrorException.raiseError[F, Unit](
sql = statement.statement.sql,
sqlOrigin = Some(statement.statement.origin),
info = info,
history = hi,
arguments = statement.statement.encoder.types.zip(statement.statement.encoder.encode(args)),
argumentsOrigin = Some(argsOrigin)
)
} yield a
}
} yield pn
}
} { Close[F].apply }

def syncAndFail[A](
statement: PreparedStatement[F, A],
args: A,
argsOrigin: Origin,
info: Map[Char, String]
): F[Unit] =
for {
hi <- history(Int.MaxValue)
_ <- send(Sync)
_ <- expect { case ReadyForQuery(_) => }
a <- PostgresErrorException.raiseError[F, Unit](
sql = statement.statement.sql,
sqlOrigin = Some(statement.statement.origin),
info = info,
history = hi,
arguments = statement.statement.encoder.types.zip(statement.statement.encoder.encode(args)),
argumentsOrigin = Some(argsOrigin)
)
} yield a

}

}
8 changes: 3 additions & 5 deletions modules/core/src/main/scala/net/protocol/Describe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ object Describe {
def apply[F[_]: MonadError[?[_], Throwable]: Exchange: MessageSocket: Trace]: Describe[F] =
new Describe[F] {

// promote a command to a query ... weird case, see below
def promote[A](cmd: skunk.Command[A]): skunk.Query[A, skunk.Void] =
skunk.Query(cmd.sql, cmd.origin, cmd.encoder, skunk.Void.codec)

override def apply(cmd: skunk.Command[_], id: StatementId, ty: Typer): F[Unit] =
exchange("describe") {
for {
Expand All @@ -44,7 +40,9 @@ object Describe {
// types then *that's* the error we will raise; only if we decode it
// successfully we will raise the underlying error. Seems a little confusing
// but it's a very unlikely case so I think we're ok for the time being.
case Left(err) => UnknownOidException(promote(cmd), err, ty.strategy).raiseError[F, Unit]
case Left(err) =>
val promoted = skunk.Query(cmd.sql, cmd.origin, cmd.encoder, skunk.Void.codec)
UnknownOidException(promoted, err, ty.strategy).raiseError[F, Unit]
case Right(td) => UnexpectedRowsException(cmd, td).raiseError[F, Unit]
}
}
Expand Down
30 changes: 14 additions & 16 deletions modules/core/src/main/scala/net/protocol/Execute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@ object Execute {
_ <- send(Flush)
c <- flatExpect {
case CommandComplete(c) => send(Sync) *> expect { case ReadyForQuery(_) => c } // https://github.com/tpolecat/skunk/issues/210
case ErrorResponse(info) => syncAndFail[A](portal, info)
case ErrorResponse(info) =>
for {
hi <- history(Int.MaxValue)
_ <- send(Sync)
_ <- expect { case ReadyForQuery(_) => }
a <- new PostgresErrorException(
sql = portal.preparedCommand.command.sql,
sqlOrigin = Some(portal.preparedCommand.command.origin),
info = info,
history = hi,
arguments = portal.preparedCommand.command.encoder.types.zip(portal.preparedCommand.command.encoder.encode(portal.arguments)),
argumentsOrigin = Some(portal.argumentsOrigin)
).raiseError[F, Completion]
} yield a
}
} yield c
}
Expand All @@ -49,21 +62,6 @@ object Execute {
} yield rs
}

def syncAndFail[A](portal: Protocol.CommandPortal[F, A], info: Map[Char, String]): F[Completion] =
for {
hi <- history(Int.MaxValue)
_ <- send(Sync)
_ <- expect { case ReadyForQuery(_) => }
a <- new PostgresErrorException(
sql = portal.preparedCommand.command.sql,
sqlOrigin = Some(portal.preparedCommand.command.origin),
info = info,
history = hi,
arguments = portal.preparedCommand.command.encoder.types.zip(portal.preparedCommand.command.encoder.encode(portal.arguments)),
argumentsOrigin = Some(portal.argumentsOrigin)
).raiseError[F, Completion]
} yield a

}

}

0 comments on commit 3885565

Please sign in to comment.