mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 10:43:06 +08:00
support view trigger producer function
This commit is contained in:
@ -8,8 +8,13 @@ import java.util.List;
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.jdbc.DefaultMetaService;
|
||||
import ai.chat2db.spi.model.Database;
|
||||
import ai.chat2db.spi.model.Function;
|
||||
import ai.chat2db.spi.model.Procedure;
|
||||
import ai.chat2db.spi.model.Schema;
|
||||
import ai.chat2db.spi.model.Table;
|
||||
import ai.chat2db.spi.model.Trigger;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
|
||||
import static ai.chat2db.plugin.postgresql.consts.SQLConst.FUNCTION_SQL;
|
||||
|
||||
@ -74,4 +79,130 @@ public class PostgreSQLMetaData extends DefaultMetaService implements MetaData {
|
||||
return databases;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private static String ROUTINES_SQL
|
||||
= " SELECT p.proname, p.prokind, pg_catalog.pg_get_functiondef(p.oid) as \"code\" FROM pg_catalog.pg_proc p "
|
||||
+ "where p.prokind = '%s' and p.proname='%s';";
|
||||
|
||||
@Override
|
||||
public Function function(Connection connection, @NotEmpty String databaseName, String schemaName,
|
||||
String functionName) {
|
||||
|
||||
String sql = String.format(ROUTINES_SQL,"f", functionName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
Function function = new Function();
|
||||
function.setDatabaseName(databaseName);
|
||||
function.setSchemaName(schemaName);
|
||||
function.setFunctionName(functionName);
|
||||
function.setFunctionBody(resultSet.getString("code"));
|
||||
return function;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private static String TRIGGER_SQL
|
||||
= "SELECT n.nspname AS \"schema\", c.relname AS \"table_name\", t.tgname AS \"trigger_name\", t.tgenabled AS "
|
||||
+ "\"enabled\", pg_get_triggerdef(t.oid) AS \"trigger_body\" FROM pg_trigger t JOIN pg_class c ON c.oid = t"
|
||||
+ ".tgrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = '%s' AND t.tgname ='%s';";
|
||||
|
||||
private static String TRIGGER_SQL_LIST
|
||||
= "SELECT n.nspname AS \"schema\", c.relname AS \"table_name\", t.tgname AS \"trigger_name\", t.tgenabled AS "
|
||||
+ "\"enabled\", pg_get_triggerdef(t.oid) AS \"trigger_body\" FROM pg_trigger t JOIN pg_class c ON c.oid = t"
|
||||
+ ".tgrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = '%s';";
|
||||
|
||||
|
||||
@Override
|
||||
public List<Trigger> triggers(Connection connection,String databaseName, String schemaName) {
|
||||
List<Trigger> triggers = new ArrayList<>();
|
||||
String sql = String.format(TRIGGER_SQL_LIST, schemaName);
|
||||
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, schemaName, 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("trigger_body"));
|
||||
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,"p", procedureName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
Procedure procedure = new Procedure();
|
||||
procedure.setDatabaseName(databaseName);
|
||||
procedure.setSchemaName(schemaName);
|
||||
procedure.setProcedureName(procedureName);
|
||||
procedure.setProcedureBody(resultSet.getString("code"));
|
||||
return procedure;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private static String VIEW_SQL
|
||||
= "SELECT schemaname, viewname, definition FROM pg_views WHERE schemaname = '%s' AND viewname = '%s';";
|
||||
|
||||
@Override
|
||||
public Table view(Connection connection, String databaseName, String schemaName, String viewName) {
|
||||
String sql = String.format(VIEW_SQL, schemaName, viewName);
|
||||
return SQLExecutor.getInstance().executeSql(connection, sql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
Table table = new Table();
|
||||
table.setDatabaseName(databaseName);
|
||||
table.setSchemaName(schemaName);
|
||||
table.setName(viewName);
|
||||
table.setDdl(resultSet.getString("definition"));
|
||||
return table;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user