views:

62

answers:

1

I have a set of sql - queries:

List<String> queries = ...
queries[0] = "select id from person where ...";
...
queries[8756] = "select id from person where ...";

Each query selects rows from the same table 'person'. The only difference is the where-clause. Table 'person' looks like this:

   id | name | ... many other columns

How can i determine which queries will contain a certain person in it's result set?

For example:

List<Integer> matchingQueries = magicMethod(queries, [23,45]); 

The list obtained by 'magicMethod' filters all sql queries present in the list 'queries' (defined above) and returns only those that contain either the person with id 23 OR a person with id 45.

Why i need it: I am dealing with an application that contains products and categories where the categories are sql queries that define which products belong to them (queries stored in a table also). Now i have a requirement where an admin has to see all categories an item belongs to immediately after the item was created. Btw, over 8.000 categories defined (so far, more to come).

language and db: java && postgreSQL

Thanks,

A: 

I can think of 2 ways. First way is to create a value object Query which contains two properties: a Long id query ID and a List<Person> results. Another way is to include the query ID in the results. E.g.

queries[0] = "SELECT id, name, etc, 0 AS query FROM person WHERE ...";
// ...
queries[8756] = "SELECT id, name, etc, 8756 AS query FROM person WHERE ...";

or

queries[0] = "SELECT id, name, etc, %d AS query FROM person WHERE ...";
// ...
queries[8756] = "SELECT id, name, etc, %d AS query FROM person WHERE ...";

for (int i = 0; i < queries.length; i++) {
    String sql = String.format(queries[i], i);
    // ...
}

so that you can re-obtain it by

int queryId = resultSet.getInt("query");

That said, this is all with all a bit a smell (DRY). I'd recommend to rethink the whole approach. Feel free to ask it as a new question, "I have functional requirement XYZ, I have approached it as follows. Is it right?".

BalusC
that was the soltion i was thinking of. well, the situation is like this: existing app with 800.000 products and around 8.000 categories. the products are created by 'people in black suits' that dont know a thing about a propper category. if a poduct belongs to a category or not depends on its attributes. also over time some categories change. its easier to change 50 category definitions than to walk through 800.000 products and re-assign them. anyway a category is nothing more than a requirement a product has to fullfill. But i am open for suggestions :)
ManBugra