Skip to content

Commit

Permalink
Add support for #null# values in defaults and scripts
Browse files Browse the repository at this point in the history
Bug Fix of #null# handling
see details in FitNesseRoot/PlugIns/JdbcSlim/UserGuide/9NullTrailingSpacesAndEmptyStrings.wiki
  • Loading branch information
six42 committed Apr 28, 2018
1 parent dcf92d7 commit 4e39e4c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
---
LastModifyingUser: six42
---
#Copyright (C) 2015 by six42, All rights reserved. Contact the author via http://github.com/six42
!*> Setup jdbcSlim
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}..."
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/six42/fitnesse/jdbcslim/SQLCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ private List<List<String>> 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);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/six42/fitnesse/jdbcslim/SQLStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class SQLStatement {
private String finalCommandString;
private List<List<String>> inputParamterList;
private Map<String, String> defaultList;
private String nullText;

public SQLStatement(String sqlCommand) {
public SQLStatement(String sqlCommand, String nullText) {
this.initialCommandString = sqlCommand;
this.nullText = nullText;
inputParamterList = new ArrayList<List<String>>();
defaultList = new HashMap<String, String>();

Expand Down Expand Up @@ -137,13 +139,15 @@ public SortedMap<Integer, String> 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);
else
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
Expand Down

0 comments on commit 4e39e4c

Please sign in to comment.