当前位置: 首页 > 软件库 > 数据库相关 > >

sqlx

授权协议 View license
开发语言 C/C++
所属分类 数据库相关
软件类型 开源软件
地区 不详
投 递 者 左丘智渊
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

SQLx

�� The Rust SQL Toolkit


Built with ❤️ by The LaunchBadge team

Have a question? Be sure to check the FAQ first!

SQLx is an async, pure Rust SQL crate featuring compile-time checked queries without a DSL.

  • Truly Asynchronous. Built from the ground-up using async/await for maximum concurrency.

  • Compile-time checked queries (if you want). See SQLx is not an ORM.

  • Database Agnostic. Support for PostgreSQL, MySQL, SQLite, and MSSQL.

  • Pure Rust. The Postgres and MySQL/MariaDB drivers are written in pure Rust using zero unsafe†† code.

  • Runtime Agnostic. Works on different runtimes (async-std / tokio / actix) and TLS backends (native-tls, rustls).

† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only waywe could be pure Rust for SQLite is by porting all of SQLite to Rust).

†† SQLx uses #![forbid(unsafe_code)] unless the sqlite feature is enabled. As the SQLite driver interactswith C, those interactions are unsafe.


  • Cross-platform. Being native Rust, SQLx will compile anywhere Rust is supported.

  • Built-in connection pooling with sqlx::Pool.

  • Row streaming. Data is read asynchronously from the database and decoded on-demand.

  • Automatic statement preparation and caching. When using the high-level query API (sqlx::query), statements areprepared and cached per-connection.

  • Simple (unprepared) query execution including fetching results into the same Row types used bythe high-level API. Supports batch execution and returning results from all statements.

  • Transport Layer Security (TLS) where supported (MySQL and PostgreSQL).

  • Asynchronous notifications using LISTEN and NOTIFY for PostgreSQL.

  • Nested transactions with support for save points.

  • Any database driver for changing the database driver at runtime. An AnyPool connects to the driver indicated by the URI scheme.

Install

SQLx is compatible with the async-std, tokio and actix runtimes; and, the native-tls and rustls TLS backends. When adding the dependency, you must chose a runtime feature that is runtime + tls.

# Cargo.toml
[dependencies]
# tokio + rustls
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls" ] }
# async-std + native-tls
sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls" ] }

The runtime and TLS backend not being separate feature sets to select is a workaround for a Cargo issue.

Cargo Feature Flags

  • runtime-async-std-native-tls (on by default): Use the async-std runtime and native-tls TLS backend.

  • runtime-async-std-rustls: Use the async-std runtime and rustls TLS backend.

  • runtime-tokio-native-tls: Use the tokio runtime and native-tls TLS backend.

  • runtime-tokio-rustls: Use the tokio runtime and rustls TLS backend.

  • runtime-actix-native-tls: Use the actix runtime and native-tls TLS backend.

  • runtime-actix-rustls: Use the actix runtime and rustls TLS backend.

  • postgres: Add support for the Postgres database server.

  • mysql: Add support for the MySQL/MariaDB database server.

  • mssql: Add support for the MSSQL database server.

  • sqlite: Add support for the self-contained SQLite database engine.

  • any: Add support for the Any database driver, which can proxy to a database driver at runtime.

  • macros: Add support for the query*! macros, which allow compile-time checked queries.

  • migrate: Add support for the migration management and migrate! macro, which allow compile-time embedded migrations.

  • uuid: Add support for UUID (in Postgres).

  • chrono: Add support for date and time types from chrono.

  • time: Add support for date and time types from time crate (alternative to chrono, which is preferred by query! macro, if both enabled)

  • bstr: Add support for bstr::BString.

  • git2: Add support for git2::Oid.

  • bigdecimal: Add support for NUMERIC using the bigdecimal crate.

  • decimal: Add support for NUMERIC using the rust_decimal crate.

  • ipnetwork: Add support for INET and CIDR (in postgres) using the ipnetwork crate.

  • json: Add support for JSON and JSONB (in postgres) using the serde_json crate.

  • tls: Add support for TLS connections.

