github.com/dolthub/go-mysql-server@v0.18.0/sql/mysql_db/mysql_db_load.go (about) 1 // Copyright 2022 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 "time" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/mysql_db/serial" 22 ) 23 24 // loadPrivilegeTypes is a helper method that loads privilege types given the length and loading function 25 // and returns them as a set 26 func loadPrivilegeTypes(n int, f func(j int) int32) map[sql.PrivilegeType]struct{} { 27 privs := make(map[sql.PrivilegeType]struct{}, n) 28 for i := 0; i < n; i++ { 29 privs[sql.PrivilegeType(f(i))] = struct{}{} 30 } 31 return privs 32 } 33 34 func loadColumn(serialColumn *serial.PrivilegeSetColumn) *PrivilegeSetColumn { 35 return &PrivilegeSetColumn{ 36 name: string(serialColumn.Name()), 37 privs: loadPrivilegeTypes(serialColumn.PrivsLength(), serialColumn.Privs), 38 } 39 } 40 41 func loadTable(serialTable *serial.PrivilegeSetTable) *PrivilegeSetTable { 42 columns := make(map[string]PrivilegeSetColumn, serialTable.ColumnsLength()) 43 for i := 0; i < serialTable.ColumnsLength(); i++ { 44 serialColumn := new(serial.PrivilegeSetColumn) 45 if !serialTable.Columns(serialColumn, i) { 46 continue 47 } 48 column := loadColumn(serialColumn) 49 columns[column.Name()] = *column 50 } 51 52 return &PrivilegeSetTable{ 53 name: string(serialTable.Name()), 54 privs: loadPrivilegeTypes(serialTable.PrivsLength(), serialTable.Privs), 55 columns: columns, 56 } 57 } 58 59 func loadRoutine(serialRoutine *serial.PrivilegeSetRoutine) *PrivilegeSetRoutine { 60 return &PrivilegeSetRoutine{ 61 name: string(serialRoutine.Name()), 62 privs: loadPrivilegeTypes(serialRoutine.PrivsLength(), serialRoutine.Privs), 63 isProc: serialRoutine.IsProc(), 64 } 65 } 66 67 func loadDatabase(serialDatabase *serial.PrivilegeSetDatabase) *PrivilegeSetDatabase { 68 tables := make(map[string]PrivilegeSetTable, serialDatabase.TablesLength()) 69 for i := 0; i < serialDatabase.TablesLength(); i++ { 70 serialTable := new(serial.PrivilegeSetTable) 71 if !serialDatabase.Tables(serialTable, i) { 72 continue 73 } 74 table := loadTable(serialTable) 75 tables[table.Name()] = *table 76 } 77 78 routines := make(map[routineKey]PrivilegeSetRoutine, serialDatabase.RoutinesLength()) 79 for i := 0; i < serialDatabase.RoutinesLength(); i++ { 80 serialRoutine := new(serial.PrivilegeSetRoutine) 81 if !serialDatabase.Routines(serialRoutine, i) { 82 continue 83 } 84 routine := loadRoutine(serialRoutine) 85 key := routineKey{routine.RoutineName(), routine.isProc} 86 routines[key] = *routine 87 } 88 89 return &PrivilegeSetDatabase{ 90 name: string(serialDatabase.Name()), 91 privs: loadPrivilegeTypes(serialDatabase.PrivsLength(), serialDatabase.Privs), 92 tables: tables, 93 routines: routines, 94 } 95 } 96 97 func loadPrivilegeSet(serialPrivilegeSet *serial.PrivilegeSet) *PrivilegeSet { 98 databases := make(map[string]PrivilegeSetDatabase, serialPrivilegeSet.DatabasesLength()) 99 for i := 0; i < serialPrivilegeSet.DatabasesLength(); i++ { 100 serialDatabase := new(serial.PrivilegeSetDatabase) 101 if !serialPrivilegeSet.Databases(serialDatabase, i) { 102 continue 103 } 104 database := loadDatabase(serialDatabase) 105 databases[database.Name()] = *database 106 } 107 108 globalDynamic := make(map[string]bool) 109 for i := 0; i < serialPrivilegeSet.GlobalDynamicLength(); i++ { 110 globalDynamic[string(serialPrivilegeSet.GlobalDynamic(i))] = serialPrivilegeSet.GlobalDynamicWgo(i) 111 } 112 return &PrivilegeSet{ 113 globalStatic: loadPrivilegeTypes(serialPrivilegeSet.GlobalStaticLength(), serialPrivilegeSet.GlobalStatic), 114 globalDynamic: globalDynamic, 115 databases: databases, 116 } 117 } 118 119 func LoadUser(serialUser *serial.User) *User { 120 serialPrivilegeSet := new(serial.PrivilegeSet) 121 serialUser.PrivilegeSet(serialPrivilegeSet) 122 privilegeSet := loadPrivilegeSet(serialPrivilegeSet) 123 attributesBuf := serialUser.Attributes() 124 attributesVal := string(attributesBuf) 125 var attributes *string 126 if attributesBuf != nil { 127 attributes = &attributesVal 128 } 129 130 return &User{ 131 User: string(serialUser.User()), 132 Host: string(serialUser.Host()), 133 PrivilegeSet: *privilegeSet, 134 Plugin: string(serialUser.Plugin()), 135 Password: string(serialUser.Password()), 136 PasswordLastChanged: time.Unix(serialUser.PasswordLastChanged(), 0), 137 Locked: serialUser.Locked(), 138 Attributes: attributes, 139 Identity: string(serialUser.Identity()), 140 } 141 } 142 143 func LoadRoleEdge(serialRoleEdge *serial.RoleEdge) *RoleEdge { 144 return &RoleEdge{ 145 FromHost: string(serialRoleEdge.FromHost()), 146 FromUser: string(serialRoleEdge.FromUser()), 147 ToHost: string(serialRoleEdge.ToHost()), 148 ToUser: string(serialRoleEdge.ToUser()), 149 } 150 } 151 152 func LoadReplicaSourceInfo(serialReplicaSourceInfo *serial.ReplicaSourceInfo) *ReplicaSourceInfo { 153 return &ReplicaSourceInfo{ 154 Host: string(serialReplicaSourceInfo.Host()), 155 User: string(serialReplicaSourceInfo.User()), 156 Password: string(serialReplicaSourceInfo.Password()), 157 Port: serialReplicaSourceInfo.Port(), 158 Uuid: string(serialReplicaSourceInfo.Uuid()), 159 ConnectRetryInterval: serialReplicaSourceInfo.ConnectRetryInterval(), 160 ConnectRetryCount: serialReplicaSourceInfo.ConnectRetryCount(), 161 } 162 }