jet/internal/jet/serializer.go

96 lines
2.3 KiB
Go
Raw Normal View History

2019-08-11 09:52:02 +02:00
package jet
2019-08-17 18:32:01 +02:00
// SerializeOption type
2019-08-11 09:52:02 +02:00
type SerializeOption int
2019-08-17 18:32:01 +02:00
// Serialize options
2019-08-11 09:52:02 +02:00
const (
NoWrap SerializeOption = iota
SkipNewLine
fallTroughOptions // fall trough options
ShortName
2019-08-11 09:52:02 +02:00
)
// WithFallTrough extends existing serialize options with additional
func (s SerializeOption) WithFallTrough(options []SerializeOption) []SerializeOption {
return append(FallTrough(options), s)
}
2019-08-17 18:32:01 +02:00
// StatementType is type of the SQL statement
2019-08-11 09:52:02 +02:00
type StatementType string
2019-08-17 18:32:01 +02:00
// Statement types
2019-08-11 09:52:02 +02:00
const (
SelectStatementType StatementType = "SELECT"
InsertStatementType StatementType = "INSERT"
UpdateStatementType StatementType = "UPDATE"
DeleteStatementType StatementType = "DELETE"
SetStatementType StatementType = "SET"
LockStatementType StatementType = "LOCK"
UnLockStatementType StatementType = "UNLOCK"
2019-08-11 09:52:02 +02:00
)
2019-08-17 18:32:01 +02:00
// Serializer interface
2019-08-11 09:52:02 +02:00
type Serializer interface {
2019-08-17 18:32:01 +02:00
serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption)
2019-08-11 09:52:02 +02:00
}
2019-08-17 18:32:01 +02:00
// Serialize func
func Serialize(exp Serializer, statementType StatementType, out *SQLBuilder, options ...SerializeOption) {
exp.serialize(statementType, out, options...)
2019-08-11 09:52:02 +02:00
}
func contains(options []SerializeOption, option SerializeOption) bool {
for _, opt := range options {
if opt == option {
return true
}
}
return false
}
// FallTrough filters fall-trough options from the list
func FallTrough(options []SerializeOption) []SerializeOption {
var ret []SerializeOption
for _, option := range options {
if option > fallTroughOptions {
ret = append(ret, option)
}
}
return ret
}
2019-12-08 11:07:49 +01:00
// ListSerializer serializes list of serializers with separator
type ListSerializer struct {
Serializers []Serializer
Separator string
}
func (s ListSerializer) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
for i, ser := range s.Serializers {
if i > 0 {
out.WriteString(s.Separator)
}
ser.serialize(statement, out, FallTrough(options)...)
}
}
// NewSerializerClauseImpl is constructor for Seralizer with list of clauses
func NewSerializerClauseImpl(clauses ...Clause) Serializer {
return &serializerImpl{Clauses: clauses}
}
type serializerImpl struct {
Clauses []Clause
}
func (s serializerImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
for _, clause := range s.Clauses {
clause.Serialize(statement, out, FallTrough(options)...)
}
}