|
| 1 | +/* |
| 2 | + * Copyright 2024 Datastrato Pvt Ltd. |
| 3 | + * This software is licensed under the Apache License version 2. |
| 4 | + */ |
| 5 | +package com.datastrato.gravitino.storage.relational.converters; |
| 6 | + |
| 7 | +import com.datastrato.gravitino.Config; |
| 8 | +import com.datastrato.gravitino.Configs; |
| 9 | +import com.google.common.base.Preconditions; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +public class SQLExceptionConverterFactory { |
| 14 | + private static final Pattern TYPE_PATTERN = Pattern.compile("jdbc:(\\w+):"); |
| 15 | + private static SQLExceptionConverter converter; |
| 16 | + |
| 17 | + private SQLExceptionConverterFactory() {} |
| 18 | + |
| 19 | + public static synchronized void initConverter(Config config) { |
| 20 | + if (converter == null) { |
| 21 | + String jdbcUrl = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL); |
| 22 | + Matcher typeMatcher = TYPE_PATTERN.matcher(jdbcUrl); |
| 23 | + if (typeMatcher.find()) { |
| 24 | + String jdbcType = typeMatcher.group(1); |
| 25 | + if (jdbcType.equalsIgnoreCase("mysql")) { |
| 26 | + converter = new MySQLExceptionConverter(); |
| 27 | + } else if (jdbcType.equalsIgnoreCase("h2")) { |
| 28 | + converter = new H2ExceptionConverter(); |
| 29 | + } else { |
| 30 | + throw new IllegalArgumentException(String.format("Unsupported jdbc type: %s", jdbcType)); |
| 31 | + } |
| 32 | + } else { |
| 33 | + throw new IllegalArgumentException( |
| 34 | + String.format("Cannot find jdbc type in jdbc url: %s", jdbcUrl)); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static SQLExceptionConverter getConverter() { |
| 40 | + Preconditions.checkState(converter != null, "Exception converter is not initialized."); |
| 41 | + return converter; |
| 42 | + } |
| 43 | +} |
0 commit comments