fnc12 / sqlite_orm

❤️ SQLite ORM light header only library for modern C++
GNU Affero General Public License v3.0
2.27k stars 313 forks source link

How to chain/assign conditions? #1287

Closed figaro-smartotum closed 4 months ago

figaro-smartotum commented 4 months ago

Hi, in order to prevent the duplications of long code fragments, I am looking for a way to chain where conditions in switch/if clauses.

Please look at the following code snippet:

    using namespace sqlite_orm;

    auto condition = c(&HaccpDetail::event) == HACCP_EVENT_START or  c(&HaccpDetail::event) == HACCP_EVENT_STOP;

    switch (typeFilter) {
        case HACCP_TYPE_EVERYTHING:
            break;

        case HACCP_TYPE_COOKINGS:
           // Missing operator= error
            condition = condition and c(&Haccp::type) == HaccpType::HACCP_TYPE_COOKING; 
            break;

        case HACCP_TYPE_ALARMS:
            // Missing operator= error
            condition = condition and c(&Haccp::type) == HaccpType::HACCP_TYPE_ALARM;
            break;
    }

auto result = this->storage->iterate<HaccpSamples>(where(condition));

// Iterate results

Is there a way to achieve this coding style?

fnc12 commented 4 months ago

Hi. No there is no way to make AST dynamic in sqlite_orm cause AST is based on templates. But you can achieve your idea differently. Please read https://github.com/fnc12/sqlite_orm/issues/459 and https://github.com/fnc12/sqlite_orm/issues/671

figaro-smartotum commented 4 months ago

Many thanks, alternatively, is there a way to perform raw-queries instead?

fnc12 commented 4 months ago

is there a way to perform raw-queries instead?

no. To use raw query you can always use any other regular ORM or libsqlite

fnc12 commented 4 months ago

@figaro-smartotum I am closing this issue cause it doesn't look like something that requires commits to be made. If you have something else please feel free to reopen it