2020-08-06 20:58:47 +03:00
|
|
|
package libsq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-20 09:44:36 +03:00
|
|
|
"database/sql"
|
2023-07-03 18:34:19 +03:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/samber/lo"
|
2023-11-20 04:06:36 +03:00
|
|
|
"golang.org/x/sync/errgroup"
|
2023-06-18 04:28:11 +03:00
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/ast"
|
2023-04-07 11:00:49 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/ast/render"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
2023-11-20 09:44:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgm"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/record"
|
2020-08-23 22:00:13 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlmodel"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlz"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/tablefq"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// pipeline is used to execute a SLQ query,
|
|
|
|
// and write the resulting records to a RecordWriter.
|
|
|
|
type pipeline struct {
|
2023-06-18 04:28:11 +03:00
|
|
|
// query is the SLQ query
|
|
|
|
query string
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// qc is the context in which the query is executed.
|
2023-04-01 12:48:24 +03:00
|
|
|
qc *QueryContext
|
2021-03-08 09:27:35 +03:00
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// rc is the Context for rendering SQL.
|
2023-07-03 18:34:19 +03:00
|
|
|
// This field is set during pipeline.prepare. It can't be set before
|
|
|
|
// then because the target DB to use is calculated during pipeline.prepare,
|
2023-04-07 11:00:49 +03:00
|
|
|
// based on the input query and other context.
|
|
|
|
rc *render.Context
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
// tasks contains tasks that must be completed before targetSQL
|
2023-11-19 05:21:14 +03:00
|
|
|
// is executed against targetPool. Typically tasks is used to
|
2021-03-08 09:27:35 +03:00
|
|
|
// set up the joindb before it is queried.
|
|
|
|
tasks []tasker
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// targetSQL is the ultimate SQL query to be executed against targetPool.
|
2021-03-08 09:27:35 +03:00
|
|
|
targetSQL string
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// targetPool is the destination for the ultimate SQL query to
|
2021-03-08 09:27:35 +03:00
|
|
|
// be executed against.
|
2023-11-19 05:21:14 +03:00
|
|
|
targetPool driver.Pool
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// newPipeline parses query, returning a pipeline prepared for
|
|
|
|
// execution via pipeline.execute.
|
|
|
|
func newPipeline(ctx context.Context, qc *QueryContext, query string) (*pipeline, error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
2023-04-02 22:49:45 +03:00
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
a, err := ast.Parse(log, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
qModel, err := buildQueryModel(qc, a)
|
2023-04-01 12:48:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
p := &pipeline{
|
2023-06-18 04:28:11 +03:00
|
|
|
qc: qc,
|
|
|
|
query: query,
|
2023-04-01 12:48:24 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
if err = p.prepare(ctx, qModel); err != nil {
|
2023-04-01 12:48:24 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
return p, nil
|
2023-04-01 12:48:24 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// execute executes the pipeline, writing results to recw.
|
|
|
|
func (p *pipeline) execute(ctx context.Context, recw RecordWriter) error {
|
2023-11-20 09:44:36 +03:00
|
|
|
log := lg.FromContext(ctx)
|
|
|
|
log.Debug(
|
2023-04-02 22:49:45 +03:00
|
|
|
"Execute SQL query",
|
2023-11-19 05:21:14 +03:00
|
|
|
lga.Src, p.targetPool.Source(),
|
2023-07-03 18:34:19 +03:00
|
|
|
lga.SQL, p.targetSQL,
|
2023-04-02 22:49:45 +03:00
|
|
|
)
|
2021-03-08 09:27:35 +03:00
|
|
|
|
2023-11-20 09:44:36 +03:00
|
|
|
errw := p.targetPool.SQLDriver().ErrWrapFunc()
|
|
|
|
|
|
|
|
// TODO: The tasks might like to be executed in parallel. However,
|
|
|
|
// what happens if a task does something that is session/connection-dependent?
|
|
|
|
// When the query executes later (below), it could be on a different
|
|
|
|
// connection. Maybe the tasks need a means of declaring that they
|
2023-11-20 09:45:59 +03:00
|
|
|
// must be executed on the same connection as the main query?
|
2023-07-03 18:34:19 +03:00
|
|
|
if err := p.executeTasks(ctx); err != nil {
|
2023-11-20 09:44:36 +03:00
|
|
|
return errw(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var conn sqlz.DB
|
|
|
|
if len(p.qc.PreExecStmts) > 0 || len(p.qc.PostExecStmts) > 0 {
|
|
|
|
// If there's pre/post exec work to do, we need to
|
|
|
|
// obtain a connection from the pool. We are responsible
|
|
|
|
// for closing these resources.
|
|
|
|
db, err := p.targetPool.DB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errw(err)
|
|
|
|
}
|
|
|
|
defer lg.WarnIfCloseError(log, lgm.CloseDB, db)
|
|
|
|
|
|
|
|
if conn, err = db.Conn(ctx); err != nil {
|
|
|
|
return errw(err)
|
|
|
|
}
|
|
|
|
defer lg.WarnIfCloseError(log, lgm.CloseConn, conn.(*sql.Conn))
|
|
|
|
|
|
|
|
for _, stmt := range p.qc.PreExecStmts {
|
|
|
|
if _, err = conn.ExecContext(ctx, stmt); err != nil {
|
|
|
|
return errw(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := QuerySQL(ctx, p.targetPool, conn, recw, p.targetSQL); err != nil {
|
2020-08-06 20:58:47 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-20 09:44:36 +03:00
|
|
|
if conn != nil && len(p.qc.PostExecStmts) > 0 {
|
|
|
|
for _, stmt := range p.qc.PostExecStmts {
|
|
|
|
if _, err := conn.ExecContext(ctx, stmt); err != nil {
|
|
|
|
return errw(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-03-08 09:27:35 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// executeTasks executes any tasks in pipeline.tasks.
|
2021-03-08 09:27:35 +03:00
|
|
|
// These tasks may exist if preparatory work must be performed
|
2023-07-03 18:34:19 +03:00
|
|
|
// before pipeline.targetSQL can be executed.
|
|
|
|
func (p *pipeline) executeTasks(ctx context.Context) error {
|
|
|
|
switch len(p.tasks) {
|
2021-03-08 09:27:35 +03:00
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
2023-07-03 18:34:19 +03:00
|
|
|
return p.tasks[0].executeTask(ctx)
|
2021-03-08 09:27:35 +03:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-04-03 00:17:15 +03:00
|
|
|
g, gCtx := errgroup.WithContext(ctx)
|
2023-05-03 15:36:10 +03:00
|
|
|
g.SetLimit(driver.OptTuningErrgroupLimit.Get(options.FromContext(ctx)))
|
2023-04-03 00:17:15 +03:00
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
for _, task := range p.tasks {
|
2021-03-08 09:27:35 +03:00
|
|
|
task := task
|
|
|
|
|
|
|
|
g.Go(func() error {
|
2023-04-08 21:09:27 +03:00
|
|
|
select {
|
|
|
|
case <-gCtx.Done():
|
|
|
|
return gCtx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
return task.executeTask(gCtx)
|
2021-03-08 09:27:35 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.Wait()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// prepareNoTable is invoked when the queryModel doesn't have a table.
|
2023-06-18 04:28:11 +03:00
|
|
|
// That is to say, the query doesn't have a "FROM table" clause. It is
|
|
|
|
// this function's responsibility to figure out what source to use, and
|
2023-07-03 18:34:19 +03:00
|
|
|
// to set the relevant pipeline fields.
|
|
|
|
func (p *pipeline) prepareNoTable(ctx context.Context, qm *queryModel) error {
|
|
|
|
log := lg.FromContext(ctx)
|
|
|
|
log.Debug("No table in query; will look for source to use...")
|
2023-06-18 04:28:11 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
src *source.Source
|
|
|
|
err error
|
|
|
|
handle = ast.NewInspector(qm.AST).FindFirstHandle()
|
|
|
|
)
|
|
|
|
|
|
|
|
if handle == "" {
|
2023-07-03 18:34:19 +03:00
|
|
|
if src = p.qc.Collection.Active(); src == nil {
|
|
|
|
log.Debug("No active source, will use scratchdb.")
|
2023-11-19 05:21:14 +03:00
|
|
|
p.targetPool, err = p.qc.ScratchPoolOpener.OpenScratch(ctx, "scratch")
|
2023-06-18 04:28:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
p.rc = &render.Context{
|
2023-11-19 05:21:14 +03:00
|
|
|
Renderer: p.targetPool.SQLDriver().Renderer(),
|
2023-07-03 18:34:19 +03:00
|
|
|
Args: p.qc.Args,
|
2023-11-19 05:21:14 +03:00
|
|
|
Dialect: p.targetPool.SQLDriver().Dialect(),
|
2023-06-18 04:28:11 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
log.Debug("Using active source.", lga.Src, src)
|
|
|
|
} else if src, err = p.qc.Collection.Get(handle); err != nil {
|
2023-06-18 04:28:11 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, src is non-nil.
|
2023-11-19 05:21:14 +03:00
|
|
|
if p.targetPool, err = p.qc.PoolOpener.Open(ctx, src); err != nil {
|
2023-06-18 04:28:11 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
p.rc = &render.Context{
|
2023-11-19 05:21:14 +03:00
|
|
|
Renderer: p.targetPool.SQLDriver().Renderer(),
|
2023-07-03 18:34:19 +03:00
|
|
|
Args: p.qc.Args,
|
2023-11-19 05:21:14 +03:00
|
|
|
Dialect: p.targetPool.SQLDriver().Dialect(),
|
2023-06-18 04:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-25 19:29:24 +03:00
|
|
|
// prepareFromTable builds the "FROM table" fragment.
|
2023-04-07 11:00:49 +03:00
|
|
|
//
|
2023-07-03 18:34:19 +03:00
|
|
|
// When this function returns, pipeline.rc will be set.
|
|
|
|
func (p *pipeline) prepareFromTable(ctx context.Context, tblSel *ast.TblSelectorNode) (fromClause string,
|
2023-11-19 05:21:14 +03:00
|
|
|
fromPool driver.Pool, err error,
|
2022-12-18 11:35:59 +03:00
|
|
|
) {
|
2023-06-18 04:28:11 +03:00
|
|
|
handle := tblSel.Handle()
|
|
|
|
if handle == "" {
|
2023-07-03 18:34:19 +03:00
|
|
|
handle = p.qc.Collection.ActiveHandle()
|
2023-06-18 04:28:11 +03:00
|
|
|
if handle == "" {
|
|
|
|
return "", nil, errz.New("query does not specify source, and no active source")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
src, err := p.qc.Collection.Get(handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
fromPool, err = p.qc.PoolOpener.Open(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
rndr := fromPool.SQLDriver().Renderer()
|
2023-07-03 18:34:19 +03:00
|
|
|
p.rc = &render.Context{
|
2023-04-07 11:00:49 +03:00
|
|
|
Renderer: rndr,
|
2023-07-03 18:34:19 +03:00
|
|
|
Args: p.qc.Args,
|
2023-11-19 05:21:14 +03:00
|
|
|
Dialect: fromPool.SQLDriver().Dialect(),
|
2023-04-07 11:00:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
fromClause, err = rndr.FromTable(p.rc, tblSel)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
return fromClause, fromPool, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// joinClause models the SQL "JOIN" construct.
|
|
|
|
type joinClause struct {
|
|
|
|
leftTbl *ast.TblSelectorNode
|
|
|
|
joins []*ast.JoinNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// tables returns a new slice containing all referenced tables.
|
|
|
|
func (jc *joinClause) tables() []*ast.TblSelectorNode {
|
|
|
|
tbls := make([]*ast.TblSelectorNode, len(jc.joins)+1)
|
|
|
|
tbls[0] = jc.leftTbl
|
|
|
|
for i := range jc.joins {
|
|
|
|
tbls[i+1] = jc.joins[i].Table()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
return tbls
|
|
|
|
}
|
|
|
|
|
|
|
|
// handles returns the set of (non-empty) handles from the tables,
|
|
|
|
// without any duplicates.
|
|
|
|
func (jc *joinClause) handles() []string {
|
|
|
|
handles := make([]string, len(jc.joins)+1)
|
|
|
|
handles[0] = jc.leftTbl.Handle()
|
|
|
|
for i := 0; i < len(jc.joins); i++ {
|
|
|
|
handles[i+1] = jc.joins[i].Table().Handle()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
handles = lo.Uniq(handles)
|
|
|
|
handles = lo.Without(handles, "")
|
|
|
|
return handles
|
|
|
|
}
|
|
|
|
|
|
|
|
// isSingleSource returns true if the joins refer to the same handle.
|
|
|
|
func (jc *joinClause) isSingleSource() bool {
|
|
|
|
leftHandle := jc.leftTbl.Handle()
|
|
|
|
|
|
|
|
for _, join := range jc.joins {
|
|
|
|
joinHandle := join.Table().Handle()
|
|
|
|
if joinHandle == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if joinHandle != leftHandle {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareFromJoin builds the "JOIN" clause.
|
|
|
|
//
|
|
|
|
// When this function returns, pipeline.rc will be set.
|
|
|
|
func (p *pipeline) prepareFromJoin(ctx context.Context, jc *joinClause) (fromClause string,
|
2023-11-19 05:21:14 +03:00
|
|
|
fromConn driver.Pool, err error,
|
2023-07-03 18:34:19 +03:00
|
|
|
) {
|
|
|
|
if jc.isSingleSource() {
|
|
|
|
return p.joinSingleSource(ctx, jc)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
return p.joinCrossSource(ctx, jc)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-25 19:29:24 +03:00
|
|
|
// joinSingleSource sets up a join against a single source.
|
2023-04-07 11:00:49 +03:00
|
|
|
//
|
2023-07-03 18:34:19 +03:00
|
|
|
// On return, pipeline.rc will be set.
|
|
|
|
func (p *pipeline) joinSingleSource(ctx context.Context, jc *joinClause) (fromClause string,
|
2023-11-19 05:21:14 +03:00
|
|
|
fromPool driver.Pool, err error,
|
2022-12-18 11:35:59 +03:00
|
|
|
) {
|
2023-07-03 18:34:19 +03:00
|
|
|
src, err := p.qc.Collection.Get(jc.leftTbl.Handle())
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
fromPool, err = p.qc.PoolOpener.Open(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
rndr := fromPool.SQLDriver().Renderer()
|
2023-07-03 18:34:19 +03:00
|
|
|
p.rc = &render.Context{
|
2023-04-07 11:00:49 +03:00
|
|
|
Renderer: rndr,
|
2023-07-03 18:34:19 +03:00
|
|
|
Args: p.qc.Args,
|
2023-11-19 05:21:14 +03:00
|
|
|
Dialect: fromPool.SQLDriver().Dialect(),
|
2023-04-07 11:00:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
fromClause, err = rndr.Join(p.rc, jc.leftTbl, jc.joins)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
return fromClause, fromPool, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-25 19:29:24 +03:00
|
|
|
// joinCrossSource returns a FROM clause that forms part of
|
2020-08-06 20:58:47 +03:00
|
|
|
// the SQL SELECT statement against fromDB.
|
2023-04-07 11:00:49 +03:00
|
|
|
//
|
2023-07-03 18:34:19 +03:00
|
|
|
// On return, pipeline.rc will be set.
|
|
|
|
func (p *pipeline) joinCrossSource(ctx context.Context, jc *joinClause) (fromClause string,
|
2023-11-19 05:21:14 +03:00
|
|
|
fromDB driver.Pool, err error,
|
2022-12-18 11:35:59 +03:00
|
|
|
) {
|
2023-07-03 18:34:19 +03:00
|
|
|
handles := jc.handles()
|
|
|
|
srcs := make([]*source.Source, 0, len(handles))
|
|
|
|
for _, handle := range handles {
|
|
|
|
var src *source.Source
|
|
|
|
if src, err = p.qc.Collection.Get(handle); err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
srcs = append(srcs, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2021-03-08 09:27:35 +03:00
|
|
|
|
|
|
|
// Open the join db
|
2023-11-19 05:21:14 +03:00
|
|
|
joinPool, err := p.qc.JoinPoolOpener.OpenJoin(ctx, srcs...)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
rndr := joinPool.SQLDriver().Renderer()
|
2023-07-03 18:34:19 +03:00
|
|
|
p.rc = &render.Context{
|
2023-04-07 11:00:49 +03:00
|
|
|
Renderer: rndr,
|
2023-07-03 18:34:19 +03:00
|
|
|
Args: p.qc.Args,
|
2023-11-19 05:21:14 +03:00
|
|
|
Dialect: joinPool.SQLDriver().Dialect(),
|
2023-04-07 11:00:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
leftHandle := jc.leftTbl.Handle()
|
|
|
|
// TODO: verify not empty
|
2021-03-08 09:27:35 +03:00
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
tbls := jc.tables()
|
|
|
|
for _, tbl := range tbls {
|
|
|
|
tbl := tbl
|
|
|
|
handle := tbl.Handle()
|
|
|
|
if handle == "" {
|
|
|
|
handle = leftHandle
|
|
|
|
}
|
|
|
|
var src *source.Source
|
|
|
|
if src, err = p.qc.Collection.Get(handle); err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2023-11-19 05:21:14 +03:00
|
|
|
var db driver.Pool
|
|
|
|
if db, err = p.qc.PoolOpener.Open(ctx, src); err != nil {
|
2023-07-03 18:34:19 +03:00
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &joinCopyTask{
|
2023-11-19 05:21:14 +03:00
|
|
|
fromPool: db,
|
|
|
|
fromTbl: tbl.Table(),
|
|
|
|
toPool: joinPool,
|
|
|
|
toTbl: tbl.TblAliasOrName(),
|
2023-07-03 18:34:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tbl.SyncTblNameAlias()
|
2021-03-08 09:27:35 +03:00
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
p.tasks = append(p.tasks, task)
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
fromClause, err = rndr.Join(p.rc, jc.leftTbl, jc.joins)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
return fromClause, joinPool, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
// tasker is the interface for executing a DB task.
|
|
|
|
type tasker interface {
|
|
|
|
// executeTask executes a task against the DB.
|
2023-04-02 22:49:45 +03:00
|
|
|
executeTask(ctx context.Context) error
|
2021-03-08 09:27:35 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// joinCopyTask is a specification of a table data copy task to be performed
|
|
|
|
// for a cross-source join. That is, the data in fromDB.fromTblName will
|
2023-11-19 05:21:14 +03:00
|
|
|
// be copied to a table in toPool. If colNames is
|
2023-11-19 03:05:48 +03:00
|
|
|
// empty, all cols in fromTbl are to be copied.
|
2020-08-06 20:58:47 +03:00
|
|
|
type joinCopyTask struct {
|
2023-11-19 05:21:14 +03:00
|
|
|
fromPool driver.Pool
|
|
|
|
fromTbl tablefq.T
|
|
|
|
toPool driver.Pool
|
|
|
|
toTbl tablefq.T
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
func (jt *joinCopyTask) executeTask(ctx context.Context) error {
|
2023-11-19 05:21:14 +03:00
|
|
|
return execCopyTable(ctx, jt.fromPool, jt.fromTbl, jt.toPool, jt.toTbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// execCopyTable performs the work of copying fromDB.fromTbl to destPool.destTbl.
|
|
|
|
func execCopyTable(ctx context.Context, fromDB driver.Pool, fromTbl tablefq.T,
|
|
|
|
destPool driver.Pool, destTbl tablefq.T,
|
2022-12-18 11:35:59 +03:00
|
|
|
) error {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
2023-04-02 22:49:45 +03:00
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
createTblHook := func(ctx context.Context, originRecMeta record.Meta, destPool driver.Pool,
|
2022-12-18 11:35:59 +03:00
|
|
|
tx sqlz.DB,
|
|
|
|
) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
destColNames := originRecMeta.Names()
|
|
|
|
destColKinds := originRecMeta.Kinds()
|
2023-11-19 03:05:48 +03:00
|
|
|
destTblDef := sqlmodel.NewTableDef(destTbl.Table, destColNames, destColKinds)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
err := destPool.SQLDriver().CreateTable(ctx, tx, destTblDef)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-11-19 05:21:14 +03:00
|
|
|
return errz.Wrapf(err, "failed to create dest table %s.%s", destPool.Source().Handle, destTbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
inserter := NewDBWriter(
|
2023-11-19 05:21:14 +03:00
|
|
|
destPool,
|
2023-11-19 03:05:48 +03:00
|
|
|
destTbl.Table,
|
2023-11-19 05:21:14 +03:00
|
|
|
driver.OptTuningRecChanSize.Get(destPool.Source().Options),
|
2023-05-03 15:36:10 +03:00
|
|
|
createTblHook,
|
|
|
|
)
|
2021-01-04 05:56:22 +03:00
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
query := "SELECT * FROM " + fromTbl.Render(fromDB.SQLDriver().Dialect().Enquote)
|
|
|
|
err := QuerySQL(ctx, fromDB, nil, inserter, query)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-11-19 05:21:14 +03:00
|
|
|
return errz.Wrapf(err, "insert %s.%s failed", destPool.Source().Handle, destTbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := inserter.Wait() // Wait for the writer to finish processing
|
|
|
|
if err != nil {
|
2023-11-19 05:21:14 +03:00
|
|
|
return errz.Wrapf(err, "insert %s.%s failed", destPool.Source().Handle, destTbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-07-03 18:34:19 +03:00
|
|
|
log.Debug("Copied rows to dest", lga.Count, affected,
|
2023-11-19 03:05:48 +03:00
|
|
|
lga.From, fmt.Sprintf("%s.%s", fromDB.Source().Handle, fromTbl),
|
2023-11-19 05:21:14 +03:00
|
|
|
lga.To, fmt.Sprintf("%s.%s", destPool.Source().Handle, destTbl))
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil
|
|
|
|
}
|