@@ -93,7 +93,7 @@ public void writeEntity(
9393 boolean nameOrParentChanged ,
9494 PolarisBaseEntity originalEntity ) {
9595 try {
96- persistEntity (callCtx , entity , originalEntity , datasourceOperations );
96+ persistEntity (callCtx , entity , originalEntity , datasourceOperations :: executeUpdate );
9797 } catch (SQLException e ) {
9898 throw new RuntimeException ("Error persisting entity" , e );
9999 }
@@ -111,7 +111,6 @@ public void writeEntities(
111111 PolarisBaseEntity entity = entities .get (i );
112112 PolarisBaseEntity originalEntity =
113113 originalEntities != null ? originalEntities .get (i ) : null ;
114-
115114 // first, check if the entity has already been created, in which case we will simply
116115 // return it.
117116 PolarisBaseEntity entityFound =
@@ -123,7 +122,7 @@ public void writeEntities(
123122 // already been updated after the creation.
124123 continue ;
125124 }
126- persistEntity (callCtx , entity , originalEntity , statement );
125+ persistEntity (callCtx , entity , originalEntity , statement :: executeUpdate );
127126 }
128127 return true ;
129128 });
@@ -139,12 +138,12 @@ private void persistEntity(
139138 @ Nonnull PolarisCallContext callCtx ,
140139 @ Nonnull PolarisBaseEntity entity ,
141140 PolarisBaseEntity originalEntity ,
142- Object executor )
141+ QueryAction queryAction )
143142 throws SQLException {
144143 ModelEntity modelEntity = ModelEntity .fromEntity (entity );
145144 if (originalEntity == null ) {
146145 try {
147- execute ( executor , generateInsertQuery (modelEntity , realmId ));
146+ queryAction . apply ( generateInsertQuery (modelEntity , realmId ));
148147 } catch (SQLException e ) {
149148 if (datasourceOperations .isConstraintViolation (e )) {
150149 PolarisBaseEntity existingEntity =
@@ -172,7 +171,7 @@ private void persistEntity(
172171 "realm_id" ,
173172 realmId );
174173 try {
175- int rowsUpdated = execute ( executor , generateUpdateQuery (modelEntity , params ));
174+ int rowsUpdated = queryAction . apply ( generateUpdateQuery (modelEntity , params ));
176175 if (rowsUpdated == 0 ) {
177176 throw new RetryOnConcurrencyException (
178177 "Entity '%s' id '%s' concurrently modified; expected version %s" ,
@@ -185,17 +184,6 @@ private void persistEntity(
185184 }
186185 }
187186
188- private int execute (Object executor , String query ) throws SQLException {
189- if (executor instanceof Statement ) {
190- // used for running in transaction
191- return ((Statement ) executor ).executeUpdate (query );
192- } else if (executor instanceof DatasourceOperations ) {
193- return ((DatasourceOperations ) executor ).executeUpdate (query );
194- } else {
195- throw new IllegalArgumentException ("Unsupported executor: " + executor );
196- }
197- }
198-
199187 @ Override
200188 public void writeToGrantRecords (
201189 @ Nonnull PolarisCallContext callCtx , @ Nonnull PolarisGrantRecord grantRec ) {
@@ -930,4 +918,9 @@ PolarisStorageIntegration<T> loadPolarisStorageIntegration(
930918 BaseMetaStoreManager .extractStorageConfiguration (callContext , entity );
931919 return storageIntegrationProvider .getStorageIntegrationForConfig (storageConfig );
932920 }
921+
922+ @ FunctionalInterface
923+ private interface QueryAction {
924+ Integer apply (String query ) throws SQLException ;
925+ }
933926}
0 commit comments