In my situation, one of the query with params needs to be looped through based on the length of request object.
string Query = QM.FindQuery("12");
int paramCnt = Count * 2;
SqlParameter[] sqlParam = new SqlParameter[1 + paramCnt];
sqlParam[0] = new SqlParameter("#NUMBER", SqlDbType.Char);
sqlParam[0].Value = Convert.ToString(StudRequest.STUDNUMBER);
Queries.Append(Query);
for (int Index = 0; Index < Count; Index++)
{
Query = QM.FindQuery("2");
//Param1
//Param2
Queries.Append(Query);
}
Queries.AppendLine("END");
DM.ExecuteNonQuery(Queries.ToString(), sqlParam);
Now in above case, how to handle Param1 & Param2. Or any other alternative way to handle the query with SQLparams
Related
I know jdbcTemplate can be used to create PreparedStatements if you set it up to do so:
i.e.
private JdbcTemplate jdbcTemplate;
String sqlQuery = "Select * from table where column = ?";
String value = "value";
this.jbdcTemplate.query(sqlQuery, new Object[] { value }, rs, rowNum -> {
System.out.println(rs.getString("column"));
});
However, I have a query with many AND operators in the Where clause, and depending on some condition, a specific AND statement may or may not be added to the query string.
i.e.
private JdbcTemplate jdbcTemplate;
StringBuilder sqlQuery = new StringBuilder("Select * from table where 1=1 "); //base query
if(someCondition)
sqlQuery.append("And column1 = '" + value1 + "'");
if(someCondition2)
sqlQuery.append("And column2 = '" + value2 + "'");
if(someCondition3)
sqlQuery.append("And column3 = '" + value3 + "'");
//etc...
With this type of dynamic query, I am unable to know exactly how many values to place in the new Object[] {} field at compile time, because the someCondition fields are always changing at runtime.
Is there a way to write the this.jdbcTemplate.query(sqlQuery.toString(), new Object[] {}... to accommodate these dynamic AND statements?
I found a solution after taking into consideration what #mustaccio said in his comment to my original question. I also took part of the solution from this stackoverflow question and used it in my own solution.
The main issue I was having was dynamically creating an Object[] array at runtime, since you can't dynamically add elements to an Object[] array. They must have a defined size when initialized.
First, I create an arraylist of strings called queryArgs. Every time one of the if conditions proves true and we add an AND statement to the query, I also add another line of code that adds the value to be plugged in the preparedStatement to the queryArgs arraylist. Once that's done, I create a new Object[] array whose size is initialized to the size of the queryArgs arraylist. Lastly, I loop through each element in the Object[] array, setting them equal to the values in queryArgs.
private JdbcTemplate jdbcTemplate;
List<QueryResults> jdbcQuery(QueryParams queryParams) {
/* base query */
StringBuilder sqlQuery = new StringBuilder("Select * from table where 1=1 ");
/* stores the dynamic preparedStatement arguments */
List<String> queryArgs = new ArrayList<>();
if(someCondition){
sqlQuery.append("And column1 = ? ");
queryArgs.add(queryParams.value1);
}
if(someCondition2){
sqlQuery.append("And column2 = ? ");
queryArgs.add(queryParams.value2);
}
if(someCondition3){
sqlQuery.append("And column3 = ? ");
queryArgs.add(queryParams.value3);
}
//etc...
/* this is the part I used from the above stackoverflow question */
Object[] preparedStatementArgs = new Object[queryArgs.size()];
for(int i = 0; i < preparedStatementArgs.length; i++){
preparedStatementArgs[i] = queryArgs.get(i);
}
/* Lastly, execute the query */
return this.jdbcTemplate.query(sqlQuery.toString(),
preparedStatementArgs, (rs, rowNum) -> {
QueryResults result = new QueryResults();
/* store the results of the query... */
});
}
The outlier is that one of the dynamic AND statements above is written like this:
AND column4 IN ('x','y','z','etc..')
, where the values inside of the parentheses are also dynamic at runtime. My service receives a string value that looks like this:
String queryParams.value4 = "x,y,z,etc...";
I can't write the preparedStatement like this: AND column4 IN (?) and then simply plug in queryParams.value4 because it will treat queryParams.value4 as a string literal, which leads to errors.
To solve this issue, I create another arraylist of strings called value4Array. I loop through every character in queryParams.value4, and I check if the current character in the loop is equal to a comma, our delimiter. If it is, then I create a substring of all the characters leading up to that comma, and add that newly created string to value4Array.
The next step is to create the dynamic AND column4 IN (?) statement. I do this by looping through each string value in the value4Array arraylist we just created, and doing a sql.append("?"), based on how many strings are in value4Array. After this, the rest of the logic is the same as my solution above.
/* this function takes the comma delimited string literal (value4 : "x,y,z,etc...")
and parses it into an array of strings. */
private List<String> parseValue4(String value4){
int valueIndex= 0;
List<String> value4Array = new ArrayList<>();
for(int i = 0; i < value4.length(); i++){
if(value4.charAt(i) == ','){
value4Array.add(value4.substring(valueIndex, i));
valueIndex = i + 1;
}
else if(i == value4.length() - 1){
value4Array.add(value4.substring(valueIndex, value4.length()));
}
}
return value4Array;
}
if(someCondition4){
List<String> value4Array = parseValue4(queryParams.value4);
sqlQuery.append("And column4 IN ("); /* base AND statement */
for(int i = 0; i < value4Array.size(); i++){
if(i == value4Array.size() - 1)
sqlQuery.append("?)");
else /* dynamically appending ?'s */
sqlQuery.append("?,");
queryArgs.add(value4Array.get(i));
}
}
You are taking list of string type (List<String> queryArgs = new ArrayList<>();).
What if the parameter is an integer type? Is there any way to store multiple typle of data?
Im trying to get all the data rows that contain a certain set of data.
In my database i have some values, including start and length.
I have an array that contains integers called startTimes and another called endTimes.
I need a where clause that would return the records that have a start value contained in startTimes, OR start+length contained in endTimes.
Is there any way to do this?
Thanks
IQueryable<request> proposedRequest = db.requests.Include(r => r.rooms);
proposedRequest = proposedRequest.Where(r=>r.booked.Equals(1));
proposedRequest = proposedRequest.Where(r=>r.roundID.Equals(roundID))8;
proposedRequest = proposedRequest.Where(r=>r.day.Equals(day));
int[] startTimes;
int[] endTimes;
for(var q=0;q<time;q++){
startTimes[startTimes.Length] = time + q;
endTimes[endTimes.Length] = time + q + 1;
}
proposedRequest = proposedRequest.Where(//what goes in here);
i have an array startTime=[1,3,4] and an array endTime=[2,4,5] lets
say. I need to see if any of those values match my records? I dont
think the above would do the job
To check if you have value in Array of int, Use the Contains Method:
proposedRequest = proposedRequest.Where(s => startTimes.Contains(s.INT_ID)
|| endTimes.Contains(s.INT_ID));
Syntax: ARRAY.Contains(ID_TO_SEARCH)
it returns a boolean:
var list = new List<int> {1,2,3,4,5};
var intVar = 4;
var exists = list.Contains(intVar);
will something like this suffice?
var data = collection.Where(
t => startTimes.Contains(t.theTime) ||
endTimes.Contains(t.theTime + theLength));
proposedRequest.Where(pr => startTimesArray.Contains(pr.start) || endTimesArray.Contains(pr.start + pr.length));
var pageSize = 6;
var totalPages = 0;
var count = 0;
var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1;
var offset = (page -1) * pageSize;
string selectQueryString = "SELECT * FROM PropertyInfo ";
//some (if) statements which append various (where) clauses to the query
selectQueryString += "ORDER BY NumBedrooms DESC OFFSET #0 ROWS FETCH NEXT #1 ROWS ONLY;";
string[] argArray = argList.ToArray();
So then i try and piece it all together by defining the values of #0 and #1
queryResults = db.Query(selectQueryString, argArray, offset, pageSize);
But this gives me the error "No mapping exists from object type System.String[] to a known managed provider native type."
any ideas what i'm doing wrong?
I don't believe that you can combine passing an array and parameter list together - either switch all parameters into the array, or switch them all to use #0, #1 style ordinals.
Also, as a side note, the line:
var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1;
Can be simplified using the default parameter of AsInt() with:
var page = UrlData[0].AsInt(1);
I have a list which contains a dictionary of ExpandoObjects. Im binding this to a grid but now I want to sort the list.
var rows = new List<dynamic>();
for (int i = 0; i < 1000; i++)
{
dynamic expandy = new ExpandoObject();
var dictionary = (IDictionary<string, object>)expandy;
dictionary.Add("ID", i);
dictionary.Add("Name", "Name" + i);
rows.Add(dictionary);
}
So looking at the test code above how would I sort rows (ascending or decending) say on "ID" or "Name" or any other property that I dynamically add?
A bit more info, I'm looking to sort it like this (this doesnt work);
var newOrder = from r in rows
orderby ("Name") ascending
select r;
Not sure how I missed this but anyway this works,
var newOrder = rows.OrderByDescending(x => x["Name"]);
WooHoo's answer does not work for Dynamic.ExpandoObject; this works:
var newOrder = rows.OrderByDescending(x =>((IDictionary<string,object>)x)["Name"]);
Wondering how to achieve pagination in Lucene, as it does not inherently support pagination. I basically need to search for 'top 10 entries' (based on some parameter) then 'next 10 entries' and so on. And at the same time I don't want Lucene to hog memory.
Any piece of advice would be appreciated.
Thanks in advance.
You will need to apply your own paging mechanism, something similar to that below.
IList<Document> luceneDocuments = new List<Document>();
IndexReader indexReader = new IndexReader(directory);
Searcher searcher = new IndexSearcher(indexReader);
TopDocs results = searcher.Search("Your Query", null, skipRecords + takeRecords);
ScoreDoc[] scoreDocs = results.scoreDocs;
for (int i = skipRecords; i < results.totalHits; i++)
{
if (i > (skipRecords + takeRecords) - 1)
{
break;
}
luceneDocuments.Add(searcher.Doc(scoreDocs[i].doc));
}
You will find that iterating the scoreDocs array will be lightweight as the data contained within the index is not really used until the searcher.Doc method is called.
Please note that this example was written against a slightly modified version of Lucene.NET 2.3.2, but the basic principal should work against any recent version of Lucene.
Another version of loop, continuing with Kane's code snippet;
....................
ScoreDoc[] scoreDocs = results.scoreDocs;
int pageIndex = [User Value];
int pageSize = [Configured Value];
int startIndex = (pageIndex - 1) * pageSize;
int endIndex = pageIndex * pageSize;
endIndex = results.totalHits < endIndex? results.totalHits:endIndex;
for (int i = startIndex ; i < endIndex ; i++)
{
luceneDocuments.Add(searcher.Doc(scoreDocs[i].doc));
}
I use following way to paginate, may be it help someone. If you know a better strategy, specifically from performance view point, please share.
public TopDocs search(String query, int pageNumber) throws IOException, ParseException {
Query searchQuery = parser.parse(query);
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
int startIndex = (pageNumber - 1) * MyApp.SEARCH_RESULT_PAGE_SIZE;
searcher.search(searchQuery, collector);
TopDocs topDocs = collector.topDocs(startIndex, MyApp.SEARCH_RESULT_PAGE_SIZE);
return topDocs;
}