yahoo / squidb

SquiDB is a SQLite database library for Android and iOS
https://github.com/yahoo/squidb/wiki
Apache License 2.0
1.31k stars 132 forks source link

(Wiki-request) Explanation and example usage of (custom) SQLite functions using the abstract Functions class #108

Closed dotWee closed 9 years ago

sbosley commented 9 years ago

Can you give some more details about your function? If it's a function that takes some arguments, you can simply use Function.functionWithArguments("functionName", arg1, arg2, ...). Or did you have something else in mind?

dotWee commented 9 years ago

I'm actually not sure how to use SQLite functions with Squidb. For example, if I want to use the date function: SELECT WHERE (property) IS date('now') (¹), which methods should I use and how to implement it into a query?

sbosley commented 9 years ago

Have you seen this wiki page? It outlines most of the basic use cases for functions.

SquiDB doesn't build in a wrapper for every SQLite function, but for the ones it doesn't you can use Function.functionWithArguments as I suggested above. Criterions generally work with arbitrary objects, including Functions. So in addition to doing Model.MY_PROPERTY.eq(1) you can do Model.MY_PROPERTY.eq(someFunction), e.g.:

Function dateFunction = Function.functionWithArguments("date", "now", otherArgs);
Query query = Query.select()
    .from(Model.TABLE)
    .where(Model.MY_PROPERTY.is(dateFunction));

Does that help?

dotWee commented 9 years ago

That helped me a lot! Thanks!