You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The result from the `ExecuteAsync` method will contain records received from the cluster. The SDK has `GetValue`, `TryGetValue`, and `IsNull` extension methods to simplify using the records:
74
+
The result from the `ExecuteAsync` method will contain records received from the cluster. The value of the specific column can be retrieved in the following manner:
75
75
76
76
```c#
77
-
usingScalarDB.Client.Extensions;
78
-
79
-
// ...
80
-
81
77
foreach (varrecordinresultSet.Records)
82
78
{
83
79
// Getting an integer value from the "item_id" column.
@@ -52,7 +52,7 @@ using var manager = factory.GetTransactionManager();
52
52
53
53
## Manage transactions
54
54
55
-
To execute CRUD operations, a transaction is needed. You can begin a transaction by using the transaction manager as follows:
55
+
To execute multiple CRUD operations as part of a single transaction, first, you need to begin a transaction. You can begin a transaction by using the transaction manager as follows:
A transaction has `GetAsync`, `ScanAsync`, `PutAsync`, `DeleteAsync`, and `MutateAsync` methods to execute CRUD commands against the cluster. As a parameter, these methods have a command object. A command object can be created by using the builders listed in this section.
87
+
A transaction has `GetAsync`, `ScanAsync`, `InsertAsync`, `UpsertAsync`, `UpdateAsync`, `DeleteAsync`, and `MutateAsync` methods to execute CRUD operations against the cluster. As a parameter, these methods have an operation object. An operation object can be created by using the builders listed in this section.
88
88
89
-
To use these builders add the following namespace to the `using` section:
89
+
:::note
90
+
91
+
CRUD operations can be executed in a one-shot transaction manner without needing to explicitly create a transaction. For that, a manager object has the same CRUD methods as a transaction object.
92
+
93
+
:::
94
+
95
+
To use builders, add the following namespace to the `using` section:
90
96
91
97
```c#
92
98
usingScalarDB.Client.Builders;
@@ -100,16 +106,13 @@ The cluster does not support parallel execution of commands inside one transacti
To retrieve a single record, you can use the `GetAsync` method as follows:
107
110
111
+
```c#
108
112
varget=
109
113
newGetBuilder()
110
114
.SetNamespaceName("ns")
111
115
.SetTableName("statements")
112
-
.SetGetType(GetTypeEnum.Get)
113
116
.AddPartitionKey("order_id", "1")
114
117
.AddClusteringKey("item_id", 2)
115
118
.SetProjections("item_id", "count")
@@ -118,15 +121,44 @@ var get =
118
121
vargetResult=awaittransaction.GetAsync(get);
119
122
```
120
123
121
-
#### Handle `Result` objects
122
-
123
-
The `GetAsync` and `ScanAsync` methods return `Result` objects. The SDK has `GetValue`, `TryGetValue`, and `IsNull` extension methods to simplify using them:
124
+
It is possible to retrieve a record by using an index instead of a partition key. To do that, you need to set the type of operation to `GetWithIndex` as follows:
124
125
125
126
```c#
126
-
usingScalarDB.Client.Extensions;
127
+
// ...
128
+
usingScalarDB.Client.Core;
127
129
128
130
// ...
129
131
132
+
varget=
133
+
newGetBuilder()
134
+
// ...
135
+
.SetGetType(GetOperationType.GetWithIndex)
136
+
.AddPartitionKey("index_column", "1")
137
+
.Build();
138
+
```
139
+
140
+
You can also specify arbitrary conditions that a retrieved record must meet, or it won't be returned. The conditions can be set as conjunctions of conditions as follows:
In the above example, a record will be returned only if its `cost` is less than `1000`, or if its `cost` is less than `10000` and `in_stock` is true.
156
+
157
+
#### Handle `IResult` objects
158
+
159
+
The `GetAsync` and `ScanAsync` methods return `IResult` objects. An `IResult` object contains columns of the retrieved record. The value of the specific column can be retrieved in the following manner:
160
+
161
+
```c#
130
162
// Getting an integer value from the "item_id" column.
131
163
// If it fails, an exception will be thrown.
132
164
varitemId=result.GetValue<int>("item_id");
@@ -145,16 +177,13 @@ For details about which type should be used in `GetValue<T>` and `TryGetValue<T>
145
177
146
178
### `ScanAsync` method example
147
179
148
-
```c#
149
-
usingstaticScalar.Db.Cluster.Rpc.V1.Scan.Types;
150
-
151
-
// ..
180
+
To retrieve a range of records, you can use the `ScanAsync` method as follows:
152
181
182
+
```c#
153
183
varscan=
154
184
newScanBuilder()
155
185
.SetNamespaceName("ns")
156
186
.SetTableName("statements")
157
-
.SetScanType(ScanType.Scan)
158
187
.AddPartitionKey("order_id", "1")
159
188
.AddStartClusteringKey("item_id", 2)
160
189
.SetStartInclusive(true)
@@ -166,58 +195,134 @@ var scan =
166
195
varscanResult=awaittransaction.ScanAsync(scan);
167
196
```
168
197
169
-
### `PutAsync` method example
198
+
It is possible to retrieve a record by using an index instead of a partition key. To do that, you need to set the type of operation to `ScanWithIndex` as follows:
199
+
200
+
```c#
201
+
// ...
202
+
usingScalarDB.Client.Core;
203
+
204
+
// ...
205
+
206
+
varscan=
207
+
newScanBuilder()
208
+
// ...
209
+
.SetScanType(ScanOperationType.ScanWithIndex)
210
+
.AddPartitionKey("index_column", "1")
211
+
.Build();
212
+
```
213
+
214
+
The arbitrary conditions that a retrieved record must meet can also be set for a scan operation in the same way as for a [get operation](getting-started-with-distributed-transactions.mdx#getasync-method-example).
215
+
216
+
### `InsertAsync` method example
217
+
218
+
To insert a new record, you can use the `InsertAsync` method as follows:
170
219
171
220
```c#
172
-
varput=
173
-
newPutBuilder()
221
+
varinsert=
222
+
newInsertBuilder()
174
223
.SetNamespaceName("ns")
175
224
.SetTableName("statements")
176
225
.AddPartitionKey("order_id", "1")
177
226
.AddClusteringKey("item_id", 2)
178
227
.AddColumn("count", 11)
179
228
.Build();
180
229
181
-
awaitclient.PutAsync(put);
230
+
awaittransaction.InsertAsync(insert);
182
231
```
183
232
184
-
### `DeleteAsync` method example
233
+
### `UpsertAsync` method example
234
+
235
+
To upsert a record (update an existing record or insert a new one), you can use the `UpsertAsync` method as follows:
185
236
186
237
```c#
187
-
vardelete=
188
-
newDeleteBuilder()
238
+
varupsert=
239
+
newUpsertBuilder()
189
240
.SetNamespaceName("ns")
190
241
.SetTableName("statements")
191
242
.AddPartitionKey("order_id", "1")
192
243
.AddClusteringKey("item_id", 2)
244
+
.AddColumn("count", 11)
193
245
.Build();
194
246
195
-
awaitclient.DeleteAsync(delete);
247
+
awaittransaction.UpsertAsync(upsert);
196
248
```
197
249
198
-
### `MutateAsync` method example:
250
+
### `UpdateAsync` method example
251
+
252
+
To update an existing record, you can use the `UpdateAsync` method as follows:
199
253
200
254
```c#
201
-
usingScalar.Db.Cluster.Rpc.V1;
255
+
// ...
256
+
usingScalarDB.Client.Core;
202
257
203
258
// ...
204
259
205
-
varput=
206
-
newPutBuilder()
260
+
varupdate=
261
+
newUpdateBuilder()
207
262
.SetNamespaceName("ns")
208
263
.SetTableName("statements")
209
264
.AddPartitionKey("order_id", "1")
210
265
.AddClusteringKey("item_id", 2)
211
266
.AddColumn("count", 11)
267
+
.AddCondition("processed", false, Operator.Equal)
268
+
.Build();
269
+
270
+
awaittransaction.UpdateAsync(update);
271
+
```
272
+
273
+
### `DeleteAsync` method example
274
+
275
+
To delete a record, you can use the `DeleteAsync` method as follows:
276
+
277
+
```c#
278
+
// ...
279
+
usingScalarDB.Client.Core;
280
+
281
+
// ...
282
+
283
+
vardelete=
284
+
newDeleteBuilder()
285
+
.SetNamespaceName("ns")
286
+
.SetTableName("statements")
287
+
.AddPartitionKey("order_id", "1")
288
+
.AddClusteringKey("item_id", 2)
289
+
.AddCondition("processed", false, Operator.Equal)
212
290
.Build();
213
291
214
-
varmutate=newMutation { Put=put };
292
+
awaittransaction.DeleteAsync(delete);
293
+
```
294
+
295
+
### `MutateAsync` method example
296
+
297
+
The `MutateAsync` method allows you to execute more than one mutation operation in a single call to the cluster. You can do this in the following manner:
298
+
299
+
```c#
300
+
// ...
301
+
usingScalarDB.Client.Core;
302
+
303
+
// ...
215
304
216
-
awaitclient.MutateAsync(new[] { mutate });
305
+
varmutations=newIMutation[]
306
+
{
307
+
newInsertBuilder()
308
+
// ...
309
+
.Build(),
310
+
newUpsertBuilder()
311
+
// ...
312
+
.Build(),
313
+
newUpdateBuilder()
314
+
// ...
315
+
.Build(),
316
+
newDeleteBuilder()
317
+
// ...
318
+
.Build()
319
+
};
320
+
321
+
awaittransaction.MutateAsync(mutations);
217
322
```
218
323
219
324
:::note
220
325
221
-
To modify data by using the `PutAsync`, `DeleteAsync`, or `MutateAsync` method, the data must be retrieved first by using the `GetAsync` or `ScanAsync` method.
326
+
To modify data by using the `InsertAsync`, `UpsertAsync`, `UpdateAsync`, `DeleteAsync`, or `MutateAsync` method, the data must be retrieved first by using the `GetAsync` or `ScanAsync` method.
0 commit comments