Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
737 views
in Technique[技术] by (71.8m points)

karate - as for "Rules for Embedded Expressions", the string concatenation does not work

I am fetching the data from database with SQL, I need pass a variable to the where clause, however, I find that the string concatenation doesn't work, even the official example

* def batchnum = "112344552"
* def getBatchIDSQL = '#("select id from sr_sendreceive where batchnum = " + batchnum)'
* print getBatchIDSQL
* def sendReceiveBatchid = db.readValue('#(getBatchIDSQL)')

Then, I tried the official example:

      # wrong !
      * def foo = 'hello #(name)'
      # right !
      * def foo1 = '#("hello " + name)'
      * print foo1

      * def name = 'test name'
      * def temp = 'hello ' + name
      * def foo2 = '#(temp)'
      * print foo2

The result is :

#("select id from sr_sendreceive where batchnum =" + batchnum)
#("hello " + name)
#(temp)
question from:https://stackoverflow.com/questions/65853194/how-to-set-a-variable-in-sql-statement-in-karateframework

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sorry, the docs are wrong. This works for non-JSON only for match. Like this:

* def batchnum = "112344552"
* def actual = 'select id from sr_sendreceive where batchnum = 112344552'
* match actual == '#("select id from sr_sendreceive where batchnum = " + batchnum)'

Inside JSON it will work:

* def foo = { bar: '#("select id from sr_sendreceive where batchnum = " + batchnum)' }
* print foo

Thanks for pointing this out, I will update the docs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...