If I try to add a where clause, containing a lambda filter on a boolean field, to a nhibernate linq query, the filter seems to be ignored:
var result = Session.Linq().Where(x=> x.Approved);
Session is an iSession and Newspaper is a simple object with the fields NewspaperId int, Name - varchar(50) and Approved - bit.
When I run this the following sql is generated:
SELECT this_.NewspaperId as Newspape1_33_0_, this_.Name as Name33_0_, this_.Approved as Approved33_0_, FROM Newspapers this_
it seems to ignore the lambda if it is for a boolean field.
It works fine for the name field, ie:
var result = Session.Linq().Where(x=> x.Name == "The Times");
results in:
exec sp_executesql N'SELECT this_.NewspaperId as Newspape1_33_0_, this_.Name as Name33_0_, this_.Approved as Approved33_0_ FROM Newspapers this_ WHERE this_.Name = #p0',N'#p0 nvarchar(9)',#p0=N'The Times'
Anybody know why I can't query on a boolean value?
Any help is greatly appreciated
I am using NHibernate 2.1 with linq
It's been a while so you've probably got your answer somewhere else a long time ago. But to answer your question: I can't see a reason why this wouldn't work. Actually I've tried it out in both NH2.1.2 and NH3.0.0. It works in both (verified by looking at the query with SQL Profiler). So it would be interesting to see the mapping you used, perhapse there's something wrong there.
Related
This is my first try at using the Power Query... I've build a "dynamic" query in which I can change the retrieved fields as well as the filtering fields and values to be used by the query.
It's working perfectly on my computer but as soon as I try to execute it on another computer, I get the "Please rebuild this data combination" error. I saw some post saying I'll have to kind of split my query but I have not been able to figure it out.
Here is what my 2 tables look like:
Condition and fields selection
and here is my Query with the error:
Query
This might not be very elegant, but it allow me, thru a VBA script, to generate the list of fields to be retrieved and to generate the condition to be used by the SQL.
Any idea why it's not working on the other computers or how to improved the solution I'm using?
Thank you!
Notes:
Hi, all my Privacy Level are already set to 'None'.
I've tried to parametrize my code but I can't figure how. The Where condition is dynamic: it could be Where Number = "1234" but in other condition, the where might be like: 'Where Assignee = "xyz"'.
Here is a simplified example of my code:
let
Source = Sql.Database("xxxx", "yyyy", [Query=
"Select network, testid
from CM3T1M1 "
& paramConditions[Conditions]{0} &
" "])
in
Source
rebuild query, Formula.Firewall
That's a feature to prevent prevent accidentally leaking data. You can change the privacy level to ignore it
See also: docs.microsoft/dataprivacyfirewall
Is the dynamic query inserting those cells into the SQL query ? Report Parameters are nice for letting the user change variables without having to re-edit the query.
Parameterized native SQL queries
from: https://blog.crossjoin.co.uk/2016/12/11/passing-parameters-to-sql-queries-with-value-nativequery-in-power-query-and-power-bi/
let
Source = Sql.Database("localhost", "Adventure Works DW"),
Test = Value.NativeQuery(
Source,
"SELECT * FROM DimDate
WHERE EnglishMonthName=#MonthName AND
EnglishDayNameOfWeek=#DayName",
[
MonthName = "March",
DayName = "Tuesday"
]
)
in
Test
Dynamic Power Query version of SQL Query
To dynamically generate this SQL Query
select NUMBER, REQUESTED_BY from SourceTable
where NUMBER = 404115
Table.SelectRows is your Where.
SelectColumns is your select
let
Source = ...,
filterByNum = 404115,
columnNames = {"NUMBER", "REQUESTED_BY"},
removedColumns = Table.SelectColumns(
Source, columnNames, MissingField.Error
),
// I used 'MissingField.Error' so you know right away
// if there's a typo or bug
// assuming you are comparing Source[NUMBER]
filteredTable = Table.SelectRows(
Source, each [NUMBER] = filterByNum
)
in
filteredTable
We are using Lucene.net beta version - Lucene.net 48. We want to provide support for not like clause using lucene query object. We are using WildcardQuery class for wild card support and using boolean clause as 'BooleanClause.Occur.MUST_NOT'.
It is generating query --> : "-company:lucene*".
It has '-' sign before query but it is not returning data where company is not like lucene*. Ideally, it should return 'elastic', 'mongodb', etc.
WildcardQuery qfWildcard = new WildcardQuery(new Term("company","lucene*"));
BooleanQuery bq = new BooleanQuery();
bq.Add(qfWildcard, BooleanClause.Occur.MUST_NOT);
On other way, WildcardQuery with MUST clause is working.
Query --> : "+company:lucene*".
It has '+' sign before query and it is returning data where company is like 'lucene*'. It is returning 'lucene', 'lucene.net', etc.
WildcardQuery qfWildcard = new WildcardQuery(new Term("company","lucene*"));
BooleanQuery bq= new BooleanQuery();
bq.Add(qfWildcard, BooleanClause.Occur.MUST);
Please help me, if any one know about the solution using WildcardQuery class or any other class or any alternative way to solve the issue.
Please also let me know, if there is way to support - 'Is Null' and 'Is Not Null' clause.
Querying with only a MUST_NOT clause will not work. A MUST_NOT clause does only what it says, it specifies which documents must not be matched. It doesn't say anything about which documents should be matched, and doesn't imply that everything else should be retrieved (further discussion here).
You must always have a SHOULD or MUST clause in your BooleanQuery. To match everything else, you can use a MatchAllDocsQuery.
WildcardQuery qfWildcard = new WildcardQuery(new Term("company","lucene*"));
BooleanQuery bq = new BooleanQuery();
bq.Add(qfWildcard, BooleanClause.Occur.MUST_NOT);
bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
I'm getting groovy records from SQL Table or Function. Example;
String subeKodu = get_sube_kodu_bul(matcher[0][1])
private String get_sube_kodu_bul(String subeAdi) {
def sql = Sql.newInstance("jdbc:jtds:sqlserver://10.xx.xx.xx:1433/DBNAME", "usrname","pass", "net.sourceforge.jtds.jdbc.Driver")
subeAdi = subeAdi.trim()
def row = sql.firstRow("SELECT TOP 1 SUBE_KODU FROM TABLENAME WHERE SUBE_ADI= '${subeAdi}'")
row != null ? (String)row.SUBE_KODU : ''
}
But I am faced with the following error;
WARNING: In Groovy SQL please do not use quotes around dynamic expressions (which start with $) as this means we cannot use a JDBC PreparedStatement and so is a security hole. Groovy has worked around your mistake but the security hole is still there. The expression so far is: SELECT TOP 1 YETKILI FROM TABLENAME WHERE SUBE_ADI = '?'
Groovy is complaining that your code may be vulnerable to a SQL injection attack.
The proper way to do this is with JDBC Prepared Statements. In Groovy, you do this as follows:
sql.firstRow("SELECT TOP 1 SUBE_KODU FROM TABLENAME WHERE SUBE_ADI= ?", [subeAdi])
For more examples of this, see the Groovy SQL tutorial and search for "prepared statements".
Also, don't forget to call close() when you are done.
today I migrated to Astyanax 1.56.42 and discovered, that withValues() on prepared statements doesn't seem to work properly with SQL SELECT...WHERE...IN ().
ArrayList<ByteBuffer> uids = new ArrayList<ByteBuffer>(fileUids.size());
for (int i = 0; i < fileUids.size(); i++) {
uids.add(ByteBuffer.wrap(UUIDtoByteArray(fileUids.get(i)), 0, 16));
}
result = KEYSPACE.prepareQuery(CF_FILESYSTEM)
.withCql("SELECT * from files where file_uid in (?);")
.asPreparedStatement()
.withValues(uids)
.execute();
If my ArrayList contains more than one entry, this results in error
SEVERE: com.netflix.astyanax.connectionpool.exceptions.BadRequestException: BadRequestException: [host=hostname(hostname):9160, latency=5(5), attempts=1]InvalidRequestException(why:there were 1 markers(?) in CQL but 2 bound variables)
What am I doing wrong? Is there any other way to handle a SQL SELECT...WHERE...IN () - statement or did I find a bug?
Best regards
Chris
As you mentioned because you are supplying a collection (ArrayList) to a single ? Astyanax throws an exception. I think you need to add a ? for each element you want to have inside the IN clause.
Say you want to have 2 ints stored in an ArrayList called arrayListObj the where clause, your statement looks like this:
SELECT & FROM users WHERE somevalue IN (arrayListObj);
Because you are suppling a collection, this cant work, so you will need multiple ?'s. I.e. you want :
SELECT name, occupation FROM users WHERE userid IN (arrayListObj.get(0), arrayListObj.get(1));
I couldn't find anything on the Astyanax wiki about using the IN clause with prepared statements.
Using SubSonic 2.2, I have this query:
string q = #"SELECT Media.Id, Media.Title FROM Media WHERE Media.UserId = 7"
DAL.MediumCollection matches = new InlineQuery().ExecuteAsCollection<DAL.MediumCollection>(q).Load();
Looping through "matches" results in every single entry in the "Media" table.
However, when I do this:
IDataReader reader = new InlineQuery().ExecuteReader(q);
It returns the correct rows. Why is ExecuteAsCollection returning something completely different from ExecuteReader? Has anyone else experience this strange behavior?
I think it's because you're calling .Load(). That's overwriting your original query.
ExecuteAsCollection() should do it.
When you call the Load() method it's just like doing new DAL.MediumCollection().Load() that returns all the data in the table.