简体   繁体   中英

Fitnesse and dbFit: how to escape colons in SQL queries

I've a problem with escaping colons and dashes in SQL queries when I use dbFit with Fitnesse.

Such statement doesn't work:

!|Query|select to_char(my_birthday,'YYYY-MM-DD HH24:MI:SI') from family|

I need to replace colons and dashes with some other acceptable characters, ex.

!|Query|select to_char(my_birthday,'YYYY_MM_DD HH24_MI_SI') from family|

Do you know how to solve it properly without using the 2nd approach ?

Cheers, foxrafi

I think this is what you need. From http://dbfit.github.io/dbfit/docs/reference.html

Avoiding parameter mapping

If you want to prevent DbFit from mapping parameters to bind variables (eg to execute a stored procedure definition that contains the @ symbol in Sql Server), disable bind symbols option before running the query.

|set option|bind symbols|false|

|execute| insert into users (name, username) values ('@hey','uuu')|

|query|select * from users| |name|username| |@hey|uuu|

Remember to re-enable the option after the query is executed. You can use the same trick with the Execute command.

In addition to Mike's answer, you can also solve this by using bind parameters. This is useful if you have to use bind parameters at other places in the same query.

!|DatabaseEnvironment|ORACLE|
|Connect|${HOSTNAME}|${USERNAME}|${PASSWORD}|

!|Query|!-select current_timestamp ts from dual-!|
|ts?|
|>>my_birthday_ts|

|set parameter|my_birthday_ts| <<my_birthday_ts|
#Set bind parameter :MI to string ':MI'
|set parameter|MI|:MI|
# and do it in the same way with :SS.
|set parameter|SS|:SS|

!|Query|!-select to_char(:my_birthday_ts, 'YYYY-MM-DD HH24'|| :MI || :SS) bds from dual-!|
|bds?|
|>>birthday_string|

Note that you have to use !- -! around your query, otherwise FitNesse will expand the concatenation operator to table cells. The main drawback of this manner is that you cannot use ordinary FitNesse variables (${varname}) in the query.

!|DatabaseEnvironment|ORACLE|
|Connect|${HOSTNAME}|${USERNAME}|${PASSWORD}|

!|Query|!-select current_timestamp ts from dual-!|
|ts?|
|>>my_birthday_ts|


!|Query|!-select to_char(:my_birthday_ts, 'YYYY-MM-DD HH24:'||'MI:'||'SS) bds from dual-!|
|bds?|
|>>birthday_string|

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM