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

Support IL2CPP #993

Merged
merged 2 commits into from
Dec 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public SQLiteConnection (SQLiteConnectionString connectionString)
/// by providing better concurrency and better disk IO performance than the normal
/// journal mode. You only need to call this function once in the lifetime of the database.
/// </summary>
public void EnableWriteAheadLogging()
public void EnableWriteAheadLogging ()
{
ExecuteScalar<string> ("PRAGMA journal_mode=WAL");
}
Expand All @@ -352,7 +352,8 @@ public void EnableWriteAheadLogging()
static string Quote (string unsafeString)
{
// TODO: Doesn't call sqlite3_mprintf("%Q", u) because we're waiting on https://github.com/ericsink/SQLitePCL.raw/issues/153
if (unsafeString == null) return "NULL";
if (unsafeString == null)
return "NULL";
var safe = unsafeString.Replace ("'", "''");
return "'" + safe + "'";
}
Expand All @@ -366,7 +367,8 @@ static string Quote (string unsafeString)
/// <param name="key">Ecryption key plain text that is converted to the real encryption key using PBKDF2 key derivation</param>
void SetKey (string key)
{
if (key == null) throw new ArgumentNullException (nameof (key));
if (key == null)
throw new ArgumentNullException (nameof (key));
var q = Quote (key);
Execute ("pragma key = " + q);
}
Expand All @@ -380,8 +382,10 @@ void SetKey (string key)
/// <param name="key">256-bit (32 byte) ecryption key data</param>
void SetKey (byte[] key)
{
if (key == null) throw new ArgumentNullException (nameof (key));
if (key.Length != 32) throw new ArgumentException ("Key must be 32 bytes (256-bit)", nameof (key));
if (key == null)
throw new ArgumentNullException (nameof (key));
if (key.Length != 32)
throw new ArgumentException ("Key must be 32 bytes (256-bit)", nameof (key));
var s = String.Join ("", key.Select (x => x.ToString ("X2")));
Execute ("pragma key = \"x'" + s + "'\"");
}
Expand Down Expand Up @@ -569,7 +573,7 @@ public CreateTableResult CreateTable (Type ty, CreateFlags createFlags = CreateF
var decl = string.Join (",\n", decls.ToArray ());
query += decl;
query += ")";
if(map.WithoutRowId) {
if (map.WithoutRowId) {
query += " without rowid";
}

Expand Down Expand Up @@ -1782,7 +1786,7 @@ PreparedSqlLiteInsertCommand GetInsertCommand (TableMapping map, string extra)
}

prepCmd = CreateInsertCommand (map, extra);

lock (_insertCommandMap) {
if (_insertCommandMap.TryGetValue (key, out var existing)) {
prepCmd.Dispose ();
Expand Down Expand Up @@ -2441,11 +2445,15 @@ public TableMapping (Type type, CreateFlags createFlags = CreateFlags.None)
CreateFlags = createFlags;

var typeInfo = type.GetTypeInfo ();
#if ENABLE_IL2CPP
var tableAttr = typeInfo.GetCustomAttribute<TableAttribute> ();
#else
var tableAttr =
typeInfo.CustomAttributes
.Where (x => x.AttributeType == typeof (TableAttribute))
.Select (x => (TableAttribute)Orm.InflateAttribute (x))
.FirstOrDefault ();
#endif

TableName = (tableAttr != null && !string.IsNullOrEmpty (tableAttr.Name)) ? tableAttr.Name : MappedType.Name;
WithoutRowId = tableAttr != null ? tableAttr.WithoutRowId : false;
Expand Down Expand Up @@ -2567,9 +2575,14 @@ public Column (PropertyInfo prop, CreateFlags createFlags = CreateFlags.None)
var colAttr = prop.CustomAttributes.FirstOrDefault (x => x.AttributeType == typeof (ColumnAttribute));

_prop = prop;
#if ENABLE_IL2CPP
var ca = prop.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
Name = ca == null ? prop.Name : ca.Name;
#else
Name = (colAttr != null && colAttr.ConstructorArguments.Count > 0) ?
colAttr.ConstructorArguments[0].Value?.ToString () :
prop.Name;
#endif
//If this type is Nullable<T> then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the actual type instead
ColumnType = Nullable.GetUnderlyingType (prop.PropertyType) ?? prop.PropertyType;
Collation = Orm.Collation (prop);
Expand Down Expand Up @@ -2749,6 +2762,9 @@ public static bool IsPK (MemberInfo p)

public static string Collation (MemberInfo p)
{
#if ENABLE_IL2CPP
return (p.GetCustomAttribute<CollationAttribute> ()?.Value) ?? "";
#else
return
(p.CustomAttributes
.Where (x => typeof (CollationAttribute) == x.AttributeType)
Expand All @@ -2757,6 +2773,7 @@ public static string Collation (MemberInfo p)
return args.Count > 0 ? ((args[0].Value as string) ?? "") : "";
})
.FirstOrDefault ()) ?? "";
#endif
}

public static bool IsAutoInc (MemberInfo p)
Expand Down Expand Up @@ -2784,6 +2801,9 @@ public static object InflateAttribute (CustomAttributeData x)
{
var atype = x.AttributeType;
var typeInfo = atype.GetTypeInfo ();
#if ENABLE_IL2CPP
var r = Activator.CreateInstance (x.AttributeType);
#else
var args = x.ConstructorArguments.Select (a => a.Value).ToArray ();
var r = Activator.CreateInstance (x.AttributeType, args);
foreach (var arg in x.NamedArguments) {
Expand All @@ -2794,26 +2814,35 @@ public static object InflateAttribute (CustomAttributeData x)
GetProperty (typeInfo, arg.MemberName).SetValue (r, arg.TypedValue.Value);
}
}
#endif
return r;
}

