Ternary operator in select statement

Piotr

I am trying to execute a query which has a ternary operator inside a select statement:

SELECT id, 
       memory_id, 
       CASE 
         WHEN word_to =? THEN word_from 
         ELSE word_to 
       end 
FROM   translations 
WHERE  word_to =? 
       OR word_from =? 

How can I build such a query using a QueryBuilder object? I tried:

public List<Translation> findMeanings(long wordId) {

    try {
        Dao<Translation, Long> translations = getDao(Translation.class);
        QueryBuilder<Translation, Long> queryBuilder = translations.queryBuilder();
        String id = Long.toString(wordId);

        queryBuilder.selectRaw(DBColumns.ID, DBColumns.MEMORY_ID, "CASE WHEN word_to="+id+" THEN word_from ELSE word_to END");
        queryBuilder.where().eq(DBColumns.WORD_TO, id).or().eq(DBColumns.WORD_FROM, id);

        return queryBuilder.query();
    } catch (SQLException e) {
        Logger.error("db", e.getMessage(), e);
    }
    return new ArrayList<Translation>();
}

But I am getting an exception:

Could not compile this SELECT_RAW statement since the caller is expecting a SELECT statement. Check your QueryBuilder methods.

Translation class:

@DatabaseTable(tableName = DatabaseTables.TABLE_TRANSLATIONS)
public class Translation extends BaseEntity {

    @DatabaseField(foreign = true, foreignColumnName = DBColumns.ID, columnName = DBColumns.WORD_TO)
    private Word wordTo;
    @DatabaseField(foreign = true, foreignColumnName = DBColumns.ID)
    private Memory memory;
    //...

where BaseEntity is:

public class BaseEntity {

    @DatabaseField(generatedId = true, unique = true)
    protected long id; 
    //....
Gray

Could not compile this SELECT_RAW statement since the caller is expecting a SELECT statement. Check your QueryBuilder methods.

As soon as you call qb.selectRaw(...) you will need to use qb.queryRaw(...) instead of query(). This is mentioned in the javadocs for selectRaw(...):

Add raw columns or aggregate functions (COUNT, MAX, ...) to the query. This will turn the query into something only suitable for the Dao.queryRaw(String, String...) type of statement. This can be called multiple times to add more columns to select.

Typically the selectRaw(...) is used with some sort of aggregation function like COUNT() or MAX() and is therefore not compatible with an entity type returned by query().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

IEnumerable Select statement with ternary operator

From Dev

IEnumerable Select statement with ternary operator

From Dev

Ternary operator is not a statement

From Dev

Ternary Operator in jQuery statement

From Dev

Convert if statement to ternary operator

From Dev

SQL statement equivalent to ternary operator

From Dev

Ternary operator inside a return statement

From Dev

Max Ternary operator IF statement compare

From Dev

convert some lines of php statement into ternary operator

From Dev

How to use AND in if statement using ternary operator

From Dev

Question Regarding Using Ternary Operator in Switch Statement

From Dev

convert some lines of php statement into ternary operator

From Dev

How to use AND in if statement using ternary operator

From Dev

Cannot embed ternary operator into echo statement

From Dev

PHP inline statement using ternary logic operator "?:"

From Dev

ternary operator causes an error inside if statement

From Dev

Can we use ternary operator within an if statement?

From Dev

MySql SELECT with if statement and LIKE operator

From Dev

Select statement using 'EXISTS' and 'IN' operator

From Dev

Ternary operator evaluating conditional statement while condition not met

From Dev

Difference between ternary (conditional) operator and if statement returning an Action

From Dev

Ternary Operator (in-line "if statement") conversion from MySQL to MySQLi?

From Dev

Java Ternary operator outputs different result than if else statement

From Dev

(How) is it possible to catch an exception in a Java ternary operator statement?

From Dev

How would I write this if else statement as ternary operator

From Dev

How can I convert this if else statement into ternary operator?

From Dev

Undefined variable when converting a ternary operator into traditional if statement

From Dev

+ operator in a ternary operator

From Dev

Ruby ternary operator (or) or operator

Related Related

  1. 1

    IEnumerable Select statement with ternary operator

  2. 2

    IEnumerable Select statement with ternary operator

  3. 3

    Ternary operator is not a statement

  4. 4

    Ternary Operator in jQuery statement

  5. 5

    Convert if statement to ternary operator

  6. 6

    SQL statement equivalent to ternary operator

  7. 7

    Ternary operator inside a return statement

  8. 8

    Max Ternary operator IF statement compare

  9. 9

    convert some lines of php statement into ternary operator

  10. 10

    How to use AND in if statement using ternary operator

  11. 11

    Question Regarding Using Ternary Operator in Switch Statement

  12. 12

    convert some lines of php statement into ternary operator

  13. 13

    How to use AND in if statement using ternary operator

  14. 14

    Cannot embed ternary operator into echo statement

  15. 15

    PHP inline statement using ternary logic operator "?:"

  16. 16

    ternary operator causes an error inside if statement

  17. 17

    Can we use ternary operator within an if statement?

  18. 18

    MySql SELECT with if statement and LIKE operator

  19. 19

    Select statement using 'EXISTS' and 'IN' operator

  20. 20

    Ternary operator evaluating conditional statement while condition not met

  21. 21

    Difference between ternary (conditional) operator and if statement returning an Action

  22. 22

    Ternary Operator (in-line "if statement") conversion from MySQL to MySQLi?

  23. 23

    Java Ternary operator outputs different result than if else statement

  24. 24

    (How) is it possible to catch an exception in a Java ternary operator statement?

  25. 25

    How would I write this if else statement as ternary operator

  26. 26

    How can I convert this if else statement into ternary operator?

  27. 27

    Undefined variable when converting a ternary operator into traditional if statement

  28. 28

    + operator in a ternary operator

  29. 29

    Ruby ternary operator (or) or operator

HotTag

Archive