NHibernate icriteria SQLFunction Left - sql

How do i implement the following statement in NHibernate iCriteria
Select * From LECNNy where Left(Address,4)='test'
It has to be something like
Projections.SqlFunction("left"("Address"),
But how do i pass the number of chars and the var ?

Some further research gave the following answer :
store.Add(
Restrictions.Eq(
Projections.SqlFunction(
"left",
NHibernate.NHibernateUtil.String,
Projections.Property("Address"),
Projections.Constant(4)
),
'test'
)
);

Related

SQL query to Laravel

I am trying to convert the following query to Laravel:
select libéllé
from application
where libéllé not in (select application_id from application_user
where user_id = $id)
Laravel whereNotIn supports closures for subqueries, so it will be as simple as this:
Using Eloquent:
// Add this to top of your file.
use App\{ Application, ApplicationUser };
// Get your entries.
$rows = Application::whereNotIn('libéllè', function($query) use ($id) {
$query->select('application_id')->from((new ApplicationUser)->getTable())->where('user_id', $id);
})->get(['libéllè']);
Using Query Builder:
$rows = DB::table('application')->whereNotIn('libéllè', function($query) use ($id) {
$query->select('application_id')->from('application_user')->where('user_id', $id);
})->get(['libéllè']);
Please Try it.
$results = DB::select(
select libéllé
from application
where (libéllé)
not in(
select application_id from application_user
where user_id = $id
)
);
Also see this answer: How to convert mysql to laravel query builder
Also see this documentation: https://laracasts.com/discuss/channels/laravel/laravel5-resolve-username-from-2-tables?page=1

Why does RavenDB perform an OR operation instead of an AND?

Why is there a OR instead of an AND between the SEARCH and the WHERE?
The problem is that the current Lucene query is:
"OrganizationType:Boo ( Name:(Foo) ShortName:(Foo))"
instead of:
"OrganizationType:Boo AND ( Name:(Foo) ShortName:(Foo))"
How can I change that?
RavenQueryStatistics stats;
var organizationQuery = session.Query<Organization>()
.Statistics(out stats)
.Skip((request.Page - 1) * request.PageSize)
.Take(request.PageSize);
if (request.OrganizationType != default(OrganizationType))
{
organizationQuery = organizationQuery.Where(o => o.OrganizationType == request.OrganizationType);
}
if (!string.IsNullOrEmpty(request.Query))
{
organizationQuery = organizationQuery
.Search(c => c.Name, request.Query, escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard)
.Search(c => c.ShortName, request.Query, escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard);
}
I have added a screenshot with the proposed solution:
To get only documents matching all sub-queries you to have to use Intersect. See the article How to use intersect in the RavenDB documentation.
Because Search is using OR by default. There is an optional parameter that set it to use AND.

Select sql code in Laravel

Following query returning six values
SELECT tbl_start FROM timetable inner join route ON tbl_rte_id = id WHERE rte_origin = "UL" and rte_destination = "HW" ORDER BY(tbl_start) DESC;
And my laravel code is returning only one value
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'id')
->where('rte_origin', $origin, 'AND')
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get();
foreach ($tables as $table) {
$result[$table->id] = $table->tbl_start;
}
This laravel code is not similar or similar. Can anyone help me.
Change this part:
->where('rte_origin', $origin, 'AND')
// to:
->where('rte_origin', $origin)
It will know by default that it's AND operator
And if you want to provide this operator, then do this:
->where('rte_origin', '=', $origin, 'AND')
You may try something like this:
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'timetable.id')
->where('rte_origin', $origin)
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get()->lists('tbl_start', 'id');
The $tables will contain an array of id => tbl_start pairs.
Add a listener in your routes.php
Event::listen('illuminate.query', function($sql){
var_dump($sql);
});
Then execute both queries and check if you have the same result

nhibernate CreateCriteria wildcard Like when

In SQL I can write
SELECT blah FROM Clients
Where #p1 Like '%'+lastname+'%'
How do I represent this with CreateCriteria in Nhibernate?
I've tried s.CreateCriteria<Client>.Add(Restrictions.Where<Client>(c => "something".Contains(c.LastName))
but get an error
System.Exception: Unrecognised method call: System.String:Boolean Contains(System.String)\r\n at NHibernate.Impl.ExpressionProcessor.ProcessCustomMethodCall(MethodCallExpression methodCallExpression)
I've also tried
s.CreateCriteria<Client>.Add(Restrictions.Where<Client>(c => "something".IndexOf(c.LastName) != -1))
but get
"variable 'c' of type 'TrinityFinance.Data.Entities.Client' referenced from scope '', but it is not defined"
Note the order is important here.
#p1 Like '%'+lastname+'%'
is not the same as
lastname Like '%'+#p1+'%'
s.CreateCriteria<Client>().Add(
Restrictions.InsensitiveLike( "LastName", "something", MatchMode.Anywhere))
Thanks to a friend I've solved my issue.
var searchCriteria = GetSession().CreateCriteria<Client>();
searchCriteria.Add(Expression.Sql(string.Format("'{0}' like '%' + {1} + '%'", p.ClientInputText,p.DbField)));
var results = searchCriteria.List<Client>();
For Case-Insensitive %Like% Search
Criteria criteria = session.createCriteria(Any.class);
criteria.add(Restrictions.ilike(propertyName, value, MatchMode.ANYWHERE);
criteria.list();

NHibernate: Get object by SQL Query

Is it possible to do something like this in NHibernate?
Product GetSpecificProduct()
{
return session.CreateSQLQuery("SELECT * FROM Products WHERE price =
$500").UniqueResult<Product>();
}
When I try to run this code I get:
System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type Product.
Or do I have to use the NHibernate query language ?
If you have to use CreateSqlQuery, you can use the following:
Product GetSpecificProduct()
{
ISQLQuery query = session.CreateSQLQuery("SELECT * FROM Products WHERE price = $500");
Product p = query.SetResultTransformer(Transformers.AliasToBean<Product>()).UniqueResult<Product>();
}
I suggest you better use ICriteria as in:
Product GetSpecificProduct()
{
ICriteria c = session.CreateCriteria();
c.Add(Expression.Eq("Price", 500));
return c.UniqueResult<Product>();
}
Yes you can. Check out Entity Queries using Native SQL : http://codewut.de/content/using-native-sql-nhibernate