Skip to content

Commit fac5923

Browse files
committed
database/sql docs
1 parent a8e2d1b commit fac5923

File tree

1 file changed

+102
-8
lines changed

1 file changed

+102
-8
lines changed

SQL.md

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Package `ydb-go-sdk` provides usage `database/sql` API also.
1111
* [On database object](#queries-db)
1212
* [With transaction](#queries-tx)
1313
3. [Query modes (DDL, DML, DQL, etc.)](#query-modes)
14+
4. [Retryers for `YDB` `database/sql` driver](#retry)
15+
* [Over `sql.Conn` object](#retry-conn)
16+
* [Over `sql.Tx`](#retry-tx)
1417

1518
## Initialization of `database/sql` driver <a name="init"></a>
1619

@@ -78,14 +81,13 @@ for rows.Next() { // iterate over rows
7881
if err = rows.Err(); err != nil { // always check final rows err
7982
log.Fatal(err)
8083
}
81-
8284
```
8385

8486
### With transaction <a name="queries-tx"></a>
85-
Supports only `default` transaction options which mapped to `YDB`'s `SerializableReadWrite` transaction settings.
87+
Supports only `default` transaction options which mapped to `YDB` `SerializableReadWrite` transaction settings.
8688
`YDB`'s `OnlineReadOnly` and `StaleReadOnly` transaction settings are not compatible with interactive transactions such as `database/sql`'s `*sql.Tx`.
8789
`YDB`'s `OnlineReadOnly` and `StaleReadOnly` transaction settings can be explicitly applied to each query outside interactive transaction (see more in [Isolation levels support](#tx-control))
88-
```
90+
```go
8991
tx, err := db.BeginTx(ctx, sql.TxOptions{})
9092
if err != nil {
9193
log.Fatal(err)
@@ -120,7 +122,7 @@ if err = tx.Commit(); err != nil {
120122
## Query modes (DDL, DML, DQL, etc.) <a name="query-modes"></a>
121123
The `YDB` server API is currently requires to select a specific method by specific request type. For example, `DDL` must be called with `table.session.ExecuteSchemeQuery`, `DML` must be called with `table.session.Execute`, `DQL` may be called with `table.session.Execute` or `table.session.StreamExecuteScanQuery` etc. `YDB` have a `scripting` service also, which provides different query types with single method, but not supports transactions.
122124

123-
Thats why needs to select query mode on client side currently.
125+
That's why needs to select query mode on client side currently.
124126

125127
`YDB` team have a roadmap goal to implements a single method for executing different query types.
126128

@@ -132,16 +134,108 @@ Thats why needs to select query mode on client side currently.
132134
* `ydb.ScriptingQueryMode` - for `DDL`, `DML`, `DQL` queries (not a `TCL`). Be careful: queries executes longer than with other query modes and consumes bigger server-side resources
133135

134136
Example for changing default query mode:
135-
```
137+
```go
136138
res, err = db.ExecContext(ydb.WithQueryMode(ctx, ydb.SchemeQueryMode),
137139
"DROP TABLE `/full/path/to/table/series`",
138140
)
139141
```
140142

141143
## Specify `YDB` transaction control <a name="tx-control"></a>
142144

143-
## Retryer's for `YDB` `database/sql` driver
145+
Default `YDB`'s transaction control is a `SerializableReadWrite`.
146+
Default transaction control outside interactive transactions may be changed with context:
147+
```
148+
ctx := ydb.WithTxControl(ctx, table.OnlineReadOnlyTxControl())
149+
```
150+
151+
## Retryers for `YDB` `database/sql` driver <a name="retry"></a>
152+
153+
`YDB` is a distributed `RDBMS` with non-stop 24/7 releases flow.
154+
It means some nodes mey be not available for queries.
155+
Also network errors may be occurred.
156+
That's why some queries may be finished with errors.
157+
Most of those errors are transient.
158+
`ydb-go-sdk`'s "knows" what to do on specific error: retry or not, with or without backoff, with or whithout deleting session, etc.
159+
`ydb-go-sdk` provides retry helpers for two popular cases:
160+
* on plain database object
161+
* or on interactive transaction object
162+
163+
### Over `sql.Conn` object <a name="retry-conn"></a>
164+
165+
`retry.Do` helper allow custom lambda, which must return error for processing or nil if retry operation is ok.
166+
```
167+
import (
168+
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
169+
)
170+
...
171+
err := retry.Do(context.TODO(), db, func(ctx context.Context, cc *sql.Conn) error {
172+
// work with cc
173+
rows, err := cc.QueryContext(ctx, "SELECT 1;")
174+
if err != nil {
175+
return err // return err to retryer
176+
}
177+
...
178+
return nil // good final of retry operation
179+
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
180+
181+
```
182+
183+
### Over `sql.Tx` <a name="retry-tx"></a>
144184

145-
### Over `sql.Conn` object
185+
`retry.DoTx` helper allow custom lambda, which must return error for processing or nil if retry operation is ok.
186+
187+
`tx` object is a prepared transaction object which not requires commit or rollback at the end - `retry.DoTx` do it automatically.
188+
```
189+
import (
190+
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
191+
)
192+
...
193+
err := retry.DoTx(context.TODO(), db, func(ctx context.Context, tx *sql.Tx) error {
194+
// work with tx
195+
rows, err := tx.QueryContext(ctx, "SELECT 1;")
196+
if err != nil {
197+
return err // return err to retryer
198+
}
199+
...
200+
return nil // good final of retry tx operation
201+
}, retry.WithDoTxRetryOptions(retry.WithIdempotent(true)))
202+
```
146203

147-
### Over `sql.Tx`
204+
## Query args types
205+
206+
`database/sql` driver for `YDB` supports next types of query args:
207+
* multiple `sql.NamedArg` (uniform `database/sql` arg)
208+
```
209+
rows, err := cc.QueryContext(ctx, `
210+
DECLARE $seasonTitle AS Utf8;
211+
DECLARE $views AS Uint64;
212+
SELECT season_id FROM seasons WHERE title LIKE $seasonTitle AND vews > $views;
213+
`,
214+
sql.Named("seasonTitle", "%Season 1%"),
215+
sql.Named("views", uint64(1000)),
216+
)
217+
```
218+
* multiple native for `ydb-go-sdk` `table.ParameterOption` which constructs from `table.ValueParam("name", value)`
219+
```
220+
rows, err := cc.QueryContext(ctx, `
221+
DECLARE $seasonTitle AS Utf8;
222+
DECLARE $views AS Uint64;
223+
SELECT season_id FROM seasons WHERE title LIKE $seasonTitle AND vews > $views;
224+
`,
225+
table.ValueParam("seasonTitle", types.TextValue("%Season 1%")),
226+
table.ValueParam("views", types.Uint64Value((1000)),
227+
)
228+
```
229+
* single native for `ydb-go-sdk` `*table.QueryParameters` which constructs from `table.NewQueryParameters(parameterOptions...)`
230+
```
231+
rows, err := cc.QueryContext(ctx, `
232+
DECLARE $seasonTitle AS Utf8;
233+
DECLARE $views AS Uint64;
234+
SELECT season_id FROM seasons WHERE title LIKE $seasonTitle AND vews > $views;
235+
`,
236+
table.NewQueryParameters(
237+
table.ValueParam("seasonTitle", types.TextValue("%Season 1%")),
238+
table.ValueParam("views", types.Uint64Value((1000)),
239+
),
240+
)
241+
```

0 commit comments

Comments
 (0)