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

Build failure when referencing types from another table in a Query #575

Closed
sburba opened this issue Dec 30, 2016 · 1 comment

Comments

@sburba
Copy link

commented Dec 30, 2016

When you reference a type in an IN clause in a sql query with a type from another table, sqldelight generates code that cannot be compiled.

(To skip to end, just clone this repo and try to build it)

With a table structure like this:

TableOne.sq

CREATE TABLE table_one (
    _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
);

select_with_types:
SELECT * FROM table_one, table_two
    WHERE type IN :types;

TableTwo.sq

import com.samburba.sqldelighterrorsample.TableTwo;

CREATE TABLE table_two(
    _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    type TEXT AS TableTwo.Type
);

And a TableTwo model that looks like this:

package com.samburba.sqldelighterrorsample;


import android.support.annotation.Nullable;

import com.squareup.sqldelight.ColumnAdapter;
import com.squareup.sqldelight.EnumColumnAdapter;

public class TableTwo implements TableTwoModel {
    private static final Creator<TableTwo> CREATOR = new Creator<TableTwo>() {
        @Override
        public TableTwo create(long _id, @Nullable Type type) {
            return null;
        }
    };

    private static final ColumnAdapter<Type, String> TYPE_ADAPTER = EnumColumnAdapter.create(Type.class);

    static Factory<TableTwo> FACTORY = new Factory<>(CREATOR, TYPE_ADAPTER);

    enum Type {
        TYPE_ONE, TYPE_TWO
    }

    @Override
    public long _id() {
        return 0;
    }

    @Nullable
    @Override
    public Type type() {
        return null;
    }
}

Compilation will fail with the following error:

\app\build\generated\source\sqldelight\com\samburba\sqldelighterrorsample\TableOneModel.java

Error:(146, 13) error: no suitable method found for add(Object)
method Collection.add(String) is not applicable
(argument mismatch; Object cannot be converted to String)
method List.add(String) is not applicable
(argument mismatch; Object cannot be converted to String)

Which refers to these lines in TableOneModel.java

    public SqlDelightStatement select_with_types(TableTwoModel.Factory tableTwoModelFactory, @Nullable TableTwo.Type[] types) {
      List<String> args = new ArrayList<String>();
      int currentIndex = 1;
      StringBuilder query = new StringBuilder();
      query.append("SELECT * FROM table_one, table_two\r\n"
              + "    WHERE type IN ");
      query.append('(');
      for (int i = 0; i < types.length; i++) {
        if (i != 0) query.append(", ");
        query.append('?').append(currentIndex++);
        // Error occurs here
        args.add(tableTwoModelFactory.typeAdapter.encode(types[i]));
      }
      query.append(')');
      return new SqlDelightStatement(query.toString(), args.toArray(new String[args.size()]), Collections.<String>unmodifiableSet(new LinkedHashSet<String>(Arrays.asList("table_one","table_two"))));
    }
@AlecStrong

This comment has been minimized.

Copy link
Collaborator

commented Mar 21, 2017

Thanks for the test suite! Very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.