Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Apache Dolphinscheduler DataSourceUtils Query

From Leeroopedia
Revision as of 14:12, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Apache_Dolphinscheduler_DataSourceUtils_Query.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Data_Integration, SQL_Processing
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for executing SQL queries using DataSourceUtils facade with Druid-based SQL parsing (splitAndRemoveComment) and pooled connection retrieval.

Description

'DataSourceUtils provides facade methods: getConnection(DbType, ConnectionParam) obtains a pooled JDBC connection, getJdbcUrl(DbType, ConnectionParam) constructs the JDBC URL, and getDatasourceDriver(DbType) returns the driver class name. AbstractDataSourceProcessor.splitAndRemoveComment(String) at L136 uses SQLParserUtils.removeComment() followed by SQLParserUtils.split() from the Alibaba Druid library to safely parse multi-statement SQL.

Usage

Used by SQL tasks to parse and execute SQL. The splitAndRemoveComment() method is called on user-submitted SQL before execution.

Code Reference

Source Location

  • Repository: dolphinscheduler
  • File: dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/utils/DataSourceUtils.java (L61-69)
  • File: dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/datasource/AbstractDataSourceProcessor.java (L136-139)

Signature

public class DataSourceUtils {
    public static Connection getConnection(DbType dbType, ConnectionParam connectionParam);
    public static String getJdbcUrl(DbType dbType, ConnectionParam connectionParam);
    public static String getDatasourceDriver(DbType dbType);
    public static void checkDatasourceParam(BaseDataSourceParamDTO param);
    public static ConnectionParam buildConnectionParams(DbType dbType, BaseDataSourceParamDTO param);
}

// In AbstractDataSourceProcessor
public List<String> splitAndRemoveComment(String sql) {
    String cleanSql = SQLParserUtils.removeComment(sql, DbType.other.name());
    return SQLParserUtils.split(cleanSql, DbType.other.name());
}

Import

import org.apache.dolphinscheduler.plugin.datasource.api.utils.DataSourceUtils;

I/O Contract

Inputs

Name Type Required Description
sql String Yes Multi-statement SQL with possible comments
dbType DbType Yes Database type for connection lookup
connectionParam ConnectionParam Yes Resolved connection parameters

Outputs

Name Type Description
List<String> List Individual SQL statements (comments removed)
Connection java.sql.Connection Pooled JDBC connection for execution

Usage Examples

SQL Parsing and Execution

// Parse multi-statement SQL
String rawSql = "-- Extract data\n"
    + "SELECT * FROM source_table;\n"
    + "/* Insert into target */\n"
    + "INSERT INTO target_table SELECT * FROM staging;";

DataSourceProcessor processor = DataSourceProcessorManager
    .getDataSourceProcessorMap().get(DbType.MYSQL.name());

List<String> statements = processor.splitAndRemoveComment(rawSql);
// statements = ["SELECT * FROM source_table", "INSERT INTO target_table SELECT * FROM staging"]

// Execute each statement
Connection conn = DataSourceUtils.getConnection(DbType.MYSQL, connectionParam);
try {
    for (String sql : statements) {
        conn.createStatement().execute(sql);
    }
} finally {
    conn.close();
}

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment