@@ -43,8 +43,6 @@ protected Mutation(ClickHouseRequest<?> request, boolean sealed) {
43
43
44
44
this .options .putAll (request .options );
45
45
this .settings .putAll (request .settings );
46
-
47
- this .sessionId = request .sessionId ;
48
46
}
49
47
50
48
@ Override
@@ -90,13 +88,13 @@ public Mutation data(String file, ClickHouseCompression compression) {
90
88
91
89
final ClickHouseRequest <?> self = this ;
92
90
final String fileName = ClickHouseChecker .nonEmpty (file , "File" );
93
- this .input = ClickHouseDeferredValue .of (() -> {
91
+ this .input = changeProperty ( PROP_DATA , this . input , ClickHouseDeferredValue .of (() -> {
94
92
try {
95
93
return ClickHouseInputStream .of (new FileInputStream (fileName ), 123 , compression );
96
94
} catch (FileNotFoundException e ) {
97
95
throw new IllegalArgumentException (e );
98
96
}
99
- });
97
+ })) ;
100
98
return this ;
101
99
}
102
100
@@ -119,7 +117,8 @@ public Mutation data(InputStream input) {
119
117
public Mutation data (ClickHouseInputStream input ) {
120
118
checkSealed ();
121
119
122
- this .input = ClickHouseDeferredValue .of (input , ClickHouseInputStream .class );
120
+ this .input = changeProperty (PROP_DATA , this .input ,
121
+ ClickHouseDeferredValue .of (input , ClickHouseInputStream .class ));
123
122
124
123
return this ;
125
124
}
@@ -133,7 +132,7 @@ public Mutation data(ClickHouseInputStream input) {
133
132
public Mutation data (ClickHouseDeferredValue <ClickHouseInputStream > input ) {
134
133
checkSealed ();
135
134
136
- this .input = input ;
135
+ this .input = changeProperty ( PROP_DATA , this . input , input ) ;
137
136
138
137
return this ;
139
138
}
@@ -162,16 +161,7 @@ public ClickHouseResponse sendAndWait() throws ClickHouseException {
162
161
@ Override
163
162
public Mutation table (String table , String queryId ) {
164
163
checkSealed ();
165
-
166
- this .queryId = queryId ;
167
-
168
- String sql = "INSERT INTO " + ClickHouseChecker .nonBlank (table , "table" );
169
- if (!sql .equals (this .sql )) {
170
- this .sql = sql ;
171
- this .preparedQuery = null ;
172
- resetCache ();
173
- }
174
-
164
+ super .query ("INSERT INTO " + ClickHouseChecker .nonBlank (table , "table" ), queryId );
175
165
return this ;
176
166
}
177
167
@@ -190,7 +180,6 @@ public Mutation seal() {
190
180
191
181
req .input = input ;
192
182
req .queryId = queryId ;
193
- req .sessionId = sessionId ;
194
183
req .sql = sql ;
195
184
196
185
req .preparedQuery = preparedQuery ;
@@ -202,6 +191,11 @@ public Mutation seal() {
202
191
203
192
private static final long serialVersionUID = 4990313525960702287L ;
204
193
194
+ static final String PROP_DATA = "data" ;
195
+ static final String PROP_PREPARED_QUERY = "preparedQuery" ;
196
+ static final String PROP_QUERY = "query" ;
197
+ static final String PROP_QUERY_ID = "queryId" ;
198
+
205
199
private final boolean sealed ;
206
200
207
201
private transient ClickHouseClient client ;
@@ -216,7 +210,6 @@ public Mutation seal() {
216
210
217
211
protected transient ClickHouseDeferredValue <ClickHouseInputStream > input ;
218
212
protected String queryId ;
219
- protected String sessionId ;
220
213
protected String sql ;
221
214
protected ClickHouseParameterizedQuery preparedQuery ;
222
215
@@ -245,6 +238,13 @@ protected ClickHouseRequest(ClickHouseClient client, Function<ClickHouseNodeSele
245
238
this .namedParameters = new HashMap <>();
246
239
}
247
240
241
+ protected <T > T changeProperty (String property , T oldValue , T newValue ) {
242
+ if (changeListener != null && !Objects .equals (oldValue , newValue )) {
243
+ changeListener .propertyChanged (this , property , oldValue , newValue );
244
+ }
245
+ return newValue ;
246
+ }
247
+
248
248
protected void checkSealed () {
249
249
if (sealed ) {
250
250
throw new IllegalStateException ("Sealed request is immutable" );
@@ -291,7 +291,6 @@ public ClickHouseRequest<SelfT> copy() {
291
291
req .namedParameters .putAll (namedParameters );
292
292
req .input = input ;
293
293
req .queryId = queryId ;
294
- req .sessionId = sessionId ;
295
294
req .sql = sql ;
296
295
req .preparedQuery = preparedQuery ;
297
296
return req ;
@@ -412,7 +411,8 @@ public Optional<String> getQueryId() {
412
411
*/
413
412
public ClickHouseParameterizedQuery getPreparedQuery () {
414
413
if (preparedQuery == null ) {
415
- preparedQuery = ClickHouseParameterizedQuery .of (getConfig (), getQuery ());
414
+ preparedQuery = changeProperty (PROP_PREPARED_QUERY , preparedQuery ,
415
+ ClickHouseParameterizedQuery .of (getConfig (), getQuery ()));
416
416
}
417
417
418
418
return preparedQuery ;
@@ -433,6 +433,7 @@ public Map<String, Object> getSettings() {
433
433
* @return session id
434
434
*/
435
435
public Optional <String > getSessionId () {
436
+ String sessionId = (String ) getConfig ().getOption (ClickHouseClientOption .SESSION_ID );
436
437
return ClickHouseChecker .isNullOrEmpty (sessionId ) ? Optional .empty () : Optional .of (sessionId );
437
438
}
438
439
@@ -681,12 +682,7 @@ public SelfT external(Collection<ClickHouseExternalTable> tables) {
681
682
@ SuppressWarnings ("unchecked" )
682
683
public SelfT format (ClickHouseFormat format ) {
683
684
checkSealed ();
684
-
685
- if (format == null ) {
686
- removeOption (ClickHouseClientOption .FORMAT );
687
- } else {
688
- option (ClickHouseClientOption .FORMAT , format );
689
- }
685
+ option (ClickHouseClientOption .FORMAT , format );
690
686
return (SelfT ) this ;
691
687
}
692
688
@@ -739,11 +735,7 @@ public SelfT options(Map<ClickHouseOption, Serializable> options) {
739
735
m .putAll (this .options );
740
736
if (options != null ) {
741
737
for (Entry <ClickHouseOption , Serializable > e : options .entrySet ()) {
742
- if (e .getValue () == null ) {
743
- removeOption (e .getKey ());
744
- } else {
745
- option (e .getKey (), e .getValue ());
746
- }
738
+ option (e .getKey (), e .getValue ());
747
739
m .remove (e .getKey ());
748
740
}
749
741
}
@@ -1078,35 +1070,35 @@ public SelfT query(String sql) {
1078
1070
public SelfT query (ClickHouseParameterizedQuery query , String queryId ) {
1079
1071
checkSealed ();
1080
1072
1081
- if (!ClickHouseChecker .nonNull (query , "query" ).equals (this .preparedQuery )) {
1082
- this .preparedQuery = query ;
1083
- this .sql = query .getOriginalQuery ();
1073
+ if (!ClickHouseChecker .nonNull (query , PROP_QUERY ).equals (this .preparedQuery )) {
1074
+ this .preparedQuery = changeProperty ( PROP_PREPARED_QUERY , this . preparedQuery , query ) ;
1075
+ this .sql = changeProperty ( PROP_QUERY , this . sql , query .getOriginalQuery () );
1084
1076
resetCache ();
1085
1077
}
1086
1078
1087
- this .queryId = queryId ;
1079
+ this .queryId = changeProperty ( PROP_QUERY_ID , this . queryId , queryId ) ;
1088
1080
1089
1081
return (SelfT ) this ;
1090
1082
}
1091
1083
1092
1084
/**
1093
1085
* Sets query and optinally query id.
1094
1086
*
1095
- * @param sql non-empty query
1087
+ * @param query non-empty query
1096
1088
* @param queryId query id, null means no query id
1097
1089
* @return the request itself
1098
1090
*/
1099
1091
@ SuppressWarnings ("unchecked" )
1100
- public SelfT query (String sql , String queryId ) {
1092
+ public SelfT query (String query , String queryId ) {
1101
1093
checkSealed ();
1102
1094
1103
- if (!ClickHouseChecker .nonBlank (sql , "sql" ).equals (this .sql )) {
1104
- this .sql = sql ;
1105
- this .preparedQuery = null ;
1095
+ if (!ClickHouseChecker .nonBlank (query , PROP_QUERY ).equals (this .sql )) {
1096
+ this .sql = changeProperty ( PROP_QUERY , this . sql , query ) ;
1097
+ this .preparedQuery = changeProperty ( PROP_PREPARED_QUERY , this . preparedQuery , null ) ;
1106
1098
resetCache ();
1107
1099
}
1108
1100
1109
- this .queryId = queryId ;
1101
+ this .queryId = changeProperty ( PROP_QUERY_ID , this . queryId , queryId ) ;
1110
1102
1111
1103
return (SelfT ) this ;
1112
1104
}
@@ -1121,12 +1113,9 @@ public SelfT query(String sql, String queryId) {
1121
1113
public SelfT clearSession () {
1122
1114
checkSealed ();
1123
1115
1116
+ removeOption (ClickHouseClientOption .SESSION_ID );
1124
1117
removeOption (ClickHouseClientOption .SESSION_CHECK );
1125
1118
removeOption (ClickHouseClientOption .SESSION_TIMEOUT );
1126
- if (this .sessionId != null ) {
1127
- this .sessionId = null ;
1128
- resetCache ();
1129
- }
1130
1119
1131
1120
return (SelfT ) this ;
1132
1121
}
@@ -1178,12 +1167,9 @@ public SelfT session(String sessionId, Integer timeout) {
1178
1167
public SelfT session (String sessionId , Boolean check , Integer timeout ) {
1179
1168
checkSealed ();
1180
1169
1170
+ option (ClickHouseClientOption .SESSION_ID , sessionId );
1181
1171
option (ClickHouseClientOption .SESSION_CHECK , check );
1182
1172
option (ClickHouseClientOption .SESSION_TIMEOUT , timeout );
1183
- if (!Objects .equals (this .sessionId , sessionId )) {
1184
- this .sessionId = sessionId ;
1185
- resetCache ();
1186
- }
1187
1173
1188
1174
return (SelfT ) this ;
1189
1175
}
@@ -1263,12 +1249,7 @@ public SelfT table(String table, String queryId) {
1263
1249
@ SuppressWarnings ("unchecked" )
1264
1250
public SelfT use (String database ) {
1265
1251
checkSealed ();
1266
-
1267
- if (database == null ) {
1268
- removeOption (ClickHouseClientOption .DATABASE );
1269
- } else {
1270
- option (ClickHouseClientOption .DATABASE , database );
1271
- }
1252
+ option (ClickHouseClientOption .DATABASE , database );
1272
1253
return (SelfT ) this ;
1273
1254
}
1274
1255
@@ -1384,11 +1365,10 @@ public SelfT reset() {
1384
1365
}
1385
1366
this .namedParameters .clear ();
1386
1367
1387
- this .input = null ;
1388
- this .sql = null ;
1389
- this .preparedQuery = null ;
1390
- this .queryId = null ;
1391
- this .sessionId = null ;
1368
+ this .input = changeProperty (PROP_DATA , this .input , null );
1369
+ this .sql = changeProperty (PROP_QUERY , this .sql , null );
1370
+ this .preparedQuery = changeProperty (PROP_PREPARED_QUERY , this .preparedQuery , null );
1371
+ this .queryId = changeProperty (PROP_QUERY_ID , this .queryId , null );
1392
1372
1393
1373
resetCache ();
1394
1374
@@ -1414,7 +1394,6 @@ public ClickHouseRequest<SelfT> seal() {
1414
1394
1415
1395
req .input = input ;
1416
1396
req .queryId = queryId ;
1417
- req .sessionId = sessionId ;
1418
1397
req .sql = sql ;
1419
1398
req .preparedQuery = preparedQuery ;
1420
1399
}
0 commit comments