From 94f0ae1fbd00910d6f99b7ce01f8a4cb5f14d113 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Sat, 30 Oct 2021 23:20:38 +0800 Subject: [PATCH] user_account_trading removed --- examples/examples.mysql.sql | 28 ++++++----------- examples/examples.postgres.sql | 56 +++++----------------------------- examples/http_tcc_barrier.go | 9 ++---- 3 files changed, 20 insertions(+), 73 deletions(-) diff --git a/examples/examples.mysql.sql b/examples/examples.mysql.sql index 4f77eab..ec26a82 100644 --- a/examples/examples.mysql.sql +++ b/examples/examples.mysql.sql @@ -1,29 +1,19 @@ -CREATE DATABASE if not exists dtm_busi /*!40100 DEFAULT CHARACTER SET utf8mb4 */; - +CREATE DATABASE if not exists dtm_busi +/*!40100 DEFAULT CHARACTER SET utf8mb4 */ +; drop table if exists dtm_busi.user_account; create table if not exists dtm_busi.user_account( id int(11) PRIMARY KEY AUTO_INCREMENT, - user_id int(11) UNIQUE , + user_id int(11) UNIQUE, balance DECIMAL(10, 2) not null default '0', - create_time datetime DEFAULT now(), - update_time datetime DEFAULT now(), - key(create_time), - key(update_time) -); - -insert into dtm_busi.user_account (user_id, balance) values (1, 10000), (2, 10000) on DUPLICATE KEY UPDATE balance=values (balance); - -drop table if exists dtm_busi.user_account_trading; -create table if not exists dtm_busi.user_account_trading( -- 表示交易中被冻结的金额 - id int(11) PRIMARY KEY AUTO_INCREMENT, - user_id int(11) UNIQUE , trading_balance DECIMAL(10, 2) not null default '0', create_time datetime DEFAULT now(), update_time datetime DEFAULT now(), key(create_time), key(update_time) ); - -insert into dtm_busi.user_account_trading (user_id, trading_balance) values (1, 0), (2, 0) on DUPLICATE KEY UPDATE trading_balance=values (trading_balance); - - +insert into dtm_busi.user_account (user_id, balance) +values (1, 10000), + (2, 10000) on DUPLICATE KEY +UPDATE balance = +values (balance); \ No newline at end of file diff --git a/examples/examples.postgres.sql b/examples/examples.postgres.sql index d657aed..1b61bdb 100644 --- a/examples/examples.postgres.sql +++ b/examples/examples.postgres.sql @@ -1,14 +1,14 @@ -CREATE SCHEMA if not exists dtm_busi /* SQLINES DEMO *** RACTER SET utf8mb4 */; -create SCHEMA if not exists dtm_barrier /* SQLINES DEMO *** RACTER SET utf8mb4 */; - +CREATE SCHEMA if not exists dtm_busi +/* SQLINES DEMO *** RACTER SET utf8mb4 */ +; drop table if exists dtm_busi.user_account; -- SQLINES LICENSE FOR EVALUATION USE ONLY create sequence if not exists dtm_busi.user_account_seq; - create table if not exists dtm_busi.user_account( id int PRIMARY KEY DEFAULT NEXTVAL ('dtm_busi.user_account_seq'), - user_id int UNIQUE , + user_id int UNIQUE, balance DECIMAL(10, 2) not null default '0', + trading_balance DECIMAL(10, 2) not null default '0', create_time timestamp(0) DEFAULT now(), update_time timestamp(0) DEFAULT now() ); @@ -16,47 +16,7 @@ create table if not exists dtm_busi.user_account( create index if not exists create_idx on dtm_busi.user_account(create_time); -- SQLINES LICENSE FOR EVALUATION USE ONLY create index if not exists update_idx on dtm_busi.user_account(update_time); - TRUNCATE dtm_busi.user_account; -insert into dtm_busi.user_account (user_id, balance) values (1, 10000), (2, 10000); - -drop table if exists dtm_busi.user_account_trading; --- SQLINES LICENSE FOR EVALUATION USE ONLY -create sequence if not exists dtm_busi.user_account_trading_seq; - -create table if not exists dtm_busi.user_account_trading( -- SQLINES DEMO *** �冻结的金额 - id int PRIMARY KEY DEFAULT NEXTVAL ('dtm_busi.user_account_trading_seq'), - user_id int UNIQUE , - trading_balance DECIMAL(10, 2) not null default '0', - create_time timestamp(0) DEFAULT now(), - update_time timestamp(0) DEFAULT now() -); --- SQLINES LICENSE FOR EVALUATION USE ONLY -create index if not exists create_idx on dtm_busi.user_account_trading(create_time); --- SQLINES LICENSE FOR EVALUATION USE ONLY -create index if not exists update_idx on dtm_busi.user_account_trading(update_time); - -TRUNCATE dtm_busi.user_account_trading; -insert into dtm_busi.user_account_trading (user_id, trading_balance) values (1, 0), (2, 0); - - -drop table if exists dtm_barrier.barrier; --- SQLINES LICENSE FOR EVALUATION USE ONLY -create sequence if not exists dtm_barrier.barrier_seq; - -create table if not exists dtm_barrier.barrier( - id int PRIMARY KEY DEFAULT NEXTVAL ('dtm_barrier.barrier_seq'), - trans_type varchar(45) default '' , - gid varchar(128) default'', - branch_id varchar(128) default '', - branch_type varchar(45) default '', - reason varchar(45) default '' , - result varchar(2047) default null , - create_time timestamp(0) DEFAULT now(), - update_time timestamp(0) DEFAULT now(), - UNIQUE (gid, branch_id, branch_type) -); --- SQLINES LICENSE FOR EVALUATION USE ONLY -create index if not exists create_idx on dtm_barrier.barrier(create_time); --- SQLINES LICENSE FOR EVALUATION USE ONLY -create index if not exists update_idx on dtm_barrier.barrier(update_time); +insert into dtm_busi.user_account (user_id, balance) +values (1, 10000), + (2, 10000); \ No newline at end of file diff --git a/examples/http_tcc_barrier.go b/examples/http_tcc_barrier.go index 7e1c39c..f65049b 100644 --- a/examples/http_tcc_barrier.go +++ b/examples/http_tcc_barrier.go @@ -37,7 +37,7 @@ const transInUID = 1 const transOutUID = 2 func adjustTrading(db dtmcli.DB, uid int, amount int) error { - affected, err := dtmcli.DBExec(db, "update dtm_busi.user_account_trading set trading_balance=trading_balance + ? where user_id=? and trading_balance + ? + (select balance from dtm_busi.user_account where user_id=?) >= 0", amount, uid, amount, uid) + affected, err := dtmcli.DBExec(db, "update dtm_busi.user_account set trading_balance=trading_balance + ? where user_id=? and trading_balance + ? + balance >= 0", amount, uid, amount) if err == nil && affected == 0 { return fmt.Errorf("update error, maybe balance not enough") } @@ -45,12 +45,9 @@ func adjustTrading(db dtmcli.DB, uid int, amount int) error { } func adjustBalance(db dtmcli.DB, uid int, amount int) error { - affected, err := dtmcli.DBExec(db, "update dtm_busi.user_account_trading set trading_balance = trading_balance + ? where user_id=?;", -amount, uid) - if err == nil && affected == 1 { - affected, err = dtmcli.DBExec(db, "update dtm_busi.user_account set balance=balance+? where user_id=?", amount, uid) - } + affected, err := dtmcli.DBExec(db, "update dtm_busi.user_account set trading_balance = trading_balance - ?, balance=balance+? where user_id=?;", amount, amount, uid) if err == nil && affected == 0 { - return fmt.Errorf("update 0 rows") + return fmt.Errorf("update user_account 0 rows") } return err }