/// <summary> /// Returns a function that convert a SQLite row into an instance of type T using the provided builder functions. The builder /// function may return the same instane of builder more than once, provided that the instance is thread local or locked such /// that only a single given thread ever has access to the instance between calls to builder and build. /// </summary> /// <returns>The result selector function.</returns> /// <param name="builder">A function that provides a builder object that can be used to build an instance of T.</param> /// <param name="build">A function that builds and instance of T using the builder.</param> /// <typeparam name="TBuilder">The builder type.</typeparam> /// <typeparam name="T">The mapped type.</typeparam> public static Func <IReadOnlyList <IResultSetValue>, T> RowToObject <TBuilder, T>(Func <TBuilder> builder, Func <TBuilder, T> build) { Contract.Requires(builder != null); Contract.Requires(build != null); var columns = new Dictionary <string, PropertyInfo>(); var tableName = TableMapping.Get <T>().TableName; var typ = typeof(TBuilder); var props = typ.GetNotIgnoredSettableProperties(); foreach (var prop in props) { var columnName = prop.GetColumnName(); columns.Add(columnName, prop); } return((IReadOnlyList <IResultSetValue> row) => { var builderInst = builder(); foreach (var column in row) { var columnInfo = column.ColumnInfo; PropertyInfo prop; if (columnInfo.TableName == tableName && columns.TryGetValue(columnInfo.OriginName, out prop)) { var value = column.ToObject(prop.PropertyType); prop.SetValue(builderInst, value, null); } } return build(builderInst); }); }
/// <summary> /// Deletes all rows in a given table. /// </summary> /// <param name="This">The database connection.</param> /// <typeparam name="T">The mapped type.</typeparam> public static void DeleteAllRows <T>(this IDatabaseConnection This) { var tableMapping = TableMapping.Get <T>(); This.Execute(SQLBuilder.DeleteAll(tableMapping.TableName)); }
/// <summary> /// Drops the table if it exists. Otherwise this is a no-op. /// </summary> /// <param name="This">The database connection.</param> /// <typeparam name="T">The mapped type.</typeparam> /// <seealso href="https://www.sqlite.org/lang_droptable.html"/> public static void DropTableIfExists <T>(this IDatabaseConnection This) { var tableMapping = TableMapping.Get <T>(); This.Execute(SQLBuilder.DropTableIfExists(tableMapping.TableName)); }
internal static string PrimaryKeyColumn(this TableMapping This) => primaryKeyColumn.GetValue(This, mapping => // Intentionally throw if the column doesn't have a primary key mapping.Columns.Where(x => x.Value.Metadata.IsPrimaryKeyPart).Select(x => x.Key).First());