public void Update <T>(T Val) where T : class { Open(); DbEntry ValEntry = Val as DbEntry; List <SQLiteParameter> Params = new List <SQLiteParameter>(); FieldInfo[] Fields = DALFieldAttribute.GetDALFields <T>(); foreach (var F in Fields) { DALFieldAttribute DALField = F.GetCustomAttribute <DALFieldAttribute>(); Params.Add(ValEntry.CreateParameter(DALField.ColumnName)); } using (SQLiteCommand SQL = Con.CreateCommand()) { string NewValues = string.Join(", ", Params.Select(C => C.ParameterName.Substring(1) + " = " + C.ParameterName)); string Conditions = string.Join(", ", Params.Select(C => C.ParameterName)); SQL.CommandText = string.Format("UPDATE {0} SET {1} WHERE id = @id", DALTableAttribute.GetTableName <T>(), NewValues); SQL.Parameters.Add(ValEntry.CreateParameterID()); SQL.Parameters.AddRange(Params.ToArray()); SQL.ExecuteNonQuery(); } Close(); }
IEnumerable <T> Select <T>(string TableName, SQLiteParameter[] Conditions, QueryMode Mode = QueryMode.AND) where T : DbEntry, new() { DataTable Table = Query(TableName, Conditions, Mode); int RowCount = Table.Rows.Count; FieldInfo[] Fields = DALFieldAttribute.GetDALFields <T>(); for (int i = 0; i < RowCount; i++) { T Obj = new T(); for (int j = 0; j < Fields.Length; j++) { string Name = Fields[j].GetCustomAttribute <DALFieldAttribute>().ColumnName; object ColVal = Table.Rows[i][Name]; if (ColVal is DBNull) { ColVal = null; } Type FT = Fields[j].FieldType; if (ColVal != null && ColVal.GetType() != FT) { Type Underlying; if ((Underlying = Nullable.GetUnderlyingType(FT)) != null) { if (ColVal != null) { ColVal = Convert.ChangeType(ColVal, Underlying); ColVal = Activator.CreateInstance(FT, new[] { ColVal }); } } else { ColVal = Convert.ChangeType(ColVal, FT); } } Fields[j].SetValue(Obj, ColVal); } yield return(Obj); } yield break; }
public SQLiteParameter CreateParameter(string Name) { FieldInfo[] Fields = DALFieldAttribute.GetDALFields(GetType()); for (int i = 0; i < Fields.Length; i++) { DALFieldAttribute DALField = Fields[i].GetCustomAttribute <DALFieldAttribute>(); if (DALField.ColumnName == Name) { return(new SQLiteParameter("@" + Name, Fields[i].GetValue(this))); } } throw new Exception(string.Format("Field {0} not found in {1}", Name, GetType().Name)); }
void Insert <T>(string TableName, T Val) where T : DbEntry { Open(); List <SQLiteParameter> ValueColumns = new List <SQLiteParameter>(); FieldInfo[] Fields = DALFieldAttribute.GetDALFields <T>(); foreach (var F in Fields) { DALFieldAttribute DALField = F.GetCustomAttribute <DALFieldAttribute>(); if (DALField.ColumnName == "id") { continue; } object ObjVal = F.GetValue(Val); if (ObjVal != null) { SQLiteParameter Param = new SQLiteParameter("@" + DALField.ColumnName); Param.Value = ObjVal; ValueColumns.Add(Param); } } using (SQLiteCommand SQL = Con.CreateCommand()) { string ParamNames = string.Join(", ", ValueColumns.Select(C => C.ParameterName.Substring(1))); string ParamValues = string.Join(", ", ValueColumns.Select(C => C.ParameterName)); SQL.CommandText = string.Format("INSERT INTO {0}({1}) VALUES({2})", TableName, ParamNames, ParamValues); SQL.Parameters.AddRange(ValueColumns.ToArray()); SQL.ExecuteNonQuery(); } Close(); using (DataTable DT = Query(TableName, null, QueryMode.AND, 1)) { int ID = (int)DT.Rows[0].Field <long>("id"); typeof(T).GetField("ID").SetValue(Val, ID); } }