public DMSource LoadObject(int targetId) { LoadQuery query = QueryBuilder.Load(GetItem <TableItemAttribute>(typeof(DMSource))); ((LoadQuery)query).AddWhereFragment(targetId); var targetMap = new TableDataMap(typeof(DMSource)); foreach (var entry in targetMap.DataColumns) { query.AddProperty(entry.Key.ColumnName); } DMSource item = CreateNewItem(); var cmd = db.GetCommand(query.Build()); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { for (var i = 0; i < targetMap.DataColumns.Count(); i++) { SetItem(reader.GetName(i), reader.GetString(i), targetMap, item); } } } return(item); }
public DataAccessor() { TABLE = GetItem <TableItemAttribute>(typeof(DMSource)); COLUMNS = GetAllItems <DataColumnAttribute>(typeof(DMSource)); DATA_MAP = new TableDataMap(typeof(DMSource)); db = new DB(); db.Connect(); }
public List <ConnectionDM> LoadConnections(Expression <Func <ConnectionDM, bool> > exp, DMSource model, ConnectionDataMap connection) { LoadQuery query = new LoadQuery(connection.SchemaName, connection.TableName); BinaryExpression operation = (BinaryExpression)exp.Body; query.AddWhereFragment(operation, DataObject: connection); //query.AddProperty("ID"); //query.AddProperty(connection.ParentID); //query.AddProperty(connection.ChildID); var targetMap = new TableDataMap(typeof(ConnectionDM)); Dictionary <string, string> currentPropertyMap = new Dictionary <string, string>(); foreach (var entry in targetMap.IDColumns) { var prop = entry.Value; if (Attribute.IsDefined(prop, typeof(LookupPropertyAttribute))) { var idData = entry.Value.GetCustomAttribute(typeof(IDColumnAttribute)) as IDColumnAttribute; var propertyName = idData.ColumnName; var value = (string)connection.GetType().GetProperty(propertyName).GetValue(connection); query.AddProperty(value); currentPropertyMap[value] = propertyName; } else { query.AddProperty(entry.Key.ColumnName); } } List <ConnectionDM> items = new List <ConnectionDM>(); var cmd = db.GetCommand(query.Build()); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { ConnectionDM item = CreateNewItem <ConnectionDM>(); for (var i = 0; i < DATA_MAP.Columns.Count(); i++) { var itemName = reader.GetName(i); if (currentPropertyMap.ContainsKey(itemName)) { itemName = currentPropertyMap[itemName]; } SetItem(itemName, reader.GetString(i), targetMap, item); } items.Add(item); } } return(items); }
private static void SetItem(string name, string value, TableDataMap targetMap, object target) { if (targetMap[name].PropertyType == typeof(int)) { targetMap[name].SetValue(target, Convert.ToInt32(value)); } else if (targetMap[name].PropertyType == typeof(string)) { targetMap[name].SetValue(target, value); } }