11package ai .chat2db .plugin .mysql ;
22
33import java .sql .Connection ;
4- import java .sql .SQLException ;
54import java .util .ArrayList ;
65import java .util .List ;
76
@@ -20,13 +19,9 @@ public String tableDDL(Connection connection, @NotEmpty String databaseName, Str
2019 @ NotEmpty String tableName ) {
2120 String sql = "SHOW CREATE TABLE " + format (databaseName ) + "."
2221 + format (tableName );
23- return SQLExecutor .getInstance ().executeSql (connection , sql , resultSet -> {
24- try {
25- if (resultSet .next ()) {
26- return resultSet .getString ("Create Table" );
27- }
28- } catch (SQLException e ) {
29- throw new RuntimeException (e );
22+ return SQLExecutor .getInstance ().execute (connection , sql , resultSet -> {
23+ if (resultSet .next ()) {
24+ return resultSet .getString ("Create Table" );
3025 }
3126 return null ;
3227 });
@@ -37,127 +32,105 @@ public static String format(String tableName) {
3732 }
3833
3934 private static String ROUTINES_SQL
40- = "SELECT SPECIFIC_NAME, ROUTINE_COMMENT, ROUTINE_DEFINITION FROM information_schema.routines WHERE routine_type = '%s' AND ROUTINE_SCHEMA ='%s' AND "
41- + "routine_name = '%s';" ;
35+ =
36+ "SELECT SPECIFIC_NAME, ROUTINE_COMMENT, ROUTINE_DEFINITION FROM information_schema.routines WHERE "
37+ + "routine_type = '%s' AND ROUTINE_SCHEMA ='%s' AND "
38+ + "routine_name = '%s';" ;
4239
4340 @ Override
4441 public Function function (Connection connection , @ NotEmpty String databaseName , String schemaName ,
4542 String functionName ) {
4643
47- String sql = String .format (ROUTINES_SQL ,"FUNCTION" , databaseName , functionName );
48- return SQLExecutor .getInstance ().executeSql (connection , sql , resultSet -> {
49- try {
50- if (resultSet .next ()) {
51- Function function = new Function ();
52- function .setDatabaseName (databaseName );
53- function .setSchemaName (schemaName );
54- function .setSpecificName (resultSet .getString ("SPECIFIC_NAME" ));
55- function .setRemarks (resultSet .getString ("ROUTINE_COMMENT" ));
56- function .setFunctionName (functionName );
57- function .setFunctionBody (resultSet .getString ("ROUTINE_DEFINITION" ));
58- return function ;
59- }
60- } catch (SQLException e ) {
61- throw new RuntimeException (e );
44+ String sql = String .format (ROUTINES_SQL , "FUNCTION" , databaseName , functionName );
45+ return SQLExecutor .getInstance ().execute (connection , sql , resultSet -> {
46+ Function function = new Function ();
47+ function .setDatabaseName (databaseName );
48+ function .setSchemaName (schemaName );
49+ function .setFunctionName (functionName );
50+ if (resultSet .next ()) {
51+ function .setSpecificName (resultSet .getString ("SPECIFIC_NAME" ));
52+ function .setRemarks (resultSet .getString ("ROUTINE_COMMENT" ));
53+ function .setFunctionBody (resultSet .getString ("ROUTINE_DEFINITION" ));
6254 }
63- return null ;
55+ return function ;
6456 });
6557
6658 }
6759
6860 private static String TRIGGER_SQL
69- = "SELECT TRIGGER_NAME,EVENT_MANIPULATION, ACTION_STATEMENT FROM INFORMATION_SCHEMA.TRIGGERS where TRIGGER_SCHEMA = '%s' AND TRIGGER_NAME = '%s';" ;
61+ = "SELECT TRIGGER_NAME,EVENT_MANIPULATION, ACTION_STATEMENT FROM INFORMATION_SCHEMA.TRIGGERS where "
62+ + "TRIGGER_SCHEMA = '%s' AND TRIGGER_NAME = '%s';" ;
7063
7164 private static String TRIGGER_SQL_LIST
7265 = "SELECT TRIGGER_NAME FROM INFORMATION_SCHEMA.TRIGGERS where TRIGGER_SCHEMA = '%s';" ;
7366
74-
7567 @ Override
76- public List <Trigger > triggers (Connection connection ,String databaseName , String schemaName ) {
68+ public List <Trigger > triggers (Connection connection , String databaseName , String schemaName ) {
7769 List <Trigger > triggers = new ArrayList <>();
7870 String sql = String .format (TRIGGER_SQL_LIST , databaseName );
79- return SQLExecutor .getInstance ().executeSql (connection , sql , resultSet -> {
80- try {
81- while (resultSet .next ()) {
82- Trigger trigger = new Trigger ();
83- trigger .setTriggerName (resultSet .getString ("TRIGGER_NAME" ));
84- trigger .setSchemaName (schemaName );
85- trigger .setDatabaseName (databaseName );
86- triggers .add (trigger );
87- }
88- } catch (SQLException e ) {
89- throw new RuntimeException (e );
71+ return SQLExecutor .getInstance ().execute (connection , sql , resultSet -> {
72+ while (resultSet .next ()) {
73+ Trigger trigger = new Trigger ();
74+ trigger .setTriggerName (resultSet .getString ("TRIGGER_NAME" ));
75+ trigger .setSchemaName (schemaName );
76+ trigger .setDatabaseName (databaseName );
77+ triggers .add (trigger );
9078 }
9179 return triggers ;
9280 });
9381 }
9482
95-
9683 @ Override
9784 public Trigger trigger (Connection connection , @ NotEmpty String databaseName , String schemaName ,
9885 String triggerName ) {
9986
10087 String sql = String .format (TRIGGER_SQL , databaseName , triggerName );
101- return SQLExecutor .getInstance ().executeSql (connection , sql , resultSet -> {
102- try {
103- if (resultSet .next ()) {
104- Trigger trigger = new Trigger ();
105- trigger .setDatabaseName (databaseName );
106- trigger .setSchemaName (schemaName );
107- trigger .setTriggerName (triggerName );
108- trigger .setTriggerBody (resultSet .getString ("ACTION_STATEMENT" ));
109- return trigger ;
110- }
111- } catch (SQLException e ) {
112- throw new RuntimeException (e );
88+ return SQLExecutor .getInstance ().execute (connection , sql , resultSet -> {
89+ Trigger trigger = new Trigger ();
90+ trigger .setDatabaseName (databaseName );
91+ trigger .setSchemaName (schemaName );
92+ trigger .setTriggerName (triggerName );
93+ if (resultSet .next ()) {
94+ trigger .setTriggerBody (resultSet .getString ("ACTION_STATEMENT" ));
11395 }
114- return null ;
96+ return trigger ;
11597 });
11698 }
11799
118100 @ Override
119101 public Procedure procedure (Connection connection , @ NotEmpty String databaseName , String schemaName ,
120102 String procedureName ) {
121- String sql = String .format (ROUTINES_SQL ,"PROCEDURE" , databaseName , procedureName );
122- return SQLExecutor .getInstance ().executeSql (connection , sql , resultSet -> {
123- try {
124- if (resultSet .next ()) {
125- Procedure procedure = new Procedure ();
126- procedure .setDatabaseName (databaseName );
127- procedure .setSchemaName (schemaName );
128- procedure .setSpecificName (resultSet .getString ("SPECIFIC_NAME" ));
129- procedure .setRemarks (resultSet .getString ("ROUTINE_COMMENT" ));
130- procedure .setProcedureName (procedureName );
131- procedure .setProcedureBody (resultSet .getString ("ROUTINE_DEFINITION" ));
132- return procedure ;
133- }
134- } catch (SQLException e ) {
135- throw new RuntimeException (e );
103+ String sql = String .format (ROUTINES_SQL , "PROCEDURE" , databaseName , procedureName );
104+ return SQLExecutor .getInstance ().execute (connection , sql , resultSet -> {
105+ Procedure procedure = new Procedure ();
106+ procedure .setDatabaseName (databaseName );
107+ procedure .setSchemaName (schemaName );
108+ procedure .setProcedureName (procedureName );
109+ if (resultSet .next ()) {
110+ procedure .setSpecificName (resultSet .getString ("SPECIFIC_NAME" ));
111+ procedure .setRemarks (resultSet .getString ("ROUTINE_COMMENT" ));
112+ procedure .setProcedureBody (resultSet .getString ("ROUTINE_DEFINITION" ));
136113 }
137- return null ;
114+ return procedure ;
138115 });
139116 }
140117
141118 private static String VIEW_SQL
142- = "SELECT TABLE_SCHEMA AS DatabaseName, TABLE_NAME AS ViewName, VIEW_DEFINITION AS definition, CHECK_OPTION, IS_UPDATABLE FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s';" ;
119+ = "SELECT TABLE_SCHEMA AS DatabaseName, TABLE_NAME AS ViewName, VIEW_DEFINITION AS definition, CHECK_OPTION, "
120+ + "IS_UPDATABLE FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s';" ;
143121
144122 @ Override
145123 public Table view (Connection connection , String databaseName , String schemaName , String viewName ) {
146124 String sql = String .format (VIEW_SQL , databaseName , viewName );
147- return SQLExecutor .getInstance ().executeSql (connection , sql , resultSet -> {
148- try {
149- if (resultSet .next ()) {
150- Table table = new Table ();
151- table .setDatabaseName (databaseName );
152- table .setSchemaName (schemaName );
153- table .setName (viewName );
154- table .setDdl (resultSet .getString ("definition" ));
155- return table ;
156- }
157- } catch (SQLException e ) {
158- throw new RuntimeException (e );
125+ return SQLExecutor .getInstance ().execute (connection , sql , resultSet -> {
126+ Table table = new Table ();
127+ table .setDatabaseName (databaseName );
128+ table .setSchemaName (schemaName );
129+ table .setName (viewName );
130+ if (resultSet .next ()) {
131+ table .setDdl (resultSet .getString ("definition" ));
159132 }
160- return null ;
133+ return table ;
161134 });
162135 }
163136}
0 commit comments