package sqlite3

import (
	
	

	
)

// Value is any value that can be stored in a database table.
//
// https://sqlite.org/c3ref/value.html
type Value struct {
	c      *Conn
	handle ptr_t
}

// Dup makes a copy of the SQL value and returns a pointer to that copy.
//
// https://sqlite.org/c3ref/value_dup.html
func ( Value) () *Value {
	 := ptr_t(.c.call("sqlite3_value_dup", stk_t(.handle)))
	return &Value{
		c:      .c,
		handle: ,
	}
}

// Close frees an SQL value previously obtained by [Value.Dup].
//
// https://sqlite.org/c3ref/value_dup.html
func ( *Value) () error {
	.c.call("sqlite3_value_free", stk_t(.handle))
	.handle = 0
	return nil
}

// Type returns the initial datatype of the value.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () Datatype {
	return Datatype(.c.call("sqlite3_value_type", stk_t(.handle)))
}

// Subtype returns the subtype of the value.
//
// https://sqlite.org/c3ref/value_subtype.html
func ( Value) () uint {
	return uint(uint32(.c.call("sqlite3_value_subtype", stk_t(.handle))))
}

// NumericType returns the numeric datatype of the value.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () Datatype {
	return Datatype(.c.call("sqlite3_value_numeric_type", stk_t(.handle)))
}

// Bool returns the value as a bool.
// SQLite does not have a separate boolean storage class.
// Instead, boolean values are retrieved as numbers,
// with 0 converted to false and any other value to true.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () bool {
	return .Float() != 0
}

// Int returns the value as an int.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () int {
	return int(.Int64())
}

// Int64 returns the value as an int64.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () int64 {
	return int64(.c.call("sqlite3_value_int64", stk_t(.handle)))
}

// Float returns the value as a float64.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () float64 {
	 := uint64(.c.call("sqlite3_value_double", stk_t(.handle)))
	return math.Float64frombits()
}

// Time returns the value as a [time.Time].
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) ( TimeFormat) time.Time {
	var  any
	switch .Type() {
	case INTEGER:
		 = .Int64()
	case FLOAT:
		 = .Float()
	case TEXT, BLOB:
		 = .Text()
	case NULL:
		return time.Time{}
	default:
		panic(util.AssertErr())
	}
	,  := .Decode()
	return 
}

// Text returns the value as a string.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () string {
	return string(.RawText())
}

// Blob appends to buf and returns
// the value as a []byte.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) ( []byte) []byte {
	return append(, .RawBlob()...)
}

// RawText returns the value as a []byte.
// The []byte is owned by SQLite and may be invalidated by
// subsequent calls to [Value] methods.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () []byte {
	 := ptr_t(.c.call("sqlite3_value_text", stk_t(.handle)))
	return .rawBytes(, 1)
}

// RawBlob returns the value as a []byte.
// The []byte is owned by SQLite and may be invalidated by
// subsequent calls to [Value] methods.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () []byte {
	 := ptr_t(.c.call("sqlite3_value_blob", stk_t(.handle)))
	return .rawBytes(, 0)
}

func ( Value) ( ptr_t,  int32) []byte {
	if  == 0 {
		return nil
	}

	 := int32(.c.call("sqlite3_value_bytes", stk_t(.handle)))
	return util.View(.c.mod, , int64(+))[:]
}

// Pointer gets the pointer associated with this value,
// or nil if it has no associated pointer.
func ( Value) () any {
	 := ptr_t(.c.call("sqlite3_value_pointer_go", stk_t(.handle)))
	return util.GetHandle(.c.ctx, )
}

// NoChange returns true if and only if the value is unchanged
// in a virtual table update operatiom.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () bool {
	 := int32(.c.call("sqlite3_value_nochange", stk_t(.handle)))
	return  != 0
}

// FromBind returns true if value originated from a bound parameter.
//
// https://sqlite.org/c3ref/value_blob.html
func ( Value) () bool {
	 := int32(.c.call("sqlite3_value_frombind", stk_t(.handle)))
	return  != 0
}

// InFirst returns the first element
// on the right-hand side of an IN constraint.
//
// https://sqlite.org/c3ref/vtab_in_first.html
func ( Value) () (Value, error) {
	defer .c.arena.mark()()
	 := .c.arena.new(ptrlen)
	 := res_t(.c.call("sqlite3_vtab_in_first", stk_t(.handle), stk_t()))
	if  := .c.error();  != nil {
		return Value{}, 
	}
	return Value{
		c:      .c,
		handle: util.Read32[ptr_t](.c.mod, ),
	}, nil
}

// InNext returns the next element
// on the right-hand side of an IN constraint.
//
// https://sqlite.org/c3ref/vtab_in_first.html
func ( Value) () (Value, error) {
	defer .c.arena.mark()()
	 := .c.arena.new(ptrlen)
	 := res_t(.c.call("sqlite3_vtab_in_next", stk_t(.handle), stk_t()))
	if  := .c.error();  != nil {
		return Value{}, 
	}
	return Value{
		c:      .c,
		handle: util.Read32[ptr_t](.c.mod, ),
	}, nil
}