Skip to content

MikhailNazarov/ydb-rs-sqlx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sqlx intergration for ydb-rs-sdk

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

Basic examples

Connection from .env

You could use DATABASE_URL or YDB_CONNECTION_STRING environment variable to connect to ydb server.

# .env file
YDB_CONNECTION_STRING=grpc://localhost:2136?database=/local
# .env file
DATABASE_URL=grpcs://ydb.serverless.yandexcloud.net:2135/?database=/ru-central1/xxxxxxxxxxxxxxx/yyyyyyyyyy&connection_timeout=5&sa-key=./key.json
let pool = Ydb::connect_env().await?;

Connection options

let pool = Ydb::connect_env_opts(|opt|opt.log_statements(LevelFilter::Info)).await?;

Connection from url

let pool = Ydb::connect("grpc://localhost:2136?database=/local").await?;

or

let pool = Ydb::connect_opts("grpc://localhost:2136?database=/local", |opt|opt.log_statements(LevelFilter::Info)).await?;

Simple select

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {

    let pool = Ydb::connect_env().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 pool = Ydb::connect_env().await?;

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

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

    Ok(())
}

Schema queries

Schema queries should be executed with SchemaExecutor.

You could use pool.schema() or conn.schema() to get SchemaExecutor:

sqlx::query("CREATE TABLE test2 (id Uint64 NOT NULL, name Utf8, age UInt8, description Utf8, PRIMARY KEY (id))")
        .execute(pool.schema())
        .await?;

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 dollr sign, but in query you should use $-started name.
        bind(with_name("age", 30))
  • named by tuple ("name", value)
        bind(("age", 30))

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 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

About

Sqlx integration for YDB rust SDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages