132 lines
3.8 KiB
Go
132 lines
3.8 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 (
|
|
"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"
|
|
)
|
|
|
|
// initText registers the functions to the catalog.
|
|
func initText() {
|
|
framework.RegisterFunction(textin)
|
|
framework.RegisterFunction(textout)
|
|
framework.RegisterFunction(textrecv)
|
|
framework.RegisterFunction(textsend)
|
|
framework.RegisterFunction(bttextcmp)
|
|
framework.RegisterFunction(bttextnamecmp)
|
|
}
|
|
|
|
// textin represents the PostgreSQL function of text type IO input.
|
|
var textin = framework.Function1{
|
|
Name: "textin",
|
|
Return: pgtypes.Text,
|
|
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Cstring},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
|
|
return val.(string), nil
|
|
},
|
|
}
|
|
|
|
// textout represents the PostgreSQL function of text type IO output.
|
|
var textout = framework.Function1{
|
|
Name: "textout",
|
|
Return: pgtypes.Cstring,
|
|
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Text},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
|
|
return val, nil
|
|
},
|
|
}
|
|
|
|
// textrecv represents the PostgreSQL function of text type IO receive.
|
|
var textrecv = framework.Function1{
|
|
Name: "textrecv",
|
|
Return: pgtypes.Text,
|
|
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
|
|
}
|
|
return string(data), nil
|
|
},
|
|
}
|
|
|
|
// textsend represents the PostgreSQL function of text type IO send.
|
|
var textsend = framework.Function1{
|
|
Name: "textsend",
|
|
Return: pgtypes.Bytea,
|
|
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Text},
|
|
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
|
|
}
|
|
}
|
|
writer := utils.NewWireWriter()
|
|
writer.WriteString(val.(string))
|
|
return writer.BufferData(), nil
|
|
},
|
|
}
|
|
|
|
// bttextcmp represents the PostgreSQL function of text type compare.
|
|
var bttextcmp = framework.Function2{
|
|
Name: "bttextcmp",
|
|
Return: pgtypes.Int32,
|
|
Parameters: [2]*pgtypes.DoltgresType{pgtypes.Text, pgtypes.Text},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [3]*pgtypes.DoltgresType, val1, val2 any) (any, error) {
|
|
ab := val1.(string)
|
|
bb := val2.(string)
|
|
if ab == bb {
|
|
return int32(0), nil
|
|
} else if ab < bb {
|
|
return int32(-1), nil
|
|
} else {
|
|
return int32(1), nil
|
|
}
|
|
},
|
|
}
|
|
|
|
// bttextnamecmp represents the PostgreSQL function of text type compare with name.
|
|
var bttextnamecmp = framework.Function2{
|
|
Name: "bttextnamecmp",
|
|
Return: pgtypes.Int32,
|
|
Parameters: [2]*pgtypes.DoltgresType{pgtypes.Text, pgtypes.Text},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [3]*pgtypes.DoltgresType, val1, val2 any) (any, error) {
|
|
ab := val1.(string)
|
|
bb := val2.(string)
|
|
if ab == bb {
|
|
return int32(0), nil
|
|
} else if ab < bb {
|
|
return int32(-1), nil
|
|
} else {
|
|
return int32(1), nil
|
|
}
|
|
},
|
|
}
|