├── .gitignore
├── LICENSE
├── README.md
├── README_EN.md
├── images
└── 数据库同步工具.png
├── libs
└── DmJdbcDriver18.jar
├── pom.xml
└── src
└── main
├── java
└── com
│ └── xwintop
│ └── xJavaFxTool
│ ├── RdbmsSyncToolMain.java
│ ├── controller
│ └── debugTools
│ │ └── RdbmsSyncToolController.java
│ ├── services
│ └── debugTools
│ │ └── RdbmsSyncToolService.java
│ ├── tools
│ ├── DataxJsonUtil.java
│ └── SqlUtil.java
│ ├── utils
│ ├── ActionJob.java
│ └── ActionScheduleUtil.java
│ └── view
│ └── debugTools
│ └── RdbmsSyncToolView.java
└── resources
├── com
└── xwintop
│ └── xJavaFxTool
│ └── fxmlView
│ └── debugTools
│ └── RdbmsSyncTool.fxml
├── config
└── toolFxmlLoaderConfiguration.xml
└── locale
├── RdbmsSyncTool.properties
└── RdbmsSyncTool_en_US.properties
/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | #/.gitignore
3 | .idea
4 | /x-RdbmsSyncTool.iml
5 | /log/
6 | /target/
7 | /javaFxConfigure
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | 木兰宽松许可证, 第2版
2 |
3 | 木兰宽松许可证, 第2版
4 | 2020年1月 http://license.coscl.org.cn/MulanPSL2
5 |
6 |
7 | 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束:
8 |
9 | 0. 定义
10 |
11 | “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
12 |
13 | “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
14 |
15 | “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
16 |
17 | “法人实体”是指提交贡献的机构及其“关联实体”。
18 |
19 | “关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
20 |
21 | 1. 授予版权许可
22 |
23 | 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。
24 |
25 | 2. 授予专利许可
26 |
27 | 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。
28 |
29 | 3. 无商标许可
30 |
31 | “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。
32 |
33 | 4. 分发限制
34 |
35 | 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
36 |
37 | 5. 免责声明与责任限制
38 |
39 | “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。
40 |
41 | 6. 语言
42 | “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。
43 |
44 | 条款结束
45 |
46 | 如何将木兰宽松许可证,第2版,应用到您的软件
47 |
48 | 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步:
49 |
50 | 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
51 |
52 | 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中;
53 |
54 | 3, 请将如下声明文本放入每个源文件的头部注释中。
55 |
56 | Copyright (c) [Year] [name of copyright holder]
57 | [Software Name] is licensed under Mulan PSL v2.
58 | You can use this software according to the terms and conditions of the Mulan PSL v2.
59 | You may obtain a copy of Mulan PSL v2 at:
60 | http://license.coscl.org.cn/MulanPSL2
61 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
62 | See the Mulan PSL v2 for more details.
63 |
64 |
65 | Mulan Permissive Software License,Version 2
66 |
67 | Mulan Permissive Software License,Version 2 (Mulan PSL v2)
68 | January 2020 http://license.coscl.org.cn/MulanPSL2
69 |
70 | Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions:
71 |
72 | 0. Definition
73 |
74 | Software means the program and related documents which are licensed under this License and comprise all Contribution(s).
75 |
76 | Contribution means the copyrightable work licensed by a particular Contributor under this License.
77 |
78 | Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License.
79 |
80 | Legal Entity means the entity making a Contribution and all its Affiliates.
81 |
82 | Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity.
83 |
84 | 1. Grant of Copyright License
85 |
86 | Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not.
87 |
88 | 2. Grant of Patent License
89 |
90 | Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken.
91 |
92 | 3. No Trademark License
93 |
94 | No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4.
95 |
96 | 4. Distribution Restriction
97 |
98 | You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software.
99 |
100 | 5. Disclaimer of Warranty and Limitation of Liability
101 |
102 | THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
103 |
104 | 6. Language
105 |
106 | THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL.
107 |
108 | END OF THE TERMS AND CONDITIONS
109 |
110 | How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software
111 |
112 | To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps:
113 |
114 | i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner;
115 |
116 | ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package;
117 |
118 | iii Attach the statement to the appropriate annotated syntax at the beginning of each source file.
119 |
120 |
121 | Copyright (c) [Year] [name of copyright holder]
122 | [Software Name] is licensed under Mulan PSL v2.
123 | You can use this software according to the terms and conditions of the Mulan PSL v2.
124 | You may obtain a copy of Mulan PSL v2 at:
125 | http://license.coscl.org.cn/MulanPSL2
126 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
127 | See the Mulan PSL v2 for more details.
128 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | RdbmsSyncTool 关系型数据库同步工具
2 |
3 | #### 项目简介:
4 | RdbmsSyncTool是使用javaFx开发的关系型数据库同步工具,完成关系型数据库表结构获取,快捷执行一些常用数据库脚本,支持多种类型数据库直接数据转移,同步。
5 |
6 | 目前支持的数据库类型有mysql、Oracle、sqlserver、PostgreSql、达梦、sqlite、h2、access、db2等。
7 |
8 | **xJavaFxTool交流QQ群:== [387473650](https://jq.qq.com/?_wv=1027&k=59UDEAD) ==**
9 |
10 | #### 环境搭建说明:
11 | - 开发环境为jdk1.8,基于maven构建
12 | - 使用eclipase或Intellij Idea开发(推荐使用[Intellij Idea](https://www.jetbrains.com/?from=xJavaFxTool))
13 | - 该项目为javaFx开发的实用小工具集[xJavaFxTool](https://gitee.com/xwintop/xJavaFxTool)的插件。
14 | - 本项目使用了[lombok](https://projectlombok.org/),在查看本项目时如果您没有下载lombok 插件,请先安装,不然找不到get/set等方法
15 | - 依赖的[xcore包](https://gitee.com/xwintop/xcore)已上传至git托管的maven平台,git托管maven可参考教程(若无法下载请拉取项目自行编译)。[教程地址:点击进入](http://blog.csdn.net/u011747754/article/details/78574026)
16 |
17 | 
18 |
19 | #### 版本记录
20 | - 0.0.1 20200419
21 | 1. 完成基本功能配置(对表进行查询、删除、建表语句、同步数据等操作)
22 | 2. 支持mysql、Oracle、sqlserver、PostgreSql、达梦等数据库连接
23 | - 0.0.2 20200421
24 | 1. 添加sqlite、h2、access数据库支持
25 | 2. 优化数据同步功能
26 | - 0.0.3
27 | 1. 添加多Schema输入框
28 | 2. 添加表名过滤正则支持
29 | 3. 添加时间和主键过滤条件支持
30 | 4. 添加db2数据库支持
31 |
--------------------------------------------------------------------------------
/README_EN.md:
--------------------------------------------------------------------------------
1 | RdbmsSyncTool
2 |
--------------------------------------------------------------------------------
/images/数据库同步工具.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/864381832/x-RdbmsSyncTool/6c24f346235590dc30efb07cb16add07602c6697/images/数据库同步工具.png
--------------------------------------------------------------------------------
/libs/DmJdbcDriver18.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/864381832/x-RdbmsSyncTool/6c24f346235590dc30efb07cb16add07602c6697/libs/DmJdbcDriver18.jar
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.xwintop
6 | x-RdbmsSyncTool
7 | 0.0.3
8 | jar
9 | x-RdbmsSyncTool
10 |
11 |
12 | UTF-8
13 | 4.3.8.RELEASE
14 |
15 |
16 |
17 |
18 | aliyunmaven
19 | http://maven.aliyun.com/nexus/content/groups/public/
20 |
21 |
22 | xwintop-maven
23 | https://xwintop.gitee.io/maven/repository
24 |
25 |
26 | spring-snapshots
27 | http://repo.spring.io/snapshot
28 |
29 | true
30 |
31 |
32 |
33 | spring-milestones
34 | http://repo.spring.io/milestone
35 |
36 |
37 |
38 |
39 |
40 | junit
41 | junit
42 | 4.12
43 | test
44 |
45 |
46 | com.xwintop
47 | xcore
48 | 0.0.6
49 |
50 |
51 |
52 |
53 | org.projectlombok
54 | lombok
55 | 1.18.6
56 | provided
57 |
58 |
59 |
60 | com.alibaba
61 | druid
62 | 1.1.24
63 |
64 |
65 |
66 |
67 | mysql
68 | mysql-connector-java
69 | 8.0.16
70 |
71 |
72 |
73 |
74 | cn.easyproject
75 | ojdbc7
76 | 12.1.0.2.0
77 |
78 |
79 |
80 |
81 | com.microsoft.sqlserver
82 | sqljdbc4
83 | 4.0
84 |
85 |
86 | net.sourceforge.jtds
87 | jtds
88 | 1.3.1
89 |
90 |
91 |
92 | org.postgresql
93 | postgresql
94 | 42.2.12
95 |
96 |
97 |
98 |
99 | com.h2database
100 | h2
101 | 1.4.200
102 |
103 |
104 |
105 |
106 | org.xerial
107 | sqlite-jdbc
108 | 3.28.0
109 |
110 |
111 |
112 | net.sf.ucanaccess
113 | ucanaccess
114 | 4.0.4
115 |
116 |
117 |
118 |
119 | com.ibm.db2
120 | jcc
121 | 11.5.5.0
122 |
123 |
124 |
125 | com.dm
126 | dmjdbc7
127 | 1.7.0
128 | system
129 | ${project.basedir}/libs/DmJdbcDriver18.jar
130 |
131 |
132 |
133 | org.springframework
134 | spring-jdbc
135 | ${spring-framework.version}
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 | org.apache.maven.plugins
144 | maven-assembly-plugin
145 | 2.5.5
146 |
147 | false
148 | utf-8
149 |
150 |
151 | com.xwintop.xJavaFxTool.RdbmsSyncToolMain
152 |
153 |
154 |
155 | jar-with-dependencies
156 |
157 |
158 |
159 |
160 | make-assembly
161 | package
162 |
163 | single
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
--------------------------------------------------------------------------------
/src/main/java/com/xwintop/xJavaFxTool/RdbmsSyncToolMain.java:
--------------------------------------------------------------------------------
1 | package com.xwintop.xJavaFxTool;
2 |
3 | import com.xwintop.xcore.util.javafx.JavaFxSystemUtil;
4 | import javafx.application.Application;
5 | import javafx.event.EventHandler;
6 | import javafx.fxml.FXMLLoader;
7 | import javafx.scene.Parent;
8 | import javafx.scene.Scene;
9 | import javafx.stage.Stage;
10 | import javafx.stage.WindowEvent;
11 | import lombok.extern.slf4j.Slf4j;
12 |
13 | import java.net.URL;
14 | import java.util.ResourceBundle;
15 |
16 | @Slf4j
17 | public class RdbmsSyncToolMain extends Application {
18 | public static void main(String[] args) {
19 | try {
20 | launch(args);
21 | } catch (Exception e) {
22 | e.printStackTrace();
23 | }
24 | }
25 |
26 | @Override
27 | public void start(Stage primaryStage) throws Exception {
28 | double[] screenSize = JavaFxSystemUtil.getScreenSizeByScale(0.74, 0.8);
29 | FXMLLoader fXMLLoader = RdbmsSyncToolMain.getFXMLLoader();
30 | ResourceBundle resourceBundle = fXMLLoader.getResources();
31 | Parent root = fXMLLoader.load();
32 | primaryStage.setResizable(true);
33 | primaryStage.setTitle(resourceBundle.getString("Title"));
34 | // primaryStage.getIcons().add(new Image("/images/icon.jpg"));
35 | primaryStage.setScene(new Scene(root, screenSize[0], screenSize[1]));
36 | primaryStage.show();
37 | primaryStage.setOnCloseRequest(new EventHandler() {
38 | @Override
39 | public void handle(WindowEvent event) {
40 | System.exit(0);
41 | }
42 | });
43 | }
44 |
45 | public static FXMLLoader getFXMLLoader() {
46 | ResourceBundle resourceBundle = ResourceBundle.getBundle("locale.RdbmsSyncTool");
47 | URL url = Object.class.getResource("/com/xwintop/xJavaFxTool/fxmlView/debugTools/RdbmsSyncTool.fxml");
48 | FXMLLoader fXMLLoader = new FXMLLoader(url, resourceBundle);
49 | return fXMLLoader;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/xwintop/xJavaFxTool/controller/debugTools/RdbmsSyncToolController.java:
--------------------------------------------------------------------------------
1 | package com.xwintop.xJavaFxTool.controller.debugTools;
2 |
3 | import cn.hutool.core.swing.clipboard.ClipboardUtil;
4 | import cn.hutool.core.thread.ThreadUtil;
5 | import com.xwintop.xJavaFxTool.services.debugTools.RdbmsSyncToolService;
6 | import com.xwintop.xJavaFxTool.tools.DataxJsonUtil;
7 | import com.xwintop.xJavaFxTool.utils.ActionScheduleUtil;
8 | import com.xwintop.xJavaFxTool.view.debugTools.RdbmsSyncToolView;
9 | import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
10 | import com.xwintop.xcore.util.javafx.TextFieldInputHistoryDialog;
11 | import com.xwintop.xcore.util.javafx.TooltipUtil;
12 | import javafx.event.ActionEvent;
13 | import javafx.fxml.FXML;
14 | import javafx.scene.control.*;
15 | import javafx.scene.control.cell.CheckBoxTreeCell;
16 | import javafx.scene.input.MouseButton;
17 | import lombok.Getter;
18 | import lombok.Setter;
19 | import lombok.extern.slf4j.Slf4j;
20 | import org.apache.commons.lang3.StringUtils;
21 | import org.controlsfx.control.MaskerPane;
22 |
23 | import java.net.URL;
24 | import java.util.ResourceBundle;
25 | import java.util.concurrent.Future;
26 | import java.util.concurrent.TimeUnit;
27 |
28 | @Getter
29 | @Setter
30 | @Slf4j
31 | public class RdbmsSyncToolController extends RdbmsSyncToolView {
32 | private RdbmsSyncToolService entDataToolService = new RdbmsSyncToolService(this);
33 | private TextFieldInputHistoryDialog textFieldInputHistoryDialog = new TextFieldInputHistoryDialog("./javaFxConfigure/dbUrlDocumentConfigure.yml", "host", "port", "dbName", "dbType", "userName", "password", "jdbcUrl", "schema");
34 | private ContextMenu contextMenu = new ContextMenu();
35 | private String[] dbTypeStrings = new String[]{"mysql", "oracle", "oracleSid", "postgresql", "sqlserver", "sqlserverold", "dm", "sqlite", "h2Embedded", "h2Server", "access", "db2"};
36 | private String[] outputPathStrings = new String[]{"./executor"};
37 | private String[] tableTypeStrings = new String[]{"TABLE+VIEW", "TABLE", "VIEW", "SYSTEM_TABLE", "GLOBAL_TEMPORARY", "LOCAL_TEMPORARY", "ALIAS", "SYNONYM"};
38 | private String[] dataSourceTypeStrings = new String[]{"Druid", "Driver", "Simple", "Hikari"};
39 | private MaskerPane masker = new MaskerPane();
40 | private ActionScheduleUtil actionScheduleUtil;
41 |
42 | @Override
43 | public void initialize(URL location, ResourceBundle resources) {
44 | initView();
45 | initEvent();
46 | initService();
47 | }
48 |
49 | private void initView() {
50 | masker.setVisible(false);
51 | loadingStackPane.getChildren().add(masker);
52 | // hostText1.setText("127.0.0.1");
53 | // dbNameText1.setText("admin");
54 | // pwdText1.setText("admin");
55 |
56 | JavaFxViewUtil.setPasswordTextFieldFactory(pwdText1);
57 | JavaFxViewUtil.setPasswordTextFieldFactory(pwdText2);
58 | dbTypeText1.getItems().addAll(dbTypeStrings);
59 | dbTypeText1.setValue(dbTypeStrings[0]);
60 | dbTypeText2.getItems().addAll(dbTypeStrings);
61 | dbTypeText2.setValue(dbTypeStrings[0]);
62 | outputPathComboBox.getItems().addAll(outputPathStrings);
63 |
64 | tableTreeView1.setCellFactory(CheckBoxTreeCell.forTreeView());
65 | tableTreeView1.setRoot(new CheckBoxTreeItem<>("源端库表"));
66 | tableTreeView1.getRoot().setExpanded(true);
67 | tableTreeView1.setShowRoot(true);
68 | tableTreeView2.setCellFactory(CheckBoxTreeCell.forTreeView());
69 | tableTreeView2.setRoot(new CheckBoxTreeItem<>("目标端库表"));
70 | tableTreeView2.getRoot().setExpanded(true);
71 | tableTreeView2.setShowRoot(true);
72 | JavaFxViewUtil.setSpinnerValueFactory(syncDataNumberSpinner, -1, Integer.MAX_VALUE, 10);
73 |
74 | tableTypeChoiceBox.getItems().addAll(tableTypeStrings);
75 | tableTypeChoiceBox.setValue(tableTypeChoiceBox.getItems().get(1));
76 | dataSourceTypeChoiceBox.getItems().addAll(dataSourceTypeStrings);
77 | dataSourceTypeChoiceBox.setValue(dataSourceTypeChoiceBox.getItems().get(0));
78 |
79 | actionScheduleUtil = new ActionScheduleUtil();
80 | actionScheduleUtil.setScheduleNode(actionScheduleHBox);
81 | actionScheduleUtil.setJobAction(() -> {
82 | try {
83 | entDataToolService.testInstertSqlAction();
84 | } catch (Exception e) {
85 | log.error("同步数据失败:", e);
86 | TooltipUtil.showToast("同步数据失败:" + e.getMessage());
87 | }
88 | });
89 | }
90 |
91 | private void initEvent() {
92 | addUrlDocumentDialogController(hostText1);
93 | addUrlDocumentDialogController(hostText2);
94 | addTableTreeViewMouseClicked(tableTreeView1);
95 | addTableTreeViewMouseClicked(tableTreeView2);
96 | dbTypeText1.valueProperty().addListener((observable, oldValue, newValue) -> {
97 | portText1.setText(DataxJsonUtil.getDbDefaultPort(newValue));
98 | });
99 | dbTypeText2.valueProperty().addListener((observable, oldValue, newValue) -> {
100 | portText2.setText(DataxJsonUtil.getDbDefaultPort(newValue));
101 | });
102 | }
103 |
104 | private void addTableTreeViewMouseClicked(TreeView tableTreeView) {
105 | tableTreeView.setOnMouseClicked(event -> {
106 | TreeItem selectedItem = tableTreeView.getSelectionModel().getSelectedItem();
107 | if (selectedItem == null) {
108 | return;
109 | }
110 | if (event.getButton() == MouseButton.PRIMARY) {
111 | selectedItem.setExpanded(!selectedItem.isExpanded());
112 | } else if (event.getButton() == MouseButton.SECONDARY) {
113 | MenuItem menu_UnfoldAll = new MenuItem("展开所有");
114 | menu_UnfoldAll.setOnAction(event1 -> {
115 | tableTreeView.getRoot().setExpanded(true);
116 | tableTreeView.getRoot().getChildren().forEach(stringTreeItem -> {
117 | stringTreeItem.setExpanded(true);
118 | });
119 | });
120 | MenuItem menu_FoldAll = new MenuItem("折叠所有");
121 | menu_FoldAll.setOnAction(event1 -> {
122 | tableTreeView.getRoot().getChildren().forEach(stringTreeItem -> {
123 | stringTreeItem.setExpanded(false);
124 | });
125 | });
126 | MenuItem menu_executeSql = new MenuItem("执行Sql");
127 | menu_executeSql.setOnAction(event1 -> {
128 | entDataToolService.executeSql(tableTreeView);
129 | });
130 | ContextMenu contextMenu = new ContextMenu(menu_UnfoldAll, menu_FoldAll, menu_executeSql);
131 | if ("源端库表".equals(selectedItem.getValue()) || "目标端库表".equals(selectedItem.getValue())) {
132 | JavaFxViewUtil.addMenuItem(contextMenu, "一键复制表名", event1 -> {
133 | String tableNames = String.join(",", RdbmsSyncToolService.getSelectNameList(selectedItem));
134 | ClipboardUtil.setStr(tableNames);
135 | TooltipUtil.showToast("复制表名成功:" + tableNames);
136 | });
137 | JavaFxViewUtil.addMenuItem(contextMenu, "一键复制查询语句", event1 -> {
138 | entDataToolService.copySelectSql(null, tableTreeView, false);
139 | });
140 | JavaFxViewUtil.addMenuItem(contextMenu, "一键复制查询语句Mysql", event1 -> {
141 | entDataToolService.copySelectSql(null, tableTreeView, true);
142 | });
143 | JavaFxViewUtil.addMenuItem(contextMenu, "一键生成建表语句Mysql", event1 -> {
144 | entDataToolService.showSqlAction("mysql");
145 | });
146 | JavaFxViewUtil.addMenuItem(contextMenu, "一键生成建表语句Oracle", event1 -> {
147 | entDataToolService.showSqlAction("oracle");
148 | });
149 | JavaFxViewUtil.addMenuItem(contextMenu, "一键生成查询表中数据量语句", event1 -> {
150 | entDataToolService.copySelectTableCount("*", tableTreeView);
151 | });
152 | JavaFxViewUtil.addMenuItem(contextMenu, "一键查看表中数据量", event1 -> {
153 | entDataToolService.selectTableCount("*", tableTreeView);
154 | });
155 | JavaFxViewUtil.addMenuItem(contextMenu, "一键生成查询表中最大值语句", event1 -> {
156 | entDataToolService.copySelectTableMax("*", tableTreeView, null);
157 | });
158 | JavaFxViewUtil.addMenuItem(contextMenu, "一键查看表中数据最大值", event1 -> {
159 | entDataToolService.selectTableMax("*", tableTreeView, null);
160 | });
161 | JavaFxViewUtil.addMenuItem(contextMenu, "一键Drop删除表结构", event1 -> {
162 | entDataToolService.dropTable("*", tableTreeView);
163 | });
164 | JavaFxViewUtil.addMenuItem(contextMenu, "一键delete删除表数据", event1 -> {
165 | entDataToolService.deleteTableData("*", tableTreeView);
166 | });
167 | JavaFxViewUtil.addMenuItem(contextMenu, "一键truncate删除表数据", event1 -> {
168 | entDataToolService.truncateTableData("*", tableTreeView);
169 | });
170 | } else {
171 | if ("源端库表".equals(selectedItem.getParent().getValue()) || "目标端库表".equals(selectedItem.getParent().getValue())) {
172 | JavaFxViewUtil.addMenuItem(contextMenu, "复制表名", event1 -> {
173 | ClipboardUtil.setStr(selectedItem.getValue());
174 | TooltipUtil.showToast("复制表名成功:" + selectedItem.getValue());
175 | });
176 | JavaFxViewUtil.addMenuItem(contextMenu, "复制字段名", event1 -> {
177 | ClipboardUtil.setStr(String.join(",", RdbmsSyncToolService.getSelectNameList(selectedItem)));
178 | TooltipUtil.showToast("复制字段名成功:" + String.join(",", RdbmsSyncToolService.getSelectNameList(selectedItem)));
179 | });
180 | JavaFxViewUtil.addMenuItem(contextMenu, "复制查询语句", event1 -> {
181 | entDataToolService.copySelectSql((CheckBoxTreeItem) selectedItem, tableTreeView, false);
182 | });
183 | JavaFxViewUtil.addMenuItem(contextMenu, "复制查询语句mysql", event1 -> {
184 | entDataToolService.copySelectSql((CheckBoxTreeItem) selectedItem, tableTreeView, true);
185 | });
186 | JavaFxViewUtil.addMenuItem(contextMenu, "查看表中数据量", event1 -> {
187 | entDataToolService.selectTableCount(selectedItem.getValue(), tableTreeView);
188 | });
189 | JavaFxViewUtil.addMenuItem(contextMenu, "复制查看表中数据量语句", event1 -> {
190 | entDataToolService.copySelectTableCount(selectedItem.getValue(), tableTreeView);
191 | });
192 | JavaFxViewUtil.addMenuItem(contextMenu, "查看表中数据最大值", event1 -> {
193 | entDataToolService.selectTableMax(selectedItem.getValue(), tableTreeView, selectedItem);
194 | });
195 | JavaFxViewUtil.addMenuItem(contextMenu, "复制查询表中最大值语句", event1 -> {
196 | entDataToolService.copySelectTableMax(selectedItem.getValue(), tableTreeView, selectedItem);
197 | });
198 | JavaFxViewUtil.addMenuItem(contextMenu, "查看表内容", event1 -> {
199 | entDataToolService.showTableData(selectedItem.getValue(), tableTreeView);
200 | });
201 | JavaFxViewUtil.addMenuItem(contextMenu, "Drop删除表结构", event1 -> {
202 | entDataToolService.dropTable(selectedItem.getValue(), tableTreeView);
203 | });
204 | JavaFxViewUtil.addMenuItem(contextMenu, "delete删除表数据", event1 -> {
205 | entDataToolService.deleteTableData(selectedItem.getValue(), tableTreeView);
206 | });
207 | JavaFxViewUtil.addMenuItem(contextMenu, "truncate删除表数据", event1 -> {
208 | entDataToolService.truncateTableData(selectedItem.getValue(), tableTreeView);
209 | });
210 | } else {
211 | JavaFxViewUtil.addMenuItem(contextMenu, "复制字段名", event1 -> {
212 | ClipboardUtil.setStr(selectedItem.getValue());
213 | });
214 | }
215 | }
216 |
217 | tableTreeView.setContextMenu(contextMenu);
218 | }
219 | });
220 | }
221 |
222 | private void initService() {
223 | }
224 |
225 | private void addUrlDocumentDialogController(TextField hostText) {
226 | textFieldInputHistoryDialog.setOnMouseClicked(hostText, map -> {
227 | hostText.setText(map.get("host"));
228 | if (hostText == hostText1) {
229 | portText1.setText(map.get("port"));
230 | dbNameText1.setText(map.get("dbName"));
231 | dbTypeText1.setValue(map.get("dbType"));
232 | userNameText1.setText(map.get("userName"));
233 | pwdText1.setText(map.get("password"));
234 | jdbcUrlField1.setText(StringUtils.defaultString(map.get("jdbcUrl")));
235 | schemaTextField1.setText(StringUtils.defaultString(map.get("schema")));
236 | } else if (hostText == hostText2) {
237 | portText2.setText(map.get("port"));
238 | dbNameText2.setText(map.get("dbName"));
239 | dbTypeText2.setValue(map.get("dbType"));
240 | userNameText2.setText(map.get("userName"));
241 | pwdText2.setText(map.get("password"));
242 | jdbcUrlField2.setText(StringUtils.defaultString(map.get("jdbcUrl")));
243 | schemaTextField2.setText(StringUtils.defaultString(map.get("schema")));
244 | }
245 | }, map -> map.get("name") + "/" + map.get("dbName") + "/" + map.get("userName"));
246 | }
247 |
248 | @FXML
249 | private void connectAction1(ActionEvent event) {
250 | masker.setVisible(true);
251 | ThreadUtil.execute(() -> {
252 | try {
253 | Future> future = ThreadUtil.execAsync(() -> {
254 | try {
255 | textFieldInputHistoryDialog.addConfig(hostText1.getText(), portText1.getText(), dbNameText1.getText(), dbTypeText1.getValue(), userNameText1.getText(), pwdText1.getText(), jdbcUrlField1.getText(), schemaTextField1.getText());
256 | entDataToolService.connectAction(dbTypeText1.getValue(), hostText1.getText(), portText1.getText(), dbNameText1.getText(), userNameText1.getText(), pwdText1.getText(), jdbcUrlField1.getText(), schemaTextField1.getText(), tableTreeView1);
257 | } catch (Exception e) {
258 | log.error("连接失败:", e);
259 | }
260 | });
261 | future.get(30, TimeUnit.SECONDS);
262 | } catch (Exception e) {
263 | log.error("连接超时:", e);
264 | } finally {
265 | masker.setVisible(false);
266 | }
267 | });
268 | }
269 |
270 | @FXML
271 | private void connectAction2(ActionEvent event) {
272 | masker.setVisible(true);
273 | ThreadUtil.execute(() -> {
274 | try {
275 | Future> future = ThreadUtil.execAsync(() -> {
276 | try {
277 | textFieldInputHistoryDialog.addConfig(hostText2.getText(), portText2.getText(), dbNameText2.getText(), dbTypeText2.getValue(), userNameText2.getText(), pwdText2.getText(), jdbcUrlField2.getText(), schemaTextField2.getText());
278 | entDataToolService.connectAction(dbTypeText2.getValue(), hostText2.getText(), portText2.getText(), dbNameText2.getText(), userNameText2.getText(), pwdText2.getText(), jdbcUrlField2.getText(), schemaTextField2.getText(), tableTreeView2);
279 | } catch (Exception e) {
280 | log.error("连接失败:", e);
281 | }
282 | });
283 | future.get(30, TimeUnit.SECONDS);
284 | } catch (Exception e) {
285 | log.error("连接超时:", e);
286 | } finally {
287 | masker.setVisible(false);
288 | }
289 | });
290 | }
291 |
292 | @FXML
293 | private void testInstertSqlAction(ActionEvent event) {
294 | try {
295 | entDataToolService.testInstertSqlAction();
296 | } catch (Exception e) {
297 | log.error("同步数据失败:", e);
298 | TooltipUtil.showToast("同步数据失败:" + e.getMessage());
299 | }
300 | }
301 | }
302 |
--------------------------------------------------------------------------------
/src/main/java/com/xwintop/xJavaFxTool/services/debugTools/RdbmsSyncToolService.java:
--------------------------------------------------------------------------------
1 | package com.xwintop.xJavaFxTool.services.debugTools;
2 |
3 | import cn.hutool.core.swing.clipboard.ClipboardUtil;
4 | import cn.hutool.db.meta.Column;
5 | import cn.hutool.db.meta.MetaUtil;
6 | import cn.hutool.db.meta.Table;
7 | import cn.hutool.db.meta.TableType;
8 | import com.alibaba.druid.pool.DruidDataSource;
9 | import com.alibaba.druid.util.JdbcUtils;
10 | import com.xwintop.xJavaFxTool.controller.debugTools.RdbmsSyncToolController;
11 | import com.xwintop.xJavaFxTool.tools.DataxJsonUtil;
12 | import com.xwintop.xJavaFxTool.tools.SqlUtil;
13 | import com.xwintop.xcore.javafx.dialog.FxDialog;
14 | import com.xwintop.xcore.util.javafx.AlertUtil;
15 | import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
16 | import com.xwintop.xcore.util.javafx.TooltipUtil;
17 | import javafx.collections.FXCollections;
18 | import javafx.collections.ObservableList;
19 | import javafx.scene.control.*;
20 | import javafx.scene.layout.HBox;
21 | import lombok.Getter;
22 | import lombok.Setter;
23 | import lombok.extern.slf4j.Slf4j;
24 | import org.apache.commons.io.FileUtils;
25 | import org.apache.commons.lang3.StringUtils;
26 | import org.apache.commons.lang3.time.DateUtils;
27 | import org.springframework.jdbc.core.JdbcTemplate;
28 |
29 | import javax.sql.DataSource;
30 | import java.io.Closeable;
31 | import java.io.File;
32 | import java.sql.Connection;
33 | import java.sql.ResultSetMetaData;
34 | import java.sql.Timestamp;
35 | import java.sql.Types;
36 | import java.util.*;
37 | import java.util.concurrent.atomic.AtomicInteger;
38 | import java.util.concurrent.atomic.AtomicReference;
39 |
40 | @Getter
41 | @Setter
42 | @Slf4j
43 | public class RdbmsSyncToolService {
44 | private RdbmsSyncToolController rdbmsSyncToolController;
45 | private Map tableMap = new HashMap<>();
46 | private Map tableMap2 = new HashMap<>();
47 |
48 | public RdbmsSyncToolService(RdbmsSyncToolController rdbmsSyncToolController) {
49 | this.rdbmsSyncToolController = rdbmsSyncToolController;
50 | }
51 |
52 | //连接数据库
53 | public void connectAction(String dbType, String dbIp, String dbPort, String dbName, String dbUserName, String dbUserPassword, String jdbcUrl, String schema, TreeView tableTreeView) throws Exception {
54 | DataSource dataSource = null;
55 | try {
56 | List tableNames = null;
57 | dataSource = SqlUtil.getDataSource(dbType, dbIp, dbPort, dbName, dbUserName, dbUserPassword, jdbcUrl, rdbmsSyncToolController.getDataSourceTypeChoiceBox().getValue());
58 | try {
59 | if ("TABLE+VIEW".equals(rdbmsSyncToolController.getTableTypeChoiceBox().getValue())) {
60 | tableNames = MetaUtil.getTables(dataSource, DataxJsonUtil.convertDatabaseCharsetType(dbUserName, schema, dbType), TableType.TABLE, TableType.VIEW);
61 | } else {
62 | tableNames = MetaUtil.getTables(dataSource, DataxJsonUtil.convertDatabaseCharsetType(dbUserName, schema, dbType), TableType.valueOf(rdbmsSyncToolController.getTableTypeChoiceBox().getValue()));
63 | }
64 | } catch (Throwable e) {
65 | log.error("getTables is error!尝试使用sql语句获取", e);
66 | if ("sqlserver".equals(dbType) || "sqlserverold".equals(dbType) || "access".equals(dbType)) {
67 | tableNames = SqlUtil.showSqlServerTables(dataSource, dbType);
68 | } else {
69 | if ("oracleSid".equals(dbType)) {
70 | dbType = "oracle";
71 | }
72 | Connection connection = dataSource.getConnection();
73 | try {
74 | tableNames = JdbcUtils.showTables(connection, dbType);
75 | } finally {
76 | JdbcUtils.close(connection);
77 | }
78 | }
79 | }
80 | log.info("获取到表名:" + tableNames);
81 | CheckBoxTreeItem treeItem = (CheckBoxTreeItem) tableTreeView.getRoot();
82 | treeItem.getChildren().clear();
83 | if (tableTreeView == rdbmsSyncToolController.getTableTreeView1()) {
84 | tableMap.clear();
85 | } else {
86 | tableMap2.clear();
87 | }
88 | List ignoreTableNameList = null;
89 | if (StringUtils.isNotEmpty(rdbmsSyncToolController.getIgnoreTableNameTextField().getText())) {
90 | String[] ignoreTableNames = rdbmsSyncToolController.getIgnoreTableNameTextField().getText().toLowerCase().trim().split(",");
91 | ignoreTableNameList = Arrays.asList(ignoreTableNames);
92 | }
93 | for (String tableName : tableNames) {
94 | if (ignoreTableNameList != null) {
95 | boolean isIgnore = true;
96 | for (String ignoreTableName : ignoreTableNameList) {
97 | if (tableName.toLowerCase().matches(ignoreTableName)) {
98 | isIgnore = false;
99 | break;
100 | }
101 | }
102 | if (isIgnore) {
103 | continue;
104 | }
105 | }
106 | final CheckBoxTreeItem tableNameTreeItem = new CheckBoxTreeItem<>(tableName);
107 | Table table = new Table(tableName);
108 | try {
109 | table = MetaUtil.getTableMeta(dataSource, tableName);
110 | if (table.getColumns() == null || table.getColumns().isEmpty()) {
111 | throw new Exception("未获取到表结构!");
112 | }
113 | } catch (Throwable e) {
114 | log.error("获取表结构失败!使用jdbc原生方法获取" + tableName, e);
115 | String querySql = String.format("select * from %s where 1=2", StringUtils.isEmpty(schema) ? tableName : schema + "." + tableName);
116 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
117 | Table finalTable = table;
118 | jdbcTemplate.query(querySql, rs -> {
119 | ResultSetMetaData rsMetaData = rs.getMetaData();
120 | for (int i = 1, len = rsMetaData.getColumnCount(); i <= len; i++) {
121 | Column column = new Column();
122 | column.setName(rsMetaData.getColumnName(i));
123 | column.setType(rsMetaData.getColumnType(i));
124 | column.setSize(rsMetaData.getColumnDisplaySize(i));
125 | column.setComment(rsMetaData.getColumnLabel(i));
126 | finalTable.setColumn(column);
127 | }
128 | return null;
129 | });
130 | }
131 | if (tableTreeView == rdbmsSyncToolController.getTableTreeView1()) {
132 | tableMap.put(tableName, table);
133 | } else {
134 | tableMap2.put(tableName, table);
135 | }
136 | for (Column column : table.getColumns()) {
137 | CheckBox isPkCheckBox = new CheckBox("Pk");
138 | if (table.getPkNames().contains(column.getName())) {
139 | isPkCheckBox.setSelected(true);
140 | }
141 | CheckBox isTimeCheckBox = new CheckBox("Time");
142 | HBox hBox = new HBox();
143 | hBox.getChildren().addAll(isPkCheckBox, isTimeCheckBox);
144 | final CheckBoxTreeItem columnNameTreeItem = new CheckBoxTreeItem<>(column.getName(), hBox);
145 | tableNameTreeItem.getChildren().add(columnNameTreeItem);
146 | }
147 | treeItem.getChildren().add(tableNameTreeItem);
148 | }
149 | } finally {
150 | if (dataSource instanceof DruidDataSource) {
151 | JdbcUtils.close((Closeable) dataSource);
152 | }
153 | }
154 | }
155 |
156 | public List> getCheckBoxTreeItemList() {
157 | List> rootList = new ArrayList<>();
158 | rootList.addAll(rdbmsSyncToolController.getTableTreeView1().getRoot().getChildren());
159 | rootList.addAll(rdbmsSyncToolController.getTableTreeView2().getRoot().getChildren());
160 | List> checkBoxTreeItemList = new ArrayList<>();
161 | for (TreeItem treeItem : rootList) {
162 | CheckBoxTreeItem tableNameTreeItem = (CheckBoxTreeItem) treeItem;
163 | if (tableNameTreeItem.isSelected()) {
164 | checkBoxTreeItemList.add(tableNameTreeItem);
165 | }
166 | }
167 | return checkBoxTreeItemList;
168 | }
169 |
170 | public List> getCheckBoxTreeItemListByTreeView(TreeView tableTreeView) {
171 | List> checkBoxTreeItemList = new ArrayList<>();
172 | for (TreeItem treeItem : tableTreeView.getRoot().getChildren()) {
173 | CheckBoxTreeItem tableNameTreeItem = (CheckBoxTreeItem) treeItem;
174 | if (tableNameTreeItem.isSelected()) {
175 | checkBoxTreeItemList.add(tableNameTreeItem);
176 | }
177 | }
178 | return checkBoxTreeItemList;
179 | }
180 |
181 | public Map getTableInfoMap(ObservableList> tableNameTreeItemList) {
182 | List columnList = new ArrayList();
183 | String splitPk = "";
184 | String where = "";
185 | for (TreeItem childTreeItem : tableNameTreeItemList) {
186 | CheckBoxTreeItem columnNameTreeItem = (CheckBoxTreeItem) childTreeItem;
187 | HBox hBox = (HBox) columnNameTreeItem.getGraphic();
188 | CheckBox isPkCheckBox = (CheckBox) hBox.getChildren().get(0);
189 | CheckBox isTimeCheckBox = (CheckBox) hBox.getChildren().get(1);
190 | if (isPkCheckBox.isSelected()) {
191 | splitPk = columnNameTreeItem.getValue();
192 | }
193 | if (isTimeCheckBox.isSelected()) {
194 | where = columnNameTreeItem.getValue();
195 | }
196 | if (columnNameTreeItem.isSelected()) {
197 | columnList.add(columnNameTreeItem.getValue());
198 | }
199 | }
200 | Map tableInfoMap = new HashMap();
201 | tableInfoMap.put("columnList", columnList.toArray(new String[0]));
202 | tableInfoMap.put("splitPk", splitPk);
203 | tableInfoMap.put("where", where);
204 | return tableInfoMap;
205 | }
206 |
207 | public void showSqlAction(String dbType) {
208 | List> checkBoxTreeItemList = getCheckBoxTreeItemList();
209 | if (checkBoxTreeItemList.isEmpty()) {
210 | TooltipUtil.showToast("未勾选表!");
211 | return;
212 | }
213 | for (CheckBoxTreeItem tableNameTreeItem : checkBoxTreeItemList) {
214 | StringBuffer tableString = new StringBuffer();
215 | String tableName = tableNameTreeItem.getValue();
216 | Table table = null;
217 | if ("源端库表".equals(tableNameTreeItem.getParent().getValue())) {
218 | table = tableMap.get(tableName);
219 | } else {
220 | table = tableMap2.get(tableName);
221 | }
222 | if ("mysql".equals(dbType)) {
223 | tableString.append("DROP TABLE IF EXISTS `" + tableName + "`;\nCREATE TABLE `" + tableName + "` (\n");
224 | for (Column column : table.getColumns()) {
225 | tableString.append(" `").append(column.getName()).append("` ")//cln name
226 | .append(DataxJsonUtil.getTableColumnType(column.getType(), column.getSize()))//类型
227 | .append(" " + (column.isNullable() ? "NULL" : "NOT NULL"))//是否非空
228 | //`import_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '数据插入时间'
229 | // .append(clnName.equals("import_time") ? " DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" : "")
230 | // .append(clnName.equals("id") ? " AUTO_INCREMENT" : "")
231 | // .append(" COLLATE utf8_bin COMMENT ")
232 | .append(" COMMENT '")//注释
233 | .append(column.getComment())//字段名(中文名称)
234 | .append("',\n");
235 | }
236 | tableString.deleteCharAt(tableString.length() - 2);
237 | // tableString.append("PRIMARY KEY (`" + pk + "`)\n) ");
238 | tableString.append(")ENGINE = InnoDB\n" +
239 | "DEFAULT CHARACTER SET = utf8\n" +
240 | "COLLATE = utf8_bin\n");
241 | tableString.append("COMMENT = '" + table.getComment() + "'\n" +
242 | "ROW_FORMAT = COMPACT;");
243 | } else {
244 | String schema = ("源端库表".equals(tableNameTreeItem.getParent().getValue())) ? rdbmsSyncToolController.getSchemaTextField1().getText() : rdbmsSyncToolController.getSchemaTextField2().getText();
245 | if (StringUtils.isNotEmpty(schema)) {
246 | tableName = schema + "." + tableName;
247 | }
248 | tableString.append("CREATE TABLE " + tableName + "(\n");
249 | StringBuffer commentStr = new StringBuffer();
250 | for (Column column : table.getColumns()) {
251 | tableString.append(" ").append(column.getName()).append(" ")//cln name
252 | .append(DataxJsonUtil.getTableColumnTypeByOracle(column.getType(), column.getSize()))//类型
253 | .append(",\n");
254 | if (StringUtils.isNotEmpty(column.getComment()) && !"null".equalsIgnoreCase(column.getComment())) {
255 | commentStr.append("COMMENT ON COLUMN " + tableName + "." + column.getName() + " IS '" + column.getComment() + "';\n");
256 | }
257 | }
258 | tableString.deleteCharAt(tableString.length() - 2);
259 | tableString.append(");\n");
260 | tableString.append(commentStr.toString());
261 | }
262 | String fileName = tableName + "_" + dbType + ".sql";
263 | if (rdbmsSyncToolController.getIsShowCheckBox().isSelected()) {
264 | TextArea textArea = new TextArea(tableString.toString());
265 | textArea.setWrapText(true);
266 | JavaFxViewUtil.openNewWindow(fileName, textArea);
267 | } else {
268 | String outputPath = StringUtils.defaultIfBlank(rdbmsSyncToolController.getOutputPathComboBox().getValue(), "./executor");
269 | try {
270 | File jsonFile = new File(outputPath, fileName);
271 | FileUtils.writeStringToFile(jsonFile, tableString.toString(), "utf-8");
272 | log.info("生成成功:" + jsonFile.getCanonicalPath());
273 | TooltipUtil.showToast("生成成功:" + jsonFile.getCanonicalPath());
274 | } catch (Exception e) {
275 | log.error("生成失败:", e);
276 | TooltipUtil.showToast("生成失败:" + e.getMessage());
277 | }
278 | }
279 | }
280 | }
281 |
282 | public void testInstertSqlAction() throws Exception {
283 | List> checkBoxTreeItemList = new ArrayList<>();
284 | List> checkBoxTreeItemList2 = new ArrayList<>();
285 | for (TreeItem treeItem : rdbmsSyncToolController.getTableTreeView1().getRoot().getChildren()) {
286 | CheckBoxTreeItem tableNameTreeItem = (CheckBoxTreeItem) treeItem;
287 | if (tableNameTreeItem.isSelected()) {
288 | checkBoxTreeItemList.add(tableNameTreeItem);
289 | }
290 | }
291 | for (TreeItem treeItem : rdbmsSyncToolController.getTableTreeView2().getRoot().getChildren()) {
292 | CheckBoxTreeItem tableNameTreeItem = (CheckBoxTreeItem) treeItem;
293 | if (tableNameTreeItem.isSelected()) {
294 | checkBoxTreeItemList2.add(tableNameTreeItem);
295 | }
296 | }
297 | if (checkBoxTreeItemList.size() != 1 || checkBoxTreeItemList2.size() != 1) {
298 | TooltipUtil.showToast("两端各勾选一张表!");
299 | return;
300 | }
301 | CheckBoxTreeItem tableNameTreeItem = checkBoxTreeItemList.get(0);
302 | Map tableInfoMap = getTableInfoMap(tableNameTreeItem.getChildren());
303 | CheckBoxTreeItem tableNameTreeItem2 = checkBoxTreeItemList2.get(0);
304 | Map tableInfoMap2 = getTableInfoMap(tableNameTreeItem2.getChildren());
305 | String tableName = tableNameTreeItem.getValue();
306 | String tableName2 = tableNameTreeItem2.getValue();
307 | DataSource dataSource1 = SqlUtil.getDataSourceByViewType(rdbmsSyncToolController, rdbmsSyncToolController.getTableTreeView1());
308 | DataSource dataSource2 = SqlUtil.getDataSourceByViewType(rdbmsSyncToolController, rdbmsSyncToolController.getTableTreeView2());
309 | String[] columnList = (String[]) tableInfoMap.get("columnList");
310 | String[] columnList2 = (String[]) tableInfoMap2.get("columnList");
311 | String splitPk = tableInfoMap.get("splitPk").toString();
312 | String where = tableInfoMap.get("where").toString();
313 | AtomicInteger dataNumber = new AtomicInteger();
314 | AtomicInteger dirtyDataNumber = new AtomicInteger();
315 | try {
316 | String querySql = null;
317 | if (StringUtils.isNotEmpty(rdbmsSyncToolController.getQuerySqlTextField().getText())) {
318 | querySql = rdbmsSyncToolController.getQuerySqlTextField().getText();
319 | } else {
320 | if (columnList.length != columnList2.length) {
321 | TooltipUtil.showToast("两端表勾选列数量不一致!左边:" + columnList.length + " 右边:" + columnList2.length);
322 | return;
323 | }
324 | querySql = String.format("select %s from %s ", String.join(",", columnList), tableName);
325 | if (StringUtils.isNotEmpty(rdbmsSyncToolController.getWhereSqlTextField().getText())) {
326 | querySql = querySql + " where " + rdbmsSyncToolController.getWhereSqlTextField().getText();
327 | }
328 | if (rdbmsSyncToolController.getFilterTimeCheckBox().isSelected()) {
329 | if (StringUtils.isEmpty(where)) {
330 | TooltipUtil.showToast("时间增量模式时,未勾选时间字段!");
331 | return;
332 | }
333 | Timestamp lastSyncTime = new Timestamp(Long.MIN_VALUE);
334 | if (StringUtils.isNotEmpty(rdbmsSyncToolController.getFilterStartTimeTextField().getText())) {
335 | lastSyncTime = new Timestamp(DateUtils.parseDate(rdbmsSyncToolController.getFilterStartTimeTextField().getText(), "yyyy-MM-dd-HH:mm:ss.SSS").getTime());
336 | }
337 | Timestamp maxLastupdate = new Timestamp(253370980060114L);
338 | if (StringUtils.isNotEmpty(rdbmsSyncToolController.getFilterEntTimeTextField().getText())) {
339 | maxLastupdate = new Timestamp(DateUtils.parseDate(rdbmsSyncToolController.getFilterEntTimeTextField().getText(), "yyyy-MM-dd-HH:mm:ss.SSS").getTime());
340 | }
341 | String whereSql = SqlUtil.getDataxWhereSql(rdbmsSyncToolController.getDbTypeText1().getValue(), where, lastSyncTime, maxLastupdate);
342 | querySql = querySql + (querySql.contains("where") ? " and " : " where ") + whereSql;
343 | }
344 | if (rdbmsSyncToolController.getFilterLongKeyCheckBox().isSelected()) {
345 | if (StringUtils.isEmpty(splitPk)) {
346 | TooltipUtil.showToast("主键增量模式时,未勾选主键!");
347 | return;
348 | }
349 | Long lastMaxValue = Long.MIN_VALUE;
350 | if (StringUtils.isNotEmpty(rdbmsSyncToolController.getFilterLongKeyStartTextField().getText())) {
351 | lastMaxValue = new Long(rdbmsSyncToolController.getFilterLongKeyStartTextField().getText());
352 | }
353 | Long maxValue = Long.MAX_VALUE;
354 | if (StringUtils.isNotEmpty(rdbmsSyncToolController.getFilterLongKeyEntTextField().getText())) {
355 | maxValue = new Long(rdbmsSyncToolController.getFilterLongKeyEntTextField().getText());
356 | }
357 | String whereSql = SqlUtil.getDataxWhereSql(splitPk, lastMaxValue, maxValue);
358 | querySql = querySql + (querySql.contains("where") ? " and " : " where ") + whereSql;
359 | }
360 | }
361 | String insertSql = String.format("INSERT INTO %s (%s) VALUES (%s)", tableName2, String.join(",", columnList2), StringUtils.repeat("?", ",", columnList2.length));
362 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource1);
363 | JdbcTemplate jdbcTemplate2 = new JdbcTemplate(dataSource2);
364 | List batchUpdateData = new ArrayList();
365 | jdbcTemplate.query(querySql, rs -> {
366 | ResultSetMetaData rsMetaData = rs.getMetaData();
367 | while (rs.next()) {
368 | Object[] dataObjects = new Object[columnList2.length];
369 | for (int i = 0; i < columnList2.length; i++) {
370 | dataObjects[i] = rs.getObject(i + 1);
371 | switch (rsMetaData.getColumnType(i + 1)) {
372 | case Types.TIME:
373 | dataObjects[i] = rs.getTime(i + 1);
374 | break;
375 | case Types.DATE:
376 | if (rsMetaData.getColumnTypeName(i + 1).equalsIgnoreCase("year")) {
377 | dataObjects[i] = rs.getInt(i + 1);
378 | } else {
379 | dataObjects[i] = rs.getDate(i + 1);
380 | }
381 | break;
382 | case Types.TIMESTAMP:
383 | dataObjects[i] = rs.getTimestamp(i + 1);
384 | break;
385 | default:
386 | dataObjects[i] = rs.getObject(i + 1);
387 | }
388 | }
389 | dataNumber.getAndIncrement();
390 | batchUpdateData.add(dataObjects);
391 | if (rs.getRow() % 1000 == 0) {
392 | SqlUtil.doBatchInsert(insertSql, batchUpdateData, jdbcTemplate2, dirtyDataNumber);
393 | batchUpdateData.clear();
394 | }
395 | if (rdbmsSyncToolController.getSyncDataNumberSpinner().getValue() != -1) {
396 | if (rs.getRow() >= rdbmsSyncToolController.getSyncDataNumberSpinner().getValue()) {
397 | break;
398 | }
399 | }
400 | }
401 | return null;
402 | });
403 | if (!batchUpdateData.isEmpty()) {
404 | SqlUtil.doBatchInsert(insertSql, batchUpdateData, jdbcTemplate2, dirtyDataNumber);
405 | }
406 | } finally {
407 | if (dataSource1 instanceof DruidDataSource) {
408 | JdbcUtils.close((Closeable) dataSource1);
409 | }
410 | if (dataSource2 instanceof DruidDataSource) {
411 | JdbcUtils.close((Closeable) dataSource2);
412 | }
413 | }
414 | TooltipUtil.showToast("生成测试同步数据完成,数量:" + dataNumber.get() + " 失败数量:" + dirtyDataNumber.get());
415 | }
416 |
417 | public void showTableData(String tableName, TreeView tableTreeView) {
418 | DataSource dataSource = SqlUtil.getDataSourceByViewType(rdbmsSyncToolController, tableTreeView);
419 | String schema = (tableTreeView == rdbmsSyncToolController.getTableTreeView1()) ? rdbmsSyncToolController.getSchemaTextField1().getText() : rdbmsSyncToolController.getSchemaTextField2().getText();
420 | try {
421 | if (StringUtils.isNotEmpty(schema)) {
422 | tableName = schema + "." + tableName;
423 | }
424 | int numPerPage = rdbmsSyncToolController.getSyncDataNumberSpinner().getValue() == -1 ? 100 : rdbmsSyncToolController.getSyncDataNumberSpinner().getValue();
425 | final TableView tableView = new TableView();
426 | tableView.setEditable(true);
427 | ObservableList