SQLx is not an ORM!

SQLx supports compile-time checked queries. It does not, however, do this by providing a RustAPI or DSL (domain-specific language) for building queries. Instead, it provides macros that takeregular SQL as an input and ensure that it is valid for your database. The way this works is thatSQLx connects to your development DB at compile time to have the database itself verify (and returnsome info on) your SQL queries. This has some potentially surprising implications:

  • Since SQLx never has to parse the SQL string itself, any syntax that the development DB acceptscan be used (including things added by database extensions)
  • Due to the different amount of information databases let you retrieve about queries, the extent ofSQL verification you get from the query macros depends on the database

If you are looking for an (asynchronous) ORM, you can check out ormx, which is built on topof SQLx.

Usage

Quickstart

[dependencies]
sqlx = { version = "0.4.1", features = [ "postgres" ] }
async-std = { version = "1.6", features = [ "attributes" ] }
use sqlx::postgres::PgPoolOptions;
// use sqlx::mysql::MySqlPoolOptions;
// etc.

#[async_std::main]
// or #[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    // Create a connection pool
    //  for MySQL, use MySqlPoolOptions::new()
    //  for SQLite, use SqlitePoolOptions::new()
    //  etc.
    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect("postgres://postgres:password@localhost/test").await?;

    // Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL)
    let row: (i64,) = sqlx::query_as("SELECT $1")
        .bind(150_i64)
        .fetch_one(&pool).await?;

    assert_eq!(row.0, 150);

    Ok(())
}

Connecting

A single connection can be established using any of the database connection types and calling connect().

use sqlx::Connection;

let conn = SqliteConnection::connect("sqlite::memory:").await?;

Generally, you will want to instead create a connection pool (sqlx::Pool) in order for your application toregulate how many server-side connections it's using.

let pool = MySqlPool::connect("mysql://user:pass@host/database").await?;

Querying

In SQL, queries can be separated into prepared (parameterized) or unprepared (simple). Prepared queries have theirquery plan cached, use a binary mode of communication (lower bandwidth and faster decoding), and utilize parametersto avoid SQL injection. Unprepared queries are simple and intended only for use case where a prepared statementwill not work, such as various database commands (e.g., PRAGMA or SET or BEGIN).

SQLx supports all operations with both types of queries. In SQLx, a &str is treated as an unprepared queryand a Query or QueryAs struct is treated as a prepared query.

// low-level, Executor trait
conn.execute("BEGIN").await?; // unprepared, simple query
conn.execute(sqlx::query("DELETE FROM table")).await?; // prepared, cached query

We should prefer to use the high level, query interface whenever possible. To make this easier, there are finalizerson the type to avoid the need to wrap with an executor.

sqlx::query("DELETE FROM table").execute(&mut conn).await?;
sqlx::query("DELETE FROM table").execute(&pool).await?;

The execute query finalizer returns the number of affected rows, if any, and drops all received results.In addition, there are fetch, fetch_one, fetch_optional, and fetch_all to receive results.

The Query type returned from sqlx::query will return Row<'conn> from the database. Column values can be accessedby ordinal or by name with row.get(). As the Row retains an immutable borrow on the connection, only oneRow may exist at a time.

The fetch query finalizer returns a stream-like type that iterates through the rows in the result sets.

// provides `try_next`
use futures::TryStreamExt;

let mut rows = sqlx::query("SELECT * FROM users WHERE email = ?")
    .bind(email)
    .fetch(&mut conn);

while let Some(row) = rows.try_next().await? {
    // map the row into a user-defined domain type
    let email: &str = row.try_get("email")?;
}

To assist with mapping the row into a domain type, there are two idioms that may be used:

let mut stream = sqlx::query("SELECT * FROM users")
    .map(|row: PgRow| {
        // map the row into a user-defined domain type
    })
    .fetch(&mut conn);
#[derive(sqlx::FromRow)]
struct User { name: String, id: i64 }

let mut stream = sqlx::query_as::<_, User>("SELECT * FROM users WHERE email = ? OR name = ?")
    .bind(user_email)
    .bind(user_name)
    .fetch(&mut conn);

