153 lines
4.7 KiB
Go
153 lines
4.7 KiB
Go
// Copyright 2024 Dolthub, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package functions
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/dolthub/go-mysql-server/sql"
|
|
|
|
"github.com/dolthub/doltgresql/server/functions/framework"
|
|
pgtypes "github.com/dolthub/doltgresql/server/types"
|
|
"github.com/dolthub/doltgresql/utils"
|
|
)
|
|
|
|
// initVarChar registers the functions to the catalog.
|
|
func initVarChar() {
|
|
framework.RegisterFunction(varcharin)
|
|
framework.RegisterFunction(varcharout)
|
|
framework.RegisterFunction(varcharrecv)
|
|
framework.RegisterFunction(varcharsend)
|
|
framework.RegisterFunction(varchartypmodin)
|
|
framework.RegisterFunction(varchartypmodout)
|
|
}
|
|
|
|
// varcharin represents the PostgreSQL function of varchar type IO input.
|
|
var varcharin = framework.Function3{
|
|
Name: "varcharin",
|
|
Return: pgtypes.VarChar,
|
|
Parameters: [3]*pgtypes.DoltgresType{pgtypes.Cstring, pgtypes.Oid, pgtypes.Int32},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [4]*pgtypes.DoltgresType, val1, val2, val3 any) (any, error) {
|
|
input := val1.(string)
|
|
typmod := val3.(int32)
|
|
maxChars := pgtypes.GetCharLengthFromTypmod(typmod)
|
|
if maxChars < pgtypes.StringUnbounded {
|
|
return input, nil
|
|
}
|
|
truncated, runeLength := truncateString(input, maxChars)
|
|
if runeLength > maxChars {
|
|
return input, errors.Wrap(pgtypes.ErrCastOutOfRange, fmt.Sprintf("value too long for type varying(%v)", maxChars))
|
|
} else {
|
|
return truncated, nil
|
|
}
|
|
},
|
|
}
|
|
|
|
// varcharout represents the PostgreSQL function of varchar type IO output.
|
|
var varcharout = framework.Function1{
|
|
Name: "varcharout",
|
|
Return: pgtypes.Cstring,
|
|
Parameters: [1]*pgtypes.DoltgresType{pgtypes.VarChar},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, t [2]*pgtypes.DoltgresType, val any) (any, error) {
|
|
v, ok, err := sql.Unwrap[string](ctx, val)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !ok {
|
|
return nil, fmt.Errorf(`"varcharout" requires a string argument, got %T`, val)
|
|
}
|
|
typ := t[0]
|
|
tm := typ.GetAttTypMod()
|
|
if tm != -1 {
|
|
str, _ := truncateString(v, pgtypes.GetCharLengthFromTypmod(tm))
|
|
return str, nil
|
|
} else {
|
|
return v, nil
|
|
}
|
|
},
|
|
}
|
|
|
|
// varcharrecv represents the PostgreSQL function of varchar type IO receive.
|
|
var varcharrecv = framework.Function3{
|
|
Name: "varcharrecv",
|
|
Return: pgtypes.VarChar,
|
|
Parameters: [3]*pgtypes.DoltgresType{pgtypes.Internal, pgtypes.Oid, pgtypes.Int32},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, t [4]*pgtypes.DoltgresType, val1, val2, val3 any) (any, error) {
|
|
data := val1.([]byte)
|
|
if data == nil {
|
|
return nil, nil
|
|
}
|
|
return t[3].IoInput(ctx, string(data))
|
|
},
|
|
}
|
|
|
|
// varcharsend represents the PostgreSQL function of varchar type IO send.
|
|
var varcharsend = framework.Function1{
|
|
Name: "varcharsend",
|
|
Return: pgtypes.Bytea,
|
|
Parameters: [1]*pgtypes.DoltgresType{pgtypes.VarChar},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, t [2]*pgtypes.DoltgresType, val any) (any, error) {
|
|
if wrapper, ok := val.(sql.AnyWrapper); ok {
|
|
var err error
|
|
val, err = wrapper.UnwrapAny(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if val == nil {
|
|
return nil, nil
|
|
}
|
|
}
|
|
str, err := t[0].IoOutput(ctx, val)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
writer := utils.NewWireWriter()
|
|
writer.WriteString(str)
|
|
return writer.BufferData(), nil
|
|
},
|
|
}
|
|
|
|
// varchartypmodin represents the PostgreSQL function of varchar type IO typmod input.
|
|
var varchartypmodin = framework.Function1{
|
|
Name: "varchartypmodin",
|
|
Return: pgtypes.Int32,
|
|
Parameters: [1]*pgtypes.DoltgresType{pgtypes.CstringArray},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
|
|
return getTypModFromStringArr("varchar", val.([]any))
|
|
},
|
|
}
|
|
|
|
// varchartypmodout represents the PostgreSQL function of varchar type IO typmod output.
|
|
var varchartypmodout = framework.Function1{
|
|
Name: "varchartypmodout",
|
|
Return: pgtypes.Cstring,
|
|
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Int32},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
|
|
typmod := val.(int32)
|
|
if typmod < 5 {
|
|
return "", nil
|
|
}
|
|
maxChars := pgtypes.GetCharLengthFromTypmod(typmod)
|
|
return fmt.Sprintf("(%v)", maxChars), nil
|
|
},
|
|
}
|