Skip to content

Commit 3c35d7a

Browse files
committed
add loadPrimaryKeyByLocalFile
1 parent 9add037 commit 3c35d7a

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ jdbcKey是通过sha256(jdbcUrl+schema)计算得来。
160160
161161
```
162162

163+
### 7. 主键关系手动维护(可选)
164+
165+
在数据库中存在不存在物理的主键字段,但是存在业务主键字段,可通过手动配置的方式,手动标记字段为主键字段。在扫描后的配置文件下增加对应表名的.key文件,例如:M_USER.key
166+
```
167+
.
168+
└── beefae7e00deb825a3a591ab7a22791a4df799afba9fed71f8b549665508c7ee
169+
├── M_USER
170+
├── M_USER.key
171+
└── M_USER_2
172+
```
173+
174+
写法如下,填写字段的名称,多个用英文,分割。
175+
```
176+
USERNAME,ID
177+
```
178+
163179
## 📖 API 文档
164180

165181
### DBStreamContext

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.codingapi.dbstream</groupId>
66
<artifactId>dbstream-driver</artifactId>
7-
<version>1.0.5</version>
7+
<version>1.0.6</version>
88

99
<url>https://github.com/codingapi/dbstream-driver</url>
1010
<name>dbstream-driver</name>

src/main/java/com/codingapi/dbstream/scanner/DBScanner.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public DBScanner(Connection connection, Properties info) throws SQLException {
3232

3333
private void loadDbTableInfo(String tableName, DbTable tableInfo) throws SQLException {
3434
String dbTableName = tableInfo.getName();
35+
List<String> keys = dbTableSerializableHelper.loadPrimaryKeyByLocalFile(tableInfo.getName());
3536
if (dbTableSerializableHelper.hasSerialize(dbTableName)) {
3637
DbTable dbTableCache = dbTableSerializableHelper.deserialize(dbTableName);
3738
tableInfo.setColumns(dbTableCache.getColumns());
3839
tableInfo.setPrimaryKeys(dbTableCache.getPrimaryKeys());
40+
tableInfo.loadLocalPrimaryKeys(keys);
3941
tableInfo.reloadPrimaryColumns();
4042
return;
4143
}
@@ -60,6 +62,7 @@ private void loadDbTableInfo(String tableName, DbTable tableInfo) throws SQLExce
6062
tableInfo.addPrimaryKey(pkColumn);
6163
}
6264
pkRs.close();
65+
tableInfo.loadLocalPrimaryKeys(keys);
6366
tableInfo.reloadPrimaryColumns();
6467

6568
dbTableSerializableHelper.serialize(tableInfo);

src/main/java/com/codingapi/dbstream/scanner/DbTable.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public void addColum(DbColumn column) {
4141

4242

4343
public void addPrimaryKey(String key) {
44-
this.primaryKeys.add(key);
44+
if(!this.primaryKeys.contains(key)) {
45+
this.primaryKeys.add(key);
46+
}
4547
}
4648

4749
public boolean hasColumns() {
@@ -75,4 +77,15 @@ public void setPrimaryKeys(List<String> primaryKeys) {
7577
this.primaryKeys.clear();
7678
this.primaryKeys.addAll(primaryKeys);
7779
}
80+
81+
public void loadLocalPrimaryKeys(List<String> primaryKeys) {
82+
if(primaryKeys!=null && !primaryKeys.isEmpty()){
83+
for(String primaryKey:primaryKeys){
84+
DbColumn column = this.getColumnByName(primaryKey);
85+
if(column!=null){
86+
this.addPrimaryKey(column.getName());
87+
}
88+
}
89+
}
90+
}
7891
}

src/main/java/com/codingapi/dbstream/serializable/DBTableSerializableHelper.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.codingapi.dbstream.serializable;
22

33
import com.codingapi.dbstream.scanner.DbTable;
4+
import com.codingapi.dbstream.utils.FileReaderUtils;
45

56
import java.io.File;
67
import java.io.ObjectInputStream;
78
import java.io.ObjectOutputStream;
89
import java.nio.file.Files;
910
import java.nio.file.Paths;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.List;
1014
import java.util.logging.Level;
1115
import java.util.logging.Logger;
1216

@@ -19,7 +23,7 @@ public DBTableSerializableHelper(String jdbcKey) {
1923
this.path = new File("./.dbstream/" + jdbcKey + "/");
2024
if (!this.path.exists()) {
2125
boolean result = path.mkdirs();
22-
LOGGER.log(Level.INFO, "Serializable Table directory created: {0}, File Path: {1}",
26+
LOGGER.log(Level.INFO, "Serializable Table directory created: {0}, File Path: {1}",
2327
new Object[]{result, this.path.getAbsolutePath()});
2428
}
2529
}
@@ -30,6 +34,19 @@ public void clean() {
3034
}
3135
}
3236

37+
public List<String> loadPrimaryKeyByLocalFile(String tableName) {
38+
File file = new File(this.path + "/" + tableName + ".key");
39+
List<String> data = FileReaderUtils.read(file);
40+
if(data!=null && !data.isEmpty()) {
41+
List<String> columns = new ArrayList<>();
42+
for (String line : data) {
43+
columns.addAll(Arrays.asList(line.split(",")));
44+
}
45+
return columns;
46+
}
47+
return null;
48+
}
49+
3350
private void deleteRecursively(File file) {
3451
if (file.isDirectory()) {
3552
File[] files = file.listFiles();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.codingapi.dbstream.utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class FileReaderUtils {
13+
14+
/**
15+
* 按行读取文件内容,返回每一行的字符串列表
16+
*/
17+
public static List<String> read(File file) {
18+
if (file == null || !file.exists() || !file.isFile()) {
19+
return null;
20+
}
21+
22+
List<String> lines = new ArrayList<>();
23+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8))) {
24+
String line;
25+
while ((line = reader.readLine()) != null) {
26+
lines.add(line);
27+
}
28+
} catch (IOException e) {
29+
throw new RuntimeException(e);
30+
}
31+
return lines;
32+
}
33+
}

0 commit comments

Comments
 (0)