Rename execution package to qrm (Query Result Mapping).

This commit is contained in:
go-jet 2019-09-27 11:46:31 +02:00
parent 92de03d4b3
commit 29f43e5fe8
7 changed files with 29 additions and 28 deletions

View file

@ -3,7 +3,7 @@ package jet
import (
"context"
"database/sql"
"github.com/go-jet/jet/execution"
"github.com/go-jet/jet/qrm"
)
//Statement is common interface for all statements(SELECT, INSERT, UPDATE, DELETE, LOCK)
@ -16,15 +16,15 @@ type Statement interface {
// Query executes statement over database connection db and stores row result in destination.
// Destination can be arbitrary structure
Query(db execution.DB, destination interface{}) error
Query(db qrm.DB, destination interface{}) error
// QueryContext executes statement with a context over database connection db and stores row result in destination.
// Destination can be of arbitrary structure
QueryContext(context context.Context, db execution.DB, destination interface{}) error
QueryContext(context context.Context, db qrm.DB, destination interface{}) error
//Exec executes statement over db connection without returning any rows.
Exec(db execution.DB) (sql.Result, error)
Exec(db qrm.DB) (sql.Result, error)
//Exec executes statement with context over db connection without returning any rows.
ExecContext(context context.Context, db execution.DB) (sql.Result, error)
ExecContext(context context.Context, db qrm.DB) (sql.Result, error)
}
// SerializerStatement interface
@ -71,24 +71,24 @@ func (s *serializerStatementInterfaceImpl) DebugSql() (query string) {
return
}
func (s *serializerStatementInterfaceImpl) Query(db execution.DB, destination interface{}) error {
func (s *serializerStatementInterfaceImpl) Query(db qrm.DB, destination interface{}) error {
query, args := s.Sql()
return execution.Query(context.Background(), db, query, args, destination)
return qrm.Query(context.Background(), db, query, args, destination)
}
func (s *serializerStatementInterfaceImpl) QueryContext(context context.Context, db execution.DB, destination interface{}) error {
func (s *serializerStatementInterfaceImpl) QueryContext(context context.Context, db qrm.DB, destination interface{}) error {
query, args := s.Sql()
return execution.Query(context, db, query, args, destination)
return qrm.Query(context, db, query, args, destination)
}
func (s *serializerStatementInterfaceImpl) Exec(db execution.DB) (res sql.Result, err error) {
func (s *serializerStatementInterfaceImpl) Exec(db qrm.DB) (res sql.Result, err error) {
query, args := s.Sql()
return db.Exec(query, args...)
}
func (s *serializerStatementInterfaceImpl) ExecContext(context context.Context, db execution.DB) (res sql.Result, err error) {
func (s *serializerStatementInterfaceImpl) ExecContext(context context.Context, db qrm.DB) (res sql.Result, err error) {
query, args := s.Sql()
return db.ExecContext(context, query, args...)

View file

@ -4,9 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-jet/jet/execution"
"github.com/go-jet/jet/internal/jet"
"github.com/go-jet/jet/internal/utils"
"github.com/go-jet/jet/qrm"
"gotest.tools/assert"
"io/ioutil"
"os"
@ -16,7 +16,7 @@ import (
)
// AssertExec assert statement execution for successful execution and number of rows affected
func AssertExec(t *testing.T, stmt jet.Statement, db execution.DB, rowsAffected ...int64) {
func AssertExec(t *testing.T, stmt jet.Statement, db qrm.DB, rowsAffected ...int64) {
res, err := stmt.Exec(db)
assert.NilError(t, err)
@ -29,7 +29,7 @@ func AssertExec(t *testing.T, stmt jet.Statement, db execution.DB, rowsAffected
}
// AssertExecErr assert statement execution for failed execution with error string errorStr
func AssertExecErr(t *testing.T, stmt jet.Statement, db execution.DB, errorStr string) {
func AssertExecErr(t *testing.T, stmt jet.Statement, db qrm.DB, errorStr string) {
_, err := stmt.Exec(db)
assert.Error(t, err, errorStr)
@ -150,7 +150,7 @@ func AssertProjectionSerialize(t *testing.T, dialect jet.Dialect, projection jet
}
// AssertQueryPanicErr check if statement Query execution panics with error errString
func AssertQueryPanicErr(t *testing.T, stmt jet.Statement, db execution.DB, dest interface{}, errString string) {
func AssertQueryPanicErr(t *testing.T, stmt jet.Statement, db qrm.DB, dest interface{}, errString string) {
defer func() {
r := recover()
assert.Equal(t, r, errString)

View file

@ -1,4 +1,4 @@
package execution
package qrm
import (
"context"

View file

@ -1,12 +1,12 @@
package execution
package qrm
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"github.com/go-jet/jet/execution/internal"
"github.com/go-jet/jet/internal/utils"
"github.com/go-jet/jet/qrm/internal"
"github.com/google/uuid"
"reflect"
"strconv"
@ -14,23 +14,24 @@ import (
"time"
)
// Query executes query with list of arguments over database connection using context and stores result into destination.
// Query executes Query Result Mapping (QRM) of `query` with list of parametrized arguments `arg` over database connection `db`
// using context `ctx` into destination `destPtr`.
// Destination can be either pointer to struct or pointer to slice of structs.
func Query(context context.Context, db DB, query string, args []interface{}, destinationPtr interface{}) error {
func Query(ctx context.Context, db DB, query string, args []interface{}, destPtr interface{}) error {
utils.MustBeInitializedPtr(db, "jet: db is nil")
utils.MustBeInitializedPtr(destinationPtr, "jet: destination is nil")
utils.MustBe(destinationPtr, reflect.Ptr, "jet: destination has to be a pointer to slice or pointer to struct")
utils.MustBeInitializedPtr(destPtr, "jet: destination is nil")
utils.MustBe(destPtr, reflect.Ptr, "jet: destination has to be a pointer to slice or pointer to struct")
destinationPtrType := reflect.TypeOf(destinationPtr)
destinationPtrType := reflect.TypeOf(destPtr)
if destinationPtrType.Elem().Kind() == reflect.Slice {
return queryToSlice(context, db, query, args, destinationPtr)
return queryToSlice(ctx, db, query, args, destPtr)
} else if destinationPtrType.Elem().Kind() == reflect.Struct {
tempSlicePtrValue := reflect.New(reflect.SliceOf(destinationPtrType))
tempSliceValue := tempSlicePtrValue.Elem()
err := queryToSlice(context, db, query, args, tempSlicePtrValue.Interface())
err := queryToSlice(ctx, db, query, args, tempSlicePtrValue.Interface())
if err != nil {
return err
@ -40,7 +41,7 @@ func Query(context context.Context, db DB, query string, args []interface{}, des
return nil
}
structValue := reflect.ValueOf(destinationPtr).Elem()
structValue := reflect.ValueOf(destPtr).Elem()
firstTempStruct := tempSliceValue.Index(0).Elem()
if structValue.Type().AssignableTo(firstTempStruct.Type()) {

View file

@ -2,9 +2,9 @@ package postgres
import (
"fmt"
"github.com/go-jet/jet/execution"
"github.com/go-jet/jet/internal/testutils"
. "github.com/go-jet/jet/postgres"
"github.com/go-jet/jet/qrm"
"github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/model"
. "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/table"
"github.com/google/uuid"
@ -57,7 +57,7 @@ func TestScanToValidDestination(t *testing.T) {
t.Run("global query function scan", func(t *testing.T) {
queryStr, args := query.Sql()
err := execution.Query(nil, db, queryStr, args, &struct{}{})
err := qrm.Query(nil, db, queryStr, args, &struct{}{})
assert.NilError(t, err)
})