Running queries dynamically - spring

I have my Sql query stored as value in one of key attributes in MongoDb database.I am able to fetch the records from the MongoDb database.I want to run the sql query assosciated after retrieving it from there.
The format of record is as follow:
{
"Id":2,
"Type":"SqlQuery",
"Syntax":"select id from table_name where id = pid and optional_id in ('AC','SU')",
"ValueSpecifiers":{
"id":"request.id"
}
}
I am beginner in springboot.It would be very helpful if someone could guide me some resource or in correct direction .I am confused about is it possible to hit queries by setting few filter parameters here like id dynamically as it varies with request object.I want to run it dynamically without extending crud repository.Its just like hitting db by native queries.

You could use a character like "?" on your recorded query, and then you replace it with your filters. Example:
In the database you save the query like
select id from table_name where id = pid and optional_id in (?)
And then you retrieve the query to a variable and replace the "?" with the parameters you want to filter. Something like
varQuery.replace("?", "'AC', 'SU'")
or
Replace(varQuery, "?", "'AC', 'SU'")

Related

MariaDb - NamedQuery to filter records based on a column contains comma-separated values

I have a Spring boot application that connects to a MariaDB to fetch records. In my MariaDb table, I have a column called "tags" and this column consists of comma-separated values something like "summer,vacation,sea,sun,beach,travel".
There is a search form at the frontend. I want to create a NamedQuery to filter the search results.
I have something like this for the "tags" part:
" AND m.tags LIKE CONCAT('%', :tags, '%')"
This is working fine if the user input contains only one word like "summer" or "vacation". It works with multiple words as well but only if you write those words exactly as they appear in the tags column, like "summer,vacation".
The problem occurs when the user types something like: "vacation summer". Since these values are not in order as they persisted in the database, the result becomes an empty list. I receive input value as a list of Strings in the backend.
How can I modify my existing NamedQuery to work with all possible inputs?

Use a Field value in a Query (Seblod Query plugin)

I'm using the Query plugin that lets me create a Query field on my List & Search.
I have a hidden field with ID 'id_number' which I'm trying to use in my query. When I check the Page Source(Ctrl+U on Chrome), the hidden field has the correct value of id_number which is '22865'.
The part of my Query is-
WHERE [MATCH]p.pid||$cck->getValue('id_number')[/MATCH] AND (ban_reason in ("", "active"))
When I print this query out using the Debugger, the Query is using the literal value and querying the above as-
(p.pid LIKE '%$cck->getValue(\'id\_number\')%') ....and not like (p.pid LIKE '%22865%')
I also tried using $fields['id_number']->value but it still queries it incorrectly.
Should I not use $cck->getValue('id_number') to get the value of the hidden field in the query? Or is there something other than this that I need to use?

How to construct subquery in the form of SELECT * FROM (<subquery>) ORDER BY column;?

I am using gorm to interact with a postgres database. I'm trying to ORDER BY a query that uses DISTINCT ON and this question documents how it's not that easy to do that. So I need to end up with a query in the form of
SELECT * FROM (<subquery>) ORDER BY column;
At first glance it looks like I need to use db.QueryExpr() to turn the query I have into an expression and build another query around it. However it doesn't seem gorm has an easy way to directly specify the FROM clause. I tried using db.Model(expr) or db.Table(fmt.Sprint(expr)) but Model seems to be completely ignored and fmt.Sprint(expr) doesn't return exactly what I thought. Expressions contain a few private variables. If I could turn the original query into a completely parsed string then I could use db.Table(query) but I'm not sure if I can generate the query as a string without running it.
If I have a fully built gorm query, how can I wrap it in another query to do the ORDER BY I'm trying to do?
If you want to write raw SQL (including one that has a SQL subquery) that will be executed and the results added to an object using gorm, you can use the .Raw() and .Scan() methods:
query := `
SELECT sub.*
FROM (<subquery>) sub
ORDER BY sub.column;`
db.Raw(query).Scan(&result)
You pass a pointer reference to an object to .Scan() that is structured like the resulting rows, very similarly to how you would use .First(). .Raw() can also have data added to the query using ? in the query and adding the values as comma separated inputs to the function:
query := `
SELECT sub.*
FROM (<subquery>) sub
WHERE
sub.column1 = ?
AND sub.column2 = ?
ORDER BY sub.column;`
db.Raw(query, val1, val2).Scan(&result)
For more information on how to use the SQL builder, .Raw(), and .Scan() take a look at the examples in the documentation: http://gorm.io/advanced.html#sql-builder

Get BigQuery metadata when performing query (Ruby)

When performing a query against BigQuery, it outputs useful info in logs, but the return value is simply the query payload. Is there any way to programmatically get the query metadata in addition to the query result?
Example:
bigquery = Google::Cloud::Bigquery.new(…)
result = bigquery.query(sql)
Debug-level logs will show something like:
#total_bytes_processed=102412,
#total_rows=12915
I'm wondering how that could be accessed programmatically.
Don't know the specifics of Ruby (I don't use that language), but when you submit your query, you'll get back a "job id". Use this id to retrieve the meta information about the job/query using the Job API.
https://cloud.google.com/bigquery/docs/jobs-overview
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/get
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#resource

Dynamic Query using Linq To SQL According Multiple Fields

Hi Experts
I have a special question About dynamic Linq to Sql.
Consider we want to search in a table according two fields*(LetterNo(string) and LetterDate(Datetime))*
.OK the problem is user can enter on of that fields or even both.
I searched in the internet and found "Linq.Dynamic" library in ScottGu weblog.but in that library if we want to use SqlParameter in exported command we should use #0 and param for that.problem is I don't know how many fields user entered.
I want use one query for that and no external tool like "Linq Kit PredicateBuilder".
If I create my query string Manually(and execute using ExecuteCommand) then I will abdicate SqlParameter and risk of Sql Injenction growing up.
How Can do that?
thanks
I suspect you are wanting to do something like the following:
IQueryable<Letter> query = context.Letters;
if (!string.IsNullOrEmpty(LetterNo))
query = query.Where(letter => letter.LetterNo == LetterNo);
If (LetterDate.HasValue)
query = query.Where(letter => letter.LetterDate == LetterDate);
When you execute query, it will combine the necessary expressions and issue a single query to the database based on the user's input.

Resources