github.com/dolthub/go-mysql-server@v0.18.0/sql/mysql_db/replica_source_info.go (about) 1 // Copyright 2023 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mysql_db 16 17 import ( 18 "encoding/json" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/in_mem_table" 22 ) 23 24 // ReplicaSourceInfo represents the binlog replication metadata persisted 25 // in the mysql database. 26 // For more details, see: https://dev.mysql.com/doc/refman/8.0/en/replica-logs-status.html 27 type ReplicaSourceInfo struct { 28 Host string 29 User string 30 Password string 31 Port uint16 32 Uuid string 33 ConnectRetryInterval uint32 34 ConnectRetryCount uint64 35 } 36 37 func ReplicaSourceInfoToRow(ctx *sql.Context, v *ReplicaSourceInfo) (sql.Row, error) { 38 row := make(sql.Row, len(replicaSourceInfoTblSchema)) 39 var err error 40 for i, col := range replicaSourceInfoTblSchema { 41 row[i], err = col.Default.Eval(ctx, nil) 42 if err != nil { 43 panic(err) // Should never happen, schema is static 44 } 45 } 46 //TODO: once the remaining fields are added, fill those in as well 47 if v.Host != "" { 48 row[replicaSourceInfoTblColIndex_Host] = v.Host 49 } 50 if v.User != "" { 51 row[replicaSourceInfoTblColIndex_User_name] = v.User 52 } 53 if v.Uuid != "" { 54 row[replicaSourceInfoTblColIndex_Uuid] = v.Uuid 55 } 56 row[replicaSourceInfoTblColIndex_User_password] = v.Password 57 row[replicaSourceInfoTblColIndex_Port] = v.Port 58 row[replicaSourceInfoTblColIndex_Connect_retry] = v.ConnectRetryInterval 59 row[replicaSourceInfoTblColIndex_Retry_count] = v.ConnectRetryCount 60 61 return row, nil 62 } 63 64 func ReplicaSourceInfoFromRow(ctx *sql.Context, row sql.Row) (*ReplicaSourceInfo, error) { 65 if err := replicaSourceInfoTblSchema.CheckRow(row); err != nil { 66 return nil, err 67 } 68 69 return &ReplicaSourceInfo{ 70 Host: row[replicaSourceInfoTblColIndex_Host].(string), 71 User: row[replicaSourceInfoTblColIndex_User_name].(string), 72 Password: row[replicaSourceInfoTblColIndex_User_password].(string), 73 Port: row[replicaSourceInfoTblColIndex_Port].(uint16), 74 Uuid: row[replicaSourceInfoTblColIndex_Uuid].(string), 75 ConnectRetryInterval: row[replicaSourceInfoTblColIndex_Connect_retry].(uint32), 76 ConnectRetryCount: row[replicaSourceInfoTblColIndex_Retry_count].(uint64), 77 }, nil 78 } 79 80 func ReplicaSourceInfoEquals(left, right *ReplicaSourceInfo) bool { 81 return left.User == right.User && 82 left.Host == right.Host && 83 left.Port == right.Port && 84 left.Password == right.Password && 85 left.Uuid == right.Uuid && 86 left.ConnectRetryInterval == right.ConnectRetryInterval && 87 left.ConnectRetryCount == right.ConnectRetryCount 88 } 89 90 var ReplicaSourceInfoOps = in_mem_table.ValueOps[*ReplicaSourceInfo]{ 91 ToRow: ReplicaSourceInfoToRow, 92 FromRow: ReplicaSourceInfoFromRow, 93 UpdateWithRow: func(ctx *sql.Context, row sql.Row, e *ReplicaSourceInfo) (*ReplicaSourceInfo, error) { 94 return ReplicaSourceInfoFromRow(ctx, row) 95 }, 96 } 97 98 // NewReplicaSourceInfo constructs a new ReplicaSourceInfo instance, with defaults applied. 99 func NewReplicaSourceInfo() *ReplicaSourceInfo { 100 return &ReplicaSourceInfo{ 101 Port: 3306, 102 ConnectRetryInterval: 60, 103 ConnectRetryCount: 86400, 104 } 105 } 106 107 // FromJson implements the interface in_mem_table.Entry. 108 func (r *ReplicaSourceInfo) FromJson(_ *sql.Context, jsonStr string) (*ReplicaSourceInfo, error) { 109 newInstance := &ReplicaSourceInfo{} 110 if err := json.Unmarshal([]byte(jsonStr), newInstance); err != nil { 111 return nil, err 112 } 113 return newInstance, nil 114 } 115 116 // ToJson implements the interface in_mem_table.Entry. 117 func (r *ReplicaSourceInfo) ToJson(_ *sql.Context) (string, error) { 118 jsonData, err := json.Marshal(*r) 119 if err != nil { 120 return "", err 121 } 122 return string(jsonData), nil 123 }