github.com/dolthub/go-mysql-server@v0.18.0/sql/mysql_db/role_edges_table.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  	"fmt"
    19  	"sync"
    20  
    21  	"github.com/dolthub/vitess/go/sqltypes"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  	"github.com/dolthub/go-mysql-server/sql/expression"
    25  	"github.com/dolthub/go-mysql-server/sql/in_mem_table"
    26  	"github.com/dolthub/go-mysql-server/sql/types"
    27  )
    28  
    29  const roleEdgesTblName = "role_edges"
    30  
    31  var (
    32  	errRoleEdgePkEntry = fmt.Errorf("the primary key for the `role_edges` table was given an unknown entry")
    33  	errRoleEdgePkRow   = fmt.Errorf("the primary key for the `role_edges` table was given a row belonging to an unknown schema")
    34  	errRoleEdgeFkEntry = fmt.Errorf("the `from` secondary key for the `role_edges` table was given an unknown entry")
    35  	errRoleEdgeFkRow   = fmt.Errorf("the `from` secondary key for the `role_edges` table was given a row belonging to an unknown schema")
    36  	errRoleEdgeTkEntry = fmt.Errorf("the `to` secondary key for the `role_edges` table was given an unknown entry")
    37  	errRoleEdgeTkRow   = fmt.Errorf("the `to` secondary key for the `role_edges` table was given a row belonging to an unknown schema")
    38  
    39  	roleEdgesTblSchema sql.Schema
    40  )
    41  
    42  // RoleEdgesPrimaryKey is a key that represents the primary key for the "role_edges" Grant Table.
    43  type RoleEdgesPrimaryKey struct {
    44  	FromHost string
    45  	FromUser string
    46  	ToHost   string
    47  	ToUser   string
    48  }
    49  
    50  // RoleEdgesFromKey is a secondary key that represents the "from" columns on the "role_edges" Grant Table.
    51  type RoleEdgesFromKey struct {
    52  	FromHost string
    53  	FromUser string
    54  }
    55  
    56  // RoleEdgesToKey is a secondary key that represents the "to" columns on the "role_edges" Grant Table.
    57  type RoleEdgesToKey struct {
    58  	ToHost string
    59  	ToUser string
    60  }
    61  
    62  type RoleEdgePrimaryKeyer struct{}
    63  type RoleEdgeToKeyer struct{}
    64  type RoleEdgeFromKeyer struct{}
    65  
    66  var _ in_mem_table.Keyer[*RoleEdge] = RoleEdgePrimaryKeyer{}
    67  var _ in_mem_table.Keyer[*RoleEdge] = RoleEdgeToKeyer{}
    68  var _ in_mem_table.Keyer[*RoleEdge] = RoleEdgeFromKeyer{}
    69  
    70  func (RoleEdgePrimaryKeyer) GetKey(r *RoleEdge) any {
    71  	return RoleEdgesPrimaryKey{
    72  		FromHost: r.FromHost,
    73  		FromUser: r.FromUser,
    74  		ToHost:   r.ToHost,
    75  		ToUser:   r.ToUser,
    76  	}
    77  }
    78  
    79  func (RoleEdgeToKeyer) GetKey(r *RoleEdge) any {
    80  	return RoleEdgesToKey{
    81  		ToHost: r.ToHost,
    82  		ToUser: r.ToUser,
    83  	}
    84  }
    85  
    86  func (RoleEdgeFromKeyer) GetKey(r *RoleEdge) any {
    87  	return RoleEdgesFromKey{
    88  		FromHost: r.FromHost,
    89  		FromUser: r.FromUser,
    90  	}
    91  }
    92  
    93  func NewRoleEdgesIndexedSetTable(lock, rlock sync.Locker) *in_mem_table.IndexedSetTable[*RoleEdge] {
    94  	set := in_mem_table.NewIndexedSet[*RoleEdge](
    95  		RoleEdgeEquals,
    96  		[]in_mem_table.Keyer[*RoleEdge]{
    97  			RoleEdgePrimaryKeyer{},
    98  			RoleEdgeToKeyer{},
    99  			RoleEdgeFromKeyer{},
   100  		},
   101  	)
   102  	table := in_mem_table.NewIndexedSetTable[*RoleEdge](
   103  		roleEdgesTblName,
   104  		roleEdgesTblSchema,
   105  		sql.Collation_utf8mb3_bin,
   106  		set,
   107  		RoleEdgeOps,
   108  		lock,
   109  		rlock,
   110  	)
   111  	return table
   112  }
   113  
   114  // init creates the schema for the "role_edges" Grant Table.
   115  func init() {
   116  	// Types
   117  	char32_utf8_bin := types.MustCreateString(sqltypes.Char, 32, sql.Collation_utf8_bin)
   118  	char255_ascii_general_ci := types.MustCreateString(sqltypes.Char, 255, sql.Collation_ascii_general_ci)
   119  	enum_N_Y_utf8_general_ci := types.MustCreateEnumType([]string{"N", "Y"}, sql.Collation_utf8_general_ci)
   120  
   121  	// Column Templates
   122  	char32_utf8_bin_not_null_default_empty := &sql.Column{
   123  		Type:     char32_utf8_bin,
   124  		Default:  mustDefault(expression.NewLiteral("", char32_utf8_bin), char32_utf8_bin, true, false),
   125  		Nullable: false,
   126  	}
   127  	char255_ascii_general_ci_not_null_default_empty := &sql.Column{
   128  		Type:     char255_ascii_general_ci,
   129  		Default:  mustDefault(expression.NewLiteral("", char255_ascii_general_ci), char255_ascii_general_ci, true, false),
   130  		Nullable: false,
   131  	}
   132  	enum_N_Y_utf8_general_ci_not_null_default_N := &sql.Column{
   133  		Type:     enum_N_Y_utf8_general_ci,
   134  		Default:  mustDefault(expression.NewLiteral("N", enum_N_Y_utf8_general_ci), enum_N_Y_utf8_general_ci, true, false),
   135  		Nullable: false,
   136  	}
   137  
   138  	roleEdgesTblSchema = sql.Schema{
   139  		columnTemplate("FROM_HOST", roleEdgesTblName, true, char255_ascii_general_ci_not_null_default_empty),
   140  		columnTemplate("FROM_USER", roleEdgesTblName, true, char32_utf8_bin_not_null_default_empty),
   141  		columnTemplate("TO_HOST", roleEdgesTblName, true, char255_ascii_general_ci_not_null_default_empty),
   142  		columnTemplate("TO_USER", roleEdgesTblName, true, char32_utf8_bin_not_null_default_empty),
   143  		columnTemplate("WITH_ADMIN_OPTION", roleEdgesTblName, false, enum_N_Y_utf8_general_ci_not_null_default_N),
   144  	}
   145  }
   146  
   147  // These represent the column indexes of the "role_edges" Grant Table.
   148  const (
   149  	roleEdgesTblColIndex_FROM_HOST int = iota
   150  	roleEdgesTblColIndex_FROM_USER
   151  	roleEdgesTblColIndex_TO_HOST
   152  	roleEdgesTblColIndex_TO_USER
   153  	roleEdgesTblColIndex_WITH_ADMIN_OPTION
   154  )