For a set of sql-queries, how do you determine which result-set contains a certain row?
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 co开发者_如何学Cme).
language and db: java && postgreSQL
Thanks,
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?".
精彩评论