mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-09-20 11:15:15 +08:00
support view trigger producer function
This commit is contained in:
@ -2,18 +2,24 @@ package ai.chat2db.plugin.mysql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.jdbc.DefaultMetaService;
|
||||
import ai.chat2db.spi.model.Function;
|
||||
import ai.chat2db.spi.model.Procedure;
|
||||
import ai.chat2db.spi.model.Trigger;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
|
||||
public class MysqlMetaData extends DefaultMetaService implements MetaData {
|
||||
@Override
|
||||
public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName, @NotEmpty String tableName) {
|
||||
public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName,
|
||||
@NotEmpty String tableName) {
|
||||
String sql = "SHOW CREATE TABLE " + format(databaseName) + "."
|
||||
+ format( tableName);
|
||||
return SQLExecutor.getInstance().executeSql(connection,sql, resultSet -> {
|
||||
+ format(tableName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getString("Create Table");
|
||||
@ -28,4 +34,106 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
|
||||
public static String format(String tableName) {
|
||||
return "`" + tableName + "`";
|
||||
}
|
||||
|
||||
private static String ROUTINES_SQL
|
||||
= "SELECT SPECIFIC_NAME, ROUTINE_COMMENT, ROUTINE_DEFINITION FROM information_schema.routines WHERE routine_type = '%s' AND ROUTINE_SCHEMA ='%s' AND "
|
||||
+ "routine_name = '%s';";
|
||||
|
||||
@Override
|
||||
public Function function(Connection connection, @NotEmpty String databaseName, String schemaName,
|
||||
String functionName) {
|
||||
|
||||
String sql = String.format(ROUTINES_SQL,"FUNCTION", databaseName, functionName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
Function function = new Function();
|
||||
function.setDatabaseName(databaseName);
|
||||
function.setSchemaName(schemaName);
|
||||
function.setSpecificName(resultSet.getString("SPECIFIC_NAME"));
|
||||
function.setRemarks(resultSet.getString("ROUTINE_COMMENT"));
|
||||
function.setFunctionName(functionName);
|
||||
function.setFunctionBody(resultSet.getString("ROUTINE_DEFINITION"));
|
||||
return function;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private static String TRIGGER_SQL
|
||||
= "SELECT TRIGGER_NAME,EVENT_MANIPULATION, ACTION_STATEMENT FROM INFORMATION_SCHEMA.TRIGGERS where TRIGGER_SCHEMA = '%s' AND TRIGGER_NAME = '%s';";
|
||||
|
||||
private static String TRIGGER_SQL_LIST
|
||||
= "SELECT TRIGGER_NAME FROM INFORMATION_SCHEMA.TRIGGERS where TRIGGER_SCHEMA = '%s';";
|
||||
|
||||
|
||||
@Override
|
||||
public List<Trigger> triggers(Connection connection,String databaseName, String schemaName) {
|
||||
List<Trigger> triggers = new ArrayList<>();
|
||||
String sql = String.format(TRIGGER_SQL_LIST, databaseName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
while (resultSet.next()) {
|
||||
Trigger trigger = new Trigger();
|
||||
trigger.setTriggerName(resultSet.getString("TRIGGER_NAME"));
|
||||
trigger.setSchemaName(schemaName);
|
||||
trigger.setDatabaseName(databaseName);
|
||||
triggers.add(trigger);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return triggers;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Trigger trigger(Connection connection, @NotEmpty String databaseName, String schemaName,
|
||||
String triggerName) {
|
||||
|
||||
String sql = String.format(TRIGGER_SQL, databaseName, triggerName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
Trigger trigger = new Trigger();
|
||||
trigger.setDatabaseName(databaseName);
|
||||
trigger.setSchemaName(schemaName);
|
||||
trigger.setTriggerName(triggerName);
|
||||
trigger.setTriggerBody(resultSet.getString("ACTION_STATEMENT"));
|
||||
return trigger;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Procedure procedure(Connection connection, @NotEmpty String databaseName, String schemaName,
|
||||
String procedureName) {
|
||||
String sql = String.format(ROUTINES_SQL,"PROCEDURE", databaseName, procedureName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
Procedure procedure = new Procedure();
|
||||
procedure.setDatabaseName(databaseName);
|
||||
procedure.setSchemaName(schemaName);
|
||||
procedure.setSpecificName(resultSet.getString("SPECIFIC_NAME"));
|
||||
procedure.setRemarks(resultSet.getString("ROUTINE_COMMENT"));
|
||||
procedure.setProcedureName(procedureName);
|
||||
procedure.setProcedureBody(resultSet.getString("ROUTINE_DEFINITION"));
|
||||
return procedure;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user