how to get one column list in DBFlow - dbflow

I am using DBflow. I have tried many times to try and make a query as follows:
public class ScoreRepository {
public List<Score> findAllScore() {
return SQLite.select()
.from(Score.class)
.queryList();
}
public List<String> findScore01() {
return SQLite.select()
.from(Score.class)
.queryList();
}
}
In my case findScore01(), this method does not work. I want to make one column String List. I do not want to make an Object List.

queryList will give you list of 'Score' Class. To get custom data,
e.g ( join of two tables, or in your case, just 1 column)
you need to make a class that extends from QueryModel. then use
.queryCustomList(customclass.class);
That will give you
List< customclass > ls;
Check this answer for more details:
Get custom Data from 2 tables

You can use queryCustomList for getting a sub table from an existing table or joining two tables. Like Farhan mentioned above you can create a QueryModel class with your required fields and use it to retrieve the score, but it still be a list of your custom model query class, and not list of strings like you needed.
Only solution I can think of is looping your result list and generate a list of strings like :
List<Score> scoreList = SQLite.select(Score_Table.score01)
.from(Score.class)
.queryList();
List<String> score01List = new ArrayList<>();
foreach(Score score: scoreList) {
score01List.add(score.getScore01());
}

Related

Spring Pagination sorting with cases

I have a problem with Sorting in Spring. I have an enum class
public enum Category {
A,
B,
C,
D,
E,
F;
private static final List<Category> THRILLER;
static {
THRILLER = Arrays.asList(A, F, D);
}
private static final List<Category> DRAMA;
static {
DRAMA = Arrays.asList(C, E, B);
}
}
The Movie Entity:
#Entity
#Table
public class Movie {
...
private Category category;
private Timestamp realizationDate;
....
}
Now I have a rest endpoint that return movies List. I want to Sort this list according to the following order: Movies they belong to THRILLER should be displayed first. When two movies belong to the same category then this should be sorted by realization date.
#GetMapping(value = "/movies")
public ResponseEntity<PagedResources<Movie>> list(#PageableDefault() Pageable pageable) {
.......
}
I tried it with:
#SortDefault.SortDefaults({SortDefault(sort="category", direction=Sort.Direction.ASC), SortDefault(sort="realizationDate", direction=Sort.Direction.ASC)})
But this Sort the enum only alphabetically.
I need a possibility to do something like: Sort.by(Category.THRILLER.contains(movie.getCategory())). It is possible that I achieve that with Pageable or should I do it another way?
Spring Data directly only supports sorting by simple attributes.
So in order for this to work you need to provide an isThriller attribute.
You can do this with the existing object structure by using:
a) a trigger to fill the column in the database
b) use a view to compute the desired value
c) if you use Hibernate you may use the #Formula annotation to instruct it to compute the value.
In either case you are hardcoding the categories that make up thrillers in a String or in the database.
If you don't want that you could make Category an entity with an isThriller attribute which you then can query and sort by in the usual fashion.
AFAIK Sort in spring pagination sorts fields only in lexicographical order (same like in SQL, uneless you use some procedural version of SQL like PSQL) so you can't introduce some custom logic of sort.
You have two options:
introduce custom procedure/function into your DB and call it from code (ugly & bad approach)
Sort list of movies by playing with lambdas and custom comparators, after fetching it from DB (clean & simpler approach)

SpringBoot named query: IN-clause not working for multiple values

