Skip to content

Commit

Permalink
add support for sql
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanmetin committed Sep 1, 2023
1 parent 67178c6 commit b32809f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
30 changes: 30 additions & 0 deletions base/commands/alias/alias_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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}
Expand Down
26 changes: 15 additions & 11 deletions base/commands/shell_script_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b32809f

Please sign in to comment.