3
3
import ai .chat2db .plugin .db2 .constant .SQLConstant ;
4
4
import ai .chat2db .spi .DBManage ;
5
5
import ai .chat2db .spi .jdbc .DefaultDBManage ;
6
+ import ai .chat2db .spi .model .AsyncContext ;
6
7
import ai .chat2db .spi .sql .Chat2DBContext ;
7
8
import ai .chat2db .spi .sql .ConnectInfo ;
8
9
import ai .chat2db .spi .sql .SQLExecutor ;
18
19
public class DB2DBManage extends DefaultDBManage implements DBManage {
19
20
20
21
@ Override
21
- public String exportDatabase (Connection connection , String databaseName , String schemaName , boolean containData ) throws SQLException {
22
- StringBuilder sqlBuilder = new StringBuilder ();
23
- exportTables (connection , schemaName , sqlBuilder , containData );
24
- exportViews (connection , schemaName , sqlBuilder );
25
- exportProceduresAndFunctions (connection , schemaName , sqlBuilder );
26
- exportTriggers (connection , schemaName , sqlBuilder );
27
- return sqlBuilder .toString ();
22
+ public void exportDatabase (Connection connection , String databaseName , String schemaName , AsyncContext asyncContext ) throws SQLException {
23
+ exportTables (connection , databaseName , schemaName , asyncContext );
24
+ exportViews (connection , schemaName , asyncContext );
25
+ exportProceduresAndFunctions (connection , schemaName , asyncContext );
26
+ exportTriggers (connection , schemaName , asyncContext );
28
27
}
29
28
30
- private void exportTables (Connection connection , String schemaName , StringBuilder sqlBuilder , boolean containData ) throws SQLException {
29
+ private void exportTables (Connection connection , String databaseName , String schemaName , AsyncContext asyncContext ) throws SQLException {
31
30
try (ResultSet resultSet = connection .getMetaData ().getTables (null , schemaName , null , new String []{"TABLE" , "SYSTEM TABLE" })) {
32
31
while (resultSet .next ()) {
33
- exportTable (connection , schemaName , resultSet .getString ("TABLE_NAME" ), sqlBuilder , containData );
32
+ exportTable (connection , databaseName , schemaName , resultSet .getString ("TABLE_NAME" ), asyncContext );
34
33
}
35
34
}
36
35
}
37
36
38
37
39
- private void exportTable (Connection connection , String schemaName , String tableName , StringBuilder sqlBuilder , boolean containData ) throws SQLException {
38
+ public void exportTable (Connection connection , String databaseName , String schemaName , String tableName , AsyncContext asyncContext ) throws SQLException {
40
39
try {
41
40
SQLExecutor .getInstance ().execute (connection , SQLConstant .TABLE_DDL_FUNCTION_SQL , resultSet -> null );
42
41
} catch (Exception e ) {
@@ -45,42 +44,50 @@ private void exportTable(Connection connection, String schemaName, String tableN
45
44
String sql = String .format ("select %s.GENERATE_TABLE_DDL('%s', '%s') as sql from %s;" , schemaName , schemaName , tableName , tableName );
46
45
try (ResultSet resultSet = connection .createStatement ().executeQuery (sql )) {
47
46
if (resultSet .next ()) {
47
+ StringBuilder sqlBuilder = new StringBuilder ();
48
48
sqlBuilder .append (resultSet .getString ("sql" )).append ("\n " );
49
- if (containData ) {
50
- exportTableData (connection , schemaName , tableName , sqlBuilder );
49
+ asyncContext .write (sqlBuilder .toString ());
50
+ if (asyncContext .isContainsData ()) {
51
+ exportTableData (connection , databaseName , schemaName , tableName , asyncContext );
51
52
}
52
53
}
53
54
}
54
55
}
55
56
56
57
57
- private void exportViews (Connection connection , String schemaName , StringBuilder sqlBuilder ) throws SQLException {
58
+ private void exportViews (Connection connection , String schemaName , AsyncContext asyncContext ) throws SQLException {
58
59
String sql = String .format ("select TEXT from syscat.views where VIEWSCHEMA='%s';" , schemaName );
59
60
try (ResultSet resultSet = connection .createStatement ().executeQuery (sql )) {
60
61
while (resultSet .next ()) {
62
+ StringBuilder sqlBuilder = new StringBuilder ();
61
63
String ddl = resultSet .getString ("TEXT" );
62
64
sqlBuilder .append (ddl ).append (";" ).append ("\n " );
65
+ asyncContext .write (sqlBuilder .toString ());
63
66
}
64
67
}
65
68
}
66
69
67
- private void exportProceduresAndFunctions (Connection connection , String schemaName , StringBuilder sqlBuilder ) throws SQLException {
70
+ private void exportProceduresAndFunctions (Connection connection , String schemaName , AsyncContext asyncContext ) throws SQLException {
68
71
String sql = String .format ("select TEXT from syscat.routines where ROUTINESCHEMA='%s';" , schemaName );
69
72
try (ResultSet resultSet = connection .createStatement ().executeQuery (sql )) {
70
73
while (resultSet .next ()) {
74
+ StringBuilder sqlBuilder = new StringBuilder ();
71
75
String ddl = resultSet .getString ("TEXT" );
72
76
sqlBuilder .append (ddl ).append (";" ).append ("\n " );
77
+ asyncContext .write (sqlBuilder .toString ());
73
78
}
74
79
}
75
80
}
76
81
77
82
78
- private void exportTriggers (Connection connection , String schemaName , StringBuilder sqlBuilder ) throws SQLException {
83
+ private void exportTriggers (Connection connection , String schemaName , AsyncContext asyncContext ) throws SQLException {
79
84
String sql = String .format ("select * from SYSCAT.TRIGGERS where TRIGSCHEMA = '%s';" , schemaName );
80
85
try (ResultSet resultSet = connection .createStatement ().executeQuery (sql )) {
81
86
while (resultSet .next ()) {
87
+ StringBuilder sqlBuilder = new StringBuilder ();
82
88
String ddl = resultSet .getString ("TEXT" );
83
89
sqlBuilder .append (ddl ).append (";" ).append ("\n " );
90
+ asyncContext .write (sqlBuilder .toString ());
84
91
}
85
92
}
86
93
}
@@ -104,4 +111,14 @@ public void dropTable(Connection connection, String databaseName, String schemaN
104
111
String sql = "DROP TABLE " + tableName ;
105
112
SQLExecutor .getInstance ().execute (connection , sql , resultSet -> null );
106
113
}
114
+
115
+ @ Override
116
+ public void copyTable (Connection connection , String databaseName , String schemaName , String tableName , String newTableName ,boolean copyData ) throws SQLException {
117
+ String sql = "CREATE TABLE " + newTableName + " LIKE " + tableName + " INCLUDING INDEXES" ;
118
+ SQLExecutor .getInstance ().execute (connection , sql , resultSet -> null );
119
+ if (copyData ){
120
+ sql = "INSERT INTO " + newTableName + " SELECT * FROM " + tableName ;
121
+ SQLExecutor .getInstance ().execute (connection , sql , resultSet -> null );
122
+ }
123
+ }
107
124
}
0 commit comments