|
| 1 | +package com.yz.controller; |
| 2 | + |
| 3 | +import com.yz.bean.Property; |
| 4 | +import com.yz.database.Operation; |
| 5 | +import com.yz.database.OperationFactory; |
| 6 | +import com.yz.datasources.DataSourceContextHolder; |
| 7 | +import com.yz.datasources.DataSourceManager; |
| 8 | +import com.yz.service.MigrationService; |
| 9 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | +import org.springframework.web.bind.annotation.RestController; |
| 11 | + |
| 12 | +import javax.annotation.Resource; |
| 13 | +import javax.servlet.http.HttpSession; |
| 14 | +import javax.sql.DataSource; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by yz on 17/2/7. |
| 22 | + */ |
| 23 | +@RestController |
| 24 | +public class MigrationController { |
| 25 | + |
| 26 | + @Resource(name = "operationFactory") |
| 27 | + private OperationFactory operationFactory; |
| 28 | + |
| 29 | + @Resource(name = "migrationServiceImpl") |
| 30 | + private MigrationService migrationServiceImpl; |
| 31 | + |
| 32 | + @RequestMapping("/create_datasource") |
| 33 | + public Object createDataSource(HttpSession session, Property property, int flag, int databaseType){ |
| 34 | + switch (databaseType){ |
| 35 | + case 1: |
| 36 | + property.setDriver("com.mysql.jdbc.Driver"); |
| 37 | + break; |
| 38 | + case 2: |
| 39 | + property.setDriver("org.sqlite.JDBC"); |
| 40 | + break; |
| 41 | + case 3: |
| 42 | + property.setDriver("org.postgresql.Driver"); |
| 43 | + break; |
| 44 | + case 4: |
| 45 | + property.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
| 46 | + break; |
| 47 | + } |
| 48 | + boolean result = migrationServiceImpl.createDataSource(session.getId(), property, flag); |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + @RequestMapping("/tables") |
| 53 | + public Object getTables(HttpSession session,int databaseType,int flag){ |
| 54 | + Operation operation = operationFactory.getOperation(databaseType); |
| 55 | + DataSource dataSource = DataSourceManager.getInstance().getUserDataSource(session.getId(), flag); |
| 56 | + DataSourceContextHolder.setDataSource(dataSource); |
| 57 | + List<String> tables = operation.queryTables(); |
| 58 | + Collections.sort(tables); |
| 59 | + return tables; |
| 60 | + } |
| 61 | + |
| 62 | + @RequestMapping("/table/fields") |
| 63 | + public Object getTableSchema(HttpSession session,String tableName,int databaseType,int flag){ |
| 64 | + DataSource dataSource = DataSourceManager.getInstance().getUserDataSource(session.getId(), flag); |
| 65 | + DataSourceContextHolder.setDataSource(dataSource); |
| 66 | + Operation operation = operationFactory.getOperation(databaseType); |
| 67 | + List<Map<String, Object>> mapList = operation.queryTableSchema(tableName); |
| 68 | + List<String> fields = mapList.stream().map(map -> map.get("Field").toString()).collect(Collectors.toList()); |
| 69 | + Collections.sort(fields); |
| 70 | + return fields; |
| 71 | + } |
| 72 | + |
| 73 | + @RequestMapping("/data/migration") |
| 74 | + public Object migration(HttpSession session,String[]fields,String[]targetFields,String tableName,String targetTableName){ |
| 75 | + boolean b = migrationServiceImpl.dataMigration(session.getId(), fields, targetFields, tableName, targetTableName); |
| 76 | + return b; |
| 77 | + } |
| 78 | + |
| 79 | + @RequestMapping("/sql/execute") |
| 80 | + public Object execute(HttpSession session,String sourceSql,String tegetSql){ |
| 81 | + long execute = migrationServiceImpl.execute(session.getId(), sourceSql, tegetSql); |
| 82 | + return execute; |
| 83 | + } |
| 84 | +} |
0 commit comments