github.com/dolthub/go-mysql-server@v0.18.0/sql/mysql_db/role_edge.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  	"encoding/json"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/in_mem_table"
    24  )
    25  
    26  // RoleEdge represents a role to user mapping from the roles_edges Grant Table.
    27  type RoleEdge struct {
    28  	FromHost        string
    29  	FromUser        string
    30  	ToHost          string
    31  	ToUser          string
    32  	WithAdminOption bool
    33  }
    34  
    35  func RoleEdgeToRow(ctx *sql.Context, r *RoleEdge) (sql.Row, error) {
    36  	row := make(sql.Row, len(roleEdgesTblSchema))
    37  	row[roleEdgesTblColIndex_FROM_HOST] = r.FromHost
    38  	row[roleEdgesTblColIndex_FROM_USER] = r.FromUser
    39  	row[roleEdgesTblColIndex_TO_HOST] = r.ToHost
    40  	row[roleEdgesTblColIndex_TO_USER] = r.ToUser
    41  	if r.WithAdminOption {
    42  		row[roleEdgesTblColIndex_WITH_ADMIN_OPTION] = uint16(2)
    43  	} else {
    44  		row[roleEdgesTblColIndex_WITH_ADMIN_OPTION] = uint16(1)
    45  	}
    46  	return row, nil
    47  }
    48  
    49  func RoleEdgeFromRow(ctx *sql.Context, row sql.Row) (*RoleEdge, error) {
    50  	if err := roleEdgesTblSchema.CheckRow(row); err != nil {
    51  		return nil, err
    52  	}
    53  	return &RoleEdge{
    54  		FromHost:        row[roleEdgesTblColIndex_FROM_HOST].(string),
    55  		FromUser:        row[roleEdgesTblColIndex_FROM_USER].(string),
    56  		ToHost:          row[roleEdgesTblColIndex_TO_HOST].(string),
    57  		ToUser:          row[roleEdgesTblColIndex_TO_USER].(string),
    58  		WithAdminOption: row[roleEdgesTblColIndex_WITH_ADMIN_OPTION].(uint16) == 2,
    59  	}, nil
    60  }
    61  
    62  func RoleEdgeEquals(left, right *RoleEdge) bool {
    63  	return *left == *right
    64  }
    65  
    66  var RoleEdgeOps = in_mem_table.ValueOps[*RoleEdge]{
    67  	ToRow:   RoleEdgeToRow,
    68  	FromRow: RoleEdgeFromRow,
    69  	UpdateWithRow: func(ctx *sql.Context, row sql.Row, e *RoleEdge) (*RoleEdge, error) {
    70  		return RoleEdgeFromRow(ctx, row)
    71  	},
    72  }
    73  
    74  // FromJson implements the interface in_mem_table.Entry.
    75  func (r *RoleEdge) FromJson(ctx *sql.Context, jsonStr string) (*RoleEdge, error) {
    76  	newRoleEdge := &RoleEdge{}
    77  	if err := json.Unmarshal([]byte(jsonStr), newRoleEdge); err != nil {
    78  		return nil, err
    79  	}
    80  	return newRoleEdge, nil
    81  }
    82  
    83  // ToJson implements the interface in_mem_table.Entry.
    84  func (r *RoleEdge) ToJson(ctx *sql.Context) (string, error) {
    85  	jsonData, err := json.Marshal(*r)
    86  	if err != nil {
    87  		return "", err
    88  	}
    89  	return string(jsonData), nil
    90  }
    91  
    92  // ToString returns the "TO" user as a formatted string using the quotes given. Using the default root
    93  // account with the backtick as the quote, root@localhost would become `root`@`localhost`. Different quotes are used
    94  // in different places in MySQL. In addition, if the quote is used in a section as part of the name, it is escaped by
    95  // doubling the quote (which also mimics MySQL behavior).
    96  func (r *RoleEdge) ToString(quote string) string {
    97  	return r.stringWithQuote(r.ToUser, r.ToHost, quote)
    98  }
    99  
   100  // FromString returns the "FROM" user as a formatted string using the quotes given. Using the default root
   101  // account with the backtick as the quote, root@localhost would become `root`@`localhost`. Different quotes are used
   102  // in different places in MySQL. In addition, if the quote is used in a section as part of the name, it is escaped by
   103  // doubling the quote (which also mimics MySQL behavior).
   104  func (r *RoleEdge) FromString(quote string) string {
   105  	return r.stringWithQuote(r.FromUser, r.FromHost, quote)
   106  }
   107  
   108  // stringWithQuote returns the given user as a formatted string using the quotes given. Using the default root
   109  // account with the backtick as the quote, root@localhost would become `root`@`localhost`. Different quotes are used
   110  // in different places in MySQL. In addition, if the quote is used in a section as part of the name, it is escaped by
   111  // doubling the quote (which also mimics MySQL behavior).
   112  func (r *RoleEdge) stringWithQuote(name string, host string, quote string) string {
   113  	replacement := quote + quote
   114  	name = strings.ReplaceAll(name, quote, replacement)
   115  	host = strings.ReplaceAll(host, quote, replacement)
   116  	return fmt.Sprintf("%s%s%s@%s%s%s", quote, name, quote, quote, host, quote)
   117  }