2019-03-31 14:07:58 +02:00
|
|
|
package sqlbuilder
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-04-20 19:49:29 +02:00
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
|
|
|
|
"github.com/sub0zero/go-sqlbuilder/types"
|
|
|
|
|
)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
func serializeExpressionList(expressions []Expression, buf *bytes.Buffer) error {
|
|
|
|
|
for i, value := range expressions {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
buf.WriteString(", ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := value.SerializeSql(buf)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func serializeProjectionList(projections []Projection, buf *bytes.Buffer) error {
|
|
|
|
|
for i, value := range projections {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
buf.WriteString(", ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := value.SerializeForProjection(buf)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-04-20 19:49:29 +02:00
|
|
|
|
|
|
|
|
func Query(statement Statement, db types.Db, destination interface{}) error {
|
|
|
|
|
query, err := statement.String()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return execution.Execute(db, query, destination)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Execute(statement Statement, db types.Db) (res sql.Result, err error) {
|
|
|
|
|
query, err := statement.String()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err = db.Exec(query)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|