Skip to content

Commit 9776f4f

Browse files
committed
Use IReflectableType inplace of GetType() when possible
1 parent f053c67 commit 9776f4f

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/SQLite.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ public int Insert (object obj)
12781278
if (obj == null) {
12791279
return 0;
12801280
}
1281-
return Insert (obj, "", obj.GetType ());
1281+
return Insert (obj, "", Orm.GetType (obj));
12821282
}
12831283

12841284
/// <summary>
@@ -1299,7 +1299,7 @@ public int InsertOrReplace (object obj)
12991299
if (obj == null) {
13001300
return 0;
13011301
}
1302-
return Insert (obj, "OR REPLACE", obj.GetType ());
1302+
return Insert (obj, "OR REPLACE", Orm.GetType (obj));
13031303
}
13041304

13051305
/// <summary>
@@ -1359,7 +1359,7 @@ public int Insert (object obj, string extra)
13591359
if (obj == null) {
13601360
return 0;
13611361
}
1362-
return Insert (obj, extra, obj.GetType ());
1362+
return Insert (obj, extra, Orm.GetType (obj));
13631363
}
13641364

13651365
/// <summary>
@@ -1442,7 +1442,7 @@ public int Update (object obj)
14421442
if (obj == null) {
14431443
return 0;
14441444
}
1445-
return Update (obj, obj.GetType ());
1445+
return Update (obj, Orm.GetType (obj));
14461446
}
14471447

14481448
/// <summary>
@@ -1543,7 +1543,7 @@ public int UpdateAll (System.Collections.IEnumerable objects, bool runInTransact
15431543
/// </returns>
15441544
public int Delete (object objectToDelete)
15451545
{
1546-
var map = GetMapping (objectToDelete.GetType ());
1546+
var map = GetMapping (Orm.GetType (objectToDelete));
15471547
var pk = map.PK;
15481548
if (pk == null) {
15491549
throw new NotSupportedException ("Cannot delete " + map.TableName + ": it has no PK");
@@ -2140,6 +2140,16 @@ public static class Orm
21402140
public const string ImplicitPkName = "Id";
21412141
public const string ImplicitIndexSuffix = "Id";
21422142

2143+
public static Type GetType (object obj)
2144+
{
2145+
if (obj == null)
2146+
return typeof(object);
2147+
var rt = obj as IReflectableType;
2148+
if (rt != null)
2149+
return rt.GetTypeInfo().AsType();
2150+
return obj.GetType();
2151+
}
2152+
21432153
public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks)
21442154
{
21452155
string decl = "\"" + p.Name + "\" " + SqlType (p, storeDateTimeAsTicks) + " ";
@@ -2499,7 +2509,11 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val
24992509
}
25002510
} else if (value is DateTimeOffset) {
25012511
SQLite3.BindInt64 (stmt, index, ((DateTimeOffset)value).UtcTicks);
2502-
} else {
2512+
} else if (value is byte[]) {
2513+
SQLite3.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer);
2514+
} else if (value is Guid) {
2515+
SQLite3.BindText(stmt, index, ((Guid)value).ToString(), 72, NegativePointer);
2516+
} else {
25032517
// Now we could possibly get an enum, retrieve cached info
25042518
var valueType = value.GetType();
25052519
var enumInfo = EnumCache.GetInfo(valueType);
@@ -2509,12 +2523,8 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val
25092523
SQLite3.BindText(stmt, index, enumInfo.EnumValues[enumIntValue], -1, NegativePointer);
25102524
else
25112525
SQLite3.BindInt(stmt, index, enumIntValue);
2512-
} else if (value is byte[]){
2513-
SQLite3.BindBlob(stmt, index, (byte[]) value, ((byte[]) value).Length, NegativePointer);
2514-
} else if (value is Guid) {
2515-
SQLite3.BindText(stmt, index, ((Guid)value).ToString(), 72, NegativePointer);
25162526
} else {
2517-
throw new NotSupportedException("Cannot store type: " + value.GetType());
2527+
throw new NotSupportedException("Cannot store type: " + Orm.GetType(value));
25182528
}
25192529
}
25202530
}

0 commit comments

Comments
 (0)