/// <summary> /// Returns the first dataset from the <see cref="SQLiteConnection"/> using the provided command. /// </summary> /// <typeparam name="T">Indicates the return type, and which table should be accessed.</typeparam> /// <param name="requestCommand">The REQUEST command sent to the SQLite database.</param> /// <param name="filterTableRepresentation">Masks the <see cref="TableRepresentation"/> of each variable.</param> /// <returns>The first item in the SQLite database fullfilling the conditions of the REQUEST.</returns> protected T ExecuteRequestSingleObject <T>(string requestCommand, TableRepresentation filterTableRepresentation = TableRepresentation.All) where T : new() { var type = typeof(T); var varInfos = VariableInfo.FromType(type); T result = default; try { using var cmd = new SQLiteCommand(requestCommand, Connection); using var reader = cmd.ExecuteReader(); result = reader.Read() ? (T)ReadCurrentObject(() => new T(), varInfos, reader, filterTableRepresentation) : default; } catch (InvalidOperationException) { } catch (ArgumentException) { } catch (SQLiteException) { } return(result); }
/// <summary> /// Returns data from the <see cref="SQLiteConnection"/> using the provided command. /// </summary> /// <param name="type">Indicates the return type, and which table should be accessed.</param> /// <param name="requestCommand">The REQUEST command sent to the SQLite database.</param> /// <param name="filterTableRepresentation">Masks the <see cref="TableRepresentation"/> of each variable.</param> /// <returns>All items in the SQLite database fullfilling the conditions of the REQUEST.</returns> protected IList ExecuteRequest(Type type, string requestCommand, TableRepresentation filterTableRepresentation = TableRepresentation.All) { var varInfos = VariableInfo.FromType(type); var result = new List <object>(); try { using var cmd = new SQLiteCommand(requestCommand, Connection); using var reader = cmd.ExecuteReader(); while (reader.Read()) { var obj = ReadCurrentObject(() => Activator.CreateInstance(type), varInfos, reader, filterTableRepresentation); if (obj != null) { result.Add(obj); } } } catch (InvalidOperationException) { } catch (ArgumentException) { } catch (SQLiteException) { } return(result); }
/// <summary> /// Returns data from the <see cref="SQLiteConnection"/> using the provided command. /// </summary> /// <typeparam name="T">Indicates the return type, and which table should be accessed.</typeparam> /// <param name="requestCommand">The REQUEST command sent to the SQLite database.</param> /// <param name="filterTableRepresentation">Masks the <see cref="TableRepresentation"/> of each variable.</param> /// <returns>All items in the SQLite database fullfilling the conditions of the REQUEST.</returns> protected List <T> ExecuteRequest <T>(string requestCommand, TableRepresentation filterTableRepresentation = TableRepresentation.All) where T : new() { var type = typeof(T); var result = new List <T>(); try { var varInfos = VariableInfo.FromType(type); using var cmd = new SQLiteCommand(requestCommand, Connection); using var reader = cmd.ExecuteReader(); while (reader.Read()) { var obj = (T)ReadCurrentObject(() => new T(), varInfos, reader, filterTableRepresentation); if (obj != null) { result.Add(obj); } } } catch (InvalidOperationException) { } catch (ArgumentException) { } catch (SQLiteException) { } return(result); }
/// <summary> /// Returns the first dataset from the <see cref="SQLiteConnection"/> using the provided command. /// </summary> /// <param name="type">Indicates the return type, and which table should be accessed.</param> /// <param name="requestCommand">The REQUEST command sent to the SQLite database.</param> /// <param name="con">The <see cref="SQLiteConnection"/> used.</param> /// <param name="filterTableRepresentation">Masks the <see cref="TableRepresentation"/> of each variable.</param> /// <returns>The first item in the SQLite database fullfilling the conditions of the REQUEST.</returns> protected object ExecuteRequestSingleObject(Type type, string requestCommand, TableRepresentation filterTableRepresentation = TableRepresentation.All) { var varInfos = VariableInfo.FromType(type); object result = null; try { using var cmd = new SQLiteCommand(requestCommand, Connection); using var reader = cmd.ExecuteReader(); result = reader.Read() ? ReadCurrentObject(() => Activator.CreateInstance(type), varInfos, reader, filterTableRepresentation) : null; } catch (InvalidOperationException) { } catch (ArgumentException) { } catch (SQLiteException) { } return(result); }
public Repository(IDbConnection db) { this.db = db; typeMap = TypeTableMaps.Get <TModel>(); }
private static TableRepresentation <TTableType> GetRepresentation <TTableType>() { var type = typeof(TTableType); string tableName = type.Name + "s"; var tableAttr = type.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == nameof(System.ComponentModel.DataAnnotations.Schema.TableAttribute)) as dynamic; if (tableAttr != null) { tableName = tableAttr.Name; } var conventionKeys = new[] { idConvention, type.Name + idConvention }; var tableRepresentation = new TableRepresentation <TTableType> { TableName = tableName }; var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { var nullableBaseType = Nullable.GetUnderlyingType(property.PropertyType); var baseType = nullableBaseType ?? property.PropertyType; if (!IsMappable(property)) { continue; } var isDbGenerated = property.CustomAttributes.Any(m => m.AttributeType.Name == nameof(DbGeneratedAttribute)); var isKey = property.CustomAttributes.Any(m => m.AttributeType.Name == nameof(PrimaryKeyAttribute)) || conventionKeys.Contains(property.Name); var obj = Expression.Parameter(typeof(TTableType)); var prop = Expression.Property(obj, property); var body = Expression.Convert(prop, typeof(object)); var propertyType = nullableBaseType != null ? typeof(string) : baseType; if (baseType.IsEnum) { propertyType = baseType.GetEnumUnderlyingType(); } // If it's nullable as a base then the type used for mapping should be a string tableRepresentation.Columns.Add(new TableProperty <TTableType> { Name = property.Name, Type = propertyType, DbType = sqlDataTypeMap[propertyType], IsDbGenerated = isDbGenerated, IsKey = isKey, Get = Expression.Lambda <Func <TTableType, object> >(body, obj).Compile() }); } // Determine & Store Primary Key if (tableRepresentation.Columns.Count(m => m.IsKey) > 1) { throw new TableMappingException($"Type {type.Name} discovered multiple primary keys"); } tableRepresentation.PrimaryKeyColumn = tableRepresentation.Columns.FirstOrDefault(m => m.IsKey); return(tableRepresentation); }
public static bool MaskFilter(TableRepresentation value, TableRepresentation mask) => (value & mask) == value;