SOAPUI & Groovy Scripts, executing multiple SQL statements in one go

狂风中的少年 提交于 2019-12-31 01:59:10

问题


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


回答1:


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);



回答2:


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');
"""



回答3:


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



来源:https://stackoverflow.com/questions/3024490/soapui-groovy-scripts-executing-multiple-sql-statements-in-one-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!