package sdb

import (
	"strings"
)

const (
	TypeINTEGER = "INTEGER"
	TypeTEXT    = "TEXT"
	TypeBLOB    = "BLOB"
	TypeREAL    = "REAL"
	TypeBOOLEAN = "BOOLEAN"
	TypeUINT    = "UINT"
)

// handleColumnType 根据 SQLite 数据类型返回响应的数据类型指针
func handleColumnType(columnType string) any {
	databaseType := strings.ToUpper(columnType)
	switch databaseType {
	case TypeINTEGER, "INT", "TINYINT", "SMALLINT", "MEDIUMINT", "BIGINT", "INT2", "INT8":
		return new(int64)
	case TypeTEXT, "CLOB":
		return new(string)
	case TypeBLOB:
		return new(any)
	case TypeREAL, "DOUBLE", "DOUBLE PRECISION", "FLOAT":
		return new(float64)
	case TypeBOOLEAN, "BOOL":
		return new(bool)
	case TypeUINT, "UNSIGNED BIG INT":
		return new(uint64)
	default:
		if strings.HasPrefix(databaseType, "CHAR") ||
			strings.HasPrefix(databaseType, "VARCHAR") ||
			strings.HasPrefix(databaseType, "VARYING CHARACTER") ||
			strings.HasPrefix(databaseType, "NCHAR") ||
			strings.HasPrefix(databaseType, "NATIVE CHARACTER") ||
			strings.HasPrefix(databaseType, "NVARCHAR") {
			return new(string)
		}
		return nil
	}
}

func handleScanValue(val any) any {
	switch v := val.(type) {
	case *int64:
		return *v
	case *string:
		return *v
	case *float64:
		return *v
	case *bool:
		return *v
	case *uint64:
		return *v
	default:
		return val
	}
}
