jet/sqlbuilder/clause.go

105 lines
2.1 KiB
Go
Raw Normal View History

2019-03-31 09:17:28 +02:00
package sqlbuilder
import (
"bytes"
"errors"
"strconv"
)
2019-03-31 09:17:28 +02:00
2019-03-31 14:07:58 +02:00
type serializeOption int
const (
SKIP_DEFAULT_ALIASING = iota
2019-03-31 14:07:58 +02:00
FOR_PROJECTION
2019-05-01 14:42:46 +02:00
NO_TABLE_NAME
2019-03-31 14:07:58 +02:00
)
2019-03-31 09:17:28 +02:00
type Clause interface {
Serialize(out *queryData, options ...serializeOption) error
}
type queryData struct {
2019-05-01 14:42:46 +02:00
buff bytes.Buffer
args []interface{}
}
func (q *queryData) Write(data []byte) {
2019-05-01 14:42:46 +02:00
q.buff.Write(data)
}
func (q *queryData) WriteString(str string) {
2019-05-01 14:42:46 +02:00
q.buff.WriteString(str)
}
func (q *queryData) WriteByte(b byte) {
2019-05-01 14:42:46 +02:00
q.buff.WriteByte(b)
}
func (q *queryData) InsertArgument(arg interface{}) {
q.args = append(q.args, arg)
argPlaceholder := "$" + strconv.Itoa(len(q.args))
2019-05-01 14:42:46 +02:00
q.buff.WriteString(argPlaceholder)
}
2019-05-01 17:25:10 +02:00
func (q *queryData) Reset() {
q.buff.Reset()
q.args = []interface{}{}
}
func argToString(value interface{}) (string, error) {
switch bindVal := value.(type) {
case bool:
if bindVal {
return "TRUE", nil
} else {
return "FALSE", nil
}
case int8:
return strconv.FormatInt(int64(bindVal), 10), nil
case int:
return strconv.FormatInt(int64(bindVal), 10), nil
case int16:
return strconv.FormatInt(int64(bindVal), 10), nil
case int32:
return strconv.FormatInt(int64(bindVal), 10), nil
case int64:
return strconv.FormatInt(int64(bindVal), 10), nil
case uint8:
return strconv.FormatUint(uint64(bindVal), 10), nil
case uint:
return strconv.FormatUint(uint64(bindVal), 10), nil
case uint16:
return strconv.FormatUint(uint64(bindVal), 10), nil
case uint32:
return strconv.FormatUint(uint64(bindVal), 10), nil
case uint64:
return strconv.FormatUint(uint64(bindVal), 10), nil
case float32:
return strconv.FormatFloat(float64(bindVal), 'f', -1, 64), nil
case float64:
return strconv.FormatFloat(float64(bindVal), 'f', -1, 64), nil
case string:
return bindVal, nil
case []byte:
return string(bindVal), nil
//TODO: implement
//case time.Time:
// return bindVal.String())
default:
return "", errors.New("Unsupported literal type. ")
}
2019-03-31 14:07:58 +02:00
}
func contains(s []serializeOption, e serializeOption) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
2019-03-31 09:17:28 +02:00
}