Skip to content

Commit

Permalink
Merge pull request #516 from codenotary/doc/golang-sdk
Browse files Browse the repository at this point in the history
Update Golang SDK doc
  • Loading branch information
ostafen authored May 20, 2024
2 parents 8d39395 + 1bc4d5e commit 803687d
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion src/master/develop/sql/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,82 @@ To commit a transaction, you must call the `Commit()` method.
:::: tabs

::: tab Go
<<< @/src/code-examples/go/develop-sql-transactions/main.go

```golang
package main

import (
"context"
"fmt"
"log"

immudb "github.com/codenotary/immudb/pkg/client"
)

func handleErr(err error) {
if err != nil {
log.Fatal(err)
}
}

func main() {
opts := immudb.DefaultOptions().
WithAddress("localhost").
WithPort(3322)

client := immudb.NewClient().WithOptions(opts)
err := immudb.OpenSession(
context.TODO(),
[]byte(`immudb`),
[]byte(`immudb`),
"defaultdb",
)
handleErr(err)

defer client.CloseSession(context.TODO())

tx, err := client.NewTx(context.TODO())
handleErr(err)

err = tx.SQLExec(
context.TODO(),
`CREATE TABLE IF NOT EXISTS mytable(id INTEGER AUTO_INCREMENT, title VARCHAR[256], active BOOLEAN, PRIMARY KEY id);`,
nil,
)
handleErr(err)

nRows := 10
for i := 0; i < nRows; i++ {
err := tx.SQLExec(
context.TODO(),
"INSERT INTO mytable(title, active) VALUES (@title, @active)",
map[string]interface{}{
"title": fmt.Sprintf("title%d", i),
"active": i%2 == 0,
},
)
handleErr(err)
}

txh, err := tx.Commit(context.TODO())
handleErr(err)

fmt.Printf("Successfully committed rows %d\n", txh.UpdatedRows)

reader, err := client.SQLQueryReader(context.TODO(), "SELECT * FROM mytable", nil)
handleErr(err)

for reader.Next() {
row, err := reader.Read()
handleErr(err)

fmt.Println(row[0], row[1])
}
}
```
:::


::: tab Java
```java
package io.codenotary.immudb.helloworld;
Expand Down

0 comments on commit 803687d

Please sign in to comment.