Select statement execution and mapping to struct or slice added.

This commit is contained in:
sub0Zero 2019-03-05 18:55:47 +01:00 committed by zer0sub
parent 319c9f757d
commit 75f8e0dfec
6 changed files with 291 additions and 13 deletions

View file

@ -2,7 +2,10 @@ package sqlbuilder
import (
"bytes"
"database/sql"
"fmt"
"github.com/sub0Zero/go-sqlbuilder/sqlbuilder/execution"
"reflect"
"regexp"
"github.com/dropbox/godropbox/errors"
@ -11,6 +14,7 @@ import (
type Statement interface {
// String returns generated SQL as string.
String(database string) (sql string, err error)
Execute(db *sql.DB, destination interface{}) error
}
type SelectStatement interface {
@ -133,6 +137,10 @@ type unionStatementImpl struct {
unique bool
}
func (us *unionStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil
}
func (us *unionStatementImpl) Where(expression BoolExpression) UnionStatement {
us.where = expression
return us
@ -305,6 +313,22 @@ type selectStatementImpl struct {
distinct bool
}
func (s *selectStatementImpl) Execute(db *sql.DB, destination interface{}) error {
destinationType := reflect.TypeOf(destination)
if destinationType.Kind() == reflect.Ptr && destinationType.Elem().Kind() == reflect.Struct {
s.Limit(1)
}
query, err := s.String("dvds")
if err != nil {
return err
}
return execution.Execute(db, query, destination)
}
func (s *selectStatementImpl) Copy() SelectStatement {
ret := *s
return &ret
@ -495,6 +519,10 @@ type insertStatementImpl struct {
ignore bool
}
func (i *insertStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil
}
func (s *insertStatementImpl) Add(
row ...Expression) InsertStatement {
@ -665,6 +693,10 @@ type updateStatementImpl struct {
comment string
}
func (u *updateStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil
}
func (u *updateStatementImpl) Set(
column NonAliasColumn,
expression Expression) UpdateStatement {
@ -808,6 +840,10 @@ type deleteStatementImpl struct {
comment string
}
func (d *deleteStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil
}
func (d *deleteStatementImpl) Where(expression BoolExpression) DeleteStatement {
d.where = expression
return d
@ -895,6 +931,10 @@ type tableLock struct {
w bool
}
func (l *lockStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil
}
// AddReadLock takes read lock on the table.
func (s *lockStatementImpl) AddReadLock(t *Table) LockStatement {
s.locks = append(s.locks, tableLock{t: t, w: false})
@ -951,6 +991,10 @@ func NewUnlockStatement() UnlockStatement {
type unlockStatementImpl struct {
}
func (u *unlockStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil
}
func (s *unlockStatementImpl) String(database string) (sql string, err error) {
return "UNLOCK TABLES", nil
}
@ -968,6 +1012,10 @@ type gtidNextStatementImpl struct {
gno uint64
}
func (g *gtidNextStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil
}
func (s *gtidNextStatementImpl) String(database string) (sql string, err error) {
// This statement sets a session local variable defining what the next transaction ID is. It
// does not interact with other MySQL sessions. It is neither a DDL nor DML statement, so we