// Code generated by yo. DO NOT EDIT.

// Package models contains the types.
package models

import (
	"context"
	"fmt"

	"cloud.google.com/go/spanner"
	"google.golang.org/grpc/codes"
)

// Inflection represents a row from 'Inflectionzz'.
type Inflection struct {
	X string `spanner:"X" json:"X"` // X
	Y string `spanner:"Y" json:"Y"` // Y
}

func InflectionPrimaryKeys() []string {
	return []string{
		"X",
	}
}

func InflectionColumns() []string {
	return []string{
		"X",
		"Y",
	}
}

func InflectionWritableColumns() []string {
	return []string{
		"X",
		"Y",
	}
}

func (i *Inflection) columnsToPtrs(cols []string) ([]interface{}, error) {
	ret := make([]interface{}, 0, len(cols))
	for _, col := range cols {
		switch col {
		case "X":
			ret = append(ret, yoDecode(&i.X))
		case "Y":
			ret = append(ret, yoDecode(&i.Y))
		default:
			return nil, fmt.Errorf("unknown column: %s", col)
		}
	}
	return ret, nil
}

func (i *Inflection) columnsToValues(cols []string) ([]interface{}, error) {
	ret := make([]interface{}, 0, len(cols))
	for _, col := range cols {
		switch col {
		case "X":
			ret = append(ret, yoEncode(i.X))
		case "Y":
			ret = append(ret, yoEncode(i.Y))
		default:
			return nil, fmt.Errorf("unknown column: %s", col)
		}
	}

	return ret, nil
}

// newInflection_Decoder returns a decoder which reads a row from *spanner.Row
// into Inflection. The decoder is not goroutine-safe. Don't use it concurrently.
func newInflection_Decoder(cols []string) func(*spanner.Row) (*Inflection, error) {
	return func(row *spanner.Row) (*Inflection, error) {
		var i Inflection
		ptrs, err := i.columnsToPtrs(cols)
		if err != nil {
			return nil, err
		}

		if err := row.Columns(ptrs...); err != nil {
			return nil, err
		}

		return &i, nil
	}
}

// Insert returns a Mutation to insert a row into a table. If the row already
// exists, the write or transaction fails.
func (i *Inflection) Insert(ctx context.Context) *spanner.Mutation {
	values, _ := i.columnsToValues(InflectionWritableColumns())
	return spanner.Insert("Inflectionzz", InflectionWritableColumns(), values)
}

// Update returns a Mutation to update a row in a table. If the row does not
// already exist, the write or transaction fails.
func (i *Inflection) Update(ctx context.Context) *spanner.Mutation {
	values, _ := i.columnsToValues(InflectionWritableColumns())
	return spanner.Update("Inflectionzz", InflectionWritableColumns(), values)
}

// InsertOrUpdate returns a Mutation to insert a row into a table. If the row
// already exists, it updates it instead. Any column values not explicitly
// written are preserved.
func (i *Inflection) InsertOrUpdate(ctx context.Context) *spanner.Mutation {
	values, _ := i.columnsToValues(InflectionWritableColumns())
	return spanner.InsertOrUpdate("Inflectionzz", InflectionWritableColumns(), values)
}

// Replace returns a Mutation to insert a row into a table, deleting any
// existing row. Unlike InsertOrUpdate, this means any values not explicitly
// written become NULL.
func (i *Inflection) Replace(ctx context.Context) *spanner.Mutation {
	values, _ := i.columnsToValues(InflectionWritableColumns())
	return spanner.Replace("Inflectionzz", InflectionWritableColumns(), values)
}

// UpdateColumns returns a Mutation to update specified columns of a row in a table.
func (i *Inflection) UpdateColumns(ctx context.Context, cols ...string) (*spanner.Mutation, error) {
	// add primary keys to columns to update by primary keys
	colsWithPKeys := append(cols, InflectionPrimaryKeys()...)

	values, err := i.columnsToValues(colsWithPKeys)
	if err != nil {
		return nil, newErrorWithCode(codes.InvalidArgument, "Inflection.UpdateColumns", "Inflectionzz", err)
	}

	return spanner.Update("Inflectionzz", colsWithPKeys, values), nil
}

// FindInflection gets a Inflection by primary key
func FindInflection(ctx context.Context, db YODB, x string) (*Inflection, error) {
	_key := spanner.Key{yoEncode(x)}
	row, err := db.ReadRow(ctx, "Inflectionzz", _key, InflectionColumns())
	if err != nil {
		return nil, newError("FindInflection", "Inflectionzz", err)
	}

	decoder := newInflection_Decoder(InflectionColumns())
	i, err := decoder(row)
	if err != nil {
		return nil, newErrorWithCode(codes.Internal, "FindInflection", "Inflectionzz", err)
	}

	return i, nil
}

// ReadInflection retrieves multiples rows from Inflection by KeySet as a slice.
func ReadInflection(ctx context.Context, db YODB, keys spanner.KeySet) ([]*Inflection, error) {
	var res []*Inflection

	decoder := newInflection_Decoder(InflectionColumns())

	rows := db.Read(ctx, "Inflectionzz", keys, InflectionColumns())
	err := rows.Do(func(row *spanner.Row) error {
		i, err := decoder(row)
		if err != nil {
			return err
		}
		res = append(res, i)

		return nil
	})
	if err != nil {
		return nil, newErrorWithCode(codes.Internal, "ReadInflection", "Inflectionzz", err)
	}

	return res, nil
}

// Delete deletes the Inflection from the database.
func (i *Inflection) Delete(ctx context.Context) *spanner.Mutation {
	values, _ := i.columnsToValues(InflectionPrimaryKeys())
	return spanner.Delete("Inflectionzz", spanner.Key(values))
}
