Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i realize operation COUNT(..), MAX(..), etc? #737

Open
ValeriusGC opened this issue Dec 16, 2016 · 9 comments
Open

How can i realize operation COUNT(..), MAX(..), etc? #737

ValeriusGC opened this issue Dec 16, 2016 · 9 comments
Assignees
Labels

Comments

@ValeriusGC
Copy link
Contributor

What is elegant and effective way to do that with StorIO?

@artem-zinnatullin
Copy link
Member

artem-zinnatullin commented Dec 16, 2016 via email

@ValeriusGC
Copy link
Contributor Author

But how?
Constructon like this

Integer i = storIoSqlite
                .get()
                .object(Integer.class)
                .withQuery(RawQuery.builder()
                         .query("SELECT COUNT(*) FROM some_table WHERE some_field=some_value)
                        .build()
                )
                .prepare()
                .executeAsBlocking();

fails with
Caused by: java.lang.IllegalStateException: This type does not have type mapping: type = class java.lang.Integer,db was not touched by this operation, please add type mapping for this type

@nikitin-da
Copy link
Collaborator

Can you please try something some like this?

try (Cursor cursor = storIOSqlite
                .get()
                .cursor()
                .withQuery(RawQuery.builder()
                                   .query("SELECT COUNT(*) FROM some_table WHERE some_field =?")
                                   .args("some_value")
                                   .build())
                .prepare()
                .executeAsBlocking()
) {
    cursor.moveToFirst();
    Assertions.assertThat(count).isEqualTo(cursor.getInt(0));
}```
You can't use `get().object` for objects without get resolver, so should use `get().cursor` or `get().numberOfResults()`

@nikitin-da nikitin-da self-assigned this Dec 16, 2016
@ValeriusGC
Copy link
Contributor Author

I really really have no idea how perform this primitive operation as COUNT with this sophisticated library. It is terrible to imagine how to implement min()/max()... or get some columns... \B-(
As Metallica sang: "The Thing That Should Not Be"

@artem-zinnatullin
Copy link
Member

artem-zinnatullin commented Dec 16, 2016 via email

@ValeriusGC
Copy link
Contributor Author

Thanks to @nikitin-da problem was gone.
Maybe this solution should be in documentation.

What's the problem with cursor?

@artem-zinnatullin
Copy link
Member

artem-zinnatullin commented Dec 17, 2016 via email

@ValeriusGC
Copy link
Contributor Author

well, ok. i`d make PR for sample or readme with this 'use case'.
But i have another case: how can i perform complex update in one transaction, such as (pseudocode):

begin transaction
get COUNT
put operation on some table
put operation on another table
some ...
end transaction

Please help me with this. So then I combine all oof this in one PR

@nikitin-da
Copy link
Collaborator

You can use storIOSQLite#lowLever for this:

StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
lowLevel.beginTransaction();
try {
    // code that should be execute inside single transaction
    lowLevel.setTransactionSuccessful();
} finally {
    lowLevel.endTransaction();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants