50
50
import static org .hibernate .type .SqlTypes .TIMESTAMP ;
51
51
import static org .hibernate .type .SqlTypes .TIMESTAMP_UTC ;
52
52
import static org .hibernate .type .SqlTypes .TIMESTAMP_WITH_TIMEZONE ;
53
+ import static org .hibernate .type .SqlTypes .UUID ;
53
54
import static org .hibernate .type .SqlTypes .VARBINARY ;
54
55
55
56
public class DB2AggregateSupport extends AggregateSupportImpl {
56
57
57
58
public static final AggregateSupport INSTANCE = new DB2AggregateSupport ( false );
58
59
public static final AggregateSupport JSON_INSTANCE = new DB2AggregateSupport ( true );
60
+ private static final String JSON_QUERY_START = "json_query(" ;
61
+ private static final String JSON_QUERY_JSON_END = "')" ;
59
62
60
63
private final boolean jsonSupport ;
61
64
@@ -77,25 +80,32 @@ public String aggregateComponentCustomReadExpression(
77
80
if ( !jsonSupport ) {
78
81
break ;
79
82
}
83
+ final String parentPartExpression ;
84
+ if ( aggregateParentReadExpression .startsWith ( JSON_QUERY_START ) && aggregateParentReadExpression .endsWith ( JSON_QUERY_JSON_END ) ) {
85
+ parentPartExpression = aggregateParentReadExpression .substring ( JSON_QUERY_START .length (), aggregateParentReadExpression .length () - JSON_QUERY_JSON_END .length () ) + "." ;
86
+ }
87
+ else {
88
+ parentPartExpression = aggregateParentReadExpression + ",'$." ;
89
+ }
80
90
switch ( column .getJdbcMapping ().getJdbcType ().getDefaultSqlTypeCode () ) {
81
91
case BOOLEAN :
82
92
if ( SqlTypes .isNumericType ( column .getJdbcMapping ().getJdbcType ().getDdlTypeCode () ) ) {
83
93
return template .replace (
84
94
placeholder ,
85
- "decode(json_value(" + aggregateParentReadExpression + ",'$." + columnExpression + "'),'true',1,'false',0)"
95
+ "decode(json_value(" + parentPartExpression + columnExpression + "'),'true',1,'false',0)"
86
96
);
87
97
}
88
98
else {
89
99
return template .replace (
90
100
placeholder ,
91
- "decode(json_value(" + aggregateParentReadExpression + ",'$." + columnExpression + "'),'true',true,'false',false)"
101
+ "decode(json_value(" + parentPartExpression + columnExpression + "'),'true',true,'false',false)"
92
102
);
93
103
}
94
104
case TIMESTAMP_WITH_TIMEZONE :
95
105
case TIMESTAMP_UTC :
96
106
return template .replace (
97
107
placeholder ,
98
- "cast(trim(trailing 'Z' from json_value(" + aggregateParentReadExpression + ",'$." + columnExpression + "' returning varchar(35))) as " + column .getColumnDefinition () + ")"
108
+ "cast(trim(trailing 'Z' from json_value(" + parentPartExpression + columnExpression + "' returning varchar(35))) as " + column .getColumnDefinition () + ")"
99
109
);
100
110
case BINARY :
101
111
case VARBINARY :
@@ -104,18 +114,23 @@ public String aggregateComponentCustomReadExpression(
104
114
// We encode binary data as hex, so we have to decode here
105
115
return template .replace (
106
116
placeholder ,
107
- "hextoraw(json_value(" + aggregateParentReadExpression + ",'$." + columnExpression + "'))"
117
+ "hextoraw(json_value(" + parentPartExpression + columnExpression + "'))"
118
+ );
119
+ case UUID :
120
+ return template .replace (
121
+ placeholder ,
122
+ "hextoraw(replace(json_value(" + parentPartExpression + columnExpression + "'),'-',''))"
108
123
);
109
124
case JSON :
110
125
case JSON_ARRAY :
111
126
return template .replace (
112
127
placeholder ,
113
- "json_query(" + aggregateParentReadExpression + ",'$." + columnExpression + "')"
128
+ "json_query(" + parentPartExpression + columnExpression + "')"
114
129
);
115
130
default :
116
131
return template .replace (
117
132
placeholder ,
118
- "json_value(" + aggregateParentReadExpression + ",'$." + columnExpression + "' returning " + column .getColumnDefinition () + ")"
133
+ "json_value(" + parentPartExpression + columnExpression + "' returning " + column .getColumnDefinition () + ")"
119
134
);
120
135
}
121
136
case STRUCT :
@@ -133,6 +148,8 @@ private static String jsonCustomWriteExpression(String customWriteExpression, Jd
133
148
case BLOB :
134
149
// We encode binary data as hex
135
150
return "hex(" + customWriteExpression + ")" ;
151
+ case UUID :
152
+ return "regexp_replace(lower(hex(" + customWriteExpression + ")),'^(.{8})(.{4})(.{4})(.{4})(.{12})$','$1-$2-$3-$4-$5')" ;
136
153
case ARRAY :
137
154
case JSON_ARRAY :
138
155
return "(" + customWriteExpression + ") format json" ;
0 commit comments