Skip to content

Commit 0f02737

Browse files
committed
update
1 parent cda5bc8 commit 0f02737

File tree

13 files changed

+280
-104
lines changed

13 files changed

+280
-104
lines changed

README.md

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public class DefaultDBTableSupportProvider implements DBTableSupportProvider {
137137
138138
@Override
139139
public boolean support(Properties info, DbTable dbTable) {
140-
if (dbTable.hasColumns() && dbTable.hasPrimaryKeys()) {
140+
if (dbTable.hasColumns()) {
141141
return true;
142142
}
143143
return false;
@@ -151,7 +151,7 @@ public class DefaultDBTableSupportProvider implements DBTableSupportProvider {
151151

152152
### 6. 查看表或情况表缓存数据(可选)
153153

154-
在项目启动以后,会在项目的根路径下创建dbstream文件夹,文件夹中存储的内容为数据库的表扫描缓存数据。
154+
在项目启动以后,会在项目的根路径下创建.dbstream文件夹,文件夹中存储的内容为数据库的表扫描缓存数据。
155155
文件夹的名称为jdbcKey的字段,文件夹下的内容为缓存的表结构信息,当表结构发生变化以后可以删除对应的文件进行更新。
156156
也可以在系统中通过执行 `DBStreamContext.getInstance().clear(String jdbcKey);`进行情况数据。
157157
jdbcKey是通过sha256(jdbcUrl+schema)计算得来。
@@ -247,10 +247,11 @@ mvn clean test -P travis
247247
3. **SQL 限制**
248248
- 由于 JDBC 在执行 `INSERT INTO ... SELECT` 语句时无法获取自增 ID,框架暂不支持此类插入方式
249249
- 请避免使用 `INSERT INTO ... SELECT` 语句
250+
- 请避免对字段进行自增或自引用赋值。例如:`UPDATE set name = name+1 ` 语句
250251

251252
4. **元数据缓存**
252253
- 数据库元数据会在首次连接时自动扫描并缓存
253-
- 如果数据库表结构发生变化,可以调用 `clear()` 方法清理缓存,下次访问时会自动重新加载
254+
- 如果数据库表结构发生变化,可以调用 `clear()``metaData.addUpdateTableMateList(String tableName);` 方法清理缓存,下次访问时会自动重新加载
254255

255256
## 📄 许可证
256257

@@ -260,24 +261,6 @@ mvn clean test -P travis
260261

261262
欢迎提交 Issue 和 Pull Request!
262263

263-
## QA
264-
265-
Q: 如果存在com.github.jsqlparser冲突问题,可以在maven配置中忽略掉
266-
A:maven配置如下
267-
```
268-
<dependency>
269-
<groupId>com.codingapi.dbstream</groupId>
270-
<artifactId>dbstream-driver</artifactId>
271-
<version>${latest.version}</version>
272-
<exclusions>
273-
<exclusion>
274-
<groupId>com.github.jsqlparser</groupId>
275-
<artifactId>jsqlparser</artifactId>
276-
</exclusion>
277-
</exclusions>
278-
</dependency>
279-
```
280-
281264

282265
## 📞 联系方式
283266

src/main/java/com/codingapi/dbstream/listener/SQLInsertExecuteListener.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
import com.codingapi.dbstream.stream.DBEvent;
99
import com.codingapi.dbstream.stream.TransactionEventPools;
1010
import com.codingapi.dbstream.utils.SQLUtils;
11-
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
12-
import net.sf.jsqlparser.schema.Table;
13-
import net.sf.jsqlparser.statement.Statement;
14-
import net.sf.jsqlparser.statement.insert.Insert;
1511

1612
import java.sql.SQLException;
1713
import java.util.List;

src/main/java/com/codingapi/dbstream/parser/DBEventParser.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/main/java/com/codingapi/dbstream/parser/DeleteDBEventParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ public List<DBEvent> loadEvents(Object result) throws SQLException {
107107
DbColumn dbColumn = dbTable.getColumnByName(key);
108108
if (dbColumn != null) {
109109
event.set(dbColumn.getName(), params.get(key));
110-
event.addPrimaryKey(dbColumn.getName());
110+
if(dbColumn.isPrimaryKey()) {
111+
event.addPrimaryKey(dbColumn.getName());
112+
}
111113
}
112114
}
113115
eventList.add(event);

src/main/java/com/codingapi/dbstream/parser/InsertDBEventParser.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,45 @@ private List<DBEvent> loadDefaultInsertEvent() {
8080
String jdbcUrl = this.executeState.getJdbcUrl();
8181
List<DBEvent> eventList = new ArrayList<>();
8282
List<Map<String, Object>> primaryKeyValues = this.executeState.getStatementGenerateKeys(dbTable);
83-
for (Map<String, Object> map : primaryKeyValues) {
83+
if(!primaryKeyValues.isEmpty()) {
84+
for (Map<String, Object> map : primaryKeyValues) {
85+
DBEvent event = new DBEvent(jdbcUrl, this.dbTable.getName(), EventType.INSERT);
86+
List<Object> params = this.executeState.getListParams();
87+
List<String> insertColumns = this.sqlParser.getColumnValues();
88+
//主键
89+
for (String key : map.keySet()) {
90+
DbColumn dbColumn = dbTable.getColumnByName(key);
91+
if (dbColumn.isPrimaryKey()) {
92+
event.addPrimaryKey(dbColumn.getName());
93+
}
94+
event.set(dbColumn.getName(), map.get(key));
95+
}
96+
97+
//字段
98+
for (int i = 0; i < params.size(); i++) {
99+
Object value = params.get(i);
100+
String column = insertColumns.get(i);
101+
DbColumn dbColumn = dbTable.getColumnByName(column);
102+
if (dbColumn != null && !dbColumn.isPrimaryKey()) {
103+
event.set(dbColumn.getName(), value);
104+
}
105+
}
106+
eventList.add(event);
107+
}
108+
}else {
84109
DBEvent event = new DBEvent(jdbcUrl, this.dbTable.getName(), EventType.INSERT);
85110
List<Object> params = this.executeState.getListParams();
86111
List<String> insertColumns = this.sqlParser.getColumnValues();
87-
//主键
88-
for (String key : map.keySet()) {
89-
DbColumn dbColumn = dbTable.getColumnByName(key);
90-
if (dbColumn.isPrimaryKey()) {
91-
event.addPrimaryKey(dbColumn.getName());
92-
}
93-
event.set(dbColumn.getName(), map.get(key));
94-
}
95-
96112
//字段
97113
for (int i = 0; i < params.size(); i++) {
98114
Object value = params.get(i);
99115
String column = insertColumns.get(i);
100116
DbColumn dbColumn = dbTable.getColumnByName(column);
101-
if (dbColumn != null && !dbColumn.isPrimaryKey()) {
117+
if (dbColumn != null) {
102118
event.set(dbColumn.getName(), value);
119+
if(dbColumn.isPrimaryKey()){
120+
event.addPrimaryKey(dbColumn.getName());
121+
}
103122
}
104123
}
105124
eventList.add(event);

src/main/java/com/codingapi/dbstream/parser/UpdateDBEventParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ public List<DBEvent> loadEvents(Object result) throws SQLException {
118118
DbColumn dbColumn = dbTable.getColumnByName(key);
119119
if (dbColumn != null) {
120120
event.set(dbColumn.getName(), params.get(key));
121-
event.addPrimaryKey(dbColumn.getName());
121+
if(dbColumn.isPrimaryKey()) {
122+
event.addPrimaryKey(dbColumn.getName());
123+
}
122124
}
123125
}
124126
eventList.add(event);

src/test/java/com/example/dbstream/entity/User.java renamed to src/test/java/com/example/dbstream/entity/User1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
@Data
88
@Entity
9-
@Table(name = "m_user")
10-
public class User {
9+
@Table(name = "m_user_1")
10+
public class User1 {
1111

1212
@Id
1313
@GeneratedValue(strategy = GenerationType.IDENTITY)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.dbstream.entity;
2+
3+
import lombok.Data;
4+
5+
import javax.persistence.*;
6+
7+
@Data
8+
@Entity
9+
@Table(name = "m_user_2")
10+
public class User2 {
11+
12+
@Id
13+
private long id;
14+
15+
private String username;
16+
17+
private String password;
18+
19+
private String email;
20+
21+
private String nickname;
22+
23+
}
24+

src/test/java/com/example/dbstream/repository/UserRepository.java renamed to src/test/java/com/example/dbstream/repository/User1Repository.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package com.example.dbstream.repository;
22

3-
import com.example.dbstream.entity.User;
3+
import com.example.dbstream.entity.User1;
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.data.jpa.repository.Modifying;
66
import org.springframework.data.jpa.repository.Query;
77

8-
public interface UserRepository extends JpaRepository<User, Long> {
8+
public interface User1Repository extends JpaRepository<User1, Long> {
99

10-
User getUserById(Long id);
10+
User1 getUserById(Long id);
1111

1212
@Modifying
13-
@Query("update User set password = ?1")
13+
@Query("update User1 set password = ?1")
1414
int resetPassword(String password);
1515

1616
@Modifying
17-
@Query("update User set password = ?1 where username = ?2")
17+
@Query("update User1 set password = ?1 where username = ?2")
1818
int updatePasswordByUsername(String password, String username);
1919

2020
@Modifying
21-
@Query("delete from User where username = ?1")
21+
@Query("delete from User1 where username = ?1")
2222
int deleteByUsername(String username);
2323

24-
@Query("select count(u) from User as u")
24+
@Query("select count(u) from User1 as u")
2525
int counts();
2626

2727
@Modifying
28-
@Query("insert into User(email,username,password,nickname) select u.email,u.username,u.password,u.nickname from User u")
28+
@Query("insert into User1(email,username,password,nickname) select u.email,u.username,u.password,u.nickname from User1 u")
2929
int insertIntoFromSelect();
3030
}
3131

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.dbstream.repository;
2+
3+
import com.example.dbstream.entity.User2;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Modifying;
6+
import org.springframework.data.jpa.repository.Query;
7+
8+
public interface User2Repository extends JpaRepository<User2, Long> {
9+
10+
User2 getUserById(Long id);
11+
12+
@Modifying
13+
@Query("update User2 set password = ?1")
14+
int resetPassword(String password);
15+
16+
@Modifying
17+
@Query("update User2 set password = ?1 where username = ?2")
18+
int updatePasswordByUsername(String password, String username);
19+
20+
@Modifying
21+
@Query("delete from User2 where username = ?1")
22+
int deleteByUsername(String username);
23+
24+
@Query("select count(u) from User2 as u")
25+
int counts();
26+
27+
}
28+

0 commit comments

Comments
 (0)