shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

ON DUPLICATE KEY UPDATE support incompletely on mybatis

Open zhegexiaohuozi opened this issue 7 years ago • 13 comments

Bug Report

For English only, other languages will not accept.

Before report a bug, make sure you have:

Please pay attention on issues you submitted, because we maybe need more details. If no response more than 7 days and we cannot reproduce it on current information, we will close it.

Please answer these questions before submitting your issue. Thanks!

Which version of ShardingSphere did you use?

4.0.0-RC2

Which project did you use? Sharding-JDBC or Sharding-Proxy?

Sharding-JDBC

Expected behavior

INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + #{bean.val2} should execute successfully.

Actual behavior

got exception

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: java.sql.SQLException: No value specified for parameter 3
### The error may involve com.duoku.vipintegral.dao.vipscore.UserScoreLevelMapper.insertOrUpdate-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO test_a (id,col_1,col_2) VALUES(1,?, ?) ON DUPLICATE KEY UPDATE col_2 = col_2 + ?
### Cause: java.sql.SQLException: No value specified for parameter 3
; bad SQL grammar []; nested exception is java.sql.SQLException: No value specified for parameter 3

Reason analyze (If you can)

Debug from the source,found that the num of parameters just only get from InsertValuesSegment, but the parameter after ON DUPLICATE KEY UPDATE is not included in the calculation.And I also can not find values that belong to 'OnDuplicateKey' expression in OnDuplicateKeyColumnsSegment.

https://github.com/apache/incubator-shardingsphere/blob/85f2ff877a7aaf122998964a0d03f7c9b2830e36/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingInsertOptimizeEngine.java#L70

currentParameters do not contains the parameter for 'ON DUPLICATE KEY UPDATE',and parameters is full.

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

  • sql
CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL COMMENT '主键',
  `col_1` varchar(20) NOT NULL DEFAULT '' COMMENT 'val_1',
  `col_2` int(10) NOT NULL DEFAULT '0' COMMENT 'val_int_2',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + #{bean.val2}
  • in mybatis3
  • any shareding rule.

Example codes for reproduce this issue (such as a github link).

zhegexiaohuozi avatar Oct 10 '19 09:10 zhegexiaohuozi

@zhegexiaohuozi Please use following format.

ON DUPLICATE KEY UPDATE col_2 = col_2 + VALUES(col_2)


INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + VALUES(col_2)

lonyee1989 avatar Oct 10 '19 10:10 lonyee1989

Fixed at #3237

terrymanu avatar Oct 15 '19 03:10 terrymanu

I use the core of dev branch, The problem is still exist.

HongJinFeng avatar Nov 08 '19 01:11 HongJinFeng

I still have this problem with 4.0.0-RC3 version when use VALUES like this: SQL: insert into t_xxxx(data_type, delivery_org_id,receive_org_id,actual_fee,actual_instead_goods_fee, create_time) values ( ?,?,?,?,?,?) ON DUPLICATE KEY UPDATE actual_fee = actual_fee + VALUES(?), actual_instead_goods_fee = actual_instead_goods_fee + VALUES(?),create_time = VALUES(?)

Cause: java.sql.SQLException: No value specified for parameter 7

does anyone solve this problem ?

zhaoyongsheng1990 avatar Dec 19 '19 08:12 zhaoyongsheng1990

actual_fee = actual_fee + VALUES(?) change to actual_fee = actual_fee + VALUES(actual_fee)

在 2019年12月19日,16:34,zhaoyongsheng1990 [email protected] 写道:

VALUES(?)

lonyee1989 avatar Dec 20 '19 12:12 lonyee1989

@lonyee1989 : I need add dynamic parameter to actual_fee in mapper file like this: actual_fee = actual_fee + VALUES(#{actualFee}),

zhaoyongsheng1990 avatar Dec 23 '19 01:12 zhaoyongsheng1990

dynamic parameter => actual_fee = actual_fee + VALUES(actual_fee)

eg.. db — actual_fee = 2

parameter — 5

exec:

ON DUPLICATE KEY UPDATE actual_fee = actual_fee + VALUES(actual_fee)

then

db — actual_fee = 7

please try it!

在 2019年12月23日,09:20,zhaoyongsheng1990 [email protected] 写道:

VALUES(#{actualFee})

lonyee1989 avatar Dec 25 '19 11:12 lonyee1989

dynamic parameter => actual_fee = actual_fee + VALUES(actual_fee) eg.. db — actual_fee = 2 parameter — 5 exec: ON DUPLICATE KEY UPDATE actual_fee = actual_fee + VALUES(actual_fee) then db — actual_fee = 7 please try it! … 在 2019年12月23日,09:20,zhaoyongsheng1990 @.***> 写道: VALUES(#{actualFee})

I change mapper file like this: actual_fee = actual_fee + VALUES(actual_fee), it worked , thanks very much.

zhaoyongsheng1990 avatar Dec 26 '19 02:12 zhaoyongsheng1990

If insert value is different from update value, such as:

INSERT INTO table_a (id,col_1,col_2) VALUES(1,#{bean.val1}, #{bean.val2}) ON DUPLICATE KEY UPDATE col_2 = col_2 + #{bean.val5}

Is the syntax already supported? @lonyee1989 @terrymanu

zhegexiaohuozi avatar Dec 26 '19 02:12 zhegexiaohuozi

I don't know what is it. We only can talk about SQL, not ORM frameworks.

terrymanu avatar Dec 26 '19 09:12 terrymanu

Yeah,this issue should really be described as a support integrity issue for mybatis.Under mybatis, you do lose the variable after 'ON DUPLICATE KEY UPDATE'. Just watching the discussion, by the way.

zhegexiaohuozi avatar Dec 26 '19 10:12 zhegexiaohuozi

What is your log of sql.show?

terrymanu avatar Jan 06 '20 14:01 terrymanu

Closed for no response anymore

terrymanu avatar Jan 19 '20 04:01 terrymanu