Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support oracle and postgresql multi primary key #4863

Merged
merged 23 commits into from Aug 20, 2022
Merged

feature: support oracle and postgresql multi primary key #4863

merged 23 commits into from Aug 20, 2022

Conversation

tuwenlin
Copy link
Contributor

简单说一下实现思路:
首先,多主键只会在insert时用到,因为回滚要根据主键delete数据
Oracle主键分为四种类型处理:1.写在Insert语句中的主键.2.序列3.函数.4.NULL
1.写在Insert语句中的主键,通过解析sql获取,同时获取其他主键的类型
2.序列,通过getPkValuesBySequence方法获取,方法新增了参数pkKey,为主键的列名,先通过getGeneratedKeys方法中获取,这个方法也增加了参数pkKey,为什么要这么做,因为原getGeneratedKeys方法中,调用了targetStatement.getGeneratedKeys(),并默认获取了ResultSet中第一个作为主键,应该根据列名去获取主键,所以这两个方法增加了pkKey参数,如果无法通过targetStatement.getGeneratedKeys()获取主键value,执行对应的Sequence,去获取当前序列的值;
3.函数,函数类型的主键只能通过getGeneratedKeys方法去获取主键的value,获取不到value,则抛出不支持异常
4.NULL,NULL值的主键value也通过targetStatement.getGeneratedKeys()获取
PGSQL主键没有NULL值,有Default值,其余三种和Oracle获取value一样
PGSQL的Default类型通过getPkValuesByDefault方法获取,先通过metadata获取默认值,再根据默认值,按照3种类型再去解析
最终,多主键最多允许一个为NULL,不允许出现函数(这块跟上面有冲突,抽空验证后再觉得是否要去掉上述函数类型分支)

@tuwenlin
Copy link
Contributor Author

原pr ##4457 落后太多了,已close

@codecov-commenter
Copy link

codecov-commenter commented Aug 12, 2022

Codecov Report

Merging #4863 (4770c88) into develop (9f71c37) will increase coverage by 0.15%.
The diff coverage is 43.93%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #4863      +/-   ##
=============================================
+ Coverage      49.15%   49.30%   +0.15%     
- Complexity      4094     4118      +24     
=============================================
  Files            737      737              
  Lines          25750    25778      +28     
  Branches        3181     3187       +6     
=============================================
+ Hits           12658    12711      +53     
+ Misses         11737    11720      -17     
+ Partials        1355     1347       -8     
Impacted Files Coverage Δ
...ta/rm/datasource/exec/AbstractDMLBaseExecutor.java 57.14% <ø> (-1.69%) ⬇️
.../rm/datasource/exec/mysql/MySQLInsertExecutor.java 68.42% <0.00%> (-0.73%) ⬇️
...o/seata/rm/datasource/exec/BaseInsertExecutor.java 61.92% <22.58%> (-9.20%) ⬇️
...urce/exec/postgresql/PostgresqlInsertExecutor.java 51.42% <61.90%> (+7.67%) ⬆️
...m/datasource/exec/oracle/OracleInsertExecutor.java 73.68% <69.23%> (+11.18%) ⬆️
...torage/file/store/FileTransactionStoreManager.java 56.91% <0.00%> (+0.64%) ⬆️
... and 4 more

* @return
* @throws SQLException
*/
public List<Object> getGeneratedKeys() throws SQLException {
public List<Object> getGeneratedKeys(String pkKey) throws SQLException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议改成方法重载方式,避免涉及太多改动

@funky-eyes
Copy link
Contributor

1.在线验证,update,delete不受insert多主键支持影响,生成的后镜像where条件是准的
2.tablemeta原本就支持缓存多个主键
3.由于oracle和pgsql这块获取多主键的值很奇怪,有可能主键1的值拿到主键2,所以需要在获取的时候传入pkkey进行获取,以防出现上述问题
验证提交和回滚皆为正常

@funky-eyes funky-eyes added this to the 1.6.0 milestone Aug 12, 2022
@funky-eyes funky-eyes added type: feature Category issues or prs related to feature request. module/rm-datasource rm-datasource module labels Aug 12, 2022
@funky-eyes funky-eyes changed the title feature:support oracle and postgresql multi primary key feature: support oracle and postgresql multi primary key Aug 15, 2022
Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@jsbxyyx jsbxyyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Test pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/rm-datasource rm-datasource module multilingual type: feature Category issues or prs related to feature request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants