2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/neilotoole/sq/cli/run"
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
2023-04-30 17:18:56 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgm"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2020-08-24 05:32:59 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/output"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newSQLCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "sql QUERY|STMT",
|
|
|
|
Short: "Execute DB-native SQL query or statement",
|
2020-10-20 18:05:43 +03:00
|
|
|
Long: `Execute a SQL query or statement against the active source using the
|
|
|
|
source's SQL dialect. Use flag --src=@HANDLE to specify an alternative
|
|
|
|
source.
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
If flag --query is set, sq will run the input as a query
|
2023-03-15 10:43:48 +03:00
|
|
|
(SELECT) and return the query rows. If flag --exec is set,
|
2020-08-06 20:58:47 +03:00
|
|
|
sq will execute the input and return the result. If neither
|
2020-10-20 18:05:43 +03:00
|
|
|
flag is set, sq attempts to determine the appropriate mode.`,
|
2021-02-22 10:37:00 +03:00
|
|
|
RunE: execSQL,
|
2020-08-06 20:58:47 +03:00
|
|
|
Example: ` # Select from active source
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq sql 'SELECT * FROM actor'
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# Select from a specified source
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq sql --src=@sakila_pg12 'SELECT * FROM actor'
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# Drop table @sakila_pg12.actor
|
2023-03-15 10:43:48 +03:00
|
|
|
$ sq sql --exec --src=@sakila_pg12 'DROP TABLE actor'
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2020-10-20 18:05:43 +03:00
|
|
|
# Select from active source and write results to @sakila_ms17.actor
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq sql 'SELECT * FROM actor' --insert=@sakila_ms17.actor`,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addQueryCmdFlags(cmd)
|
|
|
|
|
|
|
|
// User explicitly wants to execute the SQL using sql.DB.Query
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().Bool(flag.SQLQuery, false, flag.SQLQueryUsage)
|
2020-08-06 20:58:47 +03:00
|
|
|
// User explicitly wants to execute the SQL using sql.DB.Exec
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().Bool(flag.SQLExec, false, flag.SQLExecUsage)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
return cmd
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func execSQL(cmd *cobra.Command, args []string) error {
|
2023-05-19 17:24:18 +03:00
|
|
|
ru := run.FromContext(cmd.Context())
|
2020-08-06 20:58:47 +03:00
|
|
|
switch len(args) {
|
|
|
|
default:
|
|
|
|
return errz.New("a single query string is required")
|
|
|
|
case 0:
|
2023-04-26 18:16:42 +03:00
|
|
|
return errz.New("no SQL query string")
|
2020-08-06 20:58:47 +03:00
|
|
|
case 1:
|
|
|
|
if strings.TrimSpace(args[0]) == "" {
|
|
|
|
return errz.New("empty SQL query string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
err := determineSources(cmd.Context(), ru)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
coll := ru.Config.Collection
|
2020-08-06 20:58:47 +03:00
|
|
|
// activeSrc is guaranteed to be non-nil after
|
|
|
|
// determineSources successfully returns.
|
2023-04-19 08:28:09 +03:00
|
|
|
activeSrc := coll.Active()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
if err = applySourceOptions(cmd, activeSrc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
if !cmdFlagChanged(cmd, flag.Insert) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// The user didn't specify the --insert=@src.tbl flag,
|
|
|
|
// so we just want to print the records.
|
2023-05-19 17:24:18 +03:00
|
|
|
return execSQLPrint(cmd.Context(), ru, activeSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instead of printing the records, they will be
|
|
|
|
// written to another database
|
2023-04-19 08:28:09 +03:00
|
|
|
insertTo, _ := cmd.Flags().GetString(flag.Insert)
|
2020-08-06 20:58:47 +03:00
|
|
|
if insertTo == "" {
|
2023-04-19 08:28:09 +03:00
|
|
|
return errz.Errorf("invalid --%s value: empty", flag.Insert)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
destHandle, destTbl, err := source.ParseTableHandle(insertTo)
|
|
|
|
if err != nil {
|
2023-04-19 08:28:09 +03:00
|
|
|
return errz.Wrapf(err, "invalid --%s value", flag.Insert)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
destSrc, err := coll.Get(destHandle)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return execSQLInsert(cmd.Context(), ru, activeSrc, destSrc, destTbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// execSQLPrint executes the SQL and prints resulting records
|
|
|
|
// to the configured writer.
|
2023-05-19 17:24:18 +03:00
|
|
|
func execSQLPrint(ctx context.Context, ru *run.Run, fromSrc *source.Source) error {
|
|
|
|
args := ru.Args
|
|
|
|
dbase, err := ru.Databases.Open(ctx, fromSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
recw := output.NewRecordWriterAdapter(ctx, ru.Writers.Record)
|
2023-04-02 22:49:45 +03:00
|
|
|
err = libsq.QuerySQL(ctx, dbase, recw, args[0])
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = recw.Wait() // Wait for the writer to finish processing
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// execSQLInsert executes the SQL and inserts resulting records
|
|
|
|
// into destTbl in destSrc.
|
2023-05-19 17:24:18 +03:00
|
|
|
func execSQLInsert(ctx context.Context, ru *run.Run,
|
|
|
|
fromSrc, destSrc *source.Source, destTbl string,
|
|
|
|
) error {
|
|
|
|
args := ru.Args
|
|
|
|
dbases := ru.Databases
|
2021-02-22 10:37:00 +03:00
|
|
|
ctx, cancelFn := context.WithCancel(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
fromDB, err := dbases.Open(ctx, fromSrc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
destDB, err := dbases.Open(ctx, destSrc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: We don't need to worry about closing fromDB and
|
|
|
|
// destDB because they are closed by dbases.Close, which
|
2023-05-19 17:24:18 +03:00
|
|
|
// is invoked by ru.Close, and ru is closed further up the
|
2020-08-06 20:58:47 +03:00
|
|
|
// stack.
|
|
|
|
|
2021-01-04 05:56:22 +03:00
|
|
|
inserter := libsq.NewDBWriter(
|
|
|
|
destDB,
|
|
|
|
destTbl,
|
2023-05-03 15:36:10 +03:00
|
|
|
driver.OptTuningRecChanSize.Get(destSrc.Options),
|
2021-01-04 05:56:22 +03:00
|
|
|
libsq.DBWriterCreateTableIfNotExistsHook(destTbl),
|
|
|
|
)
|
2023-04-02 22:49:45 +03:00
|
|
|
err = libsq.QuerySQL(ctx, fromDB, inserter, args[0])
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
return errz.Wrapf(err, "insert to {%s} failed", source.Target(destSrc, destTbl))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := inserter.Wait() // Wait for the writer to finish processing
|
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "insert %s.%s failed", destSrc.Handle, destTbl)
|
|
|
|
}
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
lg.FromContext(ctx).Debug(lgm.RowsAffected, lga.Count, affected)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
// TODO: Should really use a Printer here
|
2023-05-19 17:24:18 +03:00
|
|
|
fmt.Fprintf(ru.Out, stringz.Plu("Inserted %d row(s) into %s\n",
|
2023-04-02 22:49:45 +03:00
|
|
|
int(affected)), affected, source.Target(destSrc, destTbl))
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil
|
|
|
|
}
|