-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve already exists sql exception
- Loading branch information
xiaojiebao
committed
Apr 10, 2024
1 parent
abcfa96
commit be236e9
Showing
13 changed files
with
608 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ain/java/com/datastrato/gravitino/storage/relational/converters/H2ExceptionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Entity; | ||
import com.datastrato.gravitino.exceptions.AlreadyExistsException; | ||
import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; | ||
import java.sql.SQLException; | ||
|
||
/** Exception converter to Gravitino exception for H2. */ | ||
public class H2ExceptionConverter implements SQLExceptionConverter { | ||
|
||
@SuppressWarnings("FormatStringAnnotation") | ||
@Override | ||
public GravitinoRuntimeException toGravitinoException( | ||
SQLException se, Entity.EntityType type, String name) { | ||
switch (se.getErrorCode()) { | ||
case 23505: | ||
return new AlreadyExistsException(se, se.getMessage()); | ||
default: | ||
return new GravitinoRuntimeException(se, se.getMessage()); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../java/com/datastrato/gravitino/storage/relational/converters/MySQLExceptionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Entity; | ||
import com.datastrato.gravitino.exceptions.AlreadyExistsException; | ||
import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; | ||
import java.sql.SQLException; | ||
|
||
/** Exception converter to Gravitino exception for MySQL. */ | ||
public class MySQLExceptionConverter implements SQLExceptionConverter { | ||
|
||
@SuppressWarnings("FormatStringAnnotation") | ||
@Override | ||
public GravitinoRuntimeException toGravitinoException( | ||
SQLException se, Entity.EntityType type, String name) { | ||
switch (se.getErrorCode()) { | ||
case 1062: | ||
return new AlreadyExistsException(se, se.getMessage()); | ||
default: | ||
return new GravitinoRuntimeException(se, se.getMessage()); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...in/java/com/datastrato/gravitino/storage/relational/converters/SQLExceptionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Entity; | ||
import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; | ||
import java.sql.SQLException; | ||
|
||
/** Interface for converter JDBC SQL exceptions to Gravitino exceptions. */ | ||
public interface SQLExceptionConverter { | ||
/** | ||
* Convert JDBC exception to GravitinoException. | ||
* | ||
* @param sqlException The sql exception to map | ||
* @param type The type of the entity | ||
* @param name The name of the entity | ||
* @return A best attempt at a corresponding connector exception or generic with the SQLException | ||
* as the cause | ||
*/ | ||
GravitinoRuntimeException toGravitinoException( | ||
SQLException sqlException, Entity.EntityType type, String name); | ||
} |
43 changes: 43 additions & 0 deletions
43
.../com/datastrato/gravitino/storage/relational/converters/SQLExceptionConverterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Config; | ||
import com.datastrato.gravitino.Configs; | ||
import com.google.common.base.Preconditions; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class SQLExceptionConverterFactory { | ||
private static final Pattern TYPE_PATTERN = Pattern.compile("jdbc:(\\w+):"); | ||
private static SQLExceptionConverter converter; | ||
|
||
private SQLExceptionConverterFactory() {} | ||
|
||
public static synchronized void initConverter(Config config) { | ||
if (converter == null) { | ||
String jdbcUrl = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL); | ||
Matcher typeMatcher = TYPE_PATTERN.matcher(jdbcUrl); | ||
if (typeMatcher.find()) { | ||
String jdbcType = typeMatcher.group(1); | ||
if (jdbcType.equalsIgnoreCase("mysql")) { | ||
converter = new MySQLExceptionConverter(); | ||
} else if (jdbcType.equalsIgnoreCase("h2")) { | ||
converter = new H2ExceptionConverter(); | ||
} else { | ||
throw new IllegalArgumentException(String.format("Unsupported jdbc type: %s", jdbcType)); | ||
} | ||
} else { | ||
throw new IllegalArgumentException( | ||
String.format("Cannot find jdbc type in jdbc url: %s", jdbcUrl)); | ||
} | ||
} | ||
} | ||
|
||
public static SQLExceptionConverter getConverter() { | ||
Preconditions.checkState(converter != null, "Exception converter is not initialized."); | ||
return converter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.