go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/mysql/plugin.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package mysql 26 27 import ( 28 "github.com/jmoiron/sqlx" 29 30 "go.temporal.io/server/common/config" 31 "go.temporal.io/server/common/persistence/sql" 32 "go.temporal.io/server/common/persistence/sql/sqlplugin" 33 "go.temporal.io/server/common/persistence/sql/sqlplugin/mysql/session" 34 "go.temporal.io/server/common/resolver" 35 ) 36 37 const ( 38 // PluginName is the name of the plugin 39 PluginName = "mysql" 40 ) 41 42 type plugin struct{} 43 44 var _ sqlplugin.Plugin = (*plugin)(nil) 45 46 func init() { 47 sql.RegisterPlugin(PluginName, &plugin{}) 48 } 49 50 // CreateDB initialize the db object 51 func (p *plugin) CreateDB( 52 dbKind sqlplugin.DbKind, 53 cfg *config.SQL, 54 r resolver.ServiceResolver, 55 ) (sqlplugin.DB, error) { 56 conn, err := p.createDBConnection(cfg, r) 57 if err != nil { 58 return nil, err 59 } 60 db := newDB(dbKind, cfg.DatabaseName, conn, nil) 61 return db, nil 62 } 63 64 // CreateAdminDB initialize the db object 65 func (p *plugin) CreateAdminDB( 66 dbKind sqlplugin.DbKind, 67 cfg *config.SQL, 68 r resolver.ServiceResolver, 69 ) (sqlplugin.AdminDB, error) { 70 conn, err := p.createDBConnection(cfg, r) 71 if err != nil { 72 return nil, err 73 } 74 db := newDB(dbKind, cfg.DatabaseName, conn, nil) 75 return db, nil 76 } 77 78 // CreateDBConnection creates a returns a reference to a logical connection to the 79 // underlying SQL database. The returned object is to tied to a single 80 // SQL database and the object can be used to perform CRUD operations on 81 // the tables in the database 82 func (p *plugin) createDBConnection( 83 cfg *config.SQL, 84 resolver resolver.ServiceResolver, 85 ) (*sqlx.DB, error) { 86 mysqlSession, err := session.NewSession(cfg, resolver) 87 if err != nil { 88 return nil, err 89 } 90 return mysqlSession.DB, nil 91 }