4848
4949type EnumType struct {
5050 collation sql.CollationID
51- hashedValToIndex map [uint64 ]int
51+ hashedValToIdx map [uint64 ]int
5252 valToIdx map [string ]int
53- indexToVal []string
53+ idxToVal []string
5454 maxResponseByteLength uint32
5555}
5656
@@ -98,8 +98,8 @@ func CreateEnumType(values []string, collation sql.CollationID) (sql.EnumType, e
9898 }
9999 return EnumType {
100100 collation : collation ,
101- hashedValToIndex : hashedValToIndex ,
102- indexToVal : values ,
101+ hashedValToIdx : hashedValToIndex ,
102+ idxToVal : values ,
103103 valToIdx : valToIdx ,
104104 maxResponseByteLength : maxResponseByteLength ,
105105 }, nil
@@ -217,9 +217,9 @@ func (t EnumType) MustConvert(v interface{}) interface{} {
217217
218218// Equals implements the Type interface.
219219func (t EnumType ) Equals (otherType sql.Type ) bool {
220- if ot , ok := otherType .(EnumType ); ok && t .collation .Equals (ot .collation ) && len (t .indexToVal ) == len (ot .indexToVal ) {
221- for i , val := range t .indexToVal {
222- if ot .indexToVal [i ] != val {
220+ if ot , ok := otherType .(EnumType ); ok && t .collation .Equals (ot .collation ) && len (t .idxToVal ) == len (ot .idxToVal ) {
221+ for i , val := range t .idxToVal {
222+ if ot .idxToVal [i ] != val {
223223 return false
224224 }
225225 }
@@ -289,16 +289,19 @@ func (t EnumType) Zero() interface{} {
289289}
290290
291291// At implements EnumType interface.
292- func (t EnumType ) At (index int ) (string , bool ) {
293- // The elements listed in the column specification are assigned index numbers, beginning with 1.
294- index -= 1
295- if index <= - 1 {
296- // for index zero, the value is empty. It's used for insert ignore.
292+ func (t EnumType ) At (idx int ) (string , bool ) {
293+ // for index zero, the value is empty. It's used for insert ignore.
294+ if idx < 0 {
295+ return "" , false
296+ }
297+ if idx == 0 {
297298 return "" , true
298- } else if index >= len (t .indexToVal ) {
299+ }
300+ if idx > len (t .idxToVal ) {
299301 return "" , false
300302 }
301- return t .indexToVal [index ], true
303+ // The elements listed in the column specification are assigned index numbers, beginning with 1.
304+ return t .idxToVal [idx - 1 ], true
302305}
303306
304307// CharacterSet implements EnumType interface.
@@ -318,7 +321,7 @@ func (t EnumType) IndexOf(v string) int {
318321 }
319322 hashedVal , err := t .collation .HashToUint (v )
320323 if err == nil {
321- if index , ok := t .hashedValToIndex [hashedVal ]; ok {
324+ if index , ok := t .hashedValToIdx [hashedVal ]; ok {
322325 return index
323326 }
324327 }
@@ -334,24 +337,24 @@ func (t EnumType) IndexOf(v string) int {
334337
335338// NumberOfElements implements EnumType interface.
336339func (t EnumType ) NumberOfElements () uint16 {
337- return uint16 (len (t .indexToVal ))
340+ return uint16 (len (t .idxToVal ))
338341}
339342
340343// Values implements EnumType interface.
341344func (t EnumType ) Values () []string {
342- vals := make ([]string , len (t .indexToVal ))
343- copy (vals , t .indexToVal )
345+ vals := make ([]string , len (t .idxToVal ))
346+ copy (vals , t .idxToVal )
344347 return vals
345348}
346349
347350// WithNewCollation implements sql.TypeWithCollation interface.
348351func (t EnumType ) WithNewCollation (collation sql.CollationID ) (sql.Type , error ) {
349- return CreateEnumType (t .indexToVal , collation )
352+ return CreateEnumType (t .idxToVal , collation )
350353}
351354
352355// StringWithTableCollation implements sql.TypeWithCollation interface.
353356func (t EnumType ) StringWithTableCollation (tableCollation sql.CollationID ) string {
354- s := fmt .Sprintf ("enum('%v')" , strings .Join (t .indexToVal , `','` ))
357+ s := fmt .Sprintf ("enum('%v')" , strings .Join (t .idxToVal , `','` ))
355358 if t .CharacterSet () != tableCollation .CharacterSet () {
356359 s += " CHARACTER SET " + t .CharacterSet ().String ()
357360 }
0 commit comments