Coder Social home page Coder Social logo

Comments (2)

ptdecker avatar ptdecker commented on May 28, 2024

This code works. It manually decodes it properly step by step. I'm sure you probably have a better way of doing this in the libraries but I'm not familiar enough with how your library works internally. Hopefully this code will help and there is an easier way to solve this.

                let conn = rb.acquire().await?;
                let encoded_value = conn.query("SELECT sqlite_version()", vec![]).await;
                let array_value = match encoded_value {
                    Ok(rbs::Value::Array(array_value)) => array_value,
                    Ok(_) => return Err(anyhow!("Expected array but got something else")),
                    Err(e) => return Err(anyhow!("Query error: {}", e)),
                };
                let first_array_value = match array_value.len() {
                    1 => array_value[0].clone(),
                    _ => {
                        return Err(anyhow!(
                            "Expected only one element in the array--got {}",
                            array_value.len()
                        ))
                    }
                };
                let map_value = match first_array_value {
                    rbs::Value::Map(map_value) => map_value,
                    _ => return Err(anyhow!("Expected map but got something else")),
                };
                let first_map_element_value = match map_value.into_iter().next() {
                    Some(value) => value.1,
                    None => return Err(anyhow!("Expected map to contain one element it has none")),
                };
                let version = match first_map_element_value {
                    rbs::Value::String(decoded_version) => decoded_version,
                    _ => return Err(anyhow!("Expected string but got something else")),
                };

from rbatis.

zhuxiujia avatar zhuxiujia commented on May 28, 2024

The following code used to work to get the SQLite version:

                match rb.query_decode("SELECT sqlite_version()", vec![]).await? {
                    Some(v) => {
                        eprintln!("b");
                        Ok(v)
                    }
                    None => Err(anyhow!("Unable to obtain database version")),
                }

It fails with the following error: "Database error: Sqlite: invalid type: map, expected a string"

To get more information, I used the following code extracted from query_decode():

                let conn = rb.acquire().await?;
                let v = conn.query("SELECT sqlite_version()", vec![]).await?;
                eprintln!("v: {:?}", v);

What I get for v is:

v: Array([Map({String("sqlite_version()"): String("3.45.0")})])

So, apparently decode() [which is what is called next in query_decode] cannot handle that return structure from SQLite.

Any ideas on how to resolve?

  • this unit test work's well, did you delete Cargo.lock and run command cargo update to update rbatis version?
    #[test]
    fn test_decode_string() {
        let v: String = rbatis::decode(Value::Array(vec![to_value!{
            "a":"a",
        }])).unwrap();
        assert_eq!(v, "a");
    }
Testing started at 10:29 ...
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running tests/decode_test.rs (target/debug/deps/decode_test-96029c5cf93b50f4)

image

from rbatis.

Related Issues (20)

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.