Skip to content

Commit be34b8f

Browse files
committed
update METE-INF name
1 parent dbae473 commit be34b8f

File tree

15 files changed

+159
-91
lines changed

15 files changed

+159
-91
lines changed

pom.xml

Lines changed: 1 addition & 7 deletions
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.4</version>
7+
<version>1.0.5</version>
88

99
<url>https://github.com/codingapi/dbstream-driver</url>
1010
<name>dbstream-driver</name>
@@ -75,12 +75,6 @@
7575
<artifactId>lombok</artifactId>
7676
<version>${lombok.version}</version>
7777
</dependency>
78-
79-
<dependency>
80-
<groupId>com.github.jsqlparser</groupId>
81-
<artifactId>jsqlparser</artifactId>
82-
<version>${jsqlparser.version}</version>
83-
</dependency>
8478
</dependencies>
8579
</dependencyManagement>
8680

src/main/java/com/codingapi/dbstream/driver/DBStreamProxyDriver.java

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,102 @@
88
import com.codingapi.dbstream.scanner.DBMetaContext;
99
import com.codingapi.dbstream.scanner.DBMetaData;
1010
import com.codingapi.dbstream.scanner.DBScanner;
11+
import com.codingapi.dbstream.utils.JDBCPropertyUtils;
1112

1213
import java.sql.*;
1314
import java.util.Enumeration;
1415
import java.util.Properties;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
import java.util.concurrent.ConcurrentMap;
1518
import java.util.logging.Logger;
19+
import java.util.logging.Level;
1620

1721
public class DBStreamProxyDriver implements Driver {
1822

19-
private Driver driver;
23+
private static final ConcurrentMap<String, Driver> DRIVER_CACHE = new ConcurrentHashMap<>();
24+
private static final Logger LOGGER = Logger.getLogger(DBStreamProxyDriver.class.getName());
2025

2126
static {
2227
try {
2328
DriverManager.registerDriver(new DBStreamProxyDriver());
24-
}catch (Exception e){
25-
throw new RuntimeException(e);
29+
} catch (Exception e) {
30+
LOGGER.log(Level.SEVERE, "Failed to register DBStreamProxyDriver", e);
31+
throw new RuntimeException("Failed to register DBStreamProxyDriver", e);
2632
}
2733
SQLRunningContext.getInstance().addListener(new SQLDeleteExecuteListener());
2834
SQLRunningContext.getInstance().addListener(new SQLInsertExecuteListener());
2935
SQLRunningContext.getInstance().addListener(new SQLUpdateExecuteListener());
30-
System.out.println("DBStreamProxyDriver init register...");
36+
LOGGER.info("DBStreamProxyDriver initialized and registered");
3137
}
3238

39+
/**
40+
* 查找接受指定 URL 的真实 JDBC 驱动
41+
*
42+
* @param url JDBC URL
43+
* @return 真实的 JDBC 驱动,如果未找到则返回 null
44+
*/
45+
private Driver findDriver(String url) throws SQLException {
46+
if (url == null) {
47+
return null;
48+
}
49+
50+
// 从缓存中查找(使用 URL 前缀作为 key)
51+
Driver cachedDriver = DRIVER_CACHE.get(url);
52+
if (cachedDriver != null) {
53+
try {
54+
// 验证缓存的驱动仍然接受该 URL
55+
if (cachedDriver.acceptsURL(url)) {
56+
return cachedDriver;
57+
} else {
58+
// 如果缓存的驱动不再接受该 URL,从缓存中移除
59+
DRIVER_CACHE.remove(url, cachedDriver);
60+
}
61+
} catch (SQLException e) {
62+
// 如果验证失败,从缓存中移除并继续查找
63+
DRIVER_CACHE.remove(url, cachedDriver);
64+
LOGGER.log(Level.FINE, "Cached driver no longer accepts URL: " + url, e);
65+
}
66+
}
67+
68+
// 遍历所有已注册的驱动
69+
Enumeration<Driver> drivers = DriverManager.getDrivers();
70+
while (drivers.hasMoreElements()) {
71+
Driver driver = drivers.nextElement();
72+
if (driver.getClass().equals(DBStreamProxyDriver.class)) {
73+
continue;
74+
}
75+
try {
76+
if (driver.acceptsURL(url)) {
77+
// 缓存驱动(使用 URL 的前缀作为 key,因为同一个数据库类型的 URL 前缀相同)
78+
DRIVER_CACHE.putIfAbsent(url, driver);
79+
return driver;
80+
}
81+
} catch (SQLException e) {
82+
// 忽略单个驱动的异常,继续查找
83+
LOGGER.log(Level.FINE, "Driver " + driver.getClass().getName() + " does not accept URL: " + url, e);
84+
}
85+
}
86+
return null;
87+
}
88+
89+
3390
@Override
3491
public Connection connect(String url, Properties info) throws SQLException {
35-
if (this.driver == null) {
36-
this.acceptsURL(url);
92+
if (url == null) {
93+
throw new SQLException("URL cannot be null");
94+
}
95+
96+
Driver driver = findDriver(url);
97+
if (driver == null) {
98+
throw new SQLException("No suitable driver found for " + url);
3799
}
100+
38101
Connection connection = driver.connect(url, info);
102+
if (connection == null) {
103+
throw new SQLException("Driver returned null connection for URL: " + url);
104+
}
39105
info.setProperty(DBMetaData.KEY_JDBC_URL, url);
40-
String jdbcKey = info.getProperty(DBMetaData.KEY_JDBC_KEY);
106+
String jdbcKey = JDBCPropertyUtils.getJdbcKey(info,connection.getSchema());
41107
DBMetaData metaData = DBMetaContext.getInstance().getMetaData(jdbcKey);
42108
if (metaData == null) {
43109
DBScanner scanner = new DBScanner(connection, info);
@@ -49,42 +115,42 @@ public Connection connect(String url, Properties info) throws SQLException {
49115

50116
@Override
51117
public boolean acceptsURL(String url) throws SQLException {
52-
Enumeration<Driver> drivers = DriverManager.getDrivers();
53-
while (drivers.hasMoreElements()) {
54-
Driver driver = drivers.nextElement();
55-
if(driver.getClass().equals(DBStreamProxyDriver.class)){
56-
continue;
57-
}
58-
if (driver.acceptsURL(url)) {
59-
this.driver = driver;
60-
return true;
61-
}
118+
if (url == null) {
119+
return false;
62120
}
63-
return false;
121+
Driver driver = findDriver(url);
122+
return driver != null;
64123
}
65124

66125
@Override
67126
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
127+
Driver driver = findDriver(url);
128+
if (driver == null) {
129+
throw new SQLException("No suitable driver found for " + url);
130+
}
68131
return driver.getPropertyInfo(url, info);
69132
}
70133

71134
@Override
72135
public int getMajorVersion() {
73-
return driver.getMajorVersion();
136+
// 返回代理驱动的主版本号
137+
return 1;
74138
}
75139

76140
@Override
77141
public int getMinorVersion() {
78-
return driver.getMinorVersion();
142+
// 返回代理驱动的次版本号
143+
return 0;
79144
}
80145

81146
@Override
82147
public boolean jdbcCompliant() {
83-
return driver.jdbcCompliant();
148+
// 代理驱动本身不直接兼容 JDBC,它依赖于底层驱动
149+
return false;
84150
}
85151

86152
@Override
87153
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
88-
return driver.getParentLogger();
154+
return LOGGER;
89155
}
90156
}

src/main/java/com/codingapi/dbstream/interceptor/SQLRunningContext.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44
import lombok.Getter;
55

66
import java.sql.SQLException;
7-
import java.util.ArrayList;
87
import java.util.List;
8+
import java.util.concurrent.CopyOnWriteArrayList;
99

1010
public class SQLRunningContext implements SQLExecuteListener {
1111

1212
@Getter
1313
private final static SQLRunningContext instance = new SQLRunningContext();
1414

1515
@Getter
16-
private final List<SQLExecuteListener> listeners = new ArrayList<>();
16+
private final List<SQLExecuteListener> listeners = new CopyOnWriteArrayList<>();
1717

1818
private SQLRunningContext() {
1919

2020
}
2121

2222
public void addListener(SQLExecuteListener listener) {
23-
listeners.add(listener);
23+
if (listener != null) {
24+
listeners.add(listener);
25+
}
2426
}
2527

2628

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class InsertDBEventParser {
2020
private final InsertSQLParser sqlParser;
2121
private final SQLExecuteState executeState;
2222
private final DbTable dbTable;
23-
private boolean defaultInsertSQL = true;
2423
private List<Map<String, Object>> dataList = new ArrayList<>();
2524
private final List<String> columns;
2625

@@ -79,30 +78,9 @@ private List<DBEvent> loadDataEvents() {
7978
return eventList;
8079
}
8180

82-
private boolean isColumnPrimaryKey(String primaryKey) {
83-
for (String column : columns) {
84-
if (primaryKey.equalsIgnoreCase(column)) {
85-
return true;
86-
}
87-
}
88-
return false;
89-
}
90-
91-
92-
private boolean columnsHasPrimaryKeys() {
93-
List<String> primaryKeys = this.dbTable.getPrimaryKeys();
94-
for (String primaryKey : primaryKeys) {
95-
if (!this.isColumnPrimaryKey(primaryKey)) {
96-
return false;
97-
}
98-
}
99-
return true;
100-
}
101-
102-
10381
public void prepare() throws SQLException {
104-
this.defaultInsertSQL = this.sqlParser.isDefaultInsertSQL();
105-
if (this.defaultInsertSQL) {
82+
boolean defaultInsertSQL = this.sqlParser.isDefaultInsertSQL();
83+
if (defaultInsertSQL) {
10684
this.loadDefaultInsertDataList();
10785
} else {
10886
this.loadSelectInsertDataList();

src/main/java/com/codingapi/dbstream/proxy/ConnectionProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public ConnectionProxy(Connection connection, DBMetaData metaData) {
2929
}
3030

3131
private void generateTransactionKey() {
32-
this.transactionKey = UUID.randomUUID().toString().replaceAll("-", "");
32+
this.transactionKey = UUID.randomUUID().toString().replace("-", "");
3333
}
3434

3535
@Override

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import lombok.Getter;
44

55
import java.util.ArrayList;
6-
import java.util.HashMap;
76
import java.util.List;
87
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
99

1010
/**
1111
* 数据库元数据信息上下文对象
@@ -19,7 +19,7 @@ private DBMetaContext() {
1919
}
2020

2121
// key:jdbcKey,value:DBMetaData
22-
private final Map<String, DBMetaData> cache = new HashMap<>();
22+
private final Map<String, DBMetaData> cache = new ConcurrentHashMap<>();
2323

2424
/**
2525
* 获取元数据库的信息
@@ -28,6 +28,9 @@ private DBMetaContext() {
2828
* @return 元数据信息
2929
*/
3030
public DBMetaData getMetaData(String key) {
31+
if(key==null){
32+
return null;
33+
}
3134
return cache.get(key);
3235
}
3336

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ public class DBMetaData {
1818
@Getter
1919
private final List<String> updateTableMetaList = new ArrayList<>();
2020

21-
/**
22-
* 创建唯一标识
23-
*
24-
* @return sha256(jdbcUrl+schema)
25-
*/
26-
private String generateKey(String schema) {
27-
String jdbcUrl = this.getJdbcUrl();
28-
String data = String.format("%s#%s", jdbcUrl, schema == null ? "" : schema);
29-
return SHA256Utils.sha256(data);
30-
}
31-
3221
/**
3322
* 数据记录时间
3423
*/
@@ -47,9 +36,8 @@ private String generateKey(String schema) {
4736
@Getter
4837
private final Properties properties;
4938

50-
public DBMetaData(Properties properties, String schema) {
39+
public DBMetaData(Properties properties) {
5140
this.properties = properties;
52-
this.properties.setProperty(DBMetaData.KEY_JDBC_KEY, this.generateKey(schema));
5341
}
5442

5543
/**
@@ -59,7 +47,7 @@ public DBMetaData(Properties properties, String schema) {
5947
*/
6048
public void addUpdateTableMateList(String tableName) {
6149
String upTableName = tableName.toUpperCase();
62-
if (this.updateTableMetaList.contains(upTableName)) {
50+
if (!this.updateTableMetaList.contains(upTableName)) {
6351
this.updateTableMetaList.add(upTableName);
6452
}
6553
}
@@ -144,6 +132,11 @@ public boolean isUpdateTableMeta(String tableName) {
144132
* @param updateList 更新的表元数据信息
145133
*/
146134
void updateDbTable(List<DbTable> updateList) {
135+
if (this.tables == null || this.tables.isEmpty()) {
136+
this.tables = new ArrayList<>(updateList);
137+
return;
138+
}
139+
147140
List<DbTable> list = new ArrayList<>();
148141
Map<String, DbTable> updateDbTables = new HashMap<>();
149142
for (DbTable update : updateList) {
@@ -162,7 +155,21 @@ void updateDbTable(List<DbTable> updateList) {
162155
}
163156
}
164157

165-
this.tables.clear();
158+
// 添加新增的表(在 updateList 中但不在原有 tables 中的表)
159+
for (DbTable update : updateList) {
160+
String tableName = update.getName().toUpperCase();
161+
boolean exists = false;
162+
for (DbTable existing : this.tables) {
163+
if (existing.getName().equalsIgnoreCase(tableName)) {
164+
exists = true;
165+
break;
166+
}
167+
}
168+
if (!exists) {
169+
list.add(update);
170+
}
171+
}
172+
166173
this.tables = list;
167174
}
168175
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public DBScanner(Connection connection, Properties info) throws SQLException {
2525
this.schema = connection.getSchema();
2626
this.metaData = connection.getMetaData();
2727
this.catalog = connection.getCatalog();
28-
this.dbMetaData = new DBMetaData(info, this.schema);
28+
this.dbMetaData = new DBMetaData(info);
2929
this.dbTableSerializableHelper = new DBTableSerializableHelper(this.dbMetaData.getKeyJdbcKey());
3030
}
3131

0 commit comments

Comments
 (0)