Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Golang SDK doc #516

Merged
merged 1 commit into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading