23
23
import org .apache .seatunnel .common .exception .SeaTunnelRuntimeException ;
24
24
25
25
import lombok .AllArgsConstructor ;
26
+ import lombok .extern .slf4j .Slf4j ;
27
+
28
+ import javax .annotation .Nonnull ;
29
+ import javax .annotation .Nullable ;
30
+
31
+ import java .util .Optional ;
26
32
27
33
import static org .apache .seatunnel .api .common .SeaTunnelAPIErrorCode .SINK_TABLE_NOT_EXIST ;
28
34
import static org .apache .seatunnel .api .common .SeaTunnelAPIErrorCode .SOURCE_ALREADY_HAS_DATA ;
29
35
30
36
@ AllArgsConstructor
37
+ @ Slf4j
31
38
public class DefaultSaveModeHandler implements SaveModeHandler {
32
39
33
- public SchemaSaveMode schemaSaveMode ;
34
- public DataSaveMode dataSaveMode ;
35
- public Catalog catalog ;
36
- public TablePath tablePath ;
37
- public CatalogTable catalogTable ;
38
- public String customSql ;
40
+ @ Nonnull public SchemaSaveMode schemaSaveMode ;
41
+ @ Nonnull public DataSaveMode dataSaveMode ;
42
+ @ Nonnull public Catalog catalog ;
43
+ @ Nonnull public TablePath tablePath ;
44
+ @ Nullable public CatalogTable catalogTable ;
45
+ @ Nullable public String customSql ;
39
46
40
47
public DefaultSaveModeHandler (
41
48
SchemaSaveMode schemaSaveMode ,
@@ -132,17 +139,58 @@ protected boolean tableExists() {
132
139
}
133
140
134
141
protected void dropTable () {
142
+ try {
143
+ log .info (
144
+ "Dropping table {} with action {}" ,
145
+ tablePath ,
146
+ catalog .previewAction (
147
+ Catalog .ActionType .DROP_TABLE , tablePath , Optional .empty ()));
148
+ } catch (UnsupportedOperationException ignore ) {
149
+ log .info ("Dropping table {}" , tablePath );
150
+ }
135
151
catalog .dropTable (tablePath , true );
136
152
}
137
153
138
154
protected void createTable () {
139
155
if (!catalog .databaseExists (tablePath .getDatabaseName ())) {
140
- catalog .createDatabase (TablePath .of (tablePath .getDatabaseName (), "" ), true );
156
+ TablePath databasePath = TablePath .of (tablePath .getDatabaseName (), "" );
157
+ try {
158
+ log .info (
159
+ "Creating database {} with action {}" ,
160
+ tablePath .getDatabaseName (),
161
+ catalog .previewAction (
162
+ Catalog .ActionType .CREATE_DATABASE ,
163
+ databasePath ,
164
+ Optional .empty ()));
165
+ } catch (UnsupportedOperationException ignore ) {
166
+ log .info ("Creating database {}" , tablePath .getDatabaseName ());
167
+ }
168
+ catalog .createDatabase (databasePath , true );
169
+ }
170
+ try {
171
+ log .info (
172
+ "Creating table {} with action {}" ,
173
+ tablePath ,
174
+ catalog .previewAction (
175
+ Catalog .ActionType .CREATE_TABLE ,
176
+ tablePath ,
177
+ Optional .ofNullable (catalogTable )));
178
+ } catch (UnsupportedOperationException ignore ) {
179
+ log .info ("Creating table {}" , tablePath );
141
180
}
142
181
catalog .createTable (tablePath , catalogTable , true );
143
182
}
144
183
145
184
protected void truncateTable () {
185
+ try {
186
+ log .info (
187
+ "Truncating table {} with action {}" ,
188
+ tablePath ,
189
+ catalog .previewAction (
190
+ Catalog .ActionType .TRUNCATE_TABLE , tablePath , Optional .empty ()));
191
+ } catch (UnsupportedOperationException ignore ) {
192
+ log .info ("Truncating table {}" , tablePath );
193
+ }
146
194
catalog .truncateTable (tablePath , true );
147
195
}
148
196
@@ -151,9 +199,30 @@ protected boolean dataExists() {
151
199
}
152
200
153
201
protected void executeCustomSql () {
202
+ log .info ("Executing custom SQL for table {} with SQL: {}" , tablePath , customSql );
154
203
catalog .executeSql (tablePath , customSql );
155
204
}
156
205
206
+ @ Override
207
+ public TablePath getHandleTablePath () {
208
+ return tablePath ;
209
+ }
210
+
211
+ @ Override
212
+ public Catalog getHandleCatalog () {
213
+ return catalog ;
214
+ }
215
+
216
+ @ Override
217
+ public SchemaSaveMode getSchemaSaveMode () {
218
+ return schemaSaveMode ;
219
+ }
220
+
221
+ @ Override
222
+ public DataSaveMode getDataSaveMode () {
223
+ return dataSaveMode ;
224
+ }
225
+
157
226
@ Override
158
227
public void close () throws Exception {
159
228
catalog .close ();
0 commit comments