private object GetColumnValue(Sqlite3Statement stmt, Type t, string columnName)
{
int columnCount = Sqlite3.sqlite3_column_count(stmt);
int columnIndex = -1;
int columnType = -1;
for(int i = 0;i <columnCount; i++)
{
string colName = Sqlite3.sqlite3_column_name(stmt, i);
if (colName.Equals(columnName, StringComparison.OrdinalIgnoreCase))
{
columnIndex = i;
columnType = Sqlite3.sqlite3_column_type(stmt, i);
break;
}
}
if(t == typeof(string))
{
return Sqlite3.sqlite3_column_text(stmt, columnIndex);
}else if ((typeof(Int32) == t)
|| (typeof(Boolean) == t)
|| (typeof(Byte) == t)
|| (typeof(UInt16) == t)
|| (typeof(Int16) == t)
|| (typeof(sbyte) == t))
{
return Convert.ChangeType(Sqlite3.sqlite3_column_int(stmt, columnIndex), t);
}
else if ((typeof(double) == t)
|| (typeof(float) == t))
{
return Convert.ChangeType(Sqlite3.sqlite3_column_double(stmt, columnIndex), t);
}
else if (typeof(DateTime) == t)
{
string time = Sqlite3.sqlite3_column_text(stmt, columnIndex);
return new DateTime(long.Parse(time, CultureInfo.InvariantCulture.NumberFormat), DateTimeKind.Utc);
}
else if (
(typeof(Int64) == t)
|| (typeof(UInt32) == t)
)
{
return Convert.ChangeType(Sqlite3.sqlite3_column_int64(stmt, columnIndex), t, null);
}
else if (typeof(System.Nullable<long>) == t)
{
if (columnType == Sqlite3.SQLITE_NULL)
{
return null;
}
else
{
long? x = Sqlite3.sqlite3_column_int64(stmt, columnIndex);
return x;
}
}
else if (typeof(System.Nullable<double>) == t)
{
if (columnType == Sqlite3.SQLITE_NULL)
{
return null;
}
else
{
double? x = Sqlite3.sqlite3_column_double(stmt, columnIndex);
return x;
}
}
else if (typeof(System.Nullable<int>) == t)
{
if (columnType == Sqlite3.SQLITE_NULL)
{
return null;
}
else
{
int? x = Sqlite3.sqlite3_column_int(stmt, columnIndex);
return x;
}
}
else if (typeof(decimal) == t)
{
return (decimal)Convert.ChangeType(Sqlite3.sqlite3_column_double(stmt, columnIndex), t);
}
else if (typeof(byte[]) == t)
{
return Sqlite3.sqlite3_column_blob(stmt, columnIndex);
}
else
{
throw new NotSupportedException("Invalid type conversion " + t.FullName);
}
}