Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Insert does not work well with Rusqlite prepare() #801

Closed
XHanL opened this issue Aug 9, 2024 · 0 comments
Closed

Query Insert does not work well with Rusqlite prepare() #801

XHanL opened this issue Aug 9, 2024 · 0 comments

Comments

@XHanL
Copy link

XHanL commented Aug 9, 2024

Description

The Rusqlite prepare() + execute() method need format like:

INSERT INTO \"virtual_file\" (\"path\", \"name\") VALUES (?, ?)

However, if we use Query::insert() without values_panic(), the build string look like this:

INSERT INTO \"virtual_file\" (\"path\", \"name\") /* NO VALUE PART */

If we execute this prepared stmt, the DB return error

SqliteFailure(Error { code: Unknown, extended_code: 1 }, Some("incomplete input"))

We have to add empty values manually like this, It's unfriendly and confusing():

.values_panic(["".into(), "".into(), ...])

Steps to Reproduce

#[derive(Iden)]
pub enum VirtualFile {
    Table,
    Id,
    Path,
    Name,
}

fn test_insert() {
    let conn = Connection::open("../sqlite.db").unwrap();

    let sql = [Table::create()
        .table(VirtualFile::Table)
        .if_not_exists()
        .col(
            ColumnDef::new(VirtualFile::Id)
                .integer()
                .primary_key()
                .auto_increment(),
        )
        .col(ColumnDef::new(VirtualFile::Path).string().not_null())
        .col(ColumnDef::new(VirtualFile::Name).string().not_null())
        .build(SqliteQueryBuilder)];
    
    conn.execute_batch(&sql.join("; ")).unwrap();

    let (sql, _) = Query::insert()
        .into_table(VirtualFile::Table)
        .columns([VirtualFile::Path, VirtualFile::Name])
        //.values_panic(["".into(), "".into()]) // need to add this manually for insert stmt prepare
        .build(SqliteQueryBuilder);

    println!(">>> {:?}", &sql);

    let mut stmt = conn.prepare(&sql).unwrap();
    stmt.execute(("path", "name")).unwrap();
}

Expected Behavior

When we use Query::insert() without values_panic(), the builder will generate VALUES (?, ?, ...) based on columns,

INSERT INTO \"virtual_file\" (\"path\", \"name\") VALUES (?, ?)

Actual Behavior

When we use Query::insert() without values_panic(), the builder generate

INSERT INTO \"virtual_file\" (\"path\", \"name\")

Reproduces How Often

  • Everytimes

Versions

  • sea-query v0.31.0
  • sea-query-derive v0.4.1 (proc-macro)
  • rusqlite
  • macOS 14.6.1 (23G93)
@XHanL XHanL changed the title Query Select does not work well with Rusqlite prepare() Query Insert does not work well with Rusqlite prepare() Aug 9, 2024
@XHanL XHanL closed this as not planned Won't fix, can't repro, duplicate, stale Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant