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

book: improve documentation about UDT values #789

Merged
merged 1 commit into from
Aug 21, 2023
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
33 changes: 26 additions & 7 deletions docs/source/data-types/udt.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
# User defined types
Scylla allows users to define their own data types with named fields.\
The driver supports this by allowing to create a custom struct with derived functionality.
Scylla allows users to define their own data types with named fields (See [the official documentation](https://opensource.docs.scylladb.com/stable/cql/types.html#user-defined-types))\
To use user defined types in the driver, you can create a corresponding struct in Rust, and use it to read and write UDT values.


For example let's say `my_type` was created using this query:
```sql
CREATE TYPE ks.my_type (int_val int, text_val text)
```

To use this type in the driver create a matching struct and derive `IntoUserType` and `FromUserType`:
To use this type in the driver, create a matching struct and derive `IntoUserType` and `FromUserType`:

```rust
# extern crate scylla;
# async fn check_only_compiles() {
use scylla::macros::{FromUserType, IntoUserType};

// Define a custom struct that matches the User Defined Type created earlier.
// Fields must be in the same order as they are in the database.
// Wrapping a field in Option will gracefully handle null field values.
#[derive(Debug, IntoUserType, FromUserType)]
struct MyType {
int_val: i32,
text_val: Option<String>,
}
# }
```

> ***Important***\
> Fields in the Rust struct must be defined in the same order as they are in the database.
> When sending and receiving values, the driver will (de)serialize fields one after another, without looking at field names.

Now it can be sent and received just like any other CQL value:
```rust
# extern crate scylla;
# use scylla::Session;
Expand All @@ -17,16 +40,12 @@ use scylla::IntoTypedRows;
use scylla::macros::{FromUserType, IntoUserType};
use scylla::cql_to_rust::FromCqlVal;

// Define custom struct that matches User Defined Type created earlier
// wrapping field in Option will gracefully handle null field values
wprzytula marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, IntoUserType, FromUserType)]
struct MyType {
int_val: i32,
text_val: Option<String>,
}

// Now it can be sent and received like any other value

// Insert my_type into the table
let to_insert = MyType {
int_val: 17,
Expand Down