Rename execution package to qrm (Query Result Mapping).
This commit is contained in:
parent
92de03d4b3
commit
29f43e5fe8
7 changed files with 29 additions and 28 deletions
|
|
@ -3,7 +3,7 @@ package jet
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"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)
|
//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.
|
// Query executes statement over database connection db and stores row result in destination.
|
||||||
// Destination can be arbitrary structure
|
// 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.
|
// QueryContext executes statement with a context over database connection db and stores row result in destination.
|
||||||
// Destination can be of arbitrary structure
|
// 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 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.
|
//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
|
// SerializerStatement interface
|
||||||
|
|
@ -71,24 +71,24 @@ func (s *serializerStatementInterfaceImpl) DebugSql() (query string) {
|
||||||
return
|
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()
|
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()
|
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()
|
query, args := s.Sql()
|
||||||
return db.Exec(query, args...)
|
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()
|
query, args := s.Sql()
|
||||||
|
|
||||||
return db.ExecContext(context, query, args...)
|
return db.ExecContext(context, query, args...)
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-jet/jet/execution"
|
|
||||||
"github.com/go-jet/jet/internal/jet"
|
"github.com/go-jet/jet/internal/jet"
|
||||||
"github.com/go-jet/jet/internal/utils"
|
"github.com/go-jet/jet/internal/utils"
|
||||||
|
"github.com/go-jet/jet/qrm"
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -16,7 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// AssertExec assert statement execution for successful execution and number of rows affected
|
// 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)
|
res, err := stmt.Exec(db)
|
||||||
|
|
||||||
assert.NilError(t, err)
|
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
|
// 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)
|
_, err := stmt.Exec(db)
|
||||||
|
|
||||||
assert.Error(t, err, errorStr)
|
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
|
// 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() {
|
defer func() {
|
||||||
r := recover()
|
r := recover()
|
||||||
assert.Equal(t, r, errString)
|
assert.Equal(t, r, errString)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package execution
|
package qrm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package execution
|
package qrm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-jet/jet/execution/internal"
|
|
||||||
"github.com/go-jet/jet/internal/utils"
|
"github.com/go-jet/jet/internal/utils"
|
||||||
|
"github.com/go-jet/jet/qrm/internal"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -14,23 +14,24 @@ import (
|
||||||
"time"
|
"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.
|
// 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(db, "jet: db is nil")
|
||||||
utils.MustBeInitializedPtr(destinationPtr, "jet: destination is nil")
|
utils.MustBeInitializedPtr(destPtr, "jet: destination is nil")
|
||||||
utils.MustBe(destinationPtr, reflect.Ptr, "jet: destination has to be a pointer to slice or pointer to struct")
|
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 {
|
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 {
|
} else if destinationPtrType.Elem().Kind() == reflect.Struct {
|
||||||
tempSlicePtrValue := reflect.New(reflect.SliceOf(destinationPtrType))
|
tempSlicePtrValue := reflect.New(reflect.SliceOf(destinationPtrType))
|
||||||
tempSliceValue := tempSlicePtrValue.Elem()
|
tempSliceValue := tempSlicePtrValue.Elem()
|
||||||
|
|
||||||
err := queryToSlice(context, db, query, args, tempSlicePtrValue.Interface())
|
err := queryToSlice(ctx, db, query, args, tempSlicePtrValue.Interface())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -40,7 +41,7 @@ func Query(context context.Context, db DB, query string, args []interface{}, des
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
structValue := reflect.ValueOf(destinationPtr).Elem()
|
structValue := reflect.ValueOf(destPtr).Elem()
|
||||||
firstTempStruct := tempSliceValue.Index(0).Elem()
|
firstTempStruct := tempSliceValue.Index(0).Elem()
|
||||||
|
|
||||||
if structValue.Type().AssignableTo(firstTempStruct.Type()) {
|
if structValue.Type().AssignableTo(firstTempStruct.Type()) {
|
||||||
|
|
@ -2,9 +2,9 @@ package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-jet/jet/execution"
|
|
||||||
"github.com/go-jet/jet/internal/testutils"
|
"github.com/go-jet/jet/internal/testutils"
|
||||||
. "github.com/go-jet/jet/postgres"
|
. "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/model"
|
||||||
. "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/table"
|
. "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/table"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
@ -57,7 +57,7 @@ func TestScanToValidDestination(t *testing.T) {
|
||||||
|
|
||||||
t.Run("global query function scan", func(t *testing.T) {
|
t.Run("global query function scan", func(t *testing.T) {
|
||||||
queryStr, args := query.Sql()
|
queryStr, args := query.Sql()
|
||||||
err := execution.Query(nil, db, queryStr, args, &struct{}{})
|
err := qrm.Query(nil, db, queryStr, args, &struct{}{})
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue