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