|
30 | 30 | /** |
31 | 31 | * Date: 2020/1/15 |
32 | 32 | * Company: www.dtstack.com |
| 33 | + * |
33 | 34 | * @author maqi |
34 | 35 | */ |
35 | | -public class SqlserverDialect implements JDBCDialect { |
| 36 | +public class SqlserverDialect implements JDBCDialect { |
36 | 37 |
|
37 | 38 | @Override |
38 | 39 | public boolean canHandle(String url) { |
39 | | - return url.startsWith("jdbc:jtds:"); |
| 40 | + return url.startsWith("jdbc:sqlserver:"); |
40 | 41 | } |
41 | 42 |
|
42 | 43 | @Override |
43 | 44 | public Optional<String> defaultDriverName() { |
44 | | - return Optional.of("net.sourceforge.jtds.jdbc.Driver"); |
| 45 | + return Optional.of("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
45 | 46 | } |
46 | 47 |
|
47 | 48 | @Override |
48 | 49 | public Optional<String> getUpsertStatement(String schema, String tableName, String[] fieldNames, String[] uniqueKeyFields, boolean allReplace) { |
49 | 50 | tableName = DtStringUtil.getTableFullPath(schema, tableName); |
50 | 51 | StringBuilder sb = new StringBuilder(); |
51 | | - sb.append("MERGE INTO " + tableName + " T1 USING " |
52 | | - + "(" + buildDualQueryStatement(fieldNames) + ") T2 ON (" |
53 | | - + buildConnectionConditions(uniqueKeyFields) + ") "); |
| 52 | + sb.append("MERGE INTO ") |
| 53 | + .append(tableName) |
| 54 | + .append(" T1 USING (") |
| 55 | + .append(buildDualQueryStatement(fieldNames)) |
| 56 | + .append(") T2 ON (") |
| 57 | + .append(buildConnectionConditions(uniqueKeyFields)) |
| 58 | + .append(") "); |
54 | 59 |
|
55 | 60 | String updateSql = buildUpdateConnection(fieldNames, uniqueKeyFields, allReplace); |
56 | 61 |
|
57 | 62 | if (StringUtils.isNotEmpty(updateSql)) { |
58 | | - sb.append(" WHEN MATCHED THEN UPDATE SET "); |
59 | | - sb.append(updateSql); |
| 63 | + sb.append(" WHEN MATCHED THEN UPDATE SET ") |
| 64 | + .append(updateSql); |
60 | 65 | } |
61 | 66 |
|
62 | | - sb.append(" WHEN NOT MATCHED THEN " |
63 | | - + "INSERT (" + Arrays.stream(fieldNames).map(this::quoteIdentifier).collect(Collectors.joining(",")) + ") VALUES (" |
64 | | - + Arrays.stream(fieldNames).map(col -> "T2." + quoteIdentifier(col)).collect(Collectors.joining(",")) + ")"); |
65 | | - sb.append(";"); |
| 67 | + sb.append(" WHEN NOT MATCHED THEN " + "INSERT (") |
| 68 | + .append( |
| 69 | + Arrays |
| 70 | + .stream(fieldNames) |
| 71 | + .map(this::quoteIdentifier) |
| 72 | + .collect(Collectors.joining(","))) |
| 73 | + .append(") VALUES (") |
| 74 | + .append( |
| 75 | + Arrays |
| 76 | + .stream(fieldNames) |
| 77 | + .map(col -> "T2." + quoteIdentifier(col)) |
| 78 | + .collect(Collectors.joining(","))) |
| 79 | + .append(");"); |
66 | 80 | return Optional.of(sb.toString()); |
67 | 81 | } |
68 | 82 |
|
69 | 83 | /** |
70 | | - * build T1."A"=T2."A" or T1."A"=nvl(T2."A",T1."A") |
| 84 | + * build T1."A"=T2."A" or T1."A"=nvl(T2."A",T1."A") |
| 85 | + * |
71 | 86 | * @param fieldNames |
72 | 87 | * @param uniqueKeyFields |
73 | 88 | * @param allReplace |
74 | 89 | * @return |
75 | 90 | */ |
76 | 91 | private String buildUpdateConnection(String[] fieldNames, String[] uniqueKeyFields, boolean allReplace) { |
77 | 92 | List<String> uniqueKeyList = Arrays.asList(uniqueKeyFields); |
78 | | - return Arrays.stream(fieldNames).filter(col -> !uniqueKeyList.contains(col)).map(col -> { |
79 | | - return allReplace ? quoteIdentifier("T1") + "." + quoteIdentifier(col) + " = " + quoteIdentifier("T2") + "." + quoteIdentifier(col) : |
80 | | - quoteIdentifier("T1") + "." + quoteIdentifier(col) + " =ISNULL(" + quoteIdentifier("T2") + "." + quoteIdentifier(col) + "," |
81 | | - + quoteIdentifier("T1") + "." + quoteIdentifier(col) + ")"; |
82 | | - }).collect(Collectors.joining(",")); |
| 93 | + return |
| 94 | + Arrays |
| 95 | + .stream(fieldNames) |
| 96 | + .filter(col -> !uniqueKeyList.contains(col)) |
| 97 | + .map(col -> allReplace ? |
| 98 | + quoteIdentifier("T1") + "." + quoteIdentifier(col) + " = " + quoteIdentifier("T2") + "." + quoteIdentifier(col) : |
| 99 | + quoteIdentifier("T1") + "." + quoteIdentifier(col) + " =ISNULL(" + quoteIdentifier("T2") + "." + quoteIdentifier(col) |
| 100 | + + "," + quoteIdentifier("T1") + "." + quoteIdentifier(col) + ")").collect(Collectors.joining(",")); |
83 | 101 | } |
84 | 102 |
|
85 | 103 |
|
86 | 104 | private String buildConnectionConditions(String[] uniqueKeyFields) { |
87 | | - return Arrays.stream(uniqueKeyFields).map(col -> "T1." + quoteIdentifier(col) + "=T2." + quoteIdentifier(col)).collect(Collectors.joining(",")); |
| 105 | + return |
| 106 | + Arrays |
| 107 | + .stream(uniqueKeyFields) |
| 108 | + .map(col -> "T1." + quoteIdentifier(col) + "=T2." + quoteIdentifier(col)) |
| 109 | + .collect(Collectors.joining(",")); |
88 | 110 | } |
89 | 111 |
|
90 | 112 | /** |
91 | 113 | * build select sql , such as (SELECT ? "A",? "B" FROM DUAL) |
92 | 114 | * |
93 | | - * @param column destination column |
| 115 | + * @param column destination column |
94 | 116 | * @return |
95 | 117 | */ |
96 | 118 | public String buildDualQueryStatement(String[] column) { |
97 | 119 | StringBuilder sb = new StringBuilder("SELECT "); |
98 | | - String collect = Arrays.stream(column).map(col -> " ? " + quoteIdentifier(col)).collect(Collectors.joining(", ")); |
| 120 | + String collect = |
| 121 | + Arrays |
| 122 | + .stream(column) |
| 123 | + .map(col -> " ? " + quoteIdentifier(col)) |
| 124 | + .collect(Collectors.joining(", ")); |
99 | 125 | sb.append(collect); |
100 | 126 | return sb.toString(); |
101 | 127 | } |
|
0 commit comments