jet/sqlbuilder/clause.go

124 lines
2.9 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-05-07 19:06:21 +02:00
type clause interface {
serialize(statement statementType, out *queryData) error
}
type queryData struct {
2019-05-01 14:42:46 +02:00
buff bytes.Buffer
args []interface{}
2019-05-03 12:51:57 +02:00
}
type statementType string
2019-05-03 12:51:57 +02:00
const (
select_statement statementType = "SELECT"
insert_statement statementType = "INSERT"
update_statement statementType = "UPDATE"
delete_statement statementType = "DELETE"
set_statement statementType = "SET"
lock_statement statementType = "LOCK"
2019-05-03 12:51:57 +02:00
)
func (q *queryData) writeProjection(statement statementType, projections []projection) error {
return serializeProjectionList(statement, projections, q)
2019-05-03 12:51:57 +02:00
}
func (q *queryData) writeWhere(statement statementType, where expression) error {
q.writeString(" WHERE ")
return where.serialize(statement, q)
2019-05-03 12:51:57 +02:00
}
func (q *queryData) writeGroupBy(statement statementType, groupBy []groupByClause) error {
q.writeString(" GROUP BY ")
2019-05-03 12:51:57 +02:00
return serializeGroupByClauseList(statement, groupBy, q)
2019-05-03 12:51:57 +02:00
}
func (q *queryData) writeOrderBy(statement statementType, orderBy []orderByClause) error {
q.writeString(" ORDER BY ")
return serializeOrderByClauseList(statement, orderBy, q)
2019-05-03 12:51:57 +02:00
}
func (q *queryData) writeHaving(statement statementType, having expression) error {
q.writeString(" HAVING ")
return having.serialize(statement, q)
}
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)
}
func (q *queryData) reset() {
2019-05-01 17:25:10 +02:00
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
}