Skip to content

feat: improve Go snippets in connect to DB dialog #2268

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion src/components/ConnectToDB/ConnectToDBDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const connectionTabs: {id: SnippetLanguage; title: string}[] = [
{id: 'bash', title: 'Bash'},
{id: 'cpp', title: 'C++'},
{id: 'csharp', title: 'C# (.NET)'},
{id: 'go', title: 'Go'},
{id: 'go_native_sdk', title: 'Go (native SDK)'},
{id: 'go_database_sql', title: 'Go (database/sql)'},
{id: 'java', title: 'Java'},
{id: 'javascript', title: 'Node JS'},
{id: 'php', title: 'PHP'},
Expand Down
5 changes: 4 additions & 1 deletion src/components/ConnectToDB/getDocsLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export function getDocsLink(snippetLang: SnippetLanguage) {
case 'csharp': {
return i18n('docs_dotnet');
}
case 'go': {
case 'go_native_sdk': {
return i18n('docs_go');
}
case 'go_database_sql': {
return i18n('docs_go');
}
case 'java': {
Expand Down
106 changes: 60 additions & 46 deletions src/components/ConnectToDB/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,69 @@ using var driver = new Driver(
await driver.Initialize();`;
}

export function getGoSnippetCode({database, endpoint}: SnippetParams) {
export function getGoNativeSdkSnippetCode({database, endpoint}: SnippetParams) {
return `package main

import (
"context"
"os"
"context"
"os"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := ydb.Open(ctx,
"${endpoint ?? '<endpoint>'}${database ?? '/<database>'}",
ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
)
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

db, err := ydb.Open(context.Background(),
"${endpoint ?? '<endpoint>'}${database ?? '/<database>'}",
ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
)
if err != nil {
panic(err)
}

defer db.Close(ctx)

row, err := db.Query().QueryRow(ctx, "SELECT 'Hello, world!'")
if err != nil {
panic(err)
}

var val string
if err := row.Scan(&val); err != nil {
panic(err)
}

println(val)
}`;
}

defer db.Close(ctx)

err = db.Table().Do(ctx,
func(ctx context.Context, s table.Session) error {
_, res, err := s.Execute(
ctx,
table.TxControl(table.BeginTx(table.WithOnlineReadOnly()), table.CommitTx()),
"SELECT 'Hello, world!'",
nil,
)
if err != nil {
return err
}
defer res.Close()
var val string

for res.NextResultSet(ctx) {
for res.NextRow() {
err = res.Scan(&val)
if err != nil {
return err
}
println(val)
}
}
return res.Err()
})
if err != nil {
panic(err)
}
export function getGoDatabaseSqlSnippetCode({database, endpoint}: SnippetParams) {
return `package main

import (
"context"
"database/sql"
"os"

_ "github.com/ydb-platform/ydb-go-sdk/v3"
)

func main() {
db, err := sql.Open("ydb", "${endpoint ?? '<endpoint>'}${database ?? '/<database>'}"+"?token="+os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS"))
if err != nil {
panic(err)
}

row := db.QueryRowContext(context.Background(), "SELECT 'Hello, world!'")

var val string
if err := row.Scan(&val); err != nil {
panic(err)
}

println(val)
}`;
}

Expand Down Expand Up @@ -212,8 +223,11 @@ export function getSnippetCode(lang: SnippetLanguage, rawParams: SnippetParams)
case 'csharp': {
return getCSharpSnippetCode(params);
}
case 'go': {
return getGoSnippetCode(params);
case 'go_native_sdk': {
return getGoNativeSdkSnippetCode(params);
}
case 'go_database_sql': {
return getGoDatabaseSqlSnippetCode(params);
}
case 'java': {
return getJavaSnippetCode(params);
Expand Down
3 changes: 2 additions & 1 deletion src/components/ConnectToDB/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export type SnippetLanguage =
| 'bash'
| 'cpp'
| 'csharp'
| 'go'
| 'go_native_sdk'
| 'go_database_sql'
| 'java'
| 'javascript'
| 'php'
Expand Down
Loading