You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sql_test.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. package sql
  18. import (
  19. "context"
  20. "database/sql"
  21. "fmt"
  22. "sync"
  23. "testing"
  24. _ "github.com/go-sql-driver/mysql"
  25. "github.com/seata/seata-go/pkg/client"
  26. "github.com/seata/seata-go/pkg/util/log"
  27. )
  28. var db *sql.DB
  29. func Test_SQLOpen(t *testing.T) {
  30. client.Init()
  31. t.SkipNow()
  32. log.Info("begin test")
  33. var err error
  34. db, err = sql.Open("seata-at-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true")
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. defer db.Close()
  39. sqlStmt := `
  40. create table if not exists foo (id integer not null primary key, name text);
  41. delete from foo;
  42. `
  43. _, err = db.Exec(sqlStmt)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. wait := sync.WaitGroup{}
  48. txInvoker := func(prefix string, offset, total int) {
  49. defer wait.Done()
  50. tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer stmt.Close()
  59. for i := 0; i < total; i++ {
  60. _, err = stmt.Exec(i+offset, fmt.Sprintf("%s-%03d", prefix, i))
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. }
  65. err = tx.Commit()
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. }
  70. wait.Add(2)
  71. t.Parallel()
  72. t.Run("", func(t *testing.T) {
  73. txInvoker("seata-go-at-1", 0, 10)
  74. })
  75. t.Run("", func(t *testing.T) {
  76. txInvoker("seata-go-at-2", 20, 10)
  77. })
  78. wait.Wait()
  79. queryMultiRow()
  80. }
  81. func queryMultiRow() {
  82. sqlStr := "select id, name from foo where id > ?"
  83. rows, err := db.Query(sqlStr, 0)
  84. if err != nil {
  85. fmt.Printf("query failed, err:%v\n", err)
  86. return
  87. }
  88. defer rows.Close()
  89. for rows.Next() {
  90. var u user
  91. err := rows.Scan(&u.id, &u.name)
  92. if err != nil {
  93. fmt.Printf("scan failed, err:%v\n", err)
  94. return
  95. }
  96. fmt.Printf("id:%d username:%s password:%s\n", u.id, u.name, u.name)
  97. }
  98. }
  99. type user struct {
  100. id int
  101. name string
  102. }