diff --git a/base/commands/alias/alias_it_test.go b/base/commands/alias/alias_it_test.go index 6d2df06a..ac8f39ab 100644 --- a/base/commands/alias/alias_it_test.go +++ b/base/commands/alias/alias_it_test.go @@ -19,6 +19,7 @@ func TestAlias(t *testing.T) { f func(t *testing.T) }{ {name: "Execute_Interactive", f: Execute_InteractiveTest}, + {name: "ExecuteSQL_Interactive", f: ExecuteSQL_InteractiveTest}, {name: "Add_Interactive", f: Add_InteractiveTest}, {name: "Remove_Interactive", f: Remove_InteractiveTest}, {name: "List_Interactive", f: List_InteractiveTest}, @@ -43,6 +44,35 @@ func Execute_InteractiveTest(t *testing.T) { }) } +func ExecuteSQL_InteractiveTest(t *testing.T) { + tcx := it.TestContext{T: t} + tcx.Tester(func(tcx it.TestContext) { + ctx := context.Background() + name := it.NewUniqueObjectName("table") + alias.Aliases.Store("sqlAlias", fmt.Sprintf(`SELECT * FROM "%s" ORDER BY __key;`+"\n", name)) + tcx.WithShell(ctx, func(tcx it.TestContext) { + tcx.WithReset(func() { + tcx.WriteStdinf(` + CREATE MAPPING "%s" ( + __key INT, + this VARCHAR + ) TYPE IMAP OPTIONS ( + 'keyFormat' = 'int', + 'valueFormat' = 'varchar' + );`+"\n", name) + tcx.WriteStdinf(` + INSERT INTO "%s" (__key, this) VALUES (10, 'foo'), (20, 'bar'); + `+"\n", name) + tcx.WriteStdinString("@sqlAlias\n") + tcx.AssertStdoutContains("10") + tcx.AssertStdoutContains("foo") + tcx.AssertStdoutContains("20") + tcx.AssertStdoutContains("bar") + }) + }) + }) +} + func Add_InteractiveTest(t *testing.T) { ctx := context.TODO() tcx := it.TestContext{T: t} diff --git a/base/commands/shell_script_common.go b/base/commands/shell_script_common.go index 28f3b909..4f9e2752 100644 --- a/base/commands/shell_script_common.go +++ b/base/commands/shell_script_common.go @@ -95,18 +95,22 @@ func convertAliasToCmd(text string) (string, error) { parts := strings.Split(text, " ") name := parts[0] suffix := strings.Join(parts[1:], " ") - data, err := os.ReadFile(filepath.Join(paths.Home(), alias.AliasFileName)) - if err != nil { - if os.IsNotExist(err) { - return "", fmt.Errorf("alias not found: %s", name) + if v, ok := alias.Aliases.Load(name); ok { // find from memory + return v.(string), nil + } else { // find from shell.clc + data, err := os.ReadFile(filepath.Join(paths.Home(), alias.AliasFileName)) + if err != nil { + if os.IsNotExist(err) { + return "", fmt.Errorf("alias not found: %s", name) + } + return "", err } - return "", err - } - lines := strings.Split(string(data), "\n") - for _, line := range lines { - p := strings.SplitN(line, "=", 2) - if len(p) == 2 && p[0] == name { - return fmt.Sprintf("%s%s %s", shell.CmdPrefix, p[1], suffix), nil + lines := strings.Split(string(data), "\n") + for _, line := range lines { + p := strings.SplitN(line, "=", 2) + if len(p) == 2 && p[0] == name { + return fmt.Sprintf("%s %s", p[1], suffix), nil + } } } return "", fmt.Errorf("alias not found: %s", name)