-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from vladlyt/master
Add SQL bindings support
- Loading branch information
Showing
7 changed files
with
548,125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package sql | ||
|
||
//#include "tree_sitter/parser.h" | ||
//TSLanguage *tree_sitter_sql(); | ||
import "C" | ||
import ( | ||
"unsafe" | ||
|
||
sitter "github.com/smacker/go-tree-sitter" | ||
) | ||
|
||
func GetLanguage() *sitter.Language { | ||
ptr := unsafe.Pointer(C.tree_sitter_sql()) | ||
return sitter.NewLanguage(ptr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sql_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
sitter "github.com/smacker/go-tree-sitter" | ||
"github.com/smacker/go-tree-sitter/sql" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const code = `SELECT * FROM my_table | ||
WHERE count > 1 | ||
ORDER BY time DESC | ||
LIMIT 5;` | ||
|
||
const expected = `(program (statement (select (keyword_select) (select_expression (term value: (all_fields)))) (from (keyword_from) (relation (object_reference name: (identifier))) (where (keyword_where) predicate: (binary_expression left: (field name: (identifier)) right: (literal))) (order_by (keyword_order) (keyword_by) (order_target (field name: (identifier)) (direction (keyword_desc)))) (limit (keyword_limit) (literal)))))` | ||
|
||
func TestGrammar(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
n, err := sitter.ParseCtx(context.Background(), []byte(code), sql.GetLanguage()) | ||
assert.NoError(err) | ||
assert.Equal( | ||
expected, | ||
n.String(), | ||
) | ||
} |
Oops, something went wrong.