// Code generated by ent, DO NOT EDIT.

package ent

import (
	"example/myco-api/ent/grainjar"
	"example/myco-api/ent/sporesyringe"
	"fmt"
	"strings"
	"time"

	"entgo.io/ent/dialect/sql"
)

// GrainJar is the model entity for the GrainJar schema.
type GrainJar struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// InnoculationDate holds the value of the "InnoculationDate" field.
	InnoculationDate time.Time `json:"InnoculationDate,omitempty"`
	// Grain holds the value of the "Grain" field.
	Grain string `json:"Grain,omitempty"`
	// HarvestDate holds the value of the "HarvestDate" field.
	HarvestDate time.Time `json:"HarvestDate,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the GrainJarQuery when eager-loading is set.
	Edges                    GrainJarEdges `json:"edges"`
	spore_syringe_grain_jars *int
}

// GrainJarEdges holds the relations/edges for other nodes in the graph.
type GrainJarEdges struct {
	// SporeSyringes holds the value of the sporeSyringes edge.
	SporeSyringes *SporeSyringe `json:"sporeSyringes,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// SporeSyringesOrErr returns the SporeSyringes value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e GrainJarEdges) SporeSyringesOrErr() (*SporeSyringe, error) {
	if e.loadedTypes[0] {
		if e.SporeSyringes == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: sporesyringe.Label}
		}
		return e.SporeSyringes, nil
	}
	return nil, &NotLoadedError{edge: "sporeSyringes"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*GrainJar) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case grainjar.FieldID:
			values[i] = new(sql.NullInt64)
		case grainjar.FieldGrain:
			values[i] = new(sql.NullString)
		case grainjar.FieldInnoculationDate, grainjar.FieldHarvestDate:
			values[i] = new(sql.NullTime)
		case grainjar.ForeignKeys[0]: // spore_syringe_grain_jars
			values[i] = new(sql.NullInt64)
		default:
			return nil, fmt.Errorf("unexpected column %q for type GrainJar", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the GrainJar fields.
func (gj *GrainJar) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case grainjar.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			gj.ID = int(value.Int64)
		case grainjar.FieldInnoculationDate:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field InnoculationDate", values[i])
			} else if value.Valid {
				gj.InnoculationDate = value.Time
			}
		case grainjar.FieldGrain:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field Grain", values[i])
			} else if value.Valid {
				gj.Grain = value.String
			}
		case grainjar.FieldHarvestDate:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field HarvestDate", values[i])
			} else if value.Valid {
				gj.HarvestDate = value.Time
			}
		case grainjar.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field spore_syringe_grain_jars", value)
			} else if value.Valid {
				gj.spore_syringe_grain_jars = new(int)
				*gj.spore_syringe_grain_jars = int(value.Int64)
			}
		}
	}
	return nil
}

// QuerySporeSyringes queries the "sporeSyringes" edge of the GrainJar entity.
func (gj *GrainJar) QuerySporeSyringes() *SporeSyringeQuery {
	return NewGrainJarClient(gj.config).QuerySporeSyringes(gj)
}

// Update returns a builder for updating this GrainJar.
// Note that you need to call GrainJar.Unwrap() before calling this method if this GrainJar
// was returned from a transaction, and the transaction was committed or rolled back.
func (gj *GrainJar) Update() *GrainJarUpdateOne {
	return NewGrainJarClient(gj.config).UpdateOne(gj)
}

// Unwrap unwraps the GrainJar entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (gj *GrainJar) Unwrap() *GrainJar {
	_tx, ok := gj.config.driver.(*txDriver)
	if !ok {
		panic("ent: GrainJar is not a transactional entity")
	}
	gj.config.driver = _tx.drv
	return gj
}

// String implements the fmt.Stringer.
func (gj *GrainJar) String() string {
	var builder strings.Builder
	builder.WriteString("GrainJar(")
	builder.WriteString(fmt.Sprintf("id=%v, ", gj.ID))
	builder.WriteString("InnoculationDate=")
	builder.WriteString(gj.InnoculationDate.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("Grain=")
	builder.WriteString(gj.Grain)
	builder.WriteString(", ")
	builder.WriteString("HarvestDate=")
	builder.WriteString(gj.HarvestDate.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// GrainJars is a parsable slice of GrainJar.
type GrainJars []*GrainJar

func (gj GrainJars) config(cfg config) {
	for _i := range gj {
		gj[_i].config = cfg
	}
}
