Skip to content

Commit 1a04de3

Browse files
committed
Update README.md to include example usage of GoSQLX and enhance resource management
1 parent 287f0ba commit 1a04de3

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

README.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,62 @@ go get github.com/ajitpratap0/GoSQLX
2121

2222
## Usage
2323

24+
Below is an example of how to use GoSQLX in your Go application. This demonstrates the basic workflow of tokenizing SQL, parsing it into an AST, and properly managing resources with the object pools.
25+
2426
```go
27+
// Example usage of GoSQLX
2528
package main
2629

2730
import (
31+
"fmt"
32+
33+
// Import the required GoSQLX packages
2834
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
2935
"github.com/ajitpratap0/GoSQLX/pkg/sql/parser"
3036
"github.com/ajitpratap0/GoSQLX/pkg/sql/tokenizer"
37+
"github.com/ajitpratap0/GoSQLX/pkg/sql/token"
38+
"github.com/ajitpratap0/GoSQLX/pkg/sql/models"
3139
)
3240

3341
func main() {
34-
// Get a tokenizer from the pool
42+
// 1. Get a tokenizer from the pool
3543
tkz := tokenizer.GetTokenizer()
3644
defer tokenizer.PutTokenizer(tkz) // Return to pool when done
3745

38-
// Tokenize the SQL query
46+
// 2. Tokenize the SQL query
3947
sql := []byte("SELECT id, name FROM users WHERE age > 18")
4048
tokens, err := tkz.Tokenize(sql)
4149
if err != nil {
4250
panic(err)
4351
}
52+
fmt.Printf("Tokenized SQL into %d tokens\n", len(tokens))
4453

45-
// Create a parser
54+
// 3. Convert TokenWithSpan to token.Token for the parser
55+
tokenSlice := make([]token.Token, len(tokens))
56+
for i, t := range tokens {
57+
tokenSlice[i] = t.Token
58+
}
59+
60+
// 4. Create a parser
4661
p := parser.NewParser()
4762
defer p.Release() // Clean up resources
4863

49-
// Parse tokens into an AST
50-
ast, err := p.Parse(tokens)
64+
// 5. Parse tokens into an AST
65+
result, err := p.Parse(tokenSlice)
5166
if err != nil {
5267
panic(err)
5368
}
5469

55-
// Work with the AST
56-
// ...
70+
// 6. Work with the AST
71+
fmt.Printf("Parsed %d statements\n", len(result.Statements))
5772

58-
// Return AST to the pool when done
59-
ast.ReleaseAST(ast)
73+
// 7. Return AST to the pool when done
74+
ast.ReleaseAST(result)
6075
}
6176
```
6277

78+
For more detailed examples, see the [examples directory](examples/).
79+
6380
## Project Structure
6481

6582
```

0 commit comments

Comments
 (0)