public static string GetCurrentValueAndIncrement <T>(string columnName) { var dataset = DatasetInfo.Get <T>(); var column = dataset[columnName]; return ($@"SELECT `{column.Name}` FROM `{dataset.Name}` WHERE id = @id FOR UPDATE; UPDATE `{dataset.Name}` SET `{column.Name}` = `{columnName}` + 1 WHERE id = @id"); }
private void KeysAndIndexesAreUnder767Bytes(object database) { foreach (var property in database.GetType().GetProperties()) { var type = property.PropertyType; if (!type.IsGenericType) { continue; } type = type.GetGenericArguments()[0]; var dataset = DatasetInfo.Get(type); foreach (var index in dataset.Indexes) { var indexLength = 0; foreach (var member in index.Members) { var m = dataset[member.Name]; indexLength += GetLength(m, dataset); } if (indexLength > 767) { throw new Exception($"table index '{index.Name}' on '{dataset.Name}' exceeds 767 bytes"); } } int primaryKeyLength = 0; foreach (var key in dataset.PrimaryKey) { primaryKeyLength += GetLength(key, dataset); } if (primaryKeyLength > 767) { throw new Exception($"primary key for '{dataset.Name}' exceeds 767 bytes. Was {primaryKeyLength} bytes."); } } }
private Array DeserializeArray(Type elementType, DbValue[] values) { var elementModel = DatasetInfo.Get(elementType); var list = Array.CreateInstance(elementType, values.Length); var i = 0; foreach (var value in values) { if (value.Kind == DbValueType.M) { list.SetValue(((AttributeCollection)value.Value).DeserializeMap(elementModel), i); } i++; } return(list); }
private object DeserializeMap(DatasetInfo model) { var instance = Activator.CreateInstance(model.Type); foreach (var attribute in items) { var member = model[attribute.Key]; if (member == null) { continue; } IDbValueConverter converter; if (DbValueConverterFactory.TryGet(member.Type, out converter)) { var value = converter.ToObject(attribute.Value, member); member.SetValue(instance, value); } else if (attribute.Value.Kind == DbValueType.M) { var map = (AttributeCollection)attribute.Value.Value; member.SetValue(instance, map.DeserializeMap(DatasetInfo.Get(member.Type))); continue; } else if (member.Type.IsArray) { var list = DeserializeArray(member.Type.GetElementType(), (DbValue[])attribute.Value.Value); member.SetValue(instance, list); } } return(instance); }
public T As <T>() => As <T>(DatasetInfo.Get <T>());
public static AttributeCollection FromObject(object instance) => FromObject(instance, DatasetInfo.Get(instance.GetType()));