Search code examples
xamarin.iosuniqueidentifiercoolstorage

Are UniqueIdentifier keys supported by Vici CoolStorage for MonoTouch?


A sqlite table declared like this:

CREATE TABLE Note(Id UNIQUEIDENTIFIER, Title TEXT)

is correctly read by Vici CoolStorage on Windows, but on MonoTouch, the following exception is thrown:

[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type. at Vici.CoolStorage.CSDataProviderSQLite.GetSchemaColumns (System.String tableName) [0x00000] in <filename unknown>:0 at Vici.CoolStorage.CSSchema.CreateColumns () [0x00000] in <filename unknown>:0 at Vici.CoolStorage.CSSchema..ctor (System.Type objType) [0x00000] in <filename unknown>:0 at Vici.CoolStorage.CSSchema.Get (System.Type objectType) [0x00000] in <filename unknown>:0 at Vici.CoolStorage.CSList``1[Store.CoolStorage.Note]..ctor () [0x00000] in <filename unknown>:0

It looks like the code that detects the data type of a column based on the type ID does not handle the UNIQUEIDENTIFIER type in Vici's CSDataProviderSqlite for MonoTouch:

From CSSqliteConnection.GetSchema:

switch (dbType) 
{
    case "TEXT": dataType = typeof(string); break;
    case "VARCHAR": dataType = typeof(string); break;
    case "INTEGER": dataType = typeof(int); break;
    case "BOOL": dataType = typeof(bool); break;
    case "DOUBLE": dataType = typeof(double); break;
    case "FLOAT": dataType = typeof(double); break;
    case "REAL": dataType = typeof(double); break;
    case "CHAR": dataType = typeof(string); break;
    case "BLOB": dataType = typeof(byte[]); break;
    case "NUMERIC": dataType = typeof(decimal); break;
    case "DATETIME": dataType = typeof(DateTime); break;
}

There isn't an handler for UNIQUEIDENTIFIER here. Is this a bug in Vici CoolStorage?


Solution

  • I would just modify Vici, since it looks like you have access to the source code:

    switch (dbType) 
    {
        case "TEXT": dataType = typeof(string); break;
        case "VARCHAR": dataType = typeof(string); break;
        case "INTEGER": dataType = typeof(int); break;
        case "BOOL": dataType = typeof(bool); break;
        case "DOUBLE": dataType = typeof(double); break;
        case "FLOAT": dataType = typeof(double); break;
        case "REAL": dataType = typeof(double); break;
        case "CHAR": dataType = typeof(string); break;
        case "BLOB": dataType = typeof(byte[]); break;
        case "NUMERIC": dataType = typeof(decimal); break;
        case "DATETIME": dataType = typeof(DateTime); break;
        case "UNIQUEIDENTIFIER": dataType = typeof(Guid); break;
    }
    

    If there is more required beyond that, you will have to try. Unless there is no try. Use the source, Luke.