16个版本

0.2.6 2023年7月13日
0.2.5 2023年7月13日
0.1.9 2023年7月6日

#418数据库接口

Download history 26/week @ 2024-03-09 14/week @ 2024-03-16 3/week @ 2024-03-30

85 每月下载量

MIT 许可证

49KB
938

rssql

结构自由Rust SQL工具。

Rust actions

简介

sqlx 基础上的sql工具,支持 mysql (mariadb)postgresqlsqlite

示例

从简单的开始

use rssql::PostgreSQL;

async fn postgresql_select_example() {
    let mut postgresql = PostgreSQL::connect("postgre://user:[email protected]:5432/test")
        .await
        .unwrap();
    let check = postgresql.check_connection().await;
    assert_eq!(check, true);
    let rets = postgresql.execute_fetch_all("SELECT * FROM info").await.unwrap();
    // let rets = postgresql.execute_fetch_one("SELECT * FROM info").await.unwrap();
    println!("{}", rets);
    println!("{}", rets.rows_affected().unwrap());
    postgresql.close().await;
}

输出

+----+-------+------------+
| id | name  |    date    |
+----+-------+------------+
| 1  | test1 | 2023-06-11 |
| 2  | test2 | 2023-06-11 |
+----+-------+------------+

更多

use rssql::PostgreSQL;

async fn test_postgresql() {
    /// Connect to database.
    let mut postgresql = PostgreSQL::connect("postgre://user:[email protected]:5432/test")
        .await
        .unwrap();
    /// Check connection.
    let check = postgresql.check_connection().await;
    assert_eq!(check, true);

    let sql = "CREATE TABLE IF NOT EXISTS info (id INT PRIMARY KEY NOT NULL, name VARCHAR(16), date DATE)";
    let _ = postgresql.execute(sql).await.unwrap();

    /// Insert 10 rows data into table `info`.
    for i in 0..10 {
        let sql = format!(
            "INSERT INTO info (id, name, date) VALUES ({}, 'test{}', '2023-07-07')",
            i, i
        );
        let rows_affected = postgresql.execute(&sql).await.unwrap();
        assert_eq!(rows_affected, 1);
    }

    /// Select all from table `info`.
    let rets = postgresql.execute_fetch_all("SELECT * FROM info").await.unwrap();
    // let rets = postgresql.execute_fetch_one("SELECT * FROM info").await.unwrap();
    println!("{}", rets);

    /// Get first one from returns by column name.
    for column in &rets.column {
        let value = rets.get_first_one(&column).unwrap();
        println!("{}", value);
    }

    /// Get all by column name.
    let values = rets.get_all("name").unwrap();
    for value in values {
        match value {
            SQLDataTypes::MySQLDataTypes(m) => (),
            SQLDataTypes::PostgreSQLDataTypes(p) => (),
            SQLDataTypes::SQLiteDataTypes(s) => match s {
                SQLiteDataTypes::Binary(b) => (),
                SQLiteDataTypes::Bool(b) => (),
                SQLiteDataTypes::DateTime(d) => (),
                SQLiteDataTypes::F64(f) => {
                    let new_f = f + 3.14;
                    println!("new float value: {}", new_f);
                }
                SQLiteDataTypes::I32(i) => (),
                SQLiteDataTypes::I64(i) => (),
                SQLiteDataTypes::NaiveDate(n) => (),
                SQLiteDataTypes::NaiveDateTime(n) => (),
                SQLiteDataTypes::NaiveTime(n) => (),
                SQLiteDataTypes::String(s) => (),
            },
        }
    }

    /// Close the connection.
    postgresql.close().await;
}

依赖

~38–52MB
~1M SLoC