Skip to content

Commit 02095ba

Browse files
authored
feat: add gorm sample (apache#8)
* feat:add gorm
1 parent e00a596 commit 02095ba

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

at/gorm/main.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ go 1.19
55
require (
66
dubbo.apache.org/dubbo-go/v3 v3.0.3-rc2
77
github.com/gin-gonic/gin v1.8.0
8-
github.com/go-sql-driver/mysql v1.6.0
8+
github.com/go-sql-driver/mysql v1.7.0
99
github.com/parnurzeal/gorequest v0.2.16
1010
github.com/seata/seata-go v1.0.3
1111
google.golang.org/grpc v1.49.0
1212
google.golang.org/protobuf v1.28.1
13+
gorm.io/driver/mysql v1.4.5
14+
gorm.io/gorm v1.24.3
1315
)
1416

1517
//replace github.com/seata/seata-go v1.0.3 => ../seata-go
@@ -71,6 +73,8 @@ require (
7173
github.com/hashicorp/go-multierror v1.1.1 // indirect
7274
github.com/hashicorp/vault/sdk v0.6.0 // indirect
7375
github.com/jinzhu/copier v0.3.5 // indirect
76+
github.com/jinzhu/inflection v1.0.0 // indirect
77+
github.com/jinzhu/now v1.1.5 // indirect
7478
github.com/jmespath/go-jmespath v0.4.0 // indirect
7579
github.com/json-iterator/go v1.1.12 // indirect
7680
github.com/k0kubun/pp v3.0.1+incompatible // indirect

go.sum

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4
285285
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
286286
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
287287
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
288-
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
289288
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
289+
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
290+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
290291
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
291292
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
292293
github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
@@ -500,6 +501,11 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
500501
github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
501502
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
502503
github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
504+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
505+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
506+
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
507+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
508+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
503509
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
504510
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
505511
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
@@ -1417,6 +1423,11 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
14171423
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14181424
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
14191425
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1426+
gorm.io/driver/mysql v1.4.5 h1:u1lytId4+o9dDaNcPCFzNv7h6wvmc92UjNk3z8enSBU=
1427+
gorm.io/driver/mysql v1.4.5/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc=
1428+
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
1429+
gorm.io/gorm v1.24.3 h1:WL2ifUmzR/SLp85CSURAfybcHnGZ+yLSGSxgYXlFBHg=
1430+
gorm.io/gorm v1.24.3/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
14201431
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
14211432
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
14221433
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

0 commit comments

Comments
 (0)