package sqlite3

import (
	
	
	
	
	
	
	

	
)

// Quote escapes and quotes a value
// making it safe to embed in SQL text.
// Strings with embedded NUL characters are truncated.
//
// https://sqlite.org/lang_corefunc.html#quote
func ( any) string {
	switch v := .(type) {
	case nil:
		return "NULL"
	case bool:
		if  {
			return "1"
		} else {
			return "0"
		}

	case int:
		return strconv.Itoa()
	case int64:
		return strconv.FormatInt(, 10)
	case float64:
		switch {
		case math.IsNaN():
			return "NULL"
		case math.IsInf(, 1):
			return "9.0e999"
		case math.IsInf(, -1):
			return "-9.0e999"
		}
		return strconv.FormatFloat(, 'g', -1, 64)
	case time.Time:
		return "'" + .Format(time.RFC3339Nano) + "'"

	case string:
		if  := strings.IndexByte(, 0);  >= 0 {
			 = [:]
		}

		 := make([]byte, 2+len()+strings.Count(, "'"))
		[0] = '\''
		 := 1
		for ,  := range []byte() {
			if  == '\'' {
				[] = 
				 += 1
			}
			[] = 
			 += 1
		}
		[len()-1] = '\''
		return unsafe.String(&[0], len())

	case []byte:
		 := make([]byte, 3+2*len())
		[1] = '\''
		[0] = 'x'
		 := 2
		for ,  := range  {
			const  = "0123456789ABCDEF"
			[+0] = [/16]
			[+1] = [%16]
			 += 2
		}
		[len()-1] = '\''
		return unsafe.String(&[0], len())

	case ZeroBlob:
		 := bytes.Repeat([]byte("0"), int(3+2*int64()))
		[1] = '\''
		[0] = 'x'
		[len()-1] = '\''
		return unsafe.String(&[0], len())
	}

	 := reflect.ValueOf()
	 := .Kind()

	if  == reflect.Interface ||  == reflect.Pointer {
		if .IsNil() {
			return "NULL"
		}
		 = .Elem()
		 = .Kind()
	}

	switch {
	case .CanInt():
		return strconv.FormatInt(.Int(), 10)
	case .CanUint():
		return strconv.FormatUint(.Uint(), 10)
	case .CanFloat():
		return (.Float())
	case  == reflect.Bool:
		return (.Bool())
	case  == reflect.String:
		return (.String())
	case ( == reflect.Slice ||  == reflect.Array && .CanAddr()) &&
		.Type().Elem().Kind() == reflect.Uint8:
		return (.Bytes())
	}

	panic(util.ValueErr)
}

// QuoteIdentifier escapes and quotes an identifier
// making it safe to embed in SQL text.
// Strings with embedded NUL characters panic.
func ( string) string {
	if strings.IndexByte(, 0) >= 0 {
		panic(util.ValueErr)
	}

	 := make([]byte, 2+len()+strings.Count(, `"`))
	[0] = '"'
	 := 1
	for ,  := range []byte() {
		if  == '"' {
			[] = 
			 += 1
		}
		[] = 
		 += 1
	}
	[len()-1] = '"'
	return unsafe.String(&[0], len())
}