Skip to content

Commit

Permalink
Merge ce6000b into e11a354
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Apr 10, 2022
2 parents e11a354 + ce6000b commit 1fa7839
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/pgengine/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import (
"os"
)

func (pge *PgEngine) CopyToFile(ctx context.Context, filename string, sql string) (int64, error) {
dbconn, err := pge.ConfigDb.Acquire(ctx)
if err != nil {
return -1, err
}
defer dbconn.Release()
f, err := os.Create(filename)
defer func() { _ = f.Close() }()
if err != nil {
return -1, err
}
res, err := dbconn.Conn().PgConn().CopyTo(ctx, f, sql)
return res.RowsAffected(), err
}

// CopyFromFile copies data from local file into database using COPY format specified by sql
func (pge *PgEngine) CopyFromFile(ctx context.Context, filename string, sql string) (int64, error) {
dbconn, err := pge.ConfigDb.Acquire(ctx)
Expand Down
17 changes: 17 additions & 0 deletions internal/scheduler/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Tasks = map[string](func(context.Context, *Scheduler, string) (string, error
"SendMail": taskSendMail,
"Download": taskDownload,
"CopyFromFile": taskCopyFromFile,
"CopyToFile": taskCopyToFile,
"Shutdown": taskShutdown}

func (sch *Scheduler) executeTask(ctx context.Context, name string, paramValues []string) (stdout string, err error) {
Expand Down Expand Up @@ -85,6 +86,22 @@ func taskCopyFromFile(ctx context.Context, sch *Scheduler, val string) (stdout s
return stdout, err
}

func taskCopyToFile(ctx context.Context, sch *Scheduler, val string) (stdout string, err error) {
type copyTo struct {
SQL string `json:"sql"`
Filename string `json:"filename"`
}
var ct copyTo
if err := json.Unmarshal([]byte(val), &ct); err != nil {
return "", err
}
count, err := sch.pgengine.CopyToFile(ctx, ct.Filename, ct.SQL)
if err == nil {
stdout = fmt.Sprintf("%d rows copied to %s", count, ct.Filename)
}
return stdout, err
}

func taskDownload(ctx context.Context, sch *Scheduler, paramValues string) (stdout string, err error) {
type downloadOpts struct {
WorkersNum int `json:"workersnum"`
Expand Down

0 comments on commit 1fa7839

Please sign in to comment.