SOAPUI& Groovy Scripts,一次性执行多条SQL语句

发布于 2024-09-05 06:46:00 字数 586 浏览 6 评论 0原文

我有一些soapUI测试,它们使用groovy脚本首先将一些数据插入表中

以前,我一直使用以下代码片段来执行此操作:

      def conn = context.dbConnEtopup
      conn.execute( "INSERT INTO A(ID, NAME) VALUES (1, "Johnny")" )

这工作正常,但是我有许多测试脚本现在执行类似(如果不相同)的 SQL 语句,因此我尝试通过从属性文件加载它来解决此问题,因此我的实际 SQL 语句仅位于一个位置,以便于编辑

但是,我的 SQL 语句尝试使用实际上是 2 个插入(或删除),因此加载的属性是:

DELETE * FROM TABLE_A; DELETE * FROM TABLE_B;

conn.execute() 无法处理 ;,这意味着我可以只使用第一个 DELETE 语句

我该如何解决这个问题?我不想单独加载每个属性并执行它们。理想情况下,我只想要一个属性,这样我就可以在将来添加更多删除语句

I've got some soapUI tests, that use groovy scripts to first insert some data into a table

Previously, I've been using the following snippet of code to do this :

      def conn = context.dbConnEtopup
      conn.execute( "INSERT INTO A(ID, NAME) VALUES (1, "Johnny")" )

This works fine, however I have many test scripts that now do a similar (if not the same) SQL statements, so I'm trying to approach this by loading it in from a properties file, so my actual SQL statement is only in one place, for easier editing

However, my SQL statement that I'm trying to use is actually 2 inserts(or deletes), so the property being loaded in is :

DELETE * FROM TABLE_A; DELETE * FROM TABLE_B;

conn.execute() can't handle the ;, which means I could only use the first DELETE statement

How can I get around this? I don't want to have to load each property separately and execute them. Ideally I just want one property, so I can add further delete statements in the future

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-09-12 06:46:00

一些 JDBC 驱动程序支持多个语句,然后可以通过 Groovy 的 Sql 类使用此功能,例如使用 MySql:

def props = [user: 'myuser', password: 'mypassword', allowMultiQueries: 'true'] as Properties
def url = 'jdbc:mysql://127.0.0.1:3306/mydb'
def driver = 'com.mysql.jdbc.Driver'
def sql = Sql.newInstance(url, props, driver)
sql.execute """
  insert into PERSON (id, firstname, lastname) values (1, 'Dierk', 'Koenig');
  insert into PERSON (id, firstname, lastname) values (2, 'Guillaume', 'Laforge');
  insert into PERSON (id, firstname, lastname) values (3, 'Jon', 'Skeet');
"""

Some JDBC drivers support multiple statements and this functionality will then be available via Groovy's Sql class, e.g. with MySql:

def props = [user: 'myuser', password: 'mypassword', allowMultiQueries: 'true'] as Properties
def url = 'jdbc:mysql://127.0.0.1:3306/mydb'
def driver = 'com.mysql.jdbc.Driver'
def sql = Sql.newInstance(url, props, driver)
sql.execute """
  insert into PERSON (id, firstname, lastname) values (1, 'Dierk', 'Koenig');
  insert into PERSON (id, firstname, lastname) values (2, 'Guillaume', 'Laforge');
  insert into PERSON (id, firstname, lastname) values (3, 'Jon', 'Skeet');
"""
匿名的好友 2024-09-12 06:46:00

难道你不能将它们存储在带有分号的属性文件中,然后在读取它们后将其删除,例如

String sqlProperty = // read SQL property from file
def statements = sqlProperty.split(";")

// Execute each statment using conn (an instance of groov.sql.Sql?)
statements.each { conn.execute(it);

Couldn't you just store them in the properties file with the semicolons, then remove them after reading them, e.g.

String sqlProperty = // read SQL property from file
def statements = sqlProperty.split(";")

// Execute each statment using conn (an instance of groov.sql.Sql?)
statements.each { conn.execute(it);
银河中√捞星星 2024-09-12 06:46:00

有时这还不够好。查看我的解决方案:从 Groovy 运行多个 SQL 语句

Sometimes that's not good enough. Check out my solution: Running multiple SQL statements from Groovy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文