I'm working on a c# project that use a Versant Object Database back end and I'm trying to build a query that contains an arithmetic operator. The documentation states that it is supported but lack any example.
I'm trying to build something like this:
SELECT * FROM _orderItemObject WHERE _qtyOrdered - _qtySent > 0
If I try this statement in the Object Inspector I get a synthax error near the '-'.
Anyone has an example of a working VQL with that kind of statement?
Thanks
I am not sure that the Object Inspector will know the syntax for the arithmtic expression. However, in your code you should be referring to the fully qualified class. Then the syntax you are using should be perfectly fine.
Query query = new Query( session,
"select * from com.yourCompany.yourClass where _qtyOrdered - _qtySent > 0 ");
QueryResult result = query.execute();
I just tried this out on one of my classes and it worked fine.
Cheers,
-Robert
With C# and OQL you have to make sure you select the proper class extent. This is done by adding the "Extent" suffix to the class name. For example, in my Pet class I would identify all the pets with "PetExtent" in the OQL string.
Class members are accessed in the predicate by defining a local moniker, p in the code below. Any arithmetic expressions will be evaluated by the query engine.
string query="SELECT * FROM PetExtent AS p WHERE p.Name = \"Ferris\" AND (p.age + 5) > 4";
IQueryResult result = scope.GetOqlQuery(query).Execute();
foreach (object o in result)
Out(o.ToString());
The best way to test OQL with Versant's C# binding is to use the OQL Query Browser integrated into Visual Studio. Look under the Versant Menu drop down in Visual Studio.
Best Regards,
Derek
Related
I want to create queries using EF FromSqlInterpolated or FromSqlRaw that allows me to use Like clauses, but I don't know what is the right way to do it without opening the application to SqlInjection attacks.
One first approach has took me to the following code
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
First test worked fine, it returns results when providing expected strings, and returns nothing when i provide something like ';select * from Category Where name='Notes'--%';
Still I don't know much about SqlInjection, at least not enough to feel safe with the query shown before.
Does someone know if the query is safe, or if there is a right way to do it?
Thanks
From this document
The FromSqlInterpolated and ExecuteSqlInterpolated methods allow using
string interpolation syntax in a way that protects against SQL injection attacks.
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
Or you can also change your query to Linq-to-Entity like this way
var results = _context.Categories.Where(p => p.name.Contains(partialName ));
I was having some performance issues using SharpRepository, and after playing around the SQL Query Profiler I found the reason.
With EF I can do stuff like this:
var books = db.Books.Where(item => item.Year == '2016');
if (!string.IsNullorEmpty(search_author))
books = books.Where(item => item.Author.Contains(search_author);
return (books.ToList());
EF will not really do anything until books is used (last line) and then it will compile a query that will select only the small set of data matching year and author from the db.
But SharpRepository evaluates books at once, so this:
var books = book_repo.Books.FindAll(item => item.Year == '2016');
if (!string.IsNullorEmpty(search_author))
books = books.Where(item => item.Author.Contains(search_author);
return (books.ToList());
will compile a query like "select * from Books where Year == '2016'" at the first line, and get ALL those records from the database! Then at the second line it will make a search for the author within the C# code... That behaviour can be a major difference in performance when using large databases, and it explains why my queries timed out...
I tried using repo.GetAll().Where() instead of repo.FindAll().... but it worked the same way.
Am I misunderstanding something here, and is there a way around this issue?
You can use repo.AsQueryable() but by doing that you lose some of the functionality that SharpRepository can provide, like caching or and aspects/hooks you are using. It basically takes you out of the generic repo layer and lets you use the underlying LINQ provider. It has it's benefits for sure but in your case you can just build the Predicate conditionally and pass that in to the FindAll method.
You can do this by building an Expression predicate or using Specifications. Working with the Linq expressions does not always feel clean, but you can do it. Or you can use the Specification pattern built into SharpRepository.
ISpecification<Book> spec = new Specification<Book>(x => x.Year == 2016);
if (!string.IsNullorEmpty(search_author))
{
spec = spec.And(x => x.Author.Contains(search_author));
}
return repo.FindAll(spec);
For more info on Specifications you can look here: https://github.com/SharpRepository/SharpRepository/blob/develop/SharpRepository.Samples/HowToUseSpecifications.cs
Ivan Stoev provided this answer:
"The problem is that most of the repository methods return IEnumerable. Try repo.AsQueryable(). "
I have this Linq To Entites query below. When I execute this query from a Console Application, it produces SQL and executes perfectly.
But when I execute it from a web application, I get an error message that says the Min() function is not recognized by Linq To Entites and it cannot be translated to a store expression.
It is EXACTLY the same query. Both project have the same settings (concerning EF 6) in their config files, and they reference the same assemblies.
from ce in db.CustomEvents
where ce.fld_start > DateTime.Now
group ce by ce.fld_ownerId into g
select new
{
fld_ownerId = g.Key,
next_appointement_date = g.Min(i => i.fld_start)
}
It seems this happens only when the Min aggregate is on DateTime property. I hadn't this issue when I had Min on decimals, for instance.
The error message I get when I execute this in the web site is this
LINQ to Entities does not recognize the method 'System.Nullable`1[System.DateTime]
Min[CustomEvent](System.Collections.Generic.IEnumerable`1[CustomEvent],
System.Func`2[CustomEvent,System.Nullable`1[System.DateTime]])'
method, and this method cannot be translated into a store expression.
But when I execute it from the Console app, it successfully generates the SQL statement below
SELECT
1 AS [C1],
[GroupBy1].[K1] AS [fld_ownerId],
[GroupBy1].[A1] AS [C2]
FROM ( SELECT
[Extent1].[fld_ownerId] AS [K1],
MIN([Extent1].[fld_start]) AS [A1]
FROM [dbo].[mtbl_CustomEvent] AS [Extent1]
WHERE [Extent1].[fld_start] > (SysDateTime())
GROUP BY [Extent1].[fld_ownerId]
) AS [GroupBy1]
Does anyone have a clue on what is happening?? Why the same query generates SQL when run in the console app, but fails with an exception when run in the web site??
UPDATE:
It seems that the problem was an ambiguity between two different implementations of Min<T>(this IEnumerable<T>) (.net's implementation, and our own implementation). Removing the "using OurLibrary;" namespace from the file fixed the problem.
After vittore's suggestion to use extension methods syntax we found that the problem was that in the web project we referenced one of our libraries that included an implementation of Min<T>(this IEnumerable<T>), and we also had the using OurLibrary.Namespace; directive in the file where the linq query was.
When we used the linq syntax, the compiler picked our implementation of Min for the query and did not throw any errors. Then, at runtime, the LINQ to Entities framework crashed because it could not recognise our implementation of Min.
from ce in db.CustomEvents
where ce.fld_start > DateTime.Now
group ce.fld_start by ce.fld_ownerId into g
select new
{
fld_ownerId = g.Key,
next_appointement_date = g.Min()
}
Here there was no compiler error.
But when we used the extension method syntax, the compiler stopped with an ambiguity error, and that's how we identified the problem.
db.CustomEvents
.Where(ce => ce.fld_start > DateTime.Now)
.GroupBy(ce => ce.fld_ownerId, ce => ce.fld_start)
.Select(g => new { g.Key, next_appointement_date = g.Min() })
Here the compiler threw an error of ambiguous call to .Min
It's weird that the compiler does not recognise the ambiguity in the Linq syntax. After all, linq is only syntactic sugar.
I know this has been asked earlier.
Most of the answers were not relevant.
Google, shows that the solution is to configure the expression in the "data flow task" and set the query.
However in the ADO .NET source, when I try to preview the output I keep getting "Must declare the variable '#'"
It does not show the full variable in this error - "#[User::GLOBAL_PARAMETER]"
I think that's because "[USER::" isn't the correct syntax inside a SQL; but then how does one set it ?!
From your description it seems like you are having an error due to using the variable name inside the query string as opposed to the processed variable value. In other words:
"SELECT * FROM #[User::TABLE]" in the expression builder would be WRONG
"SELECT * FROM " + #[User::TABLE] would be CORRECT
It would help if you shared the expression you are using as a query
Using C# 3.5 through VS 2008 and subsonic 2.2.
Anyone know if it's possible to create a subsonic query that essentially has an 'IF' in the middle of it, depending on whether a passed parameter was, for example, greater than zero.
For example, a delete method that has two passed parameters - A and B.
I want something like (pseudo code)
DELETE from Products
Where productId = A
if(B > 0)
{
AND ProductAttributeId = B
}
Obviously it wouldn't need the actual 'IF' clause in there but that's the essence of what I'm trying to do with subsonic. I know I can just have two different queries depending on whether the parameter is there or not but I was wondering if there's a cleaner way of doing it.
Thanks.
That's how I usually do it - it's not two queries, but one SqlQuery with optionally added constraints:
SqlSquery q = DAL.DB.Delete()
.From<DAL.Product()
.Where(DAL.Product.ProductIdColumn).IsEqualTo(A);
if (B > 0)
{
q.And(DAL.Product.ProductAttributeIdColumn).IsEqualTo(B);
}
q.Execute();
There may be a typo, I can't test this right now.