jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.3k stars 1.09k forks source link

Add support for clickhouse settings in NamedExec's fixBounds #948

Open stephennancekivell opened 1 month ago

stephennancekivell commented 1 month ago

Hi folks, im having trouble with bulk inserts into clickhouse. NamedExec queries are only getting the first value in the array.

This PR fixes the issue by extending the regex to support settings.

Eg this test case fails, with only joe getting inserted, not sally.

func TestInsertNamedBulk(t *testing.T) {
    _, err := db.Exec(schema)
    assert.NoError(t, err)
    defer cleanup(t)

    // Insert data
    _, err = db.NamedExec(
        `insert into users (id, name, age)
        SETTINGS async_insert=1, wait_for_async_insert=1
        values
        (:id, :name, :age)`,
        []map[string]any{
            {
                "id":   1,
                "name": "joe",
                "age":  30,
            },
            {
                "id":   2,
                "name": "sally",
                "age":  31,
            },
        })
    assert.NoError(t, err)

    users, err := findUsers()
    assert.NoError(t, err)
    assert.Equal(t, []User{
        {ID: 1, Name: "joe", Age: 30},
        {ID: 2, Name: "sally", Age: 31},
    }, users)
}