|
| 1 | +/*- |
| 2 | + * ========================LICENSE_START================================= |
| 3 | + * flyway-database-timeplus |
| 4 | + * ======================================================================== |
| 5 | + * Copyright (C) 2010 - 2024 Red Gate Software Ltd |
| 6 | + * ======================================================================== |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * =========================LICENSE_END================================== |
| 19 | + */ |
| 20 | +/* |
| 21 | + * Copyright (C) Red Gate Software Ltd 2010-2024 |
| 22 | + * |
| 23 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 | + * you may not use this file except in compliance with the License. |
| 25 | + * You may obtain a copy of the License at |
| 26 | + * |
| 27 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 28 | + * |
| 29 | + * Unless required by applicable law or agreed to in writing, software |
| 30 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 31 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 | + * See the License for the specific language governing permissions and |
| 33 | + * limitations under the License. |
| 34 | + */ |
| 35 | +package org.flywaydb.community.database.timeplus; |
| 36 | + |
| 37 | +import org.flywaydb.core.internal.database.base.Connection; |
| 38 | + |
| 39 | +import java.sql.SQLException; |
| 40 | +import java.util.Optional; |
| 41 | + |
| 42 | +public class TimeplusConnection extends Connection<TimeplusDatabase> { |
| 43 | + private static final String DEFAULT_CATALOG_TERM = "database"; |
| 44 | + |
| 45 | + TimeplusConnection(TimeplusDatabase database, java.sql.Connection connection) { |
| 46 | + super(database, connection); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected String getCurrentSchemaNameOrSearchPath() throws SQLException { |
| 51 | + var jdbcConnection = getJdbcTemplate().getConnection(); |
| 52 | + var currentSchema = useCatalog(jdbcConnection) ? |
| 53 | + jdbcConnection.getCatalog() : jdbcConnection.getSchema(); |
| 54 | + |
| 55 | + return Optional.ofNullable(currentSchema).map(database::unQuote).orElse(null); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void doChangeCurrentSchemaOrSearchPathTo(String schema) throws SQLException { |
| 60 | + // databaseTerm is catalog since driver version 0.5.0 |
| 61 | + // https://github.com/Timeplus/timeplus-java/issues/1273 & https://github.com/dbeaver/dbeaver/issues/19383 |
| 62 | + // For compatibility with old libraries, ((TimeplusConnection) getJdbcConnection()).useCatalog() should be checked |
| 63 | + var jdbcConnection = getJdbcTemplate().getConnection(); |
| 64 | + |
| 65 | + if (useCatalog(jdbcConnection)) { |
| 66 | + jdbcConnection.setCatalog(schema); |
| 67 | + } else { |
| 68 | + jdbcConnection.setSchema(schema); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + protected boolean useCatalog(java.sql.Connection jdbcConnection) throws SQLException { |
| 73 | + return DEFAULT_CATALOG_TERM.equals(jdbcConnection.getMetaData().getCatalogTerm()); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public TimeplusSchema getSchema(String name) { |
| 78 | + return new TimeplusSchema(jdbcTemplate, database, name); |
| 79 | + } |
| 80 | +} |
0 commit comments