chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
// 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 (
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// initChar registers the functions to the catalog.
|
||||
func initChar() {
|
||||
framework.RegisterFunction(charin)
|
||||
framework.RegisterFunction(charout)
|
||||
framework.RegisterFunction(charrecv)
|
||||
framework.RegisterFunction(charsend)
|
||||
framework.RegisterFunction(btcharcmp)
|
||||
}
|
||||
|
||||
// charin represents the PostgreSQL function of "char" type IO input.
|
||||
var charin = framework.Function1{
|
||||
Name: "charin",
|
||||
Return: pgtypes.InternalChar,
|
||||
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Cstring},
|
||||
Strict: true,
|
||||
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
|
||||
input := val.(string)
|
||||
c := []byte(input)
|
||||
if uint32(len(c)) > pgtypes.InternalCharLength {
|
||||
return input[:pgtypes.InternalCharLength], nil
|
||||
}
|
||||
return input, nil
|
||||
},
|
||||
}
|
||||
|
||||
// charout represents the PostgreSQL function of "char" type IO output.
|
||||
var charout = framework.Function1{
|
||||
Name: "charout",
|
||||
Return: pgtypes.Cstring,
|
||||
Parameters: [1]*pgtypes.DoltgresType{pgtypes.InternalChar},
|
||||
Strict: true,
|
||||
Callable: func(ctx *sql.Context, t [2]*pgtypes.DoltgresType, val any) (any, error) {
|
||||
str := val.(string)
|
||||
if uint32(len(str)) > pgtypes.InternalCharLength {
|
||||
return str[:pgtypes.InternalCharLength], nil
|
||||
}
|
||||
return str, nil
|
||||
},
|
||||
}
|
||||
|
||||
// charrecv represents the PostgreSQL function of "char" type IO receive.
|
||||
var charrecv = framework.Function1{
|
||||
Name: "charrecv",
|
||||
Return: pgtypes.InternalChar,
|
||||
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Internal},
|
||||
Strict: true,
|
||||
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
|
||||
data := val.([]byte)
|
||||
if data == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
return string(data[0]), nil
|
||||
},
|
||||
}
|
||||
|
||||
// charsend represents the PostgreSQL function of "char" type IO send.
|
||||
var charsend = framework.Function1{
|
||||
Name: "charsend",
|
||||
Return: pgtypes.Bytea,
|
||||
Parameters: [1]*pgtypes.DoltgresType{pgtypes.InternalChar},
|
||||
Strict: true,
|
||||
Callable: func(ctx *sql.Context, _ [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 := val.(string)
|
||||
writer := utils.NewWireWriter()
|
||||
if len(str) == 1 {
|
||||
writer.WriteUint8(str[0])
|
||||
} else if len(str) == 0 {
|
||||
writer.WriteUint8(0)
|
||||
} else {
|
||||
return nil, errors.New(`"char" found multiple characters during binary wire formatting`)
|
||||
}
|
||||
return writer.BufferData(), nil
|
||||
},
|
||||
}
|
||||
|
||||
// btcharcmp represents the PostgreSQL function of "char" type compare.
|
||||
var btcharcmp = framework.Function2{
|
||||
Name: "btcharcmp",
|
||||
Return: pgtypes.Int32,
|
||||
Parameters: [2]*pgtypes.DoltgresType{pgtypes.InternalChar, pgtypes.InternalChar},
|
||||
Strict: true,
|
||||
Callable: func(ctx *sql.Context, _ [3]*pgtypes.DoltgresType, val1, val2 any) (any, error) {
|
||||
ab := strings.TrimRight(val1.(string), " ")
|
||||
bb := strings.TrimRight(val2.(string), " ")
|
||||
if ab == bb {
|
||||
return int32(0), nil
|
||||
} else if ab < bb {
|
||||
return int32(-1), nil
|
||||
} else {
|
||||
return int32(1), nil
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user