Add support for blob expressions.

This commit is contained in:
go-jet 2025-02-28 18:23:15 +01:00
parent 26e478dc7e
commit c94216ab0e
37 changed files with 1296 additions and 81 deletions

View file

@ -42,6 +42,6 @@ func (c *cast) AS_REAL() FloatExpression {
}
// AS_BLOB cast expression to BLOB type
func (c *cast) AS_BLOB() StringExpression {
return StringExp(c.AS("BLOB"))
func (c *cast) AS_BLOB() BlobExpression {
return BlobExp(c.AS("BLOB"))
}

View file

@ -21,6 +21,12 @@ type ColumnString = jet.ColumnString
// StringColumn creates named string column.
var StringColumn = jet.StringColumn
// ColumnBlob is interface for
type ColumnBlob = jet.ColumnBlob
// BlobColumn creates new named blob column
var BlobColumn = jet.BlobColumn
// ColumnInteger is interface for SQL smallint, integer, bigint columns.
type ColumnInteger = jet.ColumnInteger

View file

@ -1,6 +1,7 @@
package sqlite
import (
"encoding/hex"
"fmt"
"github.com/go-jet/jet/v2/internal/jet"
)
@ -23,7 +24,8 @@ func newDialect() jet.Dialect {
ArgumentPlaceholder: func(int) string {
return "?"
},
ReservedWords: reservedWords2,
ArgumentToString: argumentToString,
ReservedWords: reservedWords2,
ValuesDefaultColumnName: func(index int) string {
return fmt.Sprintf("column%d", index+1)
},
@ -32,6 +34,15 @@ func newDialect() jet.Dialect {
return jet.NewDialect(mySQLDialectParams)
}
func argumentToString(value any) (string, bool) {
switch bindVal := value.(type) {
case []byte:
return fmt.Sprintf("X'%s'", hex.EncodeToString(bindVal)), true
}
return "", false
}
func sqliteBitXOR(expressions ...jet.Serializer) jet.SerializerFunc {
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
if len(expressions) < 2 {

View file

@ -12,6 +12,9 @@ type BoolExpression = jet.BoolExpression
// StringExpression interface
type StringExpression = jet.StringExpression
// BlobExpression interface
type BlobExpression = jet.BlobExpression
// NumericExpression is shared interface for integer or real expression
type NumericExpression = jet.NumericExpression
@ -46,6 +49,11 @@ var BoolExp = jet.BoolExp
// Does not add sql cast to generated sql builder output.
var StringExp = jet.StringExp
// BlobExp is blob expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as blob expression.
// Does not add sql cast to generated sql builder output.
var BlobExp = jet.BlobExp
// IntExp is int expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as int expression.
// Does not add sql cast to generated sql builder output.

View file

@ -196,11 +196,22 @@ var RTRIM = jet.RTRIM
// return jet.NewStringFunc("RIGHTSTR", str, n)
//}
// HEX function takes an input and returns its equivalent hexadecimal representation
var HEX = jet.HEX
// UNHEX for a string argument str, UNHEX(str) interprets each pair of characters in the argument
// as a hexadecimal number and converts it to the byte represented by the number.
// The return value is a binary string.
var UNHEX = jet.UNHEX
// LENGTH returns number of characters in string with a given encoding
func LENGTH(str jet.StringExpression) jet.StringExpression {
func LENGTH(str jet.StringOrBlobExpression) jet.IntegerExpression {
return jet.LENGTH(str)
}
// OCTET_LENGTH returns number of bytes in string expression
var OCTET_LENGTH = jet.OCTET_LENGTH
// LPAD fills up the string to length length by prepending the characters
// fill (a space by default). If the string is already longer than length
// then it is truncated (on the right).

View file

@ -50,6 +50,10 @@ var Decimal = jet.Decimal
// String creates new string literal expression
var String = jet.String
func Blob(data []byte) BlobExpression {
return BlobExp(jet.Literal(data))
}
// UUID is a helper function to create string literal expression from uuid object
// value can be any uuid type with a String method
var UUID = jet.UUID