// Code generated by yo. DO NOT EDIT.
// Package customtypes contains the types.
package customtypes

import (
	"context"
	"fmt"
	"time"

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

// AllowCommitTimestamp represents a row from 'AllowCommitTimestamp'.
type AllowCommitTimestamp struct {
	ID        int64     `spanner:"ID" json:"ID"`               // ID
	UpdatedAt time.Time `spanner:"UpdatedAt" json:"UpdatedAt"` // UpdatedAt
}

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

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

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

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

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

	return ret, nil
}

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

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

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

		return &act, nil
	}
}

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

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

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

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

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

	decoder := newAllowCommitTimestamp_Decoder(AllowCommitTimestampColumns())
	act, err := decoder(row)
	if err != nil {
		return nil, newErrorWithCode(codes.Internal, "FindAllowCommitTimestamp", "AllowCommitTimestamp", err)
	}

	return act, nil
}

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

	decoder := newAllowCommitTimestamp_Decoder(AllowCommitTimestampColumns())

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

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

	return res, nil
}

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