0

I had an idea for a project a while back wherein I was going to write a Java library to allow sorting and selection from arrays using an SQL syntax of sorts.

My idea was that you could select from the array like this:

int[] nums = new int[] { 0, 5, 7, 2, 4, 9, 3, 1, 6, 8 }

int result = select().from(nums).where("value > 2").and("value < 8").sort("ASC");

This would return a new array like this:

{ 3, 4, 5, 6, 7 }

I haven't really thought of much of an implementation or a reason for doing this, but I think it would be a very interesting endeavor.

My question is, does anything like this exist? Or is there any special way I should go about this?

I was also thinking this could be useful for Object arrays, where you could select multiple fields, like this:

select().from(array).where("Field1 = "+someValue).or("Field1 = "+anotherValue);

And the possibilities go on and on. But I'm unsure exactly where to start, or better put, how to start implementing these methods.

This is more of a "where to go" than a question with an answer, so any help at all will be useful.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62

3 Answers3

3

What you are looking for is something similar to LINQ in C#.

You can refer these links for starters

LINQ for Java tool

What is the Java equivalent for LINQ?

You might also want to have a look at lambda expressions that are available in c#. I'm not sure what else to suggest.

But if you are trying to build something similar to LINQ in java then its a bit complex. You need to think in much of a object oriented fashion to seamlessly form sql queries.If that is the case then the answer to it is quite long(or big err..)

Community
  • 1
  • 1
Ajai
  • 3,440
  • 5
  • 28
  • 41
  • I'm looking more to build something. I wanted to see existing things like this for reference to what I could do. Thanks for those. – Jon Egeland Dec 10 '11 at 01:27
1

So something like LINQ for Java?

Well, there's jxpath, which allows a person to do XPath-style queries on java collections, which is kind of what you're looking for.

The jOOQ library has queries similar to the ones you've described, but it's meant for use on an actual database, as far as I can tell.

Paul Wostenberg
  • 449
  • 2
  • 9
1

You're right, I think this would be an interesting project.

There are loads of approaches you could take to this. One would be to create a Query object that you build up by calling the methods in turn.

So something like this:

public class Query {

  public static Query select(String...fields){
    //code omitted
    return query;
  }

  public Query from(Collection<Object> collection){
    //code omitted
    return this;
  }

  public Query where(String field, Object value){
    //code omitted
    return this;
  }

}

So you could then do:

Query.select("field1", "field2").from(myList)....

You'd need to keep track of which values had been selected at each point and whether they next condition should add to them (as in the case of an or) or remove from them (as in the case of an and). There will be lots of edge cases. My advice would be to start off with something very simple like the ints example and work from there.

When you get on to doing the object version you'll need to use reflection to access the fields of the object.

Once you've done that you might want to consider improving the type safety of your code by using generics.

All in all, lots of scope for an interesting project. However, it feels like the sort of thing where there will be lots of edge cases, so worth thinking through the design a fair bit and would also not be a bad idea to do a fair bit of unit testing.

Russell
  • 12,261
  • 4
  • 52
  • 75
  • Great explanation! I was thinking of using reflection, and I like the idea of using a class like `Query` instead of just making a set of functions. – Jon Egeland Dec 10 '11 at 02:05