protected static NameValue GetFieldValue(DataEntityFieldInfo fi, InsertType iops, object v) { if (fi.Key) { if (iops == InsertType.New) { v = null; } else if (v != null && iops == InsertType.Last_Insert_ID) { if (v.GetType().IsValueType) { if (v.Equals(TypeUtils.GetDefaultValue(v.GetType())) || v is string && (v as string) == "") { v = DbProvider.Adapter.LastInsertId(); } } } } if (v != null && fi.Attribute.SerializeType != SerializeType.None) { v = Burst.Utils.Serialize(v, fi.Attribute.SerializeType); } return(new NameValue("@" + fi.Attribute.DbFieldName, v)); }
public static void InitializeDbInfo() { lock (_lock_initialize_dbinfo) { if (_schema != null && _allFields != null) { return; } _ensuredTableName = DbProvider.Adapter.EnsureIdentifier(TableName); _schema = DbProvider.Schema.GetTableInfo(TableName); if (_schema != null) { //KeyField foreach (var column_info in _schema.Columns.Values) { if (column_info.IsKey) { foreach (MemberInfo mi in typeof(T).GetFieldsAndProperties()) { DataFieldAttribute a = mi.GetAttribute <DataFieldAttribute>(); if (a != null) { if (a.DbFieldName == column_info.Name) { _keyField = new DataEntityFieldInfo(); _keyField.MemberInfo = mi; _keyField.Attribute = a; _keyField.EnsuredName = DbProvider.Adapter.EnsureIdentifier(a.DbFieldName); _keyField.Type = mi.GetFieldOrPropertyType(); _keyField.Name = a.DbFieldName; if (a.DisplayName == null) { _keyField.DisplayName = a.DbFieldName; } else { _keyField.DisplayName = a.DisplayName; } _keyField.Key = column_info.IsKey; _keyField.Nullable = column_info.IsNullable; } } } break; } } if (_keyField == null) { throw new DataEntityException("没有主键。", null); } //AllFields, IndexField Dictionary <int, DataEntityFieldInfo> res = new Dictionary <int, DataEntityFieldInfo>(); foreach (MemberInfo mi in typeof(T).GetFieldsAndProperties()) { DataFieldAttribute a = mi.GetAttribute <DataFieldAttribute>(); if (a != null) { DataEntityFieldInfo fi = new DataEntityFieldInfo(); fi.MemberInfo = mi; fi.Attribute = a; fi.EnsuredName = DbProvider.Adapter.EnsureIdentifier(a.DbFieldName); fi.Type = mi.GetFieldOrPropertyType(); fi.Name = a.DbFieldName; if (a.DisplayName == null) { fi.Name = a.DbFieldName; } else { fi.Name = a.DisplayName; } _schema.Columns[a.DbFieldName].Type = fi.Type; _schema.Columns[a.DbFieldName].SerializeType = fi.Attribute.SerializeType; fi.Key = _schema.Columns[a.DbFieldName].IsKey; fi.Nullable = _schema.Columns[a.DbFieldName].IsNullable; res.Add(_schema.Columns[a.DbFieldName].Index, fi); if (mi.GetAttribute <IndexFieldAttribute>() != null && _indexField == null) { _indexField = fi; } } } var items = from k in res.Keys orderby k ascending select res[k]; _allFields = items.ToArray(); } } }