// 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"
)

// FereignItem represents a row from 'FereignItems'.
type FereignItem struct {
	ID       int64 `spanner:"ID" json:"ID"`             // ID
	ItemID   int64 `spanner:"ItemID" json:"ItemID"`     // ItemID
	Category int64 `spanner:"Category" json:"Category"` // Category
}

func FereignItemPrimaryKeys() []string {
	return []string{
		"ID",
	}
}

func FereignItemColumns() []string {
	return []string{
		"ID",
		"ItemID",
		"Category",
	}
}

func FereignItemWritableColumns() []string {
	return []string{
		"ID",
		"ItemID",
		"Category",
	}
}

func (fi *FereignItem) columnsToPtrs(cols []string, customPtrs map[string]interface{}) ([]interface{}, error) {
	ret := make([]interface{}, 0, len(cols))
	for _, col := range cols {
		if val, ok := customPtrs[col]; ok {
			ret = append(ret, val)
			continue
		}

		switch col {
		case "ID":
			ret = append(ret, &fi.ID)
		case "ItemID":
			ret = append(ret, &fi.ItemID)
		case "Category":
			ret = append(ret, &fi.Category)
		default:
			return nil, fmt.Errorf("unknown column: %s", col)
		}
	}
	return ret, nil
}

func (fi *FereignItem) columnsToValues(cols []string) ([]interface{}, error) {
	ret := make([]interface{}, 0, len(cols))
	for _, col := range cols {
		switch col {
		case "ID":
			ret = append(ret, fi.ID)
		case "ItemID":
			ret = append(ret, fi.ItemID)
		case "Category":
			ret = append(ret, fi.Category)
		default:
			return nil, fmt.Errorf("unknown column: %s", col)
		}
	}

	return ret, nil
}

// newFereignItem_Decoder returns a decoder which reads a row from *spanner.Row
// into FereignItem. The decoder is not goroutine-safe. Don't use it concurrently.
func newFereignItem_Decoder(cols []string) func(*spanner.Row) (*FereignItem, error) {
	customPtrs := map[string]interface{}{}

	return func(row *spanner.Row) (*FereignItem, error) {
		var fi FereignItem
		ptrs, err := fi.columnsToPtrs(cols, customPtrs)
		if err != nil {
			return nil, err
		}

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

		return &fi, nil
	}
}

// Insert returns a Mutation to insert a row into a table. If the row already
// exists, the write or transaction fails.
func (fi *FereignItem) Insert(ctx context.Context) *spanner.Mutation {
	values, _ := fi.columnsToValues(FereignItemWritableColumns())
	return spanner.Insert("FereignItems", FereignItemWritableColumns(), 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 (fi *FereignItem) Update(ctx context.Context) *spanner.Mutation {
	values, _ := fi.columnsToValues(FereignItemWritableColumns())
	return spanner.Update("FereignItems", FereignItemWritableColumns(), 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 (fi *FereignItem) InsertOrUpdate(ctx context.Context) *spanner.Mutation {
	values, _ := fi.columnsToValues(FereignItemWritableColumns())
	return spanner.InsertOrUpdate("FereignItems", FereignItemWritableColumns(), values)
}

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

	values, err := fi.columnsToValues(colsWithPKeys)
	if err != nil {
		return nil, newErrorWithCode(codes.InvalidArgument, "FereignItem.UpdateColumns", "FereignItems", err)
	}

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

// FindFereignItem gets a FereignItem by primary key
func FindFereignItem(ctx context.Context, db YORODB, id int64) (*FereignItem, error) {
	key := spanner.Key{id}
	row, err := db.ReadRow(ctx, "FereignItems", key, FereignItemColumns())
	if err != nil {
		return nil, newError("FindFereignItem", "FereignItems", err)
	}

	decoder := newFereignItem_Decoder(FereignItemColumns())
	fi, err := decoder(row)
	if err != nil {
		return nil, newErrorWithCode(codes.Internal, "FindFereignItem", "FereignItems", err)
	}

	return fi, nil
}

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

	decoder := newFereignItem_Decoder(FereignItemColumns())

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

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

	return res, nil
}

// Delete deletes the FereignItem from the database.
func (fi *FereignItem) Delete(ctx context.Context) *spanner.Mutation {
	values, _ := fi.columnsToValues(FereignItemPrimaryKeys())
	return spanner.Delete("FereignItems", spanner.Key(values))
}
