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

// FullTextSearch represents a row from 'FullTextSearch'.
type FullTextSearch struct {
	ID      int64  `spanner:"ID" json:"ID"`           // ID
	Content string `spanner:"Content" json:"Content"` // Content
}

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

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

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

func (fts *FullTextSearch) 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, &fts.ID)
		case "Content":
			ret = append(ret, &fts.Content)
		default:
			return nil, fmt.Errorf("unknown column: %s", col)
		}
	}
	return ret, nil
}

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

	return ret, nil
}

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

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

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

		return &fts, nil
	}
}

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

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

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

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

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

	decoder := newFullTextSearch_Decoder(FullTextSearchColumns())
	fts, err := decoder(row)
	if err != nil {
		return nil, newErrorWithCode(codes.Internal, "FindFullTextSearch", "FullTextSearch", err)
	}

	return fts, nil
}

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

	decoder := newFullTextSearch_Decoder(FullTextSearchColumns())

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

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

	return res, nil
}

// Delete deletes the FullTextSearch from the database.
func (fts *FullTextSearch) Delete(ctx context.Context) *spanner.Mutation {
	values, _ := fts.columnsToValues(FullTextSearchPrimaryKeys())
	return spanner.Delete("FullTextSearch", spanner.Key(values))
}
