I'm Using Scala(2.11) and playframework(2.3) and trying to run a query using a helper function to get results through pattern matching. The function is as follows
def resultsfunc() = {
val gradeRegex = "^Class 5\."
val currRegex = "\.NCERT$"
DB.withConnection{ implicit c =>
val filterQuery = SQL(
"""
select * from tbl_graphs
where graph_name REGEXP '{grade_regex}' and
graph_name REGEXP '{curr_regex}' and org_id = 4
""")
.on("grade_regex" -> gradeRegex,
"curr_regex" -> currRegex)
filterQuery().map{ graphRecord =>
new ResultObj(graphRecord[Long]("id"),
graphRecord[String]("name"))
}.toList
}
}
I don't get any errors but I get empty result even though there are multiple records that match the pattern. The same query works if I try to run in mysql workbench and when I tried to print filterQuery the arguments were also mapped correctly.
Should Pattern matching with regex must be carried out differently in Scala Anorm ?
It has absolutely nothing to do specifically with Anorm.
Make sure that executing manually the query with exactly the same data and parameter, you get result.
When using JDBC (even through Anorm), string parameter must not be quoted in the query statement (... '{grade_regex}' ...).
Since a long time, it's recommended to use Anorm interpolation (SQL"SELECT x FROM y WHERE z = ${v}") rather than SQL(..) function.
Related
Note: this is a similar but NOT duplicate question with How to use sqlx to query mysql IN a slice?. I'm asking for the Rust one.
This is what I try to do.
let v = vec![..];
sqlx::query("SELECT something FROM table WHERE column IN (?)").bind(v)
...
Then I got the following error
the trait bound `std::vec::Vec<u64>: sqlx::Encode<'_, _>` is not satisfied
Answer is in first on FAQ https://github.com/launchbadge/sqlx/blob/master/FAQ.md
How can I do a SELECT ... WHERE foo IN (...) query? In 0.6 SQLx will
support binding arrays as a comma-separated list for every database,
but unfortunately there's no general solution for that currently in
SQLx itself. You would need to manually generate the query, at which
point it cannot be used with the macros.
The error shows Vec is not an Encode that is required to be as a valid DB value. The Encode doc lists all the Rust types that have implemented the trait. Vec is not one.
You can use the following way to bind the parameters in IN with the values of a vector. Firstly, you need to expand the number of '?' in the IN expression to be the same number of the parameters. Then, you need to call bind to bind the values one by one.
let v = vec![1, 2];
let params = format!("?{}", ", ?".repeat(v.len()-1));
let query_str = format!("SELECT id FROM test_table WHERE id IN ( { } )", params);
let mut query = sqlx::query(&query_str);
for i in v {
query = query.bind(i);
}
let row = query.fetch_all(&pool).await?;
Please note if the target database is not MySql, you need to use $n, like $1, $2, instead of ?, as the parameter placeholder.
I've created a widget called yrmo with the values such as 202001.
I need to call this value from a query but it doesn't recognize it, I think because the field I am applying it to is Int but I can only reference the widget with quotes around it.
If I don't use quotes then it thinks I'm using a field in the table. If I use single quote then it interprets it as a literal. I've tried getArugument but it says it doesn't recognize it (do I load something?)
The query is is scala.
val x = sqlContext.sql("select domain from TABLENAME where partsn_mo=yrmo)")
Thanks
You can use Scala's string interpolation with an expression inside of ${} that may include double quotes.
So you could do:
val x = spark.sql(s"select domain from TABLENAME where partsn_mo = ${dbutils.widgets.get("yrmo")}")
Try doing this
val query = "select domain from TABLENAME where partsn_mo=" + dbutils.widgets.get("yrmo")
val x = sqlContext.sql(query)
I am new in python as well as mysql. I am having trouble in populating proper query statement for mysql.
sql = "SELECT * FROM Persons WHERE %s"
cur = db.cursor()
cur.execute(sql,(where,))
where is a string variable which creates a string for WHERE clause; this is the point of question. When I print this variable it give the following result:
Gender = True And IsLate = False
(without any quotes) but when I add this variable to the query to execute it, it adds single quotes around the string.
I used the command
print(cur.statement)
and it prints:
SELECT * FROM Persons WHERE 'Gender = True And IsLate = False'
After supplying parameter, it puts it within single quotes and query returns 0 rows.
I have worked around by concatenating the query statement and variable together and execute the string as query, that worked,
sql = sql + where
cur.execute(sql)
But I know that is not the professional way, as I have searched and found the professional way is to use parameterized query and use variable to store the condition(s) and supplying it at the execution of query.
Looking for advice, am I thinking the right way or otherwise?
The whole point of using parameter substitution in cursor.execute() is that it protects you from SQL injection. Each parameter is treated as a literal value, not substituted into the query and re-interpreted.
If you really want it to be interprted, you need to use string formatting or concatenation, as you discovered. But then you will have to be very careful in validating the input, because the user can supply extra SQL code that you may not have expected, and cause the query to malfunction.
What you should do is build the where string and parameter list dynamically.
where = []
params = []
if gender_supplied:
where.append('gender = %s')
params.append(gender)
if islate_supplied:
where.append*('islate = %s')
params.append(islate)
sql = 'select * from persons'
if where:
query = sql + ' where ' + ' and '.join(where)
else:
query = sql
cur.execute(query, params)
I want to filter my entity by name property. I use Contains() method. When I pass parameter as "E" it works but if I pass as variable it doesn't work.
Code is below :
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageNameContains))
query = query.Where(n => n.Name.Contains(searchingModel.LanguageNameContains));
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageCodeContains))
query = query.Where(n => n.Code.Contains(searchingModel.LanguageCodeContains));
return query;
Above example doesn't work. But if I write like this
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageNameContains))
query = query.Where(n => n.Name.Contains("E"));
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageCodeContains))
query = query.Where(n => n.Code.Contains("En"));
return query;
It works. I have debugged lots of times. I passed same parameters as variable and as constants. If I pass as constant it works but If I pass as variable it doesn't work. Curiously enough, The same code works MySql.Data.Entity 6.9.3 but now I am using 6.9.5 and it doesn't work. Is it a bug or my mistake ?
Generated SQL :
-when I pass parameter as variable:
SELECT
Extent1.Id,
Extent1.Code,
Extent1.Name
FROM Languages AS Extent1
WHERE Extent1.Name LIKE '%p__linq__0%'
-- p__linq__0: 'E' (Type = String, Size = 1)
-- Executing at 30.12.2014 22:30:40 +02:00
-- Completed in 0 ms with result: EFMySqlDataReader
when I pass as interned string :
SELECT
Extent1.Id,
Extent1.Code,
Extent1.Name
FROM Languages AS Extent1
WHERE Extent1.Name LIKE '%E%'
Second one returns rows but first one doesn't return any rows.
I am trying to use system.dynamic.linq to create dynamic sorting.
this, is the query that I use:
var query = dalSession.Query<T>().AsQueryable();
var res = (from x in query orderby "x.FirstName" select x)
this is the mysql output:
select
affiliate0_.id as id0_,
affiliate0_.first_name as first6_0_,
from affiliate affiliate0_ order by 'x.FirstName' //FirstName as well
So you can see that the output went to the mysql query is the direct string, and not its reflection, ('x.FirstName') or ('FirstName').
This has no meaning in mysql context, it looks like I need order by affiliate0_.first_name.
Is there anyway to provide the Nhibernate the member itself? So the compiled query will be done normally?
You have to remove the " from you requiry. "x.FirstName" is seen as a string and translated to a sql string.
The "x" has no meaning inside the dynamic string.
Remove the x. (i.e. leave just "FirstName") and it should work.
Using a method call instead of the query comprehension syntax, you'd get:
var res = query.Orderby("FirstName")
Did you try it as follows (as Diego suggested)?
(from x in query select x).OrderBy("FirstName")
Because I think the dynamic linq orderby extension method is not executed when you use (x => "FirstName")
Otherwise try this:
(from x in query select x).OrderBy("it.FirstName")
BTW I renamed the OrderBy method in Dynamic.cs to DynamicOrderBy because I had some situations where the not dynamic linq OrderBy method was executed too:
public static IQueryable<T> DynamicOrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) and use that one instead:
(from x in query select x).DynamicOrderBy("FirstName")
var param = Expression.Parameter("x");
var prop = Expression.Property(param, "FirstName");
var lambda = Expression.Lambda<Func<User, string>>(prop, param);
query.Orderby(lambda);