Instead of a stream of results, we can use fetch_one or fetch_optional to request one required or optional resultfrom the database.

Compile-time verification

We can use the macro, sqlx::query! to achieve compile-time syntactic and semantic verification of the SQL, withan output to an anonymous record type where each SQL column is a Rust field (using raw identifiers where needed).

let countries = sqlx::query!(
        "
SELECT country, COUNT(*) as count
FROM users
GROUP BY country
WHERE organization = ?
        ",
        organization
    )
    .fetch_all(&pool) // -> Vec<{ country: String, count: i64 }>
    .await?;

// countries[0].country
// countries[0].count

Differences from query():

  • The input (or bind) parameters must be given all at once (and they are compile-time validated to bethe right number and the right type).

  • The output type is an anonymous record. In the above example the type would be similar to:

    { country: String, count: i64 }
  • The DATABASE_URL environment variable must be set at build time to a database which it can preparequeries against; the database does not have to contain any data but must be the samekind (MySQL, Postgres, etc.) and have the same schema as the database you will be connecting to at runtime.

    For convenience, you can use a .env file to set DATABASE_URL so that you don't have to pass it every time:

    DATABASE_URL=mysql://localhost/my_database
    

The biggest downside to query!() is that the output type cannot be named (due to Rust notofficially supporting anonymous records). To address that, there is a query_as!() macro that ismostly identical except that you can name the output type.

// no traits are needed
struct Country { country: String, count: i64 }

let countries = sqlx::query_as!(Country,
        "
SELECT country, COUNT(*) as count
FROM users
GROUP BY country
WHERE organization = ?
        ",
        organization
    )
    .fetch_all(&pool) // -> Vec<Country>
    .await?;

// countries[0].country
// countries[0].count

To avoid the need of having a development database around to compile the project even when nomodifications (to the database-accessing parts of the code) are done, you can enable "offline mode"to cache the results of the SQL query analysis using the sqlx command-line tool. Seesqlx-cli/README.md.

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

