I am working on simple query builder which will be used to generate orientdb queries. Orientjs has statement.js which can be use to generate the queries, but I am not sure if we could use to generate all types of queries.
For example:
select * from Employee where (FirstName like "A%" or FirstName like "B%") and (LastName like "G%" or LastName like "F%")
I tried different options to generate above query using orientjs statement, but not able to. Also, how we can generate query to do copy record using insert, select query?
Does statement.js support generating this type of queries?
For complex where clauses, you can just use a raw string:
db.select()
.from('Employee')
.where('(FirstName like "A%" or FirstName like "B%") and (LastName like "G%" or LastName like "F%")').all();
To create a copy of a vertex using select / insert, you can crate a transaction via the db.let function such as
return this.db
.let('original', (c) => {
c.select()
.from('Employee')
.where('(FirstName like "A%" or FirstName like "B%") and (LastName like "G%" or LastName like "F%")')
})
.let('copy', (c) => {c
.create('vertex','Employee')
.set('FirstName = $original[0].FirstName')
.set('LastName = $original[0].LastName')
})
.commit()
.return('$copy')
.one();
Related
I had to write a custom postgres sql query, so I used sequelize.query method. But I am a bit lost in how I can bind an array to a parameter used alongside an IN operator. The current code looks something like this, with obviously doesn't work.
sequelize.query('SELECT * FROM students WHERE grade IN $grades', {
bind: { grades: ['A+', 'A'] },
type: sequelize.QueryTypes.SELECT,
});
Use = any instead of in. Change the query text to
SELECT * FROM students WHERE grade = any(string_to_array($grades, ','))
or
SELECT * FROM students WHERE grade = any(('{'||$grades||'}')::text[])
and bind grades as a string, 'A+,A'.
The second option works for other data types too.
I have a complex entity framework query (filter, unions, calculations, ...):
IQueryable<ViewModel> query = from a in db.Entity1
where ...
select new ViewModel
{
field1: a.field1,
field2: a.field1*2 // Calculation example
};
other_part_of_my_application.DataSource = query;
This query object is an IQueryable. This means the query is not executed at this time. The object is send to another part of my application which apply additional filters, sorting and pagination.
I would like to write my query in raw SQL. Is there a way to do that without changing the other part of my application ?
I would like to do something like this:
IQueryable<ViewModel> query = I_Dont_know_what<ViewModel>("SELECT field1, (field1*2) as field2 From Entity1 Where ...");
other_part_of_my_application.DataSource = query;
My question is what should i put instead of "I_Dont_know_what" ?
Thanks
There is a simple database entity:
case class Foo(id: Option[UUID], keywords: Seq[String])
I want to implement a search function which returns all entities of type Foo which have at least one keyword that contains the search string.
I'm using Slick and tried this:
def searchKeywords(txt: String): Future[Seq[Foo]] = {
val action = Foos.filter(p => p.keywords.any like s"%$txt%").result
db.run(action)
}
This piece of code compiles, but when executing, I get this SQL error:
PSQLException: ERROR: syntax error at or near "any"
The generated sql statement looks like:
select "id", "title", "tagline", "logo", "short_desc", "keywords", "initial_condition", "work_process", "end_result", "ts", "lm", "v" from "projects" where any("keywords") like '%foo%'
And it does not work with postgresql. (I'm using v12)
Schema for the table looks like this:
CREATE TABLE foos
(
id UUID NOT NULL PRIMARY KEY,
keywords varchar[] NOT NULL
);
How can I achieve to search in a list of strings using the like operator?
From a pure SQL point of view, you need a derived table to achieve that. I hope some expert corrects me if I'm wrong but you can't use SQL operator like on a array.
Supposing your table construction is :
CREATE TABLE foos
(
id UUID NOT NULL PRIMARY KEY,
keywords varchar[] NOT NULL
);
Then an SQL way of retrieving the results would be :
select * from (
select id, unnest(keywords) as keyw from foos
) myTable where keyw like '%foo%'
Otherwise, the syntax you're using for the like operator seems correct.
myProperty like s"%$myVariable"
I am looking to sort my Customers collection by the following
{
lastName: 1,
firstName:1,
companyName:1
}
But, I want blanks to be ignored. Essentially it'd be like sorting on a field === lastName + firstName + companyName
This can be achieved by coalesce in mysql
Anyone know how to achieve this without using the aggregation framework (Meteor app)?
I've ported some of my Entity from JPA to document and now porting some of my queries.
here is the JPA query:
em.createQuery("select distinct c from CustomerImpl c left join fetch c.addresses ca where (:name is null or c.firstName LIKE :name or c.lastName LIKE :name) and (:ref is null or c.externalReference LIKE :ref) and (:city is null or ca.city LIKE :city) order by c.firstName").setParameter("name", name).setParameter("ref", customerRef).setParameter("city", city).getResultList();
below is my attempt :
Criteria orNameCriteria = new Criteria().orOperator(Criteria.where("firstName").is(null), Criteria.where("firstName").is(name), Criteria.where("lastName").is(name));
Criteria orCustomerRefCriteria = new Criteria().orOperator(Criteria.where("externalReference").is(null), Criteria.where("externalReference").regex(customerRef,"i"));
Criteria orAddress = new Criteria().orOperator(Criteria.where("addresses.city").is(null), Criteria.where("addresses.city").regex(city, "i"));
Query nameq = new Query(new Criteria().andOperator(orNameCriteria,orCustomerRefCriteria,orAddress));
this query return zero size arraylist. I've then changed the orNameCriteria to use is clause and making sure the data contained in name variable has / as suffix and prefix. That didn't work as well.
but queries from mongoVue and RockMongo clients :
{ firstName: /SAM/}
returns data.
Question 1: How do you write LIKE CLAUSE with spring-data-mongo Criteria?
Question 2 : is that the right way to use or and and clause with criteria
Thanks for reading
Criteria.where("field").regex(pattern) should work
Since I don't have the ability add comments...
If you do a static import on Criteria, it will make your where clauses look a lot better.
Criteria orAddress = new Criteria().orOperator(where("addresses.city").is(null), where("addresses.city").regex(city, "i"));