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.

db.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "database/sql"
  20. "github.com/seata/seata-go/pkg/datasource/sql/datasource"
  21. "github.com/seata/seata-go/pkg/datasource/sql/types"
  22. "github.com/seata/seata-go/pkg/datasource/sql/undo"
  23. "github.com/seata/seata-go/pkg/protocol/branch"
  24. )
  25. type dbOption func(db *DBResource)
  26. func withGroupID(id string) dbOption {
  27. return func(db *DBResource) {
  28. db.groupID = id
  29. }
  30. }
  31. func withResourceID(id string) dbOption {
  32. return func(db *DBResource) {
  33. db.resourceID = id
  34. }
  35. }
  36. func withTableMetaCache(c datasource.TableMetaCache) dbOption {
  37. return func(db *DBResource) {
  38. db.metaCache = c
  39. }
  40. }
  41. func withDBType(dt types.DBType) dbOption {
  42. return func(db *DBResource) {
  43. db.dbType = dt
  44. }
  45. }
  46. func withTarget(source *sql.DB) dbOption {
  47. return func(db *DBResource) {
  48. db.db = source
  49. }
  50. }
  51. func withDBName(dbName string) dbOption {
  52. return func(db *DBResource) {
  53. db.dbName = dbName
  54. }
  55. }
  56. func withConf(conf *seataServerConfig) dbOption {
  57. return func(db *DBResource) {
  58. db.conf = *conf
  59. }
  60. }
  61. func newResource(opts ...dbOption) (*DBResource, error) {
  62. db := new(DBResource)
  63. for i := range opts {
  64. opts[i](db)
  65. }
  66. return db, db.init()
  67. }
  68. // DBResource proxy sql.DB, enchance database/sql.DB to add distribute transaction ability
  69. type DBResource struct {
  70. // groupID
  71. groupID string
  72. // resourceID
  73. resourceID string
  74. // conf
  75. conf seataServerConfig
  76. // db
  77. db *sql.DB
  78. dbName string
  79. // dbType
  80. dbType types.DBType
  81. // undoLogMgr
  82. undoLogMgr undo.UndoLogManager
  83. // metaCache
  84. metaCache datasource.TableMetaCache
  85. }
  86. func (db *DBResource) init() error {
  87. return nil
  88. }
  89. // todo do not put meta data to rm
  90. //func (db *DBResource) init() error {
  91. // mgr := datasource.GetDataSourceManager(db.GetBranchType())
  92. // metaCache, err := mgr.CreateTableMetaCache(context.Background(), db.resourceID, db.dbType, db.db)
  93. // if err != nil {
  94. // return err
  95. // }
  96. //
  97. // db.metaCache = metaCache
  98. //
  99. // return nil
  100. //}
  101. func (db *DBResource) GetResourceGroupId() string {
  102. return db.groupID
  103. }
  104. func (db *DBResource) GetResourceId() string {
  105. return db.resourceID
  106. }
  107. func (db *DBResource) GetBranchType() branch.BranchType {
  108. return db.conf.BranchType
  109. }
  110. type SqlDBProxy struct {
  111. db *sql.DB
  112. dbName string
  113. }
  114. func (s *SqlDBProxy) GetDB() *sql.DB {
  115. return s.db
  116. }
  117. func (s *SqlDBProxy) GetDBName() string {
  118. return s.dbName
  119. }