gluesql / gluesql

GlueSQL is quite sticky. It attaches to anywhere.

Home Page:https://gluesql.org/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AUTOINCREMENT does not work?

pengyizeng opened this issue · comments

I create a simple table : user
eg:
let sql = "
CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);";

created successfully. Then I insert a record without specify the id number:

    let sql2 =  "INSERT INTO user(name) VALUES ('John'); ";

It occurs an error which tells me I did not specify the "id", how can I insert data without specify the "id"?

This feature is not currently supported.

ref: #159

This feature is not currently supported.

ref: #159

Thanks, then I use UUID as the "id" type, and insert data successfully by "generate_uuid()" . But when I select the data by an id:
eg:

 let uuid = "48743284824.....";
 let actual = table("user")
   .select()
   .filter(col("id").eq(uuid))
   .project("id, name")
   .build()
   .expect("build and execute")
   .execute(glue)
   .await;

went wrong, how to specify the filter when the id type is UUID?

(
r#"SELECT uuid_field AS uuid_field FROM UUID;"#,
Ok(select!(
uuid_field
Uuid;
parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8");
parse_uuid("550e8400-e29b-41d4-a716-446655440000");
parse_uuid("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")
)),
),
(
r#"UPDATE UUID SET uuid_field = 'urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4' WHERE uuid_field='550e8400-e29b-41d4-a716-446655440000'"#,
Ok(Payload::Update(1)),
),
(
r#"SELECT uuid_field AS uuid_field, COUNT(*) FROM UUID GROUP BY uuid_field"#,
Ok(select!(
uuid_field | "COUNT(*)"
Uuid | I64;
parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8") 1;
parse_uuid("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4") 2
)),
),
(
r#"DELETE FROM UUID WHERE uuid_field='550e8400-e29b-41d4-a716-446655440000'"#,
Ok(Payload::Delete(0)),
),
(
r#"DELETE FROM UUID WHERE uuid_field='urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4'"#,
Ok(Payload::Delete(2)),
),

(Value::Uuid(l), Literal::Text(r)) => l.partial_cmp(&parse_uuid(r).ok()?),

test(
Value::Uuid(parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8").unwrap()),
text("936DA01F9ABD4d9d80C702AF85C822A8"),
Some(Ordering::Equal),
);

Can you provide a little more information?

If I add the test case below, it works fine.

    let actual = table("Foo")
        .create_table()
        .add_column("id UUID")
        .add_column("name TEXT")
        .execute(glue)
        .await;
    let expected = Ok(Payload::Create);
    assert_eq!(actual, expected, "create table");

    let actual = table("Foo")
        .insert()
        .columns("id, name")
        .values(vec![vec![
            text("936DA01F9ABD4d9d80C702AF85C822A8"),
            text("hi"),
        ]])
        .execute(glue)
        .await;
    let expected = Ok(Payload::Insert(1));
    assert_eq!(actual, expected, "insert");

    let actual = table("Foo")
        .select()
        .filter(col("id").eq(text("936DA01F9ABD4d9d80C702AF85C822A8")))
        .project("id, name")
        .execute(glue)
        .await;

    let uuid = Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8")
        .unwrap()
        .as_u128();
    let expected = Ok(select!(
        id     | name
        Uuid   | Str;
        uuid   "hi".to_owned()
    ));
    assert_eq!(actual, expected);

I think I should use parse_uuid() instead of using the u128 string format, thanks a lot!