public void Create(object t) { try { var instance = t; char[] c = { ',' }; string fields = string.Empty; string values = string.Empty; PropertyInfo[] Properties = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo Property in Properties) { //if (!Property.CanRead || !Property.CanWrite) // continue; string Name = Property.Name; object[] att = Property.GetCustomAttributes(true); //if (fieldsToSkip.Contains("," + Name.ToLower() + ",")) // continue; try { PersistentAttribute dataAtt = null; if (att != null && att.Length > 0) { dataAtt = (PersistentAttribute)att[0]; } if (dataAtt != null && !string.IsNullOrEmpty(dataAtt.FieldName)) { if (dataAtt.IsIdentity) { continue; } fields += string.Format("{0},", dataAtt.FieldName); object v = Property.GetValue(instance, null); values += string.Format("'{0}',", FormatValue(v)); } } catch { // Ignore fields that don't exist in the Reader continue; } } string sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", _TableInfo.TableName, fields.TrimEnd(c), values.TrimEnd(c)); DataBase.ExecuteNonQuery(sql); } catch (Exception ex) { throw; } }
public static IEnumerable <Type> SearchForPossibleTypes(Type baseType, bool mustFind) { IEnumerable <Type> result; if (baseType == null || baseType == typeof(Entity)) { result = GetDomainEntityTypes(); } else if (baseType.IsInterface) { result = Database.AssemblyProviderFactories.Except(f => f.Value.SupportsPolymorphism()) .Select(a => a.Key).Where(a => a.References(baseType.Assembly)).Concat(baseType.Assembly) .SelectMany(a => a.GetExportedTypes()) .Where(t => t.Implements(baseType)).ToList(); } else { result = baseType.Assembly.GetExportedTypes().Where(t => t.GetParentTypes().Contains(baseType)).Union(new[] { baseType }); } result = result // Not transient objects: .Where(t => !TransientEntityAttribute.IsTransient(t)) // No abstract or interface: .Where(t => !t.IsAbstract && !t.IsInterface) // Unless the type is marked non-persistent: .Where(t => PersistentAttribute.IsTypePersistent(t)) // Leaf nodes first (most concrete): .OrderByDescending(t => t.GetParentTypes().Count()); result = result.Except(new[] { typeof(IApplicationEvent) }).ToArray(); if (result.None()) { if (baseType != null && mustFind) { throw new ArgumentException("No type in the current application domain can be the implementation of the type " + baseType.FullName + "."); } else if (mustFind) { throw new ArgumentException("No type in the current application domain implements Entity."); } } return(result); }
public void Update(object t) { var instance = t; char[] c = { ',' }; string fieldsValues = string.Empty; string where = string.Empty; PropertyInfo[] Properties = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo Property in Properties) { //if (!Property.CanRead || !Property.CanWrite) // continue; string Name = Property.Name; object[] att = Property.GetCustomAttributes(true); //if (fieldsToSkip.Contains("," + Name.ToLower() + ",")) // continue; try { PersistentAttribute dataAtt = null; if (att != null && att.Length > 0) { dataAtt = (PersistentAttribute)att[0]; } if (dataAtt != null && !string.IsNullOrEmpty(dataAtt.FieldName)) { if (dataAtt.IsUnique) { where = string.Format("{0}='{1}'", dataAtt.FieldName, FormatValue(Property.GetValue(instance, null))); continue; } fieldsValues += string.Format(" {0}='{1}',", dataAtt.FieldName, FormatValue(Property.GetValue(instance, null))); } } catch { // Ignore fields that don't exist in the Reader continue; } } string sql = string.Format("UPDATE {0} SET {1} WHERE {2}", _TableInfo.TableName, fieldsValues.TrimEnd(c), where); DataBase.ExecuteNonQuery(sql); }
public static string CalculateClassName(Type toWrap) { string name = ""; PersistentAttribute persistentAnnotation = toWrap.GetCustomAttribute <PersistentAttribute>(); if (persistentAnnotation != null) { name = persistentAnnotation.Name; } if (name == null || name.Equals("")) { name = toWrap.Name; } return(name); }
public static string CalculateFieldName(PropertyInfo field) { string name = ""; PersistentAttribute persistentAnnotation = field.GetCustomAttribute <PersistentAttribute>(); if (persistentAnnotation != null) { name = persistentAnnotation.Name; } if (name == null || name.Equals("")) { name = field.Name; } return(name); }
private static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef, Dictionary <string, List <SimpleDataColumn> > mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member) { string column = fieldDef.Column; TableDefinition[] tableDefinitionArray = defs.EnsureTables(fieldDef.Table, defaultTables); object defaultValue; if (!string.IsNullOrEmpty(fieldDef.DefaultStringValue)) { defaultValue = StringParser.Parse(fieldDef.DefaultStringValue, member.GetVariableType()); } else { if (string.IsNullOrEmpty(column)) { return; } defaultValue = (object)null; } foreach (TableDefinition key in tableDefinitionArray) { List <DataHolderDefinition> holderDefinitionList = defs.m_tableDataHolderMap.GetOrCreate <TableDefinition, DataHolderDefinition>(key); if (!holderDefinitionList.Contains(accessor.DataHolderDefinition)) { holderDefinitionList.Add(accessor.DataHolderDefinition); } List <SimpleDataColumn> simpleDataColumnList = mappedFields.GetOrCreate <string, SimpleDataColumn>(key.Name); PersistentAttribute persistentAttribute = ((IEnumerable <DBAttribute>)member.GetCustomAttributes <DBAttribute>()) .Where <DBAttribute>((Func <DBAttribute, bool>)(attribute => attribute is PersistentAttribute)) .FirstOrDefault <DBAttribute>() as PersistentAttribute; SimpleDataColumn simpleDataColumn; if (string.IsNullOrEmpty(column)) { simpleDataColumnList.Add(simpleDataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue)); } else { simpleDataColumn = simpleDataColumnList.Find( (Predicate <SimpleDataColumn>)(cmpField => cmpField.ColumnName == column)); if (simpleDataColumn == null) { Type type1 = member.GetActualType(); if (persistentAttribute != null) { Type type2 = persistentAttribute.ReadType; if ((object)type2 == null) { type2 = type1; } type1 = type2; } IFieldReader reader = Converters.GetReader(type1); simpleDataColumnList.Add(simpleDataColumn = new SimpleDataColumn(column, reader)); } } simpleDataColumn.FieldList.Add(accessor); } }
public void DataReaderToObject(IDataReader reader, object instance, string fieldsToSkip) { if (string.IsNullOrEmpty(fieldsToSkip)) { fieldsToSkip = string.Empty; } else { fieldsToSkip = "," + fieldsToSkip + ","; } fieldsToSkip = fieldsToSkip.ToLower(); if (reader.IsClosed) { throw new InvalidOperationException("DataReader passed to DataReaderToObject cannot be closed"); } PropertyInfo[] Properties = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo Property in Properties) { if (!Property.CanRead || !Property.CanWrite) { continue; } string Name = Property.Name; object[] att = Property.GetCustomAttributes(true); if (fieldsToSkip.Contains("," + Name.ToLower() + ",")) { continue; } object value = null; try { PersistentAttribute dataAtt = null; if (att != null && att.Length > 0) { dataAtt = (PersistentAttribute)att[0]; } if (dataAtt != null && !string.IsNullOrEmpty(dataAtt.FieldName)) { value = reader[dataAtt.FieldName]; if (value is DBNull) { value = null; } else { if (Property.PropertyType == typeof(Boolean)) { if (value.ToString() == "1") { value = true; } else { value = false; } } } } else { value = reader[Name]; if (value is DBNull) { value = null; } else { if (Property.PropertyType == typeof(Boolean)) { if (value.ToString() == "1") { value = true; } else { value = false; } } } } } catch { // Ignore fields that don't exist in the Reader continue; } Property.SetValue(instance, value, null); } }