If the sqlite feature is enabled, this is downgraded to #![deny(unsafe_code)] with #![allow(unsafe_code)] on thesqlx::sqlite module. There are several places where we interact with the C SQLite API. We try to document each call for the invariants we're assuming. We absolutely welcome auditing of, and feedback on, our unsafe code usage.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submittedfor inclusion in the work by you, as defined in the Apache-2.0 license, shall bedual licensed as above, without any additional terms or conditions.

  • sqlx介绍 在项目中我们通常可能会使用database/sql连接mysql数据库。sqlx可以认为是go语言内置database/sql的超集,它在优秀的内置database/sql基础上提供了一组扩展。这些扩展中除了大家常用来查询的,还有其他强大的功能。 安装sqlx go get github.com/jmoiron/sqlx 基本使用 连接数据库 package main impor

  • 前言 在项目中我们通常可能会使用database/sql连接MySQL数据库。本文借助使用sqlx实现批量插入数据的例子,介绍sqlx中可能被你忽视了的sqlx.In和DB.NamedExec方法。 31.1 sqlx介绍 在项目中我们通常可能会使用database/sql连接MySQL数据库。sqlx可以认为是Go语言内置database/sql的超集,它在优秀的内置database/sql基础

  • 简介 sqlx 是 rust 中的一个数据库访问工具。具有以下特点: 异步:原生就支持异步,在并发性高的场合能够得到更好的支持 编译时检查查询:sqlx可以在 cargo build 的时候检查执行sql和响应值 多数据库支持:PostgresSQL,MySql,SqlLite,MSSql,MariaDB 多运行时支持:支持主流 rust 运行时。async-std,tokio,actix,nat

  • 前言 在项目中我们通常可能会使用database/sql连接MySQL数据库。本文借助使用sqlx实现批量插入数据的例子,介绍了sqlx中可能被你忽视了的sqlx.In和DB.NamedExec方法。 sqlx介绍: 在项目中我们通常可能会使用database/sql连接MySQL数据库。sqlx可以认为是Go语言内置database/sql的超集,它在优秀的内置database/sql基础上提供

  • https://www.liwenzhou.com/posts/Go/sqlx/#autoid-0-2-0 sqlx库使用指南 在项目中我们通常可能会使用database/sql连接MySQL数据库。本文借助使用sqlx实现批量插入数据的例子,介绍了sqlx中可能被你忽视了的sqlx.In和DB.NamedExec方法。 sqlx介绍 在项目中我们通常可能会使用database/sql连接MySQ

  • go sqlx 包 1 介绍和使用 上文我们用了go-sql-driver/mysql库来操作 mysql,还有一个更优秀的库 sqlx,它也是对标准库database/sql具体的实现,并进行进一步封装和新增了一些方法 1.1 安装和快速链接 go get -u github.com/jmoiron/sqlx package main import ( "fmt" _ "github.c

  • 在项目中我们通常可能会使用database/sql连接MySQL数据库。本文借助使用sqlx实现批量插入数据的例子,介绍了sqlx中可能被你忽视了的sqlx.In和DB.NamedExec方法。 sqlx介绍 在项目中我们通常可能会使用database/sql连接MySQL数据库。sqlx可以认为是Go语言内置database/sql的超集,它在优秀的内置database/sql基础上提供了一组扩

  • sqlx使用指南 安装sqlx go get -u github.com/jmoiron/sqlx 基本使用 连接数据库 var db *sqlx.DB func initDB() (err error) { dsn := "user:password@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True" // 也可以使用

  • sqlx是一个go语言包,在内置database/sql包之上增加了很多扩展,简化数据库操作代码的书写。 准备 安装sqlx 驱动 $ goget github.com/jmoiron/sqlx 本文访问sqlite数据库 $ goget github.com/mattn/go-sqlite3 Handle Types sqlx设计和database/sql使用方法是一样的。包含有4中主要的h

  • go语言中的database/sql包提供了保证SQL或类SQL数据库的泛用接口,并不提供具体的数据库驱动。 使用database/sql包时必须注入(至少)一个数据库驱动。 Go操作MySQL go get -u github.com/go-sql-driver/mysql 连接数据库 import ( "database/sql" _ "github.com/go-sql-driver/

 相关资料
  • 问题内容: 我正在尝试使用Golang sqlx库创建准备好的语句。我想让表名成为bindVar 但是,这给了我一个语法错误。我可以不使用绑定变量作为表名吗? 问题答案: 我可以不使用绑定变量作为表名吗? 不,报价来源。 参数只能用作数据值,不能用作标识符。因此,例如,这是合理的: 但这不起作用: 但是,如果需要,可以使用表名,而将$ 1,$ 2,…保留为数据值。

  • 问题内容: 我想查询mysql数据库中的表以获取切片值: 但是我得到这个错误: 我怎样才能解决这个问题? 问题答案: sqlx为此提供了很好的帮助:在In()中,我们只需要使用args和Rebind准备查询,如下所示: 另外,我建议您在这里看看:http : //jmoiron.github.io/sqlx/有很多示例,包括 IN

  • 问题内容: 我有一个关于将xml数据类型传递给C#代码查询的问题。 首先,这是SQL Server上的表格: 其次,我有一个用C#编写的应用程序。然后将行插入到应用程序中 这是代码: 一切正常,插入行。但是,如果我运行SQL事件探查器并检查insert命令,则会看到以下内容: 我们可以看到,尽管将类型传递给查询,但仍执行了从字符串到XML的不必要的转换。XML解析器确实浪费了工作。 问题是:为什么

  • 我已经写了一个玩具应用程序来尝试通过sqlx使用Postgresql。我有一个大容量插入器 作为准备好的陈述的内容 然后,我将继续向正在创建的体量插入添加行。 然后最后执行准备好的语句 这以前工作得很好,但是现在我又回到了它,试着执行这个代码,它挂在 我是否在代码中遗漏了什么,或者这一切都与数据库引擎有关,没有响应。 这是我的完整代码。 如果有帮助的话,我也可以包括contacts.json文件的