Skip to content

Commit 2946a25

Browse files
committed
Fix issue with saving and querying boolean values (because they were being treated as json values) in the JSql implementation
1 parent c62ce4e commit 2946a25

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/main/java/picoded/dstack/connector/jsql/JSql_Base.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ protected PreparedStatement prepareSqlStatment(Connection sqlConn, String qStrin
130130
ps.setDouble(pt + 1, (Double) argObj);
131131
} else if (Float.class.isInstance(argObj)) {
132132
ps.setFloat(pt + 1, (Float) argObj);
133+
} else if (Boolean.class.isInstance(argObj)){
134+
boolean bValue = (Boolean) argObj;
135+
// note that due to a bug in dstack before 26 Aug 2024,
136+
// ... all boolean values were stored as json (Core_DataType.JSON, 31)
137+
// ...thus entry is stored with the values: nVl = NULL, sVL = null, tVl = "true" or "false"
138+
// ... thus, for boolean, we need to query against tVl to support legacy values
139+
// ... this bug has been fixed in newer versions of dstack...
140+
// ... where the entry is stored with the values: nVl = 1 or 0, sVL = "true" or "false", tVl = "true" or "false"
141+
ps.setString(pt + 1, bValue ? "true" : "false");
142+
// ps.setBoolean(pt + 1, (Boolean) argObj); // this does not work, bc this transforms the value to 1/0
133143
} else if (Date.class.isInstance(argObj)) {
134144
java.sql.Date sqlDate = new java.sql.Date(((Date) argObj).getTime());
135145
ps.setDate(pt + 1, sqlDate);

src/main/java/picoded/dstack/core/Core_DataType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ public enum Core_DataType {
5959
* String type
6060
**/
6161
STRING(25),
62-
62+
63+
/**
64+
* Boolean type
65+
*/
66+
BOOLEAN(26),
67+
6368
//
6469
// Storage types
6570
//

src/main/java/picoded/dstack/jsql/JSql_DataObjectMapUtil.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,15 @@ public static Object[] valueToValueTypeSet(Object value) {
193193
return valueTypeSet(Core_DataType.STRING.getValue(), null, shortenStringValue(value),
194194
value.toString(), EmptyArray.BYTE);
195195
}
196-
196+
197+
// Boolean type support
198+
// - stores nVl as 1 or 0
199+
// - store sVl and tVl as the string value : "true" or "false"
200+
if(value instanceof Boolean){
201+
return valueTypeSet(Core_DataType.BOOLEAN.getValue(), ((Boolean) value) ? 1 : 0, value.toString(), value.toString(),
202+
EmptyArray.BYTE);
203+
}
204+
197205
// Binary type support
198206
if (value instanceof byte[]) {
199207
return valueTypeSet(Core_DataType.BINARY.getValue(), null, null, null, (byte[]) value);

src/main/java/picoded/dstack/jsql/JSql_DataObjectMap_QueryBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,16 @@ private Query dynamicTableQueryRewrite( //
921921
"(" + collumnTableAlias + ".sVl IS NULL OR " + replacement.toString() + ")");
922922
}
923923
}
924+
} else if (argObj instanceof Boolean) {
925+
// note that due to a bug in dstack before 26 Aug 2024,
926+
// ... all boolean values were stored as json (Core_DataType.JSON, 31)
927+
// ...thus entry is stored with the values: nVl = NULL, sVL = null, tVl = "true" or "false"
928+
// ... thus, for boolean, we need to query against tVl to support legacy values
929+
// ... this bug has been fixed in newer versions of dstack...
930+
// ... where the entry is stored with the values: nVl = 1 or 0, sVL = "true" or "false", tVl = "true" or "false"
931+
replacement = QueryFilter.basicQueryFromTokens(queryArgMap, collumnTableAlias
932+
+ ".tVl", toReplace.operatorSymbol(), ":" + toReplace.argumentName() //
933+
);
924934
}
925935

926936
// Unprocessed arg type

0 commit comments

Comments
 (0)