diff --git a/.github/workflows/docker_ci.yml b/.github/workflows/docker_ci.yml index 1e79d9506d..7d2a92e4f8 100644 --- a/.github/workflows/docker_ci.yml +++ b/.github/workflows/docker_ci.yml @@ -73,6 +73,8 @@ jobs: echo $VFLAGS ./v cmd/tools/test_if_v_test_system_works.v ./cmd/tools/test_if_v_test_system_works + - name: Add dependencies + run: apt install libsqlite3-dev - name: All code is formatted run: ./v -silent test-cleancode - name: Test V fixed tests diff --git a/vlib/orm/orm.v b/vlib/orm/orm.v index 642e164cb7..df7ce59b82 100644 --- a/vlib/orm/orm.v +++ b/vlib/orm/orm.v @@ -392,17 +392,13 @@ pub fn orm_select_gen(cfg SelectConfig, q string, num bool, qm string, start_pos fn gen_where_clause(where QueryData, q string, qm string, num bool, mut c &int) string { mut str := '' + for i, field in where.fields { - mut pre_par := false - mut post_par := false - for par in where.parentheses { - if i in par { - pre_par = par[0] == i - post_par = par[1] == i - } - } - if pre_par { - str += '(' + current_pre_par := where.parentheses.count(it[0] == i) + current_post_par := where.parentheses.count(it[1] == i) + + if current_pre_par > 0 { + str += ' ( '.repeat(current_pre_par) } str += '${q}${field}${q} ${where.kinds[i].to_str()}' if !where.kinds[i].is_unary() { @@ -412,8 +408,8 @@ fn gen_where_clause(where QueryData, q string, qm string, num bool, mut c &int) c++ } } - if post_par { - str += ')' + if current_post_par > 0 { + str += ' ) '.repeat(current_post_par) } if i < where.fields.len - 1 { if where.is_and[i] { diff --git a/vlib/orm/orm_complex_where_test.v b/vlib/orm/orm_complex_where_test.v new file mode 100644 index 0000000000..90632b020b --- /dev/null +++ b/vlib/orm/orm_complex_where_test.v @@ -0,0 +1,45 @@ +// vtest build: present_sqlite3? +import db.sqlite + +struct ComplexWhere { +pub mut: + id int + name string + rank f32 +} + +fn test_create_without_id_field() { + db := sqlite.connect(':memory:')! + + sql db { + create table ComplexWhere + }! + + datas := [ + ComplexWhere{ + id: 0 + name: 'test1' + rank: 1.5 + }, + ComplexWhere{ + id: 1 + name: 'test2' + rank: 2.5 + }, + ComplexWhere{ + id: 2 + name: 'test3' + rank: 3.5 + }, + ] + + for data in datas { + sql db { + insert data into ComplexWhere + }! + } + + res := sql db { + select from ComplexWhere where name == 'a' && (id > 1 || (rank > 2.5 && rank < 3.33)) + } or { assert false, err.msg() } +}