From 4e39e4cb8f6c750c695d086ea355b2722ff7008d Mon Sep 17 00:00:00 2001 From: six42 Date: Sat, 28 Apr 2018 17:16:47 +0200 Subject: [PATCH] Add support for #null# values in defaults and scripts Bug Fix of #null# handling see details in FitNesseRoot/PlugIns/JdbcSlim/UserGuide/9NullTrailingSpacesAndEmptyStrings.wiki --- .../9NullTrailingSpacesAndEmptyStrings.wiki | 140 +++++++++++------- .../HtwoTest/ScenarioLibrary.wiki | 3 - .../HtwoTest/SuiteSetUp.wiki | 3 - build.gradle | 2 +- .../six42/fitnesse/jdbcslim/SQLCommand.java | 5 +- .../six42/fitnesse/jdbcslim/SQLStatement.java | 6 +- 6 files changed, 95 insertions(+), 64 deletions(-) diff --git a/FitNesseRoot/PlugIns/JdbcSlim/UserGuide/9NullTrailingSpacesAndEmptyStrings.wiki b/FitNesseRoot/PlugIns/JdbcSlim/UserGuide/9NullTrailingSpacesAndEmptyStrings.wiki index ffc5c9f..6ad4a69 100644 --- a/FitNesseRoot/PlugIns/JdbcSlim/UserGuide/9NullTrailingSpacesAndEmptyStrings.wiki +++ b/FitNesseRoot/PlugIns/JdbcSlim/UserGuide/9NullTrailingSpacesAndEmptyStrings.wiki @@ -1,75 +1,107 @@ --- Test --- -#Copyright (C) 2015 by six42, All rights reserved. Contact the author via http://github.com/six42 -# +#Copyright (C) 2015-2018 by six42, All rights reserved. Contact the author via http://github.com/six42 -To insert or update '''null''' values define input parameters and use "?" syntax. +!4 To insert or update '''null''' values define input parameters and use "?" syntax. Specify null as '''#null#''' -To insert leading and trailing Spaces use the '''!-!- Denver -!''''''-!''' syntax +!4 To insert leading and trailing Spaces use the '''!-!- text -!-!''' syntax +The text ''Denver'' below has 5 leading and two trailing spaces +The text ''Bob'' below has 5 trailing spaces -!|Define Properties|inParameters| -|key |value | -|Name |I:1:12:0 | +Inline Parameter +!| SQLCommand | TestDatabase | !-insert into TestData +values (DEFAULT, ?{Name:12=more}, '%Phone%', '%City%', '%Profession%')-! | !-dbgetgeneratedkeys +-! | +| SCOPE_IDENTITY()? | City | Name | Phone | Profession | Count? | +| $ID3= | Paris | #null# | 112 | | 1 | +| $ID2= | !- Denver -! | !-Bob -! | 6789 | | 1 | -!|SQLCommand|TestDatabase|!-insert into TestData -values (DEFAULT, ?, '%Phone%', '%City%', '%Profession%')-!|!-dbgetgeneratedkeys -dbQueryParameters=inParameters-!| -|SCOPE_IDENTITY()?|City |Name|Phone|Profession|Count?| -|$ID1= |Berlin|#null#|12345| |1 | -|$ID2= |!- Denver-!|!-Bob -! |6789 | |1 | +!*** Alternative locations for null +Inline default value as null +Inline default value with leading and trailing spaces -To test '''null''' values in output specify null as '''#null#''' +!| SQLCommand | TestDatabase | !-insert into TestData +values (DEFAULT, ?{Name:12=#null#}, '%Phone%', ?{City:12= Kathmandu }, '%Profession%')-! | !-dbgetgeneratedkeys +-! | +| SCOPE_IDENTITY()? | Phone | Profession | Count? | +| $ID4= | 009771 | Sherpa | 1 | -To test leading and trailing spaces use the '''!-!- text -!''''''-!''' syntax or regex '''=~/\A text \z/''' - -To test for an empty string use regex '''=~/\A\z/''' -!note Define this pattern in a variable it will likely changes in the future to something more readable -!define EmptyString {=~/\A\z/} - -!|SQLCommand|TestDatabase|select City, Name, Name!-||-! City as CityName, Profession from TestData where ID ='%ID%'| -|ID|City? |Name?|CityName?| Profession?| -|$ID1 |=~/\ABerlin\z/|#null#|#null#|| -|$ID2 |=~/\A Denver\z/|!-Bob -! | Bob Denver | ${EmptyString}| - - -if you must insert the string #null# in the database you can define a different null pattern with the property '''inputNullString''' - -!|Define Properties|inParameters| -|key |value | -|Name |I:1:12:0 | -|City |I:2:12:0 | - -!|SQLCommand|TestDatabase|update TestData set Name = ?, City = ? where ID ='%ID%'| !-inputNullString=--null-- -dbQueryParameters=inParameters-!| -|ID|City|Name|Count?| -|$ID1 |--null--|#null#|1| +Parameters in Properties +!| Define Properties | inParameters | +| key | value | +| Name | I:1:12:0 | +| City | I:2:12:0 | -!|SQLCommand|TestDatabase|select City, Name, length(City) as CityLength, length(Name) as NameLength from TestData where ID ='%ID%'| -|ID|City?|Name?|CityLength?|NameLength?| -|$ID1 |#null#|#null#|#null#|6| +!| SQLCommand | TestDatabase | !-insert into TestData +values (DEFAULT, ?, '%Phone%', ?, '%Profession%')-! | !-dbgetgeneratedkeys +dbQueryParameters=inParameters-! | +| SCOPE_IDENTITY()? | City | Name | Phone | Profession | Count? | +| $ID1= | Berlin | #null# | 12345 | | 1 | -You can also change the output format for null values with the property '''outputNullString''' +null as default in Properties +leading and trailing spaces are lost! -!|SQLCommand|TestDatabase|select City, Name, length(City) as CityLength, length(Name) as NameLength from TestData where ID ='%ID%'| outputNullString=++null++ | -|ID|City?|Name?|CityLength?|NameLength?| -|$ID1 |++null++|#null#|++null++|6| - - - -!*< Cleanup - -!| SQLCommand|TestDatabase|delete TestData where ID ='%ID%'| -|ID |Count? | -|$ID1 |1 | -|$ID2 |1 | +!| Define Properties | myDefaults | +| key | value | +| Name | #null# | +| City | !- Pune -! | +!| SQLCommand | TestDatabase | !-insert into TestData +values (DEFAULT, ?, '%Phone%', ?, '%Profession%')-! | !-dbgetgeneratedkeys +dbQueryParameters=inParameters +inputDefaults=myDefaults-! | +| SCOPE_IDENTITY()? | Phone | Profession | Count? | +| $ID0= | 0091 | | 1 | *! +!4 To test '''null''' values in output specify null as '''#null#''' +!4 To test leading and trailing spaces use the '''!-!- text -!''''''-!''' syntax or regex '''=~/\A text \z/'' +!4 To test for an empty string use regex '''=~/\A\z/''' +!note Define this pattern in a variable it will likely changes in the future to something more readable +!define EmptyString {=~/\A\z/} +!| SQLCommand | TestDatabase | select City, Name, length(City) as CityLength, length(Name) as NameLength, Profession from TestData where ID ='%ID%' | +| ID | City? | CityLength? | Name? | NameLength? | Profession? | +| $ID1 | =~/\ABerlin\z/ | 6 | #null# | #null# | | +| $ID2 | =~/\A Denver \z/ | 13 | !-Bob -! | 8 | ${EmptyString} | +| $ID3 | =~/\AParis\z/ | 5 | #null# | #null# | | +| $ID4 | !- Kathmandu -! | 11 | #null# | #null# | Sherpa | +| $ID0 | Pune | 4 | #null# | #null# | | + +!2 Configuration options +!4 if you must insert the string #null# in the database you can define a different null pattern with the property '''inputNullString''' +!| Define Properties | inParameters | +| key | value | +| Name | I:1:12:0 | +| City | I:2:12:0 | + +!| SQLCommand | TestDatabase | update TestData set Name = ?{Name:12}, City = ? where ID ='%ID%' | !-inputNullString=--null-- +dbQueryParameters=inParameters-! | +| ID | City | Name | Count? | +| $ID2 | --null-- | #null# | 1 | + +!| SQLCommand | TestDatabase | select City, Name, length(City) as CityLength, length(Name) as NameLength from TestData where ID ='%ID%' | +| ID | City? | CityLength? | Name? | NameLength? | +| $ID2 | #null# | #null# | #null# | 6 | + +!4 You can also change the output format for null values with the property '''outputNullString''' +!| SQLCommand | TestDatabase | select City, Name, length(City) as CityLength, length(Name) as NameLength from TestData where ID ='%ID%' | outputNullString=++null++ | +| ID | City? | CityLength? | Name? | NameLength? | +| $ID2 | ++null++ | ++null++ | #null# | 6 | + +!***< Cleanup + +!| SQLCommand | TestDatabase | delete TestData where ID ='%ID%' | +| ID | Count? | +| $ID0 | 1 | +| $ID1 | 1 | +| $ID2 | 1 | +| $ID3 | 1 | +| $ID4 | 1 | +*! !contents -R2 -g -p -f -h - diff --git a/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/ScenarioLibrary.wiki b/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/ScenarioLibrary.wiki index bcba33a..1e3f23d 100644 --- a/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/ScenarioLibrary.wiki +++ b/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/ScenarioLibrary.wiki @@ -1,6 +1,3 @@ ---- -LastModifyingUser: six42 ---- #Copyright (C) 2015 by six42, All rights reserved. Contact the author via http://github.com/six42 # !contents -R2 -g -p -f -h diff --git a/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/SuiteSetUp.wiki b/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/SuiteSetUp.wiki index f86e149..f8f5afa 100644 --- a/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/SuiteSetUp.wiki +++ b/FitNesseRoot/PlugIns/JdbcSlim/XxTestMoreDrivers/HtwoTest/SuiteSetUp.wiki @@ -1,6 +1,3 @@ ---- -LastModifyingUser: six42 ---- #Copyright (C) 2015 by six42, All rights reserved. Contact the author via http://github.com/six42 !*> Setup jdbcSlim diff --git a/build.gradle b/build.gradle index 3f16c85..fe3b52d 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ apply plugin: 'signing' apply plugin: 'eclipse' group 'com.github.six42' -version = '1.2.1' +version = '1.2.3' //version = new Date().format('yyyyMMdd') println "Building ${project.name} v${project.version}..." diff --git a/src/main/java/six42/fitnesse/jdbcslim/SQLCommand.java b/src/main/java/six42/fitnesse/jdbcslim/SQLCommand.java index fa745eb..0c525d4 100644 --- a/src/main/java/six42/fitnesse/jdbcslim/SQLCommand.java +++ b/src/main/java/six42/fitnesse/jdbcslim/SQLCommand.java @@ -106,9 +106,10 @@ private List> dbExecute(String sqlCommand) throws SQLException{ String updateCountHeaderName = Properties().getPropertyOrDefault(ConfigurationParameters.dbUpdateCount, defaultUpdateCountHeaderName); + String nullText = Properties().getPropertyOrDefault( + ConfigurationParameters.inputNullString, "#null#"); - - SQLStatement theStatement = new SQLStatement(sqlCommand); + SQLStatement theStatement = new SQLStatement(sqlCommand, nullText); theStatement.extractParametersFromCmd(); theStatement.addParametersFromProperties(Properties(), ConfigurationParameters.dbQueryParameters); diff --git a/src/main/java/six42/fitnesse/jdbcslim/SQLStatement.java b/src/main/java/six42/fitnesse/jdbcslim/SQLStatement.java index b968bfc..e37b9bd 100644 --- a/src/main/java/six42/fitnesse/jdbcslim/SQLStatement.java +++ b/src/main/java/six42/fitnesse/jdbcslim/SQLStatement.java @@ -17,9 +17,11 @@ class SQLStatement { private String finalCommandString; private List> inputParamterList; private Map defaultList; + private String nullText; - public SQLStatement(String sqlCommand) { + public SQLStatement(String sqlCommand, String nullText) { this.initialCommandString = sqlCommand; + this.nullText = nullText; inputParamterList = new ArrayList>(); defaultList = new HashMap(); @@ -137,6 +139,7 @@ public SortedMap setInputParameters(CallableStatement cstmt, if (sqlCommand.containsKey(columnName)) { Object obj = sqlCommand.get(columnName); String value = (obj != null) ? obj.toString() : null; + value = nullText.equals(value) ? null : value; if (scale == -1) cstmt.setObject(parameterIndex, value, sqlType); @@ -144,6 +147,7 @@ public SortedMap setInputParameters(CallableStatement cstmt, cstmt.setObject(parameterIndex, value, sqlType, scale); } else if (defaultList.containsKey(columnName)) { String value = defaultList.get(columnName); + value = nullText.equals(value) ? null : value; if (scale == -1) cstmt.setObject(parameterIndex, value, sqlType); else