saboluo commented on issue #4306: Why do distributed primary keys need to be 
retrieved in reverse order in 
ShardingGeneratedKeyInsertValueParameterRewriter.rewrite
URL: 
https://github.com/apache/incubator-shardingsphere/issues/4306#issuecomment-586539549
 
 
   **config file:**
   ```
   dataSources:  
     lunar0: !!com.alibaba.druid.pool.DruidDataSource
       driverClassName: com.mysql.jdbc.Driver
       url: jdbc:mysql://localhost:3307/lunar0
       username: 
       password: 
       initialSize: 10
       maxActive: 10
       maxWait: 60000
       keepAlive: true
     lunar1: !!com.alibaba.druid.pool.DruidDataSource
       driverClassName: com.mysql.jdbc.Driver
       url: jdbc:mysql://localhost:3308/lunar1
       username: 
       password: 
       initialSize: 10
       maxActive: 10
       maxWait: 60000
       keepAlive: true
   shardingRule:
     tables:
       user:
         actualDataNodes: lunar${0..1}.user${0..2}
         databaseStrategy:
           inline:
             shardingColumn: id
             algorithmExpression: lunar${id % 2}
         tableStrategy:
           inline:
             shardingColumn: id
             algorithmExpression: user${id % 3}
         keyGenerator:
           type: SNOWFLAKE
           column: id
     defaultDataSourceName: lunar0
     defaultTableStrategy:
       none:
   props:
     sql.show: true
   ```
   **code:**
   ```
           URL url = Main.class.getClassLoader().getResource("configFile.yaml");
           DataSource dataSource = 
YamlShardingDataSourceFactory.createDataSource(new File(url.getFile()));
           String sql = "insert into user(`name`,`age`) values 
(?,?),(?,?),(?,?),(?,?)";
           try(Connection con = dataSource.getConnection()){
               try(PreparedStatement preparedStatement = 
con.prepareStatement(sql)){
                   for (int i = 0; i < 4; i++){
                       preparedStatement.setString(i * 2 + 1,"name-" + i);
                       preparedStatement.setInt(i * 2 + 2,i);
                   }
                   preparedStatement.execute();
               }
           }
   ```
   **logic table user:**
   ```
   CREATE TABLE `user` (
     `id` bigint(20) NOT NULL,
     `name` varchar(255) DEFAULT NULL,
     `age` int(11) DEFAULT NULL,
     PRIMARY KEY (`id`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8
   ```
   
   **result:**
   Actual SQL: lunar0 ::: insert into user1(`name`,`age`, id) values (?, ?, ?) 
::: [name-0, 0, 435366707763085315]
   Actual SQL: lunar1 ::: insert into user2(`name`,`age`, id) values (?, ?, ?) 
::: [name-1, 1, 435366707763085314]
   Actual SQL: lunar0 ::: insert into user0(`name`,`age`, id) values (?, ?, ?) 
::: [name-2, 2, 435366707763085313]
   Actual SQL: lunar1 ::: insert into user1(`name`,`age`, id) values (?, ?, ?) 
::: [name-3, 3, 435366707763085312]
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to