|
| 1 | +/* |
| 2 | + * Copyright 2025 Flamingock (https://www.flamingock.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.cli.factory; |
| 17 | + |
| 18 | +import com.zaxxer.hikari.HikariConfig; |
| 19 | +import com.zaxxer.hikari.HikariDataSource; |
| 20 | +import io.flamingock.cli.config.DatabaseConfig; |
| 21 | +import io.flamingock.internal.common.sql.SqlDialect; |
| 22 | +import org.sqlite.SQLiteDataSource; |
| 23 | + |
| 24 | +import javax.sql.DataSource; |
| 25 | +import java.sql.Connection; |
| 26 | +import java.sql.DatabaseMetaData; |
| 27 | +import java.sql.SQLException; |
| 28 | +import java.sql.Statement; |
| 29 | + |
| 30 | +public class SqlDataSourceFactory { |
| 31 | + |
| 32 | + public static DataSource createSqlDataSource(DatabaseConfig.SqlConfig config) { |
| 33 | + if (config == null) { |
| 34 | + throw new IllegalArgumentException("SQL configuration is required"); |
| 35 | + } |
| 36 | + |
| 37 | + if (config.getEndpoint() == null) { |
| 38 | + throw new IllegalArgumentException("Database endpoint is required"); |
| 39 | + } |
| 40 | + |
| 41 | + if (config.getSqlDialect() == null) { |
| 42 | + throw new IllegalArgumentException("Sql dialect is required"); |
| 43 | + } |
| 44 | + |
| 45 | + if (!SqlDialect.SQLITE.equals(config.getSqlDialect())) { |
| 46 | + if (config.getUsername() == null) { |
| 47 | + throw new IllegalArgumentException("Database username is required"); |
| 48 | + } |
| 49 | + if (config.getPassword() == null) { |
| 50 | + throw new IllegalArgumentException("Database password is required"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + DataSource sqlDatasource; |
| 56 | + |
| 57 | + if (config.getSqlDialect().equals(SqlDialect.SQLITE)) { |
| 58 | + SQLiteDataSource sqliteDatasource = new SQLiteDataSource(); |
| 59 | + sqliteDatasource.setUrl(config.getEndpoint()); |
| 60 | + |
| 61 | + try (Connection conn = sqliteDatasource.getConnection(); |
| 62 | + Statement stmt = conn.createStatement()) { |
| 63 | + stmt.execute("PRAGMA journal_mode=WAL;"); |
| 64 | + stmt.execute("PRAGMA busy_timeout=5000;"); |
| 65 | + } |
| 66 | + |
| 67 | + sqlDatasource = sqliteDatasource; |
| 68 | + } else { |
| 69 | + HikariConfig datasourceConfig = new HikariConfig(); |
| 70 | + datasourceConfig.setJdbcUrl(config.getEndpoint()); |
| 71 | + datasourceConfig.setUsername(config.getUsername()); |
| 72 | + datasourceConfig.setPassword(config.getPassword()); |
| 73 | + datasourceConfig.setDriverClassName(config.getDriverClassName()); |
| 74 | + |
| 75 | + sqlDatasource = new HikariDataSource(datasourceConfig); |
| 76 | + } |
| 77 | + |
| 78 | + // Test the connection by listing tables |
| 79 | + try (Connection conn = sqlDatasource.getConnection()) { |
| 80 | + DatabaseMetaData metaData = conn.getMetaData(); |
| 81 | + metaData.getTables(null, null, "%", null); |
| 82 | + } catch (SQLException e) { |
| 83 | + throw new RuntimeException("Failed to validate SQL DataSource connection: " + e.getMessage(), e); |
| 84 | + } |
| 85 | + |
| 86 | + return sqlDatasource; |
| 87 | + } catch (Exception e) { |
| 88 | + throw new RuntimeException("Failed to create SQL DataSource: " + e.getMessage(), e); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments