Writing a query with ORMLite - ormlite

How can I write a query with ormlite instead of using .create or any other thing like that? Can you please show me how for this simple example :
SELECT name FROM client
EDIT since I can't answer myself :
I guess I had to search a little more , anyway I found how to do it with the QueryBuilder like this :
newDao.query(newDao.queryBuilder().where.eq("name",valueofname)
If someone knows how to write the full query that would be great , otherwise , I'll stick with this solution

How can I write a query with ormlite instead of using .create or any other thing like that?
Goodness, there are tons of documentation about how to do this on the ORMLite site. Here's the section on the query builder.
I'm not sure what you mean by "full query" but your example will work with some tweaks:
List<...> results = newDao.queryBuilder().where().eq("name",valueofname).query();
It does not make sense to just return the name since the Dao hierarchy is designed to return the specific Client object. If you just want the name the you can specify the name column only to return:
... clientDao.queryBuilder().selectColumns("name").where()...
That will return a list of Client objects with just the name field (and the id field if it exists) extracted from the database.
If you just want the name strings then you can use the RawResults feature.

Related

Is there a possible way to search the name of a lookup fields using the search.query using apex in salesforce?

I had a lookup field on my object and I want to search the name of the lookup using search.query in apex, how can I do this?. In my debug, I am returning nothing. Below is my sample code.
String query = 'FIND \'Sonny McNeil\' IN ALL FIELDS RETURNING ASPHPP__ASPayment_Source__c (Id, Name,ASPHPP__PPContact__r.Name), Contact';
System.debug('query '+query);
List<List<sObject>> searchRecords = Search.Query(query);
System.debug('searchRecords '+searchRecords);
return searchRecords.get(0);
I've found a solution to my problem, it's not possible to use SOQL to search such certain name of a lookup field, all I do is use a sample SQL and add some LIKE condition on my sql and query it to the database and return

Select * for Github GraphQL Search

One of the advantage of Github Search v4 (GraphQL) over v3 is that it can selectively pick the fields that we want, instead of always getting them all. However, the problem I'm facing now is how to get certain fields.
I tried the online help but it is more convolution to me than helpful. Till now, I'm still unable to find the fields for size, score and open issues for the returned repository(ies).
That's why I'm wondering if there is a way to get them all, like Select * in SQL. Thx.
GraphQL requires that when requesting a field that you also request a selection set for that field (one or more fields belonging to that field's type), unless the field resolves to a scalar like a string or number. That means unfortunately there is no syntax for "get all available fields" -- you always have to specify the fields you want the server to return.
Outside of perusing the docs, there's two additional ways you can get a better picture of the fields that are available. One is the GraphQL API Explorer, which lets you try out queries in real time. It's just a GraphiQL interface, which means when you're composing the query, you can trigger the autocomplete feature by pressing Shift+Space or Alt+Space to see a list of available fields.
If you want to look up the fields for a specific type, you can also just ask GraphQL :)
query{
__type(name:"Repository") {
fields {
name
description
type {
kind
name
description
}
args {
name
description
type {
kind
name
description
}
defaultValue
}
}
}
}
Short Answer: No, by design.
GraphQL was designed to have the client explicitly define the data required, leading to one of the primary benefits of GraphQL, which is preventing over fetching.
Technically you can use GraphQL fragments somewhere in your application for every field type, but if you don't know which fields you are trying to get it wouldn't help you.

Spring Data Couchbase findById always returns null

Am i heading entirely down the wrong road?
Should this work?
I'm trying to build a query that restricts the selection based on a boolean
e.g. findByIdAndFieldIsTrue(id)
This always returns no values. If I narrow this and just do findById(id) i still get no results which seems odd. I have tried a number of permutations of Id & field in the method name but none work.
Entity {
#Id
String id
boolean field
}
interface EntityRepository BaseRepository, Repository {
findByIdAndFieldIsTrue(id)
}
I tried without the BaseRepository thinking maybe the findOne method was interfering but no luck.
Update:
After adding the logger suggested in the answer I can get the query
{"statement":"SELECT META(`mybucket`).id AS _ID, META(`mybucket`).cas AS _CAS, `mybucket`.* FROM `mybucket` WHERE (`id` = \"entity::9627ff50-531d-4191-a9dc-07b48ba77fe9\") AND `_class` = \"com.myrepo.repository.api.Entity\"","scan_consistency":"statement_plus"}
Edit: N1QL forces you to use the USE KEY construct to select by id... That will be much much harder to integrate, looks like the workaround is the easiest path (see below)
I think this is an overlook of query derivation on the #Id field. In Couchbase, that field maps to the document's key and isn't contained in the document's body, so the N1QL query needs to take that into account.
Can you activate logging for the query generation? There's an AbstractN1qlBasedQuery.class logger that will log the generated N1QL in DEBUG mode. It will allow us to see what the query looks like and confirm that a special step must be taken for the #Id field...
I know it wouldn't be as performant if deserialization is complex, but a workaround could be to use findOne and then check the field.
Got this to work with the following:
#Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND META(#{#n1ql.bucket}).id IN $1")
List<Entity> findByIdIn(JsonArray ids);
To get the JsonArray from a list do the following:
import com.couchbase.client.java.document.json.JsonArray;
.
.
.
repository.findByIdIn(JsonArray.from(myList));

How to query from ListColumn[String] in cassandra using phantom

I am new to cassandra (started learning on my own interest few days back) and looking for help for the below problem.
I have a Cassandra table "User" and a ListColumn "interests extends ListColumn[String]".
Now, I want to fetch all users with an interest, say "playing".
like: select from user where interests.contains("playing")!
I scanned through the ListColumn api but not able to find any. Also, searched in google but no such helpful posts.
Any help guys please... Thanks in Advance :)
So there is contains among operators and here is an example how to use it. It looks like that it should work as any other operator, so just go for database.userTable.select.where(_.interests contains "playing").fetch() - of course, depending on your conventions.
This is possible with a secondary index on a collection column, which only works with a Set column, and not with a List.
Bottom line:
object interests extends SetColumn[String](this) with Index[Set[String]]
And then you can execute the following:
select.where(_.interests contains "test").fetch()
You can also use multiple restrictions if you allow filtering.
select.where(_.interests contains "test")
.and(_.interests contains "test2")
.allowFiltering()
.fetch()
The above will only match if both interests are found in a record.

Mysql to Sphinx query conversion

how can i write this query on sphinx select * from vehicle_details where make LIKE "%john%" OR id IN (1,2,3,4), can anyone help me? I've search a lot and i can't find the answer. please help
Well if you really want to use sphinx, could perhaps make id into a fake keyword, so can use it in the MATCH, eg
sql_query = SELECT id, CONCAT('id',id) as _id, make, description FROM ...
Now you have a id based keyword you can match on.
SELECT * FROM index WHERE MATCH('(#make *john*) | (#_id id1|id2|id3|id4)')
But do read up on sphinx keyword matching, as sphinx by default only matches whole words, you need to enable part word matching with wildcards, (eg with min_infix_len) so you can get close to a simple LIKE %..% match (which doesnt take into account words)
Actually pretty hard to do, becuase you mixing a string search (the LIKE which will be a MATCH) - with an attribute filter.
Would suggest two seperate queries, one to sphinx for the text filter. And the IN filter just do directly in database (mysql?). Merge the results in the application.