Browse Source

user_account_trading removed

pull/46/head
yedf2 4 years ago
parent
commit
94f0ae1fbd
  1. 28
      examples/examples.mysql.sql
  2. 56
      examples/examples.postgres.sql
  3. 9
      examples/http_tcc_barrier.go

28
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);

56
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);

9
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
}

Loading…
Cancel
Save