scylladb/gocqlx

cmp: EqLit may break the query

Opened this issue · 1 comments

There is a query builder code using the table model table.SchedRun.UpdateBuilder("status").If(qb.EqLit("status", "RUNNING")), it generates

AAA [query statement="UPDATE scheduler_task_run SET status=? WHERE cluster_id=? AND type=? AND task_id=? AND id=? IF status=RUNNING " values=[FOO 30f3ac8b-4f29-4beb-8b1d-d40020586a96 mock fe0e6697-ad19-47af-a359-8c2f8d7a4e0d eea3301d-275b-11ec-bf87-c85b76f42222] consistency=QUORUM]

Which fails with no viable alternative at input.
As it turns out the issue is the lack of quotes i.e. IF status='RUNNING'.
The workaround is by using EqNamed instead.

From what I have seen using this function, we must not use a single quote ( ' ) when the column type is UUID for an instance. I personally fixed my issue like this:

qb.EqLit("string_id", fmt.Sprintf("'%s'", stringid))

Since we can't have the column type in qb, I don't think there is any good way to fix this function. we could update the docs, or add a new function like

qb.EqLitStr that does like this:

func EqLitStr(column, literal string) Cmp {
	return Cmp{
		op:     eq,
		column: column,
		value:  litStr(literal),
	}
}
type litStr string

func (l litStr) writeCql(cql *bytes.Buffer) (names []string) {
	cql.WriteString("'")
	cql.WriteString(string(l))
	cql.WriteString("'")
	return nil
}