Skip to content

Latest commit

 

History

History
105 lines (84 loc) · 2.4 KB

README.md

File metadata and controls

105 lines (84 loc) · 2.4 KB

Sqlx intergration for ydb-rs-sdk

This crate provides Sqlx integration for ydb-rs-sdk. It is in under active development.

Basic examples

Simple select

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let connection_string = env::var("YDB_CONNECTION_STRING")?;

    let pool = YdbPoolOptions::new().connect(&connection_string).await?;
    let row: (i32,) = sqlx::query_as("SELECT 1+1").fetch_one(&pool).await?;
    assert_eq!(row.0, 2);

    Ok(())
}

Query with conditions and parse result to struct

#[derive(sqlx::FromRow)]
struct UserInfo {
    id: u64,
    name: String,
    age: u32,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let connection_string = env::var("YDB_CONNECTION_STRING")?;
    let pool = YdbPoolOptions::new().connect(&connection_string).await?;

     let users: Vec<UserInfo> =
        sqlx::query_as("SELECT * FROM test2 WHERE age > $age AND age < $arg_1")
            .bind(with_name("age", 30))
            .bind(40)
            .fetch_all(&pool)
            .await?;

    assert!(users.len() > 0);

    Ok(())
}

Arguments

There are two binding available:

  • default unnamed - with generated name like $arg_1
  • named by with_name function. you can specify name starting with or without $, but in query you should use $-started name.

Ydb requires that every query params should be declared with DECLARE clause like this:

DECLARE $age AS Uint32;

SELECT * FROM test2 WHERE age > $age;

The library is do it for you. You specify only query and bind params to it with bind function.

Checklist

  • Connect to ydb
  • Default credentials (using fromEnv)
  • Custom credentials with options
  • Basic query
  • Binding parameters
  • Support types
    • Numeric
      • Bool
      • Int8
      • Int16
      • Int32
      • Int64
      • Uint8
      • Uint16
      • Uint32
      • Uint64
      • Float
      • Double
      • Decimal
    • String types
      • String
      • Utf8
      • Json
      • JsonDocument
      • Yson
      • Uuid
    • Date and time
      • Date
      • Datetime
      • Timestamp
      • Interval
    • Optional
  • Prepared statements
  • Transactions
  • Compile-type checked queries
  • Migrations
  • Log Statements