/// <summary> /// Deletes all the objects passed with IDs equal to the passed parameters from the database. /// Relationships are not taken into account in this method /// </summary> /// <param name="conn">SQLite Net connection object</param> /// <param name="primaryKeyValues">Primary keys of the objects to be deleted from the database</param> /// <typeparam name="T">The Entity type, it should match de database entity type</typeparam> public static void DeleteAllIds <T>(this SQLiteConnection conn, IEnumerable <object> primaryKeyValues) { var type = typeof(T); var primaryKeyProperty = type.GetPrimaryKey(); conn.DeleteAllIds(primaryKeyValues.ToArray(), type.GetTableName(), primaryKeyProperty.GetColumnName()); }
static void DeleteAllObjects(this SQLiteConnection conn, IEnumerable elements) { if (elements == null) { return; } var groupedElements = elements.Cast <object>().GroupBy(o => o.GetType()); foreach (var groupElement in groupedElements) { var type = groupElement.Key; var primaryKeyProperty = type.GetPrimaryKey(); Assert(primaryKeyProperty != null, type, null, "Cannot delete objects without primary key"); var primaryKeyValues = (from element in groupElement select primaryKeyProperty.GetValue(element, null)).ToArray(); conn.DeleteAllIds(primaryKeyValues, type.GetTableName(), primaryKeyProperty.GetColumnName()); } }
private static void DeleteAllIds(this SQLiteConnection conn, object[] primaryKeyValues, string entityName, string primaryKeyName) { if (primaryKeyValues == null || primaryKeyValues.Length == 0) { return; } const int limit = 999; if (primaryKeyValues.Length <= limit) { var placeholdersString = string.Join(",", Enumerable.Repeat("?", primaryKeyValues.Length)); var deleteQuery = string.Format("delete from [{0}] where [{1}] in ({2})", entityName, primaryKeyName, placeholdersString); conn.Execute(deleteQuery, primaryKeyValues); } else { foreach (var primaryKeys in Split(primaryKeyValues.ToList(), limit)) { conn.DeleteAllIds(primaryKeys.ToArray(), entityName, primaryKeyName); } } }