2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newTblCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "tbl",
|
2022-12-25 07:04:18 +03:00
|
|
|
Short: "Useful table actions (copy, truncate, drop)",
|
2021-02-22 10:37:00 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cmd.Help()
|
|
|
|
},
|
2020-08-06 20:58:47 +03:00
|
|
|
Example: ` # Copy table actor to new table actor2
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl copy @sakila_sl3.actor actor2
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# Truncate table actor2
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl truncate @sakila_sl3.actor2
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# Drop table actor2
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl drop @sakila_sl3.actor2`,
|
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 newTblCopyCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
2021-02-22 10:37:00 +03:00
|
|
|
Use: "copy @HANDLE.TABLE NEWTABLE",
|
|
|
|
Short: "Make a copy of a table",
|
|
|
|
Long: `Make a copy of a table in the same database. The table data is also copied by default.`,
|
|
|
|
ValidArgsFunction: completeTblCopy,
|
|
|
|
RunE: execTblCopy,
|
|
|
|
Example: ` # Copy table "actor" in @sakila_sl3 to new table "actor2"
|
|
|
|
$ sq tbl copy @sakila_sl3.actor .actor2
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# Copy table "actor" in active src to table "actor2"
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl copy .actor .actor2
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# Copy table "actor" in active src to generated table name (e.g. "@sakila_sl3.actor_copy__1ae03e9b")
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl copy .actor
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# Copy table structure, but don't copy table data
|
2023-05-22 18:08:14 +03:00
|
|
|
$ sq tbl copy --data=false .actor`,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
addTextFlags(cmd)
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
2023-05-05 20:41:22 +03:00
|
|
|
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().Bool(flag.TblData, true, flag.TblDataUsage)
|
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 execTblCopy(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
|
|
|
if len(args) == 0 || len(args) > 2 {
|
|
|
|
return errz.New("one or two table args required")
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
tblHandles, err := parseTableHandleArgs(ru.DriverRegistry, ru.Config.Collection, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tblHandles[0].tbl == "" {
|
2023-04-02 22:49:45 +03:00
|
|
|
return errz.Errorf("arg {%s} does not specify a table name", args[0])
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch len(tblHandles) {
|
|
|
|
case 1:
|
|
|
|
// Make a copy of the first tbl handle
|
|
|
|
tblHandles = append(tblHandles, tblHandles[0])
|
|
|
|
// But we can't copy the table to itself, so we create a new name
|
|
|
|
tblHandles[1].tbl = stringz.UniqTableName(tblHandles[0].tbl + "_copy")
|
|
|
|
case 2:
|
|
|
|
if tblHandles[1].tbl == "" {
|
|
|
|
tblHandles[1].tbl = stringz.UniqTableName(tblHandles[0].tbl + "_copy")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errz.New("one or two table args required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tblHandles[0].src.Handle != tblHandles[1].src.Handle {
|
|
|
|
return errz.Errorf("tbl copy only works on the same source, but got %s.%s --> %s.%s",
|
|
|
|
tblHandles[0].handle, tblHandles[0].tbl,
|
|
|
|
tblHandles[1].handle, tblHandles[1].tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tblHandles[0].tbl == tblHandles[1].tbl {
|
|
|
|
return errz.Errorf("cannot copy table %s.%s to itself", tblHandles[0].handle, tblHandles[0].tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlDrvr, ok := tblHandles[0].drvr.(driver.SQLDriver)
|
|
|
|
if !ok {
|
2023-04-22 06:36:32 +03:00
|
|
|
return errz.Errorf("driver type {%s} (%s) doesn't support dropping tables", tblHandles[0].src.Type,
|
2022-12-18 05:43:53 +03:00
|
|
|
tblHandles[0].src.Handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
copyData := true // copy data by default
|
2023-04-19 08:28:09 +03:00
|
|
|
if cmdFlagChanged(cmd, flag.TblData) {
|
|
|
|
copyData, err = cmd.Flags().GetBool(flag.TblData)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
if err = applyCollectionOptions(cmd, ru.Config.Collection); err != nil {
|
2023-04-26 18:16:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
var dbase driver.Database
|
2023-05-19 17:24:18 +03:00
|
|
|
dbase, err = ru.Databases.Open(cmd.Context(), tblHandles[0].src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
copied, err := sqlDrvr.CopyTable(cmd.Context(), dbase.DB(), tblHandles[0].tbl, tblHandles[1].tbl, copyData)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "failed tbl copy %s.%s --> %s.%s",
|
|
|
|
tblHandles[0].handle, tblHandles[0].tbl,
|
|
|
|
tblHandles[1].handle, tblHandles[1].tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := fmt.Sprintf("Copied table: %s.%s --> %s.%s",
|
|
|
|
tblHandles[0].handle, tblHandles[0].tbl,
|
|
|
|
tblHandles[1].handle, tblHandles[1].tbl)
|
|
|
|
|
|
|
|
if copyData {
|
|
|
|
switch copied {
|
|
|
|
case 1:
|
|
|
|
msg += " (1 row copied)"
|
|
|
|
default:
|
|
|
|
msg += fmt.Sprintf(" (%d rows copied)", copied)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
fmt.Fprintln(ru.Out, msg)
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newTblTruncateCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
2021-02-22 10:37:00 +03:00
|
|
|
Use: "truncate @HANDLE.TABLE|.TABLE",
|
2020-08-06 20:58:47 +03:00
|
|
|
Short: "Truncate one or more tables",
|
2021-02-22 10:37:00 +03:00
|
|
|
Long: `Truncate one or more tables. Note that this command
|
|
|
|
only applies to SQL sources.`,
|
|
|
|
RunE: execTblTruncate,
|
|
|
|
ValidArgsFunction: (&handleTableCompleter{
|
|
|
|
onlySQL: true,
|
|
|
|
}).complete,
|
2020-08-06 20:58:47 +03:00
|
|
|
Example: ` # truncate table "actor"" in source @sakila_sl3
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl truncate @sakila_sl3.actor
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# truncate table "payment"" in the active src
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl truncate .payment
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# truncate multiple tables
|
2023-05-22 18:08:14 +03:00
|
|
|
$ sq tbl truncate .payment @sakila_sl3.actor`,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
addTextFlags(cmd)
|
2023-04-19 08:28:09 +03:00
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
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 execTblTruncate(cmd *cobra.Command, args []string) (err error) {
|
2023-05-19 17:24:18 +03:00
|
|
|
ru := run.FromContext(cmd.Context())
|
2020-08-06 20:58:47 +03:00
|
|
|
var tblHandles []tblHandle
|
2023-05-19 17:24:18 +03:00
|
|
|
tblHandles, err = parseTableHandleArgs(ru.DriverRegistry, ru.Config.Collection, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
if err = applyCollectionOptions(cmd, ru.Config.Collection); err != nil {
|
2023-04-26 18:16:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
for _, tblH := range tblHandles {
|
|
|
|
var affected int64
|
2021-02-22 10:37:00 +03:00
|
|
|
affected, err = tblH.drvr.Truncate(cmd.Context(), tblH.src, tblH.tbl, true)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := fmt.Sprintf("Truncated %d row(s) from %s.%s", affected, tblH.src.Handle, tblH.tbl)
|
|
|
|
msg = stringz.Plu(msg, int(affected))
|
2023-05-19 17:24:18 +03:00
|
|
|
fmt.Fprintln(ru.Out, msg)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newTblDropCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "drop @HANDLE.TABLE",
|
|
|
|
Short: "Drop one or more tables",
|
2021-02-22 10:37:00 +03:00
|
|
|
Long: `Drop one or more tables. Note that this command
|
|
|
|
only applies to SQL sources.`,
|
|
|
|
RunE: execTblDrop,
|
|
|
|
ValidArgsFunction: (&handleTableCompleter{
|
|
|
|
onlySQL: true,
|
|
|
|
}).complete,
|
2020-08-06 20:58:47 +03:00
|
|
|
Example: `# drop table "actor" in src @sakila_sl3
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl drop @sakila_sl3.actor
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# drop table "payment"" in the active src
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq tbl drop .payment
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# drop multiple tables
|
2023-05-22 18:08:14 +03:00
|
|
|
$ sq drop .payment @sakila_sl3.actor`,
|
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 execTblDrop(cmd *cobra.Command, args []string) (err error) {
|
2023-05-19 17:24:18 +03:00
|
|
|
ru := run.FromContext(cmd.Context())
|
2020-08-06 20:58:47 +03:00
|
|
|
var tblHandles []tblHandle
|
2023-05-19 17:24:18 +03:00
|
|
|
tblHandles, err = parseTableHandleArgs(ru.DriverRegistry, ru.Config.Collection, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
if err = applyCollectionOptions(cmd, ru.Config.Collection); err != nil {
|
2023-04-26 18:16:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
for _, tblH := range tblHandles {
|
|
|
|
sqlDrvr, ok := tblH.drvr.(driver.SQLDriver)
|
|
|
|
if !ok {
|
2023-04-22 06:36:32 +03:00
|
|
|
return errz.Errorf("driver type {%s} (%s) doesn't support dropping tables", tblH.src.Type, tblH.src.Handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var dbase driver.Database
|
2023-05-19 17:24:18 +03:00
|
|
|
dbase, err = ru.Databases.Open(cmd.Context(), tblH.src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-22 10:37:00 +03:00
|
|
|
err = sqlDrvr.DropTable(cmd.Context(), dbase.DB(), tblH.tbl, false)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
fmt.Fprintf(ru.Out, "Dropped table %s.%s\n", tblH.src.Handle, tblH.tbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTableHandleArgs parses args of the form:
|
|
|
|
//
|
2022-12-17 01:54:09 +03:00
|
|
|
// @HANDLE1.TABLE1 .TABLE2 .TABLE3 @HANDLE2.TABLE4 .TABLEN
|
2020-08-06 20:58:47 +03:00
|
|
|
//
|
|
|
|
// It returns a slice of tblHandle, one for each arg. If an arg
|
|
|
|
// does not have a HANDLE, the active src is assumed: it's an error
|
|
|
|
// if no active src. It is also an error if len(args) is zero.
|
2023-04-19 08:28:09 +03:00
|
|
|
func parseTableHandleArgs(dp driver.Provider, coll *source.Collection, args []string) ([]tblHandle, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if len(args) == 0 {
|
|
|
|
return nil, errz.New(msgInvalidArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tblHandles []tblHandle
|
2023-04-19 08:28:09 +03:00
|
|
|
activeSrc := coll.Active()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// We iterate over the args several times, because we want
|
|
|
|
// to present error checks consistently.
|
|
|
|
for _, arg := range args {
|
|
|
|
handle, tbl, err := source.ParseTableHandle(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tblHandles = append(tblHandles, tblHandle{
|
|
|
|
handle: handle,
|
|
|
|
tbl: tbl,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tblHandles {
|
|
|
|
if tblHandles[i].tbl == "" {
|
2023-04-02 22:49:45 +03:00
|
|
|
return nil, errz.Errorf("arg[%d] {%s} doesn't specify a table", i, args[i])
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if tblHandles[i].handle == "" {
|
|
|
|
// It's a table name without a handle, so we use the active src
|
|
|
|
if activeSrc == nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
return nil, errz.Errorf("arg[%d] {%s} doesn't specify a handle and there's no active source",
|
2020-08-06 20:58:47 +03:00
|
|
|
i, args[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
tblHandles[i].handle = activeSrc.Handle
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
src, err := coll.Get(tblHandles[i].handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
drvr, err := dp.DriverFor(src.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tblHandles[i].src = src
|
|
|
|
tblHandles[i].drvr = drvr
|
|
|
|
}
|
|
|
|
|
|
|
|
return tblHandles, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tblHandle represents a @HANDLE.TABLE, with the handle's associated
|
|
|
|
// src and driver.
|
|
|
|
type tblHandle struct {
|
|
|
|
handle string
|
|
|
|
tbl string
|
|
|
|
src *source.Source
|
|
|
|
drvr driver.Driver
|
|
|
|
}
|