Skip to content

Commit

Permalink
Remove mapper usage in tools (#15073)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanwenjun authored Oct 25, 2023
1 parent ae847ba commit a5284e4
Show file tree
Hide file tree
Showing 45 changed files with 718 additions and 586 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ spring:
leak-detection-threshold: 0
initialization-fail-timeout: 1

# Mybatis-plus configuration, you don't need to change it
mybatis-plus:
mapper-locations: classpath:org/apache/dolphinscheduler/dao/mapper/*Mapper.xml
type-aliases-package: org.apache.dolphinscheduler.dao.entity
configuration:
cache-enabled: false
call-setters-on-nulls: true
map-underscore-to-camel-case: true
jdbc-type-for-null: NULL
global-config:
db-config:
id-type: auto
banner: false

server:
port: 50053

Expand Down
14 changes: 14 additions & 0 deletions dolphinscheduler-api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ springdoc:
path: /swagger-ui.html
packages-to-scan: org.apache.dolphinscheduler.api

# Mybatis-plus configuration, you don't need to change it
mybatis-plus:
mapper-locations: classpath:org/apache/dolphinscheduler/dao/mapper/*Mapper.xml
type-aliases-package: org.apache.dolphinscheduler.dao.entity
configuration:
cache-enabled: false
call-setters-on-nulls: true
map-underscore-to-camel-case: true
jdbc-type-for-null: NULL
global-config:
db-config:
id-type: auto
banner: false

management:
endpoints:
web:
Expand Down
13 changes: 13 additions & 0 deletions dolphinscheduler-api/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ spring:
username: sa
password: ""

mybatis-plus:
mapper-locations: classpath:org/apache/dolphinscheduler/dao/mapper/*Mapper.xml
type-aliases-package: org.apache.dolphinscheduler.dao.entity
configuration:
cache-enabled: false
call-setters-on-nulls: true
map-underscore-to-camel-case: true
jdbc-type-for-null: NULL
global-config:
db-config:
id-type: auto
banner: false

registry:
type: zookeeper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package org.apache.dolphinscheduler.dao.plugin.api;

import org.apache.dolphinscheduler.dao.plugin.api.dialect.DatabaseDialect;
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor;

import com.baomidou.mybatisplus.annotation.DbType;
Expand All @@ -33,4 +34,6 @@ public interface DaoPluginConfiguration {

DatabaseMonitor databaseMonitor();

DatabaseDialect databaseDialect();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dolphinscheduler.dao.plugin.api.dialect;

public interface DatabaseDialect {

boolean tableExists(String tableName);

boolean columnExists(String tableName, String columnName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package org.apache.dolphinscheduler.dao.plugin.h2;

import org.apache.dolphinscheduler.dao.plugin.api.DaoPluginConfiguration;
import org.apache.dolphinscheduler.dao.plugin.api.dialect.DatabaseDialect;
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor;
import org.apache.dolphinscheduler.dao.plugin.h2.dialect.H2Dialect;
import org.apache.dolphinscheduler.dao.plugin.h2.monitor.H2Monitor;

import javax.sql.DataSource;
Expand Down Expand Up @@ -49,4 +51,9 @@ public DatabaseMonitor databaseMonitor() {
return new H2Monitor(dataSource);
}

@Override
public DatabaseDialect databaseDialect() {
return new H2Dialect();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dolphinscheduler.dao.plugin.h2.dialect;

import org.apache.dolphinscheduler.dao.plugin.api.dialect.DatabaseDialect;

public class H2Dialect implements DatabaseDialect {

@Override
public boolean tableExists(String tableName) {
throw new UnsupportedOperationException();
}

@Override
public boolean columnExists(String tableName, String columnName) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package org.apache.dolphinscheduler.dao.plugin.mysql;

import org.apache.dolphinscheduler.dao.plugin.api.DaoPluginConfiguration;
import org.apache.dolphinscheduler.dao.plugin.api.dialect.DatabaseDialect;
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor;
import org.apache.dolphinscheduler.dao.plugin.mysql.dialect.MysqlDialect;
import org.apache.dolphinscheduler.dao.plugin.mysql.monitor.MysqlMonitor;

import javax.sql.DataSource;
Expand All @@ -47,4 +49,9 @@ public DbType dbType() {
public DatabaseMonitor databaseMonitor() {
return new MysqlMonitor(dataSource);
}

@Override
public DatabaseDialect databaseDialect() {
return new MysqlDialect(dataSource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.dolphinscheduler.dao.plugin.mysql.dialect;

import org.apache.dolphinscheduler.dao.plugin.api.dialect.DatabaseDialect;

import java.sql.Connection;
import java.sql.ResultSet;

import javax.sql.DataSource;

import lombok.SneakyThrows;

public class MysqlDialect implements DatabaseDialect {

private final DataSource dataSource;

public MysqlDialect(DataSource dataSource) {
this.dataSource = dataSource;
}

@SneakyThrows
@Override
public boolean tableExists(String tableName) {
try (
Connection conn = dataSource.getConnection();
ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), conn.getSchema(), tableName, null)) {
return rs.next();
}
}

@SneakyThrows
@Override
public boolean columnExists(String tableName, String columnName) {
try (
Connection conn = dataSource.getConnection();
ResultSet rs =
conn.getMetaData().getColumns(conn.getCatalog(), conn.getSchema(), tableName, columnName)) {
return rs.next();

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package org.apache.dolphinscheduler.dao.plugin.postgresql;

import org.apache.dolphinscheduler.dao.plugin.api.DaoPluginConfiguration;
import org.apache.dolphinscheduler.dao.plugin.api.dialect.DatabaseDialect;
import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor;
import org.apache.dolphinscheduler.dao.plugin.postgresql.dialect.PostgresqlDialect;
import org.apache.dolphinscheduler.dao.plugin.postgresql.monitor.PostgresqlMonitor;

import javax.sql.DataSource;
Expand All @@ -48,4 +50,9 @@ public DbType dbType() {
public DatabaseMonitor databaseMonitor() {
return new PostgresqlMonitor(dataSource);
}

@Override
public DatabaseDialect databaseDialect() {
return new PostgresqlDialect(dataSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

package org.apache.dolphinscheduler.tools.datasource.dao;
package org.apache.dolphinscheduler.dao.plugin.postgresql.dialect;

import org.apache.dolphinscheduler.spi.enums.DbType;
import org.apache.dolphinscheduler.dao.plugin.api.dialect.DatabaseDialect;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -26,82 +26,47 @@

import javax.sql.DataSource;

import lombok.extern.slf4j.Slf4j;
import lombok.SneakyThrows;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
public class PostgresqlDialect implements DatabaseDialect {

@Service
@Slf4j
@Profile("postgresql")
public class PostgreSQLUpgradeDao extends UpgradeDao {
private final DataSource dataSource;

private PostgreSQLUpgradeDao(DataSource dataSource) {
super(dataSource);
public PostgresqlDialect(DataSource dataSource) {
this.dataSource = dataSource;
}

@SneakyThrows
@Override
protected String initSqlPath() {
return "create/release-1.2.0_schema/postgresql";
}

@Override
public DbType getDbType() {
return DbType.POSTGRESQL;
}

public String getSchema() {
public boolean tableExists(String tableName) {
try (
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement("select current_schema()");
ResultSet resultSet = pstmt.executeQuery()) {
while (resultSet.next()) {
if (resultSet.isFirst()) {
return resultSet.getString(1);
}
}

} catch (SQLException e) {
log.error(e.getMessage(), e);
ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), getSchema(), tableName, null)) {
return rs.next();
}
return "";
}

/**
* determines whether a table exists
*
* @param tableName tableName
* @return if table exist return true,else return false
*/
@SneakyThrows
@Override
public boolean isExistsTable(String tableName) {
public boolean columnExists(String tableName, String columnName) {
try (
Connection conn = dataSource.getConnection();
ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), getSchema(), tableName, null)) {
return rs.next();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* determines whether a field exists in the specified table
*
* @param tableName tableName
* @param columnName columnName
* @return if column name exist return true,else return false
*/
@Override
public boolean isExistsColumn(String tableName, String columnName) {
private String getSchema() throws SQLException {
try (
Connection conn = dataSource.getConnection();
ResultSet rs = conn.getMetaData().getColumns(conn.getCatalog(), getSchema(), tableName, columnName)) {
return rs.next();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
PreparedStatement pstmt = conn.prepareStatement("select current_schema()");
ResultSet resultSet = pstmt.executeQuery()) {
while (resultSet.next()) {
if (resultSet.isFirst()) {
return resultSet.getString(1);
}
}
}
return "";
}

}
Loading

0 comments on commit a5284e4

Please sign in to comment.