Skip to content

Commit

Permalink
Merge pull request #61 from augmentable-dev/helper-func
Browse files Browse the repository at this point in the history
loads a helper function for splitting strings
  • Loading branch information
patrickdevivo authored Oct 8, 2020
2 parents 680c28f + bbf488b commit 9224cc4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/gitqlite/gitqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func init() {
return err
}

err = loadHelperFuncs(conn)
if err != nil {
return err
}

return nil
},
})
Expand Down Expand Up @@ -109,3 +114,20 @@ func (g *GitQLite) ensureTables(options *Options) error {

return nil
}

func loadHelperFuncs(conn *sqlite3.SQLiteConn) error {
// str_split(inputString, splitCharacter, index) string
split := func(s, c string, i int) string {
split := strings.Split(s, c)
if i < len(split) {
return split[i]
}
return ""
}

if err := conn.RegisterFunc("str_split", split, true); err != nil {
return err
}

return nil
}
39 changes: 39 additions & 0 deletions pkg/gitqlite/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gitqlite

import (
"testing"
)

func TestStrSplit(t *testing.T) {
instance, err := New(fixtureRepoDir, &Options{})
if err != nil {
t.Fatal(err)
}

rows, err := instance.DB.Query("SELECT str_split('hello world', ' ', 0)")
if err != nil {
t.Fatal(err)
}
rowNum, contents, err := GetContents(rows)
if err != nil {
t.Fatalf("err %d at row Number %d", err, rowNum)
}

if contents[0][0] != "hello" {
t.Fatalf("expected string: %s, got %s", "hello", contents[0][0])
}


rows, err = instance.DB.Query("SELECT str_split('hello world', ' ', 10)")
if err != nil {
t.Fatal(err)
}
rowNum, contents, err = GetContents(rows)
if err != nil {
t.Fatalf("err %d at row Number %d", err, rowNum)
}

if contents[0][0] != "" {
t.Fatalf("expected string: %s, got %s", "", contents[0][0])
}
}

0 comments on commit 9224cc4

Please sign in to comment.