public static IEnumerable<IndexedAttribute> GetIndices (MemberInfo p)
{
#if ENABLE_IL2CPP
return p.GetCustomAttributes<IndexedAttribute> ();
#else
var indexedInfo = typeof (IndexedAttribute).GetTypeInfo ();
return
p.CustomAttributes
.Where (x => indexedInfo.IsAssignableFrom (x.AttributeType.GetTypeInfo ()))
.Select (x => (IndexedAttribute)InflateAttribute (x));
#endif
}

public static int? MaxStringLength (PropertyInfo p)
{
#if ENABLE_IL2CPP
return p.GetCustomAttribute<MaxLengthAttribute> ()?.Value;
#else
var attr = p.CustomAttributes.FirstOrDefault (x => x.AttributeType == typeof (MaxLengthAttribute));
if (attr != null) {
var attrv = (MaxLengthAttribute)InflateAttribute (attr);
return attrv.Value;
}
return null;
#endif
}

public static bool IsMarkedNotNull (MemberInfo p)
Expand Down Expand Up @@ -3215,17 +3244,17 @@ object ReadCol (Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clr
var text = SQLite3.ColumnString (stmt, index);
return new Guid (text);
}
else if (clrType == typeof(Uri)) {
var text = SQLite3.ColumnString(stmt, index);
return new Uri(text);
else if (clrType == typeof (Uri)) {
var text = SQLite3.ColumnString (stmt, index);
return new Uri (text);
}
else if (clrType == typeof (StringBuilder)) {
var text = SQLite3.ColumnString (stmt, index);
return new StringBuilder (text);
}
else if (clrType == typeof(UriBuilder)) {
var text = SQLite3.ColumnString(stmt, index);
return new UriBuilder(text);
else if (clrType == typeof (UriBuilder)) {
var text = SQLite3.ColumnString (stmt, index);
return new UriBuilder (text);
}
else {
throw new NotSupportedException ("Don't know how to read " + clrType);
Expand Down Expand Up @@ -3851,7 +3880,8 @@ static object ConvertTo (object obj, Type t)
Type nut = Nullable.GetUnderlyingType (t);

if (nut != null) {
if (obj == null) return null;
if (obj == null)
return null;
return Convert.ChangeType (obj, nut);
}
else {
Expand Down Expand Up @@ -4340,7 +4370,7 @@ public static long LastInsertRowid (Sqlite3DatabaseHandle db)

public static string GetErrmsg (Sqlite3DatabaseHandle db)
{
return Sqlite3.sqlite3_errmsg (db).utf8_to_string();
return Sqlite3.sqlite3_errmsg (db).utf8_to_string ();
}

public static int BindParameterIndex (Sqlite3Statement stmt, string name)
Expand Down