"In clause" not working on passing multiple values
Repository code
#Query( "select myObject from MyObject myObject where myObject.anyColumn in :values" )
public List<MyObject> findPriDestByCntryAbbr(#Param("values") List<String> values);
Calling from service
List<String> values = Arrays.asList(valuesString.split(","));
List<MyObject> result = myObjectRepository.findByAnyColumns(values);
When i am passing single value it is retrieving correct information from table, But on passing multiple values in List "values" giving empty result
Ideally it should work. You should check the values variable.
The other way you can try is- without writing query.
As per the doc we can create MyObjectRepository interface by extending Repository interface and then have a method to fetch all MyObjects. It goes like:
public interface MyObjectRepository extends Repository<MyObject,**Long**> {
List<MyObject> findBy**AnyColumn**In(Collection<String> values);
}
"AnyColumn" is your column name and second parameter for Repository interface is as per your requirement.

eloquent return two columns all records

I'm sure this is very simple - I just need two columns from a country database to populate a <select>
Looking at this URL I came up with the code below, but this seems to return an object for each record
http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_all
class countries extends Eloquent
{
}
public static function getCountrySelect()
{
return countries::all(array("name","iso_3166_2"));
}
If you want to populate custom field from database use this:->
countries::lists('name','iso_3166_2');
i.e
countries::lists('coulmnname1','coulmnname2');
and if you want to get suppose first 10 entries for that model use this:
countries::take(10)->get();

MVC2/LINQ Repository pattern help for a beginner

I have the following method in my repository that returns a mix of two objects from my database,
public IQueryable <ICustomersAndSitesM> CustomerAndSites
{
get
{
return from customer in customerTable
join site in customerSitesTable
on customer.Id equals site.CustomerId
select new CustomersAndSitesMix(customer, site);
}
}
This is my interface code for the ICustomerAndSitesM
interface ICustomersAndSitesM
{
IQueryable<Customer> Customers { get; }
IQueryable<CustomerSite> CustomerSites { get; }
}
Im struggling with working out how and where to define CustomersAndSitesMix, should this be a seperate class or a method in the interface? and will that need to have definttions for both the customer and customer site?
I would set CustomersAndSitesMix up as a class with all the properties from your other two tables that you need. I am not sure why you are needing this interface, but I would also change the return type of your method to List<CustomersAndSitesMix> and add a ToList() on the end of your LINQ query. Then you would return an inner join of both tables, which may be what you want or you may want to add an arguement, say CustomerID to your function so you could pass it in and get only a subset.

Filter every call made by a DataContext when using LinQ Entities

I'm using logical delete in my system and would like to have every call made to the database filtered automatically.
Let say that I'm loading data from the database in the following way :
product.Regions
How could I filter every request made since Regions is an EntitySet<Region> and not a custom method thus not allowing me to add isDeleted = 0
So far I found AssociateWith but I'd hate to have to write a line of code for each Table -> Association of the current project...
I'm looking into either building generic lambda Expressions or.. something else?
You could create an extension method that implements your filter and use that as your convention.
public static class RegionQuery
{
public static IQueryable<Region> GetAll(this IQueryable<Region> query, bool excludeDeleted=true)
{
if (excludeDeleted)
return query.Regions.Where(r => !r.isDeleted);
return query.Regions;
}
}
So whenever you want to query for regions you can make the following call to get only the live regions still providing an opportunity to get at the deleted ones as well.
context.Regions.GetAll();
It my be a little wonky for access the Products property, but still doable. Only issue is you would have to conform to the convention. Or extend the containing class.
someProduct.Regions.GetAll();
I hope that helps. That is what I ended up settling on because I haven't been able to find a solution to this either outside of creating more indirection. Let me know if you or anyone else comes up with a solution to this one. I'm very interested.
It looks to me like you're using a relationship between your Product and Region classes. If so, then somewhere, (the .dbml file for auto-generated LINQ-to-SQL), there exists a mapping that defines the relationship:
[Table(Name = "Product")]
public partial class Product
{
...
private EntitySet<Region> _Regions;
[Association(Storage = "_Regions")]
public EntitySet<Region> Regions
{
get { return this._Regions; }
set { this._Regions.Assign(value); }
}
...
}
You could put some logic in the accessor here, for example:
public IEnumerable<Region> Regions
{
get { return this._Regions.Where(r => !r.isDeleted); }
set { this._Regions.Assign(value); }
}
This way every access through product.Regions will return your filtered Enumerable.

Resources