profile
viewpoint
Austin Bonander abonander @launchbadge California, USA

abonander/anterofit 124

Strongly typed, asynchronous REST client framework for Rust.

abonander/buf_redux 33

A drop-in replacement for Rust's std::io::BufReader, with extra features

abonander/ferrite 13

Predecessor to Anterofit (https://github.com/abonander/anterofit)

abonander/const-cstr 9

Create static C-compatible strings from Rust string literals.

abonander/file_dialog 3

A file dialog in Rust using Conrod

abonander/dbl-buf 1

Generic double-buffer for Rust

abonander/abonander.github.io 0

Completely self-taught Android developer, chronic sufferer of Rust-mania.

abonander/ActionBarSherlock 0

Library for implementing the action bar design pattern using the native action bar on Android 4.0+ and a custom implementation on pre-4.0 through a single API and theme.

issue commentlaunchbadge/sqlx

One to Many (1:N) clarification

@abonander

What I've Tried According to #714 it seems like this query should work:

    let consumable_or_error = query_as!(
        Consumable,
        r#"
        SELECT
            c.*,
            (
                s.id,
                s.consumable_id,
                s.amount,
                s.kcal,
            ) AS "servings: Vec<Serving>"
        FROM consumable AS c
        LEFT OUTER JOIN serving AS s
                ON c.id = s.consumable_id
        WHERE id = $1
        "#,
        id
    )
    .fetch_one(pool.get_ref())
    .await;

but I still get compilation errors. error returned from database: syntax error at or near ")"

From #298 , I'm not even sure if what I'm asking is possible but I'm pretty much stuck wither way.

flopshot

comment created time in 5 hours

issue closedlaunchbadge/sqlx

[postgres] Easiest way to get N rows as a JSON array

I'm trying to figure out the simplest way to sample 100 rows from a table with an unknown schema as a JSON array (ideally serde_json::Value), here is what I'm currently using:

query(&format!(
    r#"
    SELECT JSON_AGG(t)::text "json!" AS rows
    FROM (SELECT * FROM {} TABLESAMPLE SYSTEM_ROWS(100)) t
    "#,
    &table_name
))
.map(|v: PgRow| v.get("rows"))
.fetch_one(&pool)
.await
.map(|v: String| serde_json::from_str(&v))

closed time in 8 hours

danjenson

issue commentlaunchbadge/sqlx

[postgres] Easiest way to get N rows as a JSON array

perfect thanks!

danjenson

comment created time in 9 hours

issue commentlaunchbadge/sqlx

Can't get value form mysql row

I use sqlx with version = "0.4.1",but i want to get value from MySqlRow with 'get' method,but the 'get' method is private; image image so what can i do?

eraffaelli

comment created time in a day

issue openedlaunchbadge/sqlx

Pool times out while waiting for an open connection

I tried to solve this on my own but I haven't made any progress. The mysql 20.1 crate works just fine when I use it.

When trying to execute a query, after a length delay I receive this message:

Error: 0: pool timed out while waiting for an open connection

I setup the MySQL pool like so:

    let sqlx_pool = MySqlPoolOptions::new()
        .connect_timeout(core::time::Duration::from_secs(120u64))
        .test_before_acquire(true)
        .min_connections(1)
        .max_connections(5)
        .connect("mysql://user:pass@[0:0:0:0:0:ffff:c0a8:15]:3306/schema").await?;

SQLx appears to connect successfully. The returned pool looks like so:

Pool {
    size: 0,
    num_idle: 0,
    is_closed: false,
    options: PoolOptions {
        max_connections: 5,
        min_connections: 1,
        connect_timeout: 120s,
        max_lifetime: Some(
            1800s,
        ),
        idle_timeout: Some(
            600s,
        ),
        test_before_acquire: true,
    },
}

The following code will execute successfully:

        let mut rows = query(
            "SELECT `tail`,`head` FROM `vecs`"
        ).fetch(sqlx_pool);

But SQLx will time-out here:

        while let Some(row) = rows.try_next().await? {
            let tail: u32 = row.try_get("tail")?;
            let head: u32 = row.try_get("head")?;
        }; // while

created time in a day

issue openedlaunchbadge/sqlx

`COUNT(*)` with Postgresql returning `Option<i64>` instead of `i64`

I have code like this:

        let result = sqlx::query!(
            "SELECT COUNT(*) as count FROM my_table WHERE some_column=$1",
            &foo
        )
        .fetch_one(pool) //pool is already a reference variable
        .await?;

and when I attempt to use it:

if result.count == 0 {
    //do something
}

I get an error that the count member is of type Option<i64> instead of just i64. This doesn't make sense to me since this query will always return a single row with a value in count (unless I'm missing some detail of SQL, which is possible as I haven't ever read a comprehensive book or similar on it, but my attempt to find it via research has failed). Assuming that I'm not missing any detail of SQL, this seems like a bug.

For now, I'm working around it like this, but I'm not sure if this is the best way to do it:

        match result.count {
            Some(result) => {
                if result == 0 {
                    // no row with some_column=foo exists, exit the function
                }
            }
            None => // no row with some_column=foo exists, exit the function
        };
        //at least one such row exists

created time in a day

issue closedlaunchbadge/sqlx

It isn't possible to run tests with SQLite

[dependencies]
sqlx-core = { default-features = false, features = ["sqlite", "runtime-tokio-rustls"], version = "0.4" }
tokio = { default-features = false, features = ["macros", "rt-threaded"], version = "0.2" }
#[cfg(test)]
mod tests {
    use sqlx_core::connection::Connection;

    #[tokio::test]
    async fn foo() {
        let _ = sqlx_core::sqlite::SqliteConnection::connect("file::memory:").await.unwrap();
    }
}
---- tests::foo stdout ----
thread 'tests::bar' panicked at 'can call blocking only when running on the multi-threaded runtime', .../tokio-0.2.23/src/runtime/thread_pool/worker.rs:219:25
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Only happens with the tokio runtime

closed time in a day

c410-f3r

issue commentlaunchbadge/sqlx

It isn't possible to run tests with SQLite

@exul Thanks for pointing out this tokio behavior

c410-f3r

comment created time in a day

startedabonander/const-cstr

started time in a day

issue commentlaunchbadge/sqlx

It isn't possible to run tests with SQLite

You need to use the multi-threaded scheduler because sqlx does block on some operations with sqlite. Change #[tokio::test] to #[tokio::test(threaded_scheduler)]: https://docs.rs/tokio/0.2.4/tokio/attr.test.html

c410-f3r

comment created time in a day

issue commentlaunchbadge/sqlx

Connection with mysql fails when using `127.0.0.1`

You can use this tool to convert your IPv4 to an IPv6 address: Convert IPv4 to IPv6

You can use [0:0:0:0:0:ffff:7f00:1] rather than 127.0.0.1 for your DATABASE_URL.

c410-f3r

comment created time in a day

issue commentlaunchbadge/sqlx

Connection with mysql fails when using `127.0.0.1`

I'm having the same issue with using an IP address for connecting also.

MySqlPool::connect("mysql://user:pass@192.168.0.21:3306/schema")?;

Error:
   0: error occurred while attempting to establish a TLS connection: InvalidDNSNameError
   1: InvalidDNSNameError

I was not aware of the square brackets being standard syntax, I've not seen that in my life - it doesn't seem very standard lol.

I switched to square brackets and now receive this error:

MySqlPool::connect("mysql://user:pass@[192.168.0.21]:3306/schema")?;

The application panicked (crashed).
Message:  URL ParseError { invalid IPv6 address }
Location: C:\Users\User Name\.cargo\registry\src\github.com-1ecc6299db9ec823\mysql-20.1.0\src\conn\opts.rs:960

The issue is caused by the mysql-20.1 crate. If I use the mysql-20.1 crate directly I receive the same error.

c410-f3r

comment created time in a day

issue openedlaunchbadge/sqlx

`migrate!` macro should ignore reverse migrations generated by sqlx-cli

Summary

sqlx::migrate!() should ignore *.down.sql files, but currently it does not.

sqlx migrate add -r foo command generates {{timestamp}}_foo.{up,down}.sql. *.down.sql is reverse migration, and usually it should be ignored.

Expected result

sqlx::migrate!() ignores *.down.sql files, and applications running sqlx::migrate!("./migrations").run(&pool).await?; will successfully migrate the database on startup.

Actual result

sqlx::migrate!() loads all sql files, including *.down.sql, and sqlx::migrate!("./migrations").run(&pool).await?; cannot make the database expected state. For example, it attempts to apply create_foo.down.sql before create_foo.up.sql, and fails to execute DROP TABLE foo; (which is never expected to be run).

Workaround

Use the code below, instead of `sqlx::migrate!("./migrations").run(&pool).await?;

let mut migrator = sqlx::migrate!("./migrations");
// Temporary workaround: remove reverse migrations.
migrator.migrations.to_mut().retain(|migration| !migration.description.ends_with(".down"));
migrator.run(&pool).await?;

created time in a day

issue commentlaunchbadge/sqlx

Mysql - username with '@' results in authentication error

it works fine in version = "0.3.0"

CallumDowling

comment created time in 2 days

issue openedlaunchbadge/sqlx

[postgres] Easiest way to get N rows as a JSON array

I'm trying to figure out the simplest way to get 100 rows from a sample table with an unknown schema as a JSON array, but this isn't working.

let rows = query(&format!(
    r#"
    SELECT JSON_AGG(t)::text "json!"
    FROM (SELECT * FROM {} LIMIT 100) t
    "#,
    &table_name
))
.fetch_one(&d.data_db)
.await?;

Ideally, I'd like to get it as a serde_json::Value.

created time in 2 days

issue closedlaunchbadge/sqlx

error[E0277]: `dyn Stream<Item = std::result::Result<MySqlRow, sqlx::Error>> + std::marker::Send` is not a future

error[E0277]: `dyn Stream<Item = std::result::Result<MySqlRow, sqlx::Error>> + std::marker::Send` is not a future
  --> src/main.rs:15:20
   |
15 |     let mut rows = sqlx::query("SELECT * FROM users").fetch(&pool).await;
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `dyn Stream<Item = std::result::Result<MySqlRow, sqlx::Error>> + std::marker::Send` is not a future
   |
   = help: the trait `futures::Future` is not implemented for `dyn Stream<Item = std::result::Result<MySqlRow, sqlx::Error>> + std::marker::Send`
   = note: required because of the requirements on the impl of `futures::Future` for `Pin<Box<dyn Stream<Item = std::result::Result<MySqlRow, sqlx::Error>> + std::marker::Send>>`
   = note: required by `futures::Future::poll`

warning: unused import: `futures::TryStreamExt`
 --> src/main.rs:1:5

Cargo.toml:

[package]
name = "xxx"
version = "0.1.0"
authors = ["xx <xx@xx.xx>"]
edition = "2018"

[dependencies]
#sqlx = { git = "https://github.com/launchbadge/sqlx", branch = "master", default-features = false, features = [ "runtime-tokio", "macros", "mysql"] }
sqlx = { version="0.4.1", features = ["macros", "mysql", "runtime-async-std-native-tls"] }
tokio = { version = "0.2", feature="all" }
async-std = { version="1.7.0", fetures = ["attributes"] }
futures="0.3.8"

My Code:

#![feature(async_await)]
use futures::TryStreamExt;
use async_std::task::block_on;

const DBSTR: &str = "mysql://root@localhost:3306/test";

async fn f1() -> Result<(), sqlx::Error>  {
    let pool = sqlx::MySqlPool::connect(DBSTR).await.unwrap();

    // ok
    let row: (i32, ) = sqlx::query_as("SELECT ?").bind(123i32).fetch_one(&pool).await.unwrap();
    dbg!(row);

    // error
    // referrence by: https://docs.rs/crate/sqlx/0.4.0
    let mut rows = sqlx::query("SELECT * FROM users").fetch(&pool).await;
    while let Some(row) = rows.try_next().await? {
        let email: &str = row.try_get("email")?;
    }

    Ok(())
}

fn main()  {
    block_on(f1());
}

Env:

rustc --version
rustc 1.50.0-nightly (98d66340d 2020-11-14)
rustc 1.47.0 (18bf6b4f0 2020-10-07)

cargo --version
cargo 1.49.0-nightly (2af662e22 2020-11-12)

uname -a
Darwin MacBook-Pro 18.7.0 Darwin Kernel Version 18.7.0

sqlx: 0.4.0 / 0.4.1

closed time in 2 days

xiuno

issue commentlaunchbadge/sqlx

Introduce support for Oracle

What's is current plan? Support most of database through ODBC firstly? Thanks.

mehcode

comment created time in 2 days

startedabonander/const-cstr

started time in 2 days

startedabonander/img_hash

started time in 2 days

PR opened launchbadge/sqlx

Add a compatible option to pg to handle extra_float_digits

Postgres versions older than 9 don't support extra_float_digits greater than 2, causing sqlx to fail connecting to those dbs. Amazon Redshift happens to be one of those database, which is quite widely used.

At the moment we're sending extra_float_digits 3

This PR adds a compatible query string option for the connection string, so that users have a way to connect to older databases or Redshift.

I was inspired by the compatible option that JDBC uses: https://jdbc.postgresql.org/documentation/94/connect.html

Sample usage:

DATABASE_URL=postgres://my-db.com/db_name?compatible=8.5 # sends extra_float_digits 2
DATABASE_URL=postgres://my-db.com/db_name?compatible=9    # sends extra_float_digits 3
DATABASE_URL=postgres://my-db.com/db_name                             # sends extra_float_digits 3
+28 -1

0 comment

3 changed files

pr created time in 3 days

PR opened launchbadge/sqlx

Add rename_all to FromRow, add mixedCase and CamelCase to all rename_all

As per title.

Sample usage (from test)

#[derive(Debug, sqlx::FromRow)]
#[sqlx(rename_all = "camelCase")]
struct AccountKeyword {
    user_id: i32,
    user_name: String,
    user_surname: String,
}

It looks like this feature was present in a previous version / someone's intention because it was already documented (but not working).

The choice of using camelCase and PascalCase was taken for consistency with serde and with the existing documentation, even though heck's terminology is mixedCase and CamelCase

+100 -12

0 comment

5 changed files

pr created time in 3 days

PR opened launchbadge/sqlx

Add alias for mssql pool options
  • Add PoolOptions alias for mssql
+3 -0

0 comment

1 changed file

pr created time in 3 days

IssuesEvent

issue closedlaunchbadge/sqlx

sqlx-macros build failed on Windows with stable-x86_64-pc-windows-gnu

build sqlx with cargo build --target x86_64-pc-windows-gnu --features "runtime-tokio-rustls mysql" will failed with:

= note: C:/Users/Ray Eldath/scoop/apps/gcc/current/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: error: export ordinal too large: 78283
          collect2.exe: error: ld returned 1 exit status


error: aborting due to previous error

error: could not compile `sqlx-macros`

full error log has been posted to GitHub Gist.

closed time in 3 days

Ray-Eldath

issue openedlaunchbadge/sqlx

sqlx-macros build failed on Windows with stable-x86_64-pc-windows-gnu

build sqlx with cargo build --target x86_64-pc-windows-gnu --features "runtime-tokio-rustls mysql" will failed with:

= note: C:/Users/Ray Eldath/scoop/apps/gcc/current/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: error: export ordinal too large: 78283
          collect2.exe: error: ld returned 1 exit status


error: aborting due to previous error

error: could not compile `sqlx-macros`

full error log has been posted to GitHub Gist.

created time in 3 days

PR opened launchbadge/sqlx

Add support for passing in-memory trusted certificates in addition to…

Add support for passing in-memory trusted certificates in addition to the already supported path to trusted certificates.

## Background

We have use cases where the actually connect/pool configuration including trusted certificates is loaded from parameters stores (e.g. AWS SecretsManager) and we don't want to bother with writing and keeping track of temporary files respectively we have not reasonable filesystem at all (e.g. AWS Lambda or Docker containers with read-only filesystems).

+93 -23

0 comment

7 changed files

pr created time in 3 days

issue commentlaunchbadge/sqlx

One to Many (1:N) clarification

Shouldn't it be query_as!(Consumable, "SELECT * FROM consumable WHERE id = $1", id) instead of = ?

flopshot

comment created time in 4 days

issue commentlaunchbadge/sqlx

Connection reference in a Trait

Thank you very much for your help but it still doesn't work :(

benjamingb

comment created time in 4 days

issue commentlaunchbadge/sqlx

Connection reference in a Trait

Mmh I didnt' have time to check on a running exemple but I think something like that would work no?

Or am I missing something ?

pub struct TodoRepo;

#[async_trait]
impl<P: sqlx::Database>> ITodoRepo<P> for TodoRepo {
    async fn list(pool: impl sqlx::Executor<'_, Database = P>) -> Result<Vec<TodoType>> {
        let rowset = sqlx::query_as!(TodoSchema, r#"SELECT * FROM todo"#)
            .fetch_all(pool)
            .await?
            .iter()
            .map(|row| hydrate(row))
            .collect();

        Ok(rowset)
    }
}
benjamingb

comment created time in 4 days

issue commentlaunchbadge/sqlx

Connection reference in a Trait

when I use a direct reference if it works async fn list(pool: &PgPool) -> Result<Vec<TodoType>> https://github.com/benjamingb/rust/todo.rs#L13

I am trying to implement it with a trait so that it is generic and can be used in other crates

Here is a reference to the project https://github.com/benjamingb/rust-gql/tree/develop/src

benjamingb

comment created time in 4 days

more