All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.galaxio.gatling.jdbc.actions.DBInsertAction.scala Maven / Gradle / Ivy

package org.galaxio.gatling.jdbc.actions

import io.gatling.commons.stats.{KO, OK}
import io.gatling.commons.validation._
import io.gatling.core.action.{Action, ChainableAction}
import io.gatling.core.session.{Expression, Session}
import io.gatling.core.stats.StatsEngine
import io.gatling.core.structure.ScenarioContext
import io.gatling.core.util.NameGen
import org.galaxio.gatling.jdbc.db.SQL

case class DBInsertAction(
    requestName: Expression[String],
    tableName: Expression[String],
    columns: Seq[String],
    next: Action,
    ctx: ScenarioContext,
    sessionValues: Seq[(String, Expression[Any])],
) extends ChainableAction with NameGen with ActionBase {
  override def name: String = genName("jdbcInsertAction")

  override def execute(session: Session): Unit =
    (for {
      rn        <- requestName(session)
      tName     <- tableName(session)
      iParams   <- sessionValues
                     .foldLeft(Map[String, Any]().success) { case (r, (k, v)) =>
                       r.flatMap(m => v(session).map(rv => m + (k -> rv)))
                     }
      sql       <- SQL(s"INSERT INTO $tName (${columns.mkString(",")}) VALUES(${columns.map(s => s"{$s}").mkString(",")})")
                     .withParamsMap(iParams)
                     .success
      startTime <- ctx.coreComponents.clock.nowMillis.success

    } yield dbClient
      .executeUpdate(sql.sql, sql.params)(
        _ => executeNext(session, startTime, ctx.coreComponents.clock.nowMillis, OK, next, rn, None, None),
        e =>
          executeNext(session, startTime, ctx.coreComponents.clock.nowMillis, KO, next, rn, Some("ERROR"), Some(e.getMessage)),
      ))
      .onFailure(m =>
        requestName(session).map { rn =>
          ctx.coreComponents.statsEngine.logRequestCrash(session.scenario, session.groups, rn, m)
          executeNext(
            session,
            ctx.coreComponents.clock.nowMillis,
            ctx.coreComponents.clock.nowMillis,
            KO,
            next,
            rn,
            Some("ERROR"),
            Some(m),
          )
        },
      )

  override def statsEngine: StatsEngine = ctx.coreComponents.statsEngine
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy