Search code examples
c#.netignite

Apache Ignite SQL query composed primary key NET


I wonder if it is possible to query in some way a certain property of an Class which I used as primary key. My idea was something like the below code. With _key.NAME I receive and error message saying column not exist, I tried with TestOjectPK.NAME too, but without success:

var fieldsQuery = new SqlFieldsQuery("select _val,_key from TestObject where _key.NAME= ? and VALUE= ?", "name1","value");
IQueryCursor<IList> queryCursor = cache.QueryFields(fieldsQuery);

This are my classes to represent a certain table with 3 columns, where 2 of them are PK.

class TestObjectPK : IBinarizable
{
    private Int32 id;
    private String name;

    [QuerySqlField]
    public string NAME
    {
        get{return this.name;}
        set{this.name = value;}
    }

    [QuerySqlField]
    public Int32 ID
    {
        get{return this.id;}
        set{this.id = value;}
    }

    public void WriteBinary(IBinaryWriter writer)
    {
        writer.WriteInt("ID", ID);
        writer.WriteString("NAME", NAME);
    }

    public void ReadBinary(IBinaryReader reader)
    {
        ID = reader.ReadInt("ID");
        NAME = reader.ReadString("NAME");
    }

    public override int GetHashCode()
    {
        return id.GetHashCode() + name.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return id == ((TestObjectPK)obj).id && name == ((TestObjectPK)obj).NAME;
    }
}


class TestObject : IBinarizable
{
    private String value2;
    [QuerySqlField]
    public String VALUE
    {
        get { return this.value2; }
        set { this.value2 = value; }
    }


    public void WriteBinary(IBinaryWriter writer)
    {
        writer.WriteString("VALUE", VALUE);
    }

    public void ReadBinary(IBinaryReader reader)
    {
        VALUE = reader.ReadString("VALUE");
    }
    public override int GetHashCode()
    {
        return VALUE.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return VALUE == ((TestObject)obj).VALUE;
    }

}

Solution

  • All fields marked with [QuerySqlField] from key and value objects end up in SQL table directly.

    So the correct query would be

    select _val,_key from TestObject where NAME=? and VALUE=?
    

    Also make sure to configure both key and value types for SQL:

    new CacheConfiguration("TestObject", 
            new QueryEntity(typeof(TestObjectPK), typeof(TestObject)))