|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "database/sql" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/seata/seata-go/pkg/client" |
| 26 | + sql2 "github.com/seata/seata-go/pkg/datasource/sql" |
| 27 | + "github.com/seata/seata-go/pkg/tm" |
| 28 | + "gorm.io/driver/mysql" |
| 29 | + "gorm.io/gorm" |
| 30 | +) |
| 31 | + |
| 32 | +type OrderTblModel struct { |
| 33 | + Id int64 `gorm:"column:id" json:"id"` |
| 34 | + UserId string `gorm:"column:user_id" json:"user_id"` |
| 35 | + CommodityCode string `gorm:"commodity_code" json:"commodity_code"` |
| 36 | + Count int64 `gorm:"count" json:"count"` |
| 37 | + Money int64 `gorm:"money" json:"money"` |
| 38 | + Descs string `gorm:"descs" json:"descs"` |
| 39 | +} |
| 40 | + |
| 41 | +func main() { |
| 42 | + initConfig() |
| 43 | + // insert |
| 44 | + tm.WithGlobalTx(context.Background(), &tm.GtxConfig{ |
| 45 | + Name: "ATSampleLocalGlobalTx", |
| 46 | + Timeout: time.Second * 30, |
| 47 | + }, insertData) |
| 48 | + <-make(chan struct{}) |
| 49 | +} |
| 50 | + |
| 51 | +func initConfig() { |
| 52 | + // init seata client config |
| 53 | + client.InitPath("/Users/rain/go/src/wang1309/seata-go-samples/conf/seatago.yml") |
| 54 | + // init db object |
| 55 | + initDB() |
| 56 | +} |
| 57 | + |
| 58 | +var gormDB *gorm.DB |
| 59 | + |
| 60 | +func initDB() { |
| 61 | + sqlDB, err := sql.Open(sql2.SeataATMySQLDriver, "root:12345678@tcp(127.0.0.1:3306)/seata_client?multiStatements=true&interpolateParams=true") |
| 62 | + if err != nil { |
| 63 | + panic("init service error") |
| 64 | + } |
| 65 | + |
| 66 | + gormDB, err = gorm.Open(mysql.New(mysql.Config{ |
| 67 | + Conn: sqlDB, |
| 68 | + }), &gorm.Config{}) |
| 69 | +} |
| 70 | + |
| 71 | +// insertData insert one data |
| 72 | +func insertData(ctx context.Context) error { |
| 73 | + data := OrderTblModel{ |
| 74 | + Id: 1, |
| 75 | + UserId: "NO-100003", |
| 76 | + CommodityCode: "C100001", |
| 77 | + Count: 101, |
| 78 | + Money: 11, |
| 79 | + Descs: "insert desc", |
| 80 | + } |
| 81 | + |
| 82 | + return gormDB.WithContext(ctx).Table("order_tbl").Create(&data).Error |
| 83 | +} |
| 84 | + |
| 85 | +// deleteData delete one data |
| 86 | +func deleteData(ctx context.Context) error { |
| 87 | + return gormDB.WithContext(ctx).Where("id = ?", "1").Delete(&OrderTblModel{}).Error |
| 88 | +} |
| 89 | + |
| 90 | +// updateDate update one data |
| 91 | +func updateData(ctx context.Context) error { |
| 92 | + return gormDB.WithContext(ctx).Model(&OrderTblModel{}).Where("id = ?", "1").Update("commodity_code", "C100002").Error |
| 93 | +} |
0 commit comments