How to easy create query like types : ( val1 + OR + val2 + OR + val3) with Spring Data Solr (With Dynamic Queries) - spring-data-solr

I have field "types" multivalue=true.
From client layer I receive list of keys what should be processed.
I want to create FilterQuery to produce fq like fq=types:(6+OR+4+OR+8)
logically I expect to use next code:
final Criteria criteria = new Criteria("types").in(types);
new SimpleFilterQuery(criteria);
But this code produce fq=types:(6+4+8)
It could works, but I use q.op=AND (required for other needs)
Is it any simple way to do something like inOr/inAny ?

Actually I found who ways, but this looks not so beauty as I expect:
First:
Criteria criteria = new SimpleStringCriteria("types:(" + StringUtils.join(statuses, " OR ") + ")");
new SimpleFilterQuery(criteria);
Second:
Criteria criteria = new Criteria("types");
criteria.expression("(" + StringUtils.join(statuses, " OR ") + ")");
new SimpleFilterQuery(criteria);

Related

Order of JSON returned by ORMLite?

I'm new to web services and trying to query some tables using ORMLite, it doesn't support join statements so I'm using a raw query. I was wondering if there is a way to specify how the JSON is returned. What I have right now is:
Dao<CodesModel,String> CodesDao = DaoManager.createDao(connectionSource, CodesModel.class);
GenericRawResults<String[]> rawResults =
CodesDao.queryRaw(
"select r.CodeA, s.SubCodeA, r.CodeB, s.SubCodeB " +
"from CodesTable r JOIN SubCodesTable s ON s.CodeA = r.CodeA " +
"where SubCodeB = '" + b_sub + "' AND r.CodeB = '" + b_code + "'");
And the results are returned as a String[] and always seem to be in the order of
[CodeA, SubCodeA, CodeB, SubCodeB]
but I have only tested this locally and can't find in the documentation what determines the order for variables in the array that is returned.
The results are ordered that way because that is the order in which you specified them in the select statement. If you want the results ordered in a different way, reorder them in your query.
If anyone is looking for a way to know column names when using "select *" you can also use "getColumnNames()" on the rawResults object and they will always be in the order that the results are. Examples
//The result is returned as a GenericRawResults object
List<String[]> results = rawResults.getResults();
String[] columns = rawResults.getColumnNames();
JSONObject obj = new JSONObject();
if(results.size()>0)
{
obj.put(columns[0], results.get(0)[0]);
obj.put(columns[1], results.get(0)[1]);
obj.put(columns[2], results.get(0)[2]);
obj.put(columns[3], results.get(0)[3]);
}

How to use single quotes for XPath in Selenium Webdriver?

I'm trying to pass an i value to fetch random selection check boxes with dynamic ids and the code is pasted below with the corresponding error message.
By.xpath("//div[contains(#id,'team_"+randNumber(i)+")]")) != null){
Error appears as:
The given selector //div[contains(#id,'team_'2)] is either invalid or does not result in a WebElement.
Thus the above term should has to display as //div[contains(#id,'team_2')]
This has nothing to do with Selenium, this is purely a Java String manipulation problem!
int rand = randNumber(i);
String id = "team_" + rand;
String xpathh = "//div[contains(#id, '" + id + "']"; // note the nested quotes!
By.xpath(xpathh));
If you want to roll this into one line, copy pasting everything together yields:
By.xpath("//div[contains(#id, '" + "team_" + randNumber(i) + "']"));
A couple of the quotes could be optimized out:
By.xpath("//div[contains(#id, 'team_" + randNumber(i) + "']"));
Should be something like this:
By.xpath("//div[contains(#id,'team_"+randNumber(i)+"')]")

IF-ELSE Alternative for Multiple SQL criteria for use in BIRT

