digital-fabric / extralite

Ruby on SQLite
http://www.rubydoc.info/gems/extralite
MIT License
255 stars 8 forks source link

Streamline and improve query methods #67

Closed noteflakes closed 10 months ago

noteflakes commented 10 months ago

This PR streamlines the different query APIs, adds value transforms and introduces a new argv mode:

transform = ->(h) { MyModel.new(h) }
db.query(transform, 'select * from foo')

transform = ->(a, b, c) { { a: a, b: b, c: JSON.parse(c) } }
db.query_argv(transform, 'select a, b, c from foo')

# transforms on prepared queries
q = db.prepare('select * from foo') { |h| MyModel.new(h) }
q.to_a #=> transformed rows

q = db.prepare_argv('select a, b, c from foo') { |a, b, c| { a: a, b: b, c: JSON.parse(c) } }
q.to_a #=> transformed rows

The #query_argv method can be significantly faster than #query_ary, but more benchmarks are needed. All in all the transforms in themselves don't really change much in terms of performance, it's more of a convenience.

noteflakes commented 10 months ago

@gschlager would you like to take a look and tell me what you think?

gschlager commented 10 months ago

That's a fantastic feature. I'll have a closer look at the beginning of next week.