feat(sqlite): add SQLite type mapping and conversion functions
* Implement SQLiteToCanonicalTypes for type mapping * Add ConvertSQLiteToCanonical and ConvertCanonicalToSQLite functions * Update mapDataType to utilize new conversion logic
This commit is contained in:
@@ -3,10 +3,10 @@ package sqlite
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/pgsql"
|
||||
sqlitepkg "git.warky.dev/wdevs/relspecgo/pkg/sqlite"
|
||||
)
|
||||
|
||||
// SQLite type affinities
|
||||
// SQLite type affinity constants
|
||||
const (
|
||||
TypeText = "TEXT"
|
||||
TypeInteger = "INTEGER"
|
||||
@@ -15,55 +15,27 @@ const (
|
||||
TypeBlob = "BLOB"
|
||||
)
|
||||
|
||||
// MapPostgreSQLType maps PostgreSQL data types to SQLite type affinities
|
||||
// MapTypeToSQLite maps any SQL or Go canonical type to a SQLite type affinity.
|
||||
// Handles input from any reader (PostgreSQL, MSSQL, SQLite, Go canonical).
|
||||
func MapTypeToSQLite(colType string) string {
|
||||
return sqlitepkg.ConvertCanonicalToSQLite(colType)
|
||||
}
|
||||
|
||||
// MapPostgreSQLType is an alias for MapTypeToSQLite kept for compatibility.
|
||||
//
|
||||
// Deprecated: use MapTypeToSQLite.
|
||||
func MapPostgreSQLType(pgType string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(pgType))
|
||||
normalized = strings.TrimSuffix(normalized, "[]")
|
||||
if idx := strings.Index(normalized, "("); idx != -1 {
|
||||
normalized = normalized[:idx]
|
||||
}
|
||||
// Resolve synonyms to canonical form before mapping
|
||||
normalized = pgsql.NormalizePGType(normalized)
|
||||
|
||||
switch normalized {
|
||||
case "varchar", "char", "text", "citext", "uuid",
|
||||
"timestamp", "timestamptz", "date", "time", "timetz",
|
||||
"json", "jsonb", "xml", "inet", "cidr", "macaddr":
|
||||
return TypeText
|
||||
case "integer", "smallint", "bigint",
|
||||
"serial", "smallserial", "bigserial", "boolean":
|
||||
return TypeInteger
|
||||
case "real", "float", "double precision":
|
||||
return TypeReal
|
||||
case "numeric", "money":
|
||||
return TypeNumeric
|
||||
case "bytea", "blob":
|
||||
return TypeBlob
|
||||
default:
|
||||
return TypeText
|
||||
}
|
||||
return MapTypeToSQLite(pgType)
|
||||
}
|
||||
|
||||
// IsIntegerType checks if a column type should be treated as integer
|
||||
// IsIntegerType reports whether a column type maps to SQLite INTEGER affinity.
|
||||
func IsIntegerType(colType string) bool {
|
||||
normalized := strings.ToLower(strings.TrimSpace(colType))
|
||||
normalized = strings.TrimSuffix(normalized, "[]")
|
||||
if idx := strings.Index(normalized, "("); idx != -1 {
|
||||
normalized = normalized[:idx]
|
||||
}
|
||||
normalized = pgsql.NormalizePGType(normalized)
|
||||
switch normalized {
|
||||
case "integer", "smallint", "bigint", "serial", "smallserial", "bigserial":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return MapTypeToSQLite(colType) == TypeInteger
|
||||
}
|
||||
|
||||
// MapBooleanValue converts PostgreSQL boolean literals to SQLite (0/1)
|
||||
// MapBooleanValue converts common boolean literals to SQLite integers (1/0).
|
||||
func MapBooleanValue(value string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(value))
|
||||
switch normalized {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "true", "t", "yes", "y", "1":
|
||||
return "1"
|
||||
case "false", "f", "no", "n", "0":
|
||||
|
||||
Reference in New Issue
Block a user