I want to create a report by using BIRT. I have 5 SQL criterias as the parameter for the report. Usually when I have 3 criterias, I am using nested if-else for the WHERE statement with javascript.
Since right now I have more criteria it becomes more difficult to write the code and also check the possibilities, especially for debug purposes.
For example the criteria for table employee, having these 5 criterias : age, city, department, title and education. All criteria will be dynamic, you can leave it blank to show all contents.
Do anyone know the alternative of this method?
There is a magical way to handle this without any script, which makes reports much easier to maintain! We can use this kind of SQL query:
SELECT *
FROM mytable
WHERE (?='' OR city=? )
AND (?=-1 OR age>? )
AND (?='' OR department=? )
AND (?='' OR title=? )
So each criteria has two dataset parameters, with a "OR" clause allowing to ignore a criteria when the parameter gets a specific value, an empty value or a null value as you like. All those "OR" clauses are evaluated with a constant value, therefore performances of queries can't be affected.
In this example we should have 4 report parameters, 8 dataset parameters (each report parameter is bound to 2 dataset parameters) and 0 script. See a live example of a report using this approach here.
If there are many more criteria i would recommend to use a stored procedure, hence we can do the same with just one dataset parameter per criteria.
Integer parameter handling
If we need to handle a "all" value for an integer column such age: we can declare report parameter "age" as a String type and dataset parameters "age" as an integer. Then, in parameters tab of the dataset use a value expression instead of a "linked to report parameters". For example if we like a robust input which handles both "all" "null" and empty values here is the expression to enter:
(params["age"].value=="all" || params["age"].value=="" || params["age"].value==null)?-1:params["age"].value
The sample report can be downloaded here (v 4.3.1)
Depending on the report requirements and audiance you may find this helpful.
Use text box paramaters and make the defualt value % (which is a wild card)
SELECT *
FROM mytable
WHERE city like ?
AND age like ?
AND department like ?
AND title like ?
This also allows your users to search for partial names. if the value in the city text box is %ville% it would return all the cities with "ville" anyplace in the city name.
If report parameters to be included in SQL-WHERE clause would be named according to some naming convention, for instance query_employee_[table column name], you could write Java-Script code in a generic way, so that you will not have to change it when new reporters being added.
for each param in params {
if param.name starts with query_employee_ {
where_clause += " and " + param.name.substring(after query_employee) + " == '" + param.value + "'";
}
}
You will have to check type of a parameter to make a decision whether you have to quote the parameter value.
The event handler could look as follows (implemented in Java, but it should be possible to port it to JavaScript, if you really need it to be in JavaScript):
public class WhereConditionEventHandler extends DataSetEventAdapter {
#Override
public void beforeOpen(IDataSetInstance dataSet,
IReportContext reportContext) throws ScriptException {
super.beforeOpen(dataSet, reportContext);
String whereClause = " where 1 = 1 ";
SlotHandle prms = reportContext.getDesignHandle().getParameters();
for (int i = 0; i < prms.getCount(); i++) {
if (prms.get(i) instanceof ScalarParameterHandle) {
ScalarParameterHandle prm = (ScalarParameterHandle) prms.get(i);
int n = prm.getName().indexOf("sql_customer_");
if (n > -1) {
String prmValue = "" + reportContext.getParameterValue(prm.getName());
if (DesignChoiceConstants.PARAM_TYPE_STRING.equals(prm.getDataType())) {
prmValue = "'" + prmValue + "'";
}
whereClause += " and " + prm.getName().substring("sql_customer_".length()) + " = " + prmValue;
}
}
}
System.out.println("sql: " + whereClause);
dataSet.setQueryText(dataSet.getQueryText() + whereClause);
}
}
By the way, you can pass in parameters that are not registered as report parameters in the BIRT report design. BIRT will nevertheless put them into "params" array.

DELETE FROM table WHERE var = value does not remove entries

So I fetched some data from a mdb file in c# via
"SELECT * FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE Note = '" + listBox2.GetItemText(listBox2.SelectedItem).Replace("'","\'") + "'";
which selects the right data, here it is
SELECT * FROM Main WHERE Note ='Hello'
The mdb data structure looks like this being plotted as a CSV-file:
"Record ID";Status;Placement;Private;Category;Note;Blob
14341665;4;2147483647;True;3;"""Hello"" - Neues
But when I try to remove entries with
"DELETE FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE \"Record ID\" LIKE '" + dr[0] + "';";
or
"DELETE FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE \"Record ID\" = '" + dr[0] + "';";
which looks like for instance
DELETE FROM Main WHERE "Record ID" LIKE '14341665';
The entries just stay there. I can rerun the select command even restart my application, the mdb is not changed.
Is record ID a numeric field? If so, lose the quotes.
DELETE FROM Main WHERE [Record ID] = 14341665;
Note that spaces in field (column) names will always be a problem. Such columns names have to be enclosed in square brackets, as do columns named with reserved words.
The record id is numeric, so don't put apostrophes around it:
"DELETE FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE \"Record ID\" = " + dr[0]
Note: You should avoid using select * in production code, you should specify the data that you want returned. Also, you should use parameterised queries instead of concatenating values into the query.
if i remember correctly, "like" only works on string data, please check the data type of Record ID.
If Record ID is numeric, you may want to use database's conversion function to convert it into string before filtering using "like".
btw, remember to make sure that dr[0] is properly escaped.

Is it possible to generate and execute Python code in a Python script? [Dynamic Python code]

I am working on some reports (counts) and I have to fetch counts for different parameters. Pretty simple but tedious.
A sample query for one parameter :
qCountsEmployee = (
"select count(*) from %s where EmployeeName is not null"
% (tablename)
)
CountsEmployee = execute_query(qCountsEmployee)
Now I have few hundred such parameters!
What I did was : create a list of all the parameters and generate them using a quick Python script and then copy that text and put it in the main script to avoid the tedious lines.
columnList = ['a', 'b', ............'zzzz']
for each in columnList:
print (
'q' + each + ' ='
+ '"select count(*) from %s where' + each
+ 'is not null" % (tablename)'
)
print each + ' = execute_query(' + 'q' + each + ')'
While this approach works, I was wondering if instead of a separate script to generate lines of code and copy paste into main program, can I generate them in the main script directly and let the script treat them as lines of code? That will make the code much more readable is what I think. Hope I made sense! Thank you...
It would be possible, but is not useful here.
Just do something like
columnList = ['a', 'b', ............'zzzz']
results = {}
for column in columnList:
query = (
"select count(*) from " + tablename
+ " where " + column + " is not null"
)
result = execute_query(qCountsEmployee)
results[column] = result
You as well can put all this together in a generator function and do
def do_counting(column_list):
for column in column_list:
query = (
"select count(*) from " + tablename
+ " where " + column + " is not null"
)
result = execute_query(qCountsEmployee)
yield column, result
result_dict = dict(do_counting(['...']))
You can do:
cmd = compile( 'a = 5', '<string>', 'exec' )
exec( cmd )
That is the same as just writing:
a = 5
The string passed as the first argument to compile can be built dynamically.
To build on what glglgl said, you are probably better with dynamic SQL than dynamic Python (though Dynamic Python is definitely possible using things like eval ). When working with Dynamic SQL, you should be careful of sql injection. It seems like it would not come up in your particular use-case, but it certainly comes up more often than many developers realize.
I happen to have written an article about SQL Injection and Python which is available at Simple-talk.