Giter VIP home page Giter VIP logo

ydb-rs-sqlx's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.