0

I need to translate SQL statement to Realm (to get record stored in Realm based on SQL statement). I just need to handle SELECT statement with SQL parameters and translate it to Realm. For example:

SELECT * FROM tblCustomer WHERE (Name = 'John Doe' OR Name = 'Joe Black') AND (Postcode = '10013')

I need to evaluate anything after WHERE statement so I can filter and return appropriate records. So, from example above, it is translated to Realm like this

realm.where(tblCustomer.class)
     .beginGroup()
          .equalTo("Name", "John Doe")
          .or()
          .equalTo("Name", "Joe Black")
     .endGroup()
     .beginGroup()
          .equalTo("Postcode", "10013")
     .endGroup();

I have tried to use Dijsktra's algorithm (Evaluate.java) to evaluate the WHERE parameters, but I am having problem with translating spaces.

I am just wondering, does anyone have algorithms ready to evaluate SQL parameters? If yes, I'll just use your algorithm instead of creating one myself.

Thanks.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Sam
  • 1,826
  • 26
  • 58
  • Um. You know Realm is not relational (as it is an object store), and also a NoSQL database, right? You'll run into trouble with HAVING, GROUPBY, projection for "not *", and JOINs. – EpicPandaForce May 21 '17 at 09:11
  • Yes, I know, I just need to evaluate simple SQL condition. I don't even need to worry about IN operator. – Sam May 21 '17 at 11:54
  • Btw, EpicPandaForce, you are awesome!!! Thanks for editing my question. I didn't know about beginGroup()! You are the best! – Sam May 21 '17 at 11:55
  • Basically what you need is a SQL parser. Check this question http://stackoverflow.com/questions/660609/sql-parser-library-for-java or just search for “sql parser". – beeender May 22 '17 at 02:43
  • @EpicPandaForce, your answer is excellent, but why you wrote it inside to the question? – Sergei Bubenshchikov May 22 '17 at 04:54
  • @Sergey because it doesn't answer his question, so it's a comment. – EpicPandaForce May 22 '17 at 05:42
  • @beeender, this is exactly what I am looking for. please post it as answer and I'll accept your answer. Thanks! – Sam May 22 '17 at 06:23

0 Answers0