Skip to main content

SQL execution

<name> is defined in the <dbName> database by sql "<selectStmt>"

Associates the result of an SQL database query to the given name in the global scope. The value is resolved every time the name is referenced.

Where

  • <name> is the name to bind the SQL query result to
  • <dbName> the name of the configured database to execute the query on
  • <selectStmt> is the SQL query that will return the value to assign (can be in DocString position)

Configuration

  • The GWEN_CLASSPATH environment variable must be set to include a path to the JDBC driver JAR
  • A gwen.db.<dbName>.driver setting must be set to the name of the JDBC driver implementation class
  • A gwen.db.<dbName>.url setting must be set to the JDBC URL of the database

Optionally accepts the @Eager, @Lazy or @Masked annotations.

  • @Eager immediately evaluates the <expression> and binds the result to <name>
  • @Lazy evaluates the <expression> and binds the result to <name> when it is first referenced
  • In the absence of either of the above annotations, <expression> is evaluated every time <name> is referenced
  • @Masked masks the captured value to prevent it from being logged as clear text

Example

  Given the email address is defined in the feedback database by sql
"""
select email from comments
"""
Then the email address should be "some.person@example.com"

Set GWEN_CLASSPATH variable in the environment to include the JDBC driver JAR

export GWEN_CLASSPATH=/path-to-driver/mysql-connector-java-5.1.40-bin.jar

Configure the database in your settings. In this example, <dbName>=feedback

gwen.db.feedback.driver = com.mysql.jdbc.Driver
gwen.db.feedback.url = jdbc:mysql://localhost/feedback?user=myUser&amp;password=myPassword&amp;
I update the <dbName> database by sql "<updateStmt>"

Executes an SQL udpate statement on a configured database.

Where

  • <dbName> the name of the configured database to run the update statement on
  • <updateStmt> is the SQL update statement to execute (can be in DocString position)

Returns

  • <dbName> rows affected = the number of rows affected by the update

Configuration

  • The GWEN_CLASSPATH environment variable must be set to include a path to the JDBC driver JAR
  • A gwen.db.<dbName>.driver setting must be set to the name of the JDBC driver implementation class
  • A gwen.db.<dbName>.url setting must be set to the JDBC URL of the database

Example

  When I update the feedback database by sql
"""
update comments set email='new@example.com' where email='old@example.com'
"""
Then feedback rows affected should be "1"

Set GWEN_CLASSPATH variable in the environment to include the JDBC driver JAR

export GWEN_CLASSPATH=/path-to-driver/mysql-connector-java-5.1.40-bin.jar

Configure the database in your settings. In this example, <dbName>=feedback

gwen.db.feedback.driver = com.mysql.jdbc.Driver
gwen.db.feedback.url = jdbc:mysql://localhost/feedback?user=myUser&amp;password=myPassword&amp;