lua-resty-cassandra icon indicating copy to clipboard operation
lua-resty-cassandra copied to clipboard

Support binding by name (key/value table)

Open thibaultcha opened this issue 10 years ago • 1 comments

Currently binding is only possible with ordered values:

local ok, err = session:execute([[
  INSERT INTO users (name, age, user_id)
  VALUES (?, ?, ?)
]], {"John O'Reilly", 42, cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11")})

If I want to bind a query from a table representing my entity, such as:

local user = {
  name = "John O'Reilly",
  age = 42,
  user_id = cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11")
}

local ok, err = session:execute([[
  INSERT INTO users (name, age, user_id)
  VALUES (?, ?, ?)
]], user)

It won't be possible because user is not an ordered list corresponding to the order of the ? placeholders.

A very handy feature, proposed by the sqlite driver would be to bind values by name, such as:

local user = {
  name = "John O'Reilly",
  age = 42,
  user_id = cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11")
}

local ok, err = session:execute([[
  INSERT INTO users (name, age, user_id)
  VALUES (:name, :age, :user_id)
]], user)

Would such a feature be considered? Is it a limitation from Cassandra itself and if so, could simple string manipulation be considered?

thibaultcha avatar Feb 02 '15 23:02 thibaultcha

That would be a great feature. The protocol allows this without string manipulation by the client, but only in v3. We would need to support it first (#22).

QUERY, EXECUTE and BATCH messages can now optionally provide the names

for the values of the query. As this feature is optionally enabled by clients, implementing it is at the discretion of the client. On Feb 2, 2015 9:56 PM, "thibaultCha" [email protected] wrote:

Currently binding is only possible with ordered values:

local ok, err = session:execute([[ INSERT INTO users (name, age, user_id) VALUES (?, ?, ?)]], {"John O'Reilly", 42, cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11")})

If I want to bind a query from a table representing my entity, such as:

local user = { name = "John O'Reilly", age = 42, user_id = cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11") } local ok, err = session:execute([[ INSERT INTO users (name, age, user_id) VALUES (?, ?, ?)]], user)

It won't be possible because user is not an ordered list corresponding to the order of the ? placeholders.

A very handy feature, proposed by the sqlite driver http://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki#stmt_bind_names would be to bind values by name, such as:

local user = { name = "John O'Reilly", age = 42, user_id = cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11") } local ok, err = session:execute([[ INSERT INTO users (name, age, user_id) VALUES (:name, :age, :user_id)]], user)

Would such a feature be considered? Is it a limitation from Cassandra itself and if so, could simple string manipulation be considered?

— Reply to this email directly or view it on GitHub https://github.com/jbochi/lua-resty-cassandra/issues/28.

jbochi avatar Feb 03 '15 00:02 jbochi