2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/neilotoole/sq/cli/run"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2024-01-25 09:29:55 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/drivertype"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: dump all this "internal" stuff: make the options as follows: @HANDLE, file, memory
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newScratchCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "scratch [@HANDLE|internal|internal:file|internal:mem|@scratch]",
|
2020-08-09 16:46:46 +03:00
|
|
|
// This command is likely to be ditched in favor of a generalized "config" cmd
|
|
|
|
// such as "sq config scratchdb=@my1"
|
|
|
|
Hidden: true,
|
2021-02-22 10:37:00 +03:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: execScratch,
|
2020-08-06 20:58:47 +03:00
|
|
|
Example: ` # get scratch data source
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq scratch
|
2020-08-06 20:58:47 +03:00
|
|
|
# set @my1 as scratch data source
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq scratch @my1
|
2020-08-06 20:58:47 +03:00
|
|
|
# use the default embedded db
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq scratch internal
|
2020-08-06 20:58:47 +03:00
|
|
|
# explicitly specify use of embedded file db
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq scratch internal:file
|
2020-08-06 20:58:47 +03:00
|
|
|
# explicitly specify use of embedded memory db
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq scratch internal:mem
|
2020-08-06 20:58:47 +03:00
|
|
|
# restore default scratch db (equivalent to "internal")
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq scratch @scratch`,
|
2020-08-06 20:58:47 +03:00
|
|
|
Short: "Get or set scratch data source",
|
|
|
|
Long: `Get or set scratch data source. The scratch db is used internally by sq for multiple purposes such as
|
|
|
|
importing non-SQL data, or cross-database joins. If no argument provided, get the current scratch data
|
|
|
|
source. Otherwise, set @HANDLE or an internal db as the scratch data source. The reserved handle "@scratch" resets the
|
|
|
|
`,
|
|
|
|
}
|
2024-02-09 19:08:39 +03:00
|
|
|
cmdMarkRequiresConfigLock(cmd)
|
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 execScratch(cmd *cobra.Command, args []string) error {
|
2023-05-19 17:24:18 +03:00
|
|
|
ru := run.FromContext(cmd.Context())
|
|
|
|
cfg := ru.Config
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
var src *source.Source
|
|
|
|
var err error
|
|
|
|
defaultScratch := &source.Source{
|
|
|
|
Handle: source.ScratchHandle,
|
|
|
|
Location: "internal:file",
|
2024-01-25 09:29:55 +03:00
|
|
|
Type: drivertype.SQLite,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
// Print the scratch src
|
2023-04-19 08:28:09 +03:00
|
|
|
src = cfg.Collection.Scratch()
|
2020-08-06 20:58:47 +03:00
|
|
|
if src == nil {
|
|
|
|
src = defaultScratch
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return ru.Writers.Source.Source(cfg.Collection, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-18 04:28:11 +03:00
|
|
|
// Set the scratch src
|
2020-08-06 20:58:47 +03:00
|
|
|
switch args[0] {
|
|
|
|
case "internal", "internal:file", "internal:mem":
|
|
|
|
// TODO: currently only supports file sqlite3 db, fairly trivial to do mem as well
|
2023-04-19 08:28:09 +03:00
|
|
|
_, _ = cfg.Collection.SetScratch("")
|
2020-08-06 20:58:47 +03:00
|
|
|
src = defaultScratch
|
|
|
|
default:
|
2023-04-19 08:28:09 +03:00
|
|
|
src, err = cfg.Collection.SetScratch(args[0])
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
err = ru.ConfigStore.Save(cmd.Context(), ru.Config)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return ru.Writers.Source.Source(cfg.Collection, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|