예제 #1
0
        protected override sealed async Task PersistModelAsync(PersistEvent @event, TModel model, Graph <TModel> graph, bool create)
        {
            if (create)
            {
                int autoGeneratedCount = ModelDetail.IdentityAutoGeneratedProperties.Count;

                if (autoGeneratedCount == 1)
                {
                    var rows = await Engine.ExecuteInsertGetIdentitiesAsync(model, graph, ModelDetail);

                    if (rows.Count != 1)
                    {
                        throw new Exception($"Insert failed: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                    }
                    var identity          = rows.First();
                    var modelPropertyInfo = ModelDetail.IdentityAutoGeneratedProperties[0];
                    identity = TypeAnalyzer.Convert(identity, modelPropertyInfo.Type);
                    modelPropertyInfo.Setter(model, identity);
                }
                else if (autoGeneratedCount > 1)
                {
                    var rows = await Engine.ExecuteInsertGetIdentitiesAsync(model, graph, ModelDetail);

                    if (rows.Count != 1)
                    {
                        throw new Exception($"Insert failed: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                    }
                    var identities = (IList <object>)rows.First();
                    var i          = 0;
                    foreach (var modelPropertyInfo in ModelDetail.IdentityAutoGeneratedProperties)
                    {
                        object identity = modelPropertyInfo.Getter(identities[i]);
                        identity = TypeAnalyzer.Convert(identity, modelPropertyInfo.Type);
                        modelPropertyInfo.Setter(model, identity);
                        i++;
                    }
                }
                else
                {
                    var rows = await Engine.ExecuteInsertAsync(model, graph, ModelDetail);

                    if (rows == 0)
                    {
                        throw new Exception($"No rows affected: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                    }
                }
            }
            else
            {
                var rowsAffected = await Engine.ExecuteUpdateAsync(model, graph, ModelDetail);

                if (rowsAffected == 0)
                {
                    throw new Exception($"No rows affected: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                }
            }
        }
예제 #2
0
        protected override sealed async Task PersistModelAsync(PersistEvent @event, TModel model, Graph <TModel> graph, bool create)
        {
            if (create)
            {
                string sql = GenerateSqlInsert(model, graph);

                int autoGeneratedCount = ModelInfo.IdentityAutoGeneratedProperties.Count;

                if (autoGeneratedCount == 1)
                {
                    var    modelPropertyInfo = ModelInfo.IdentityAutoGeneratedProperties[0];
                    object identity          = ExecuteSqlQueryAsync(sql);
                    if (identity == null)
                    {
                        throw new Exception(String.Format("Insert failed: {0}", String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name));
                    }
                    identity = TypeAnalyzer.Convert(identity, modelPropertyInfo.Type);
                    modelPropertyInfo.Setter(model, identity);
                }
                else if (autoGeneratedCount > 1)
                {
                    object[] identities = (object[])(await ExecuteSqlQueryAsync(sql));
                    int      i          = 0;
                    foreach (var modelPropertyInfo in ModelInfo.IdentityAutoGeneratedProperties)
                    {
                        object identity = modelPropertyInfo.Getter(identities[i]);
                        if (identity == null)
                        {
                            throw new Exception(string.Format("Insert failed: {0}", string.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name));
                        }
                        identity = TypeAnalyzer.Convert(identity, modelPropertyInfo.Type);
                        modelPropertyInfo.Setter(model, identity);
                        i++;
                    }
                }
                else
                {
                    int rowsAffected = await ExecuteSqlAsync(sql);

                    if (rowsAffected == 0)
                    {
                        throw new Exception(String.Format("No rows affected: {0}", String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name));
                    }
                }
            }
            else
            {
                string sql          = GenerateSqlUpdate(model, graph);
                int    rowsAffected = await ExecuteSqlAsync(sql);

                if (rowsAffected == 0)
                {
                    throw new Exception(String.Format("No rows affected: {0}", String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name));
                }
            }
        }
예제 #3
0
        private static void SetValue <T>(TypeDetail typeDetail, T model, string name, string value)
        {
            if (!typeDetail.TryGetMemberCaseInsensitive(name, out MemberDetail member))
            {
                return;
            }

            var convertedValue = TypeAnalyzer.Convert(value, member.Type);

            member.Setter(model, convertedValue);
        }
예제 #4
0
        public object Bind(Type type)
        {
            if (IsNull)
            {
                return(null);
            }

            var typeDetail = TypeAnalyzer.GetTypeDetail(type);

            if (typeDetail.IsIEnumerableGeneric)
            {
                if (typeDetail.SpecialType == SpecialType.Dictionary)
                {
                    if (jsonType != JsonObjectType.Object)
                    {
                        throw new InvalidCastException();
                    }
                    var dictionary = (IDictionary)typeDetail.Creator();
                    foreach (var item in valueProperties)
                    {
                        var key   = TypeAnalyzer.Convert(item.Key, typeDetail.IEnumerableGenericInnerTypeDetails.InnerTypes[0]);
                        var value = item.Value.Bind(typeDetail.IEnumerableGenericInnerTypeDetails.InnerTypes[1]);
                        dictionary.Add(key, value);
                    }
                    return(dictionary);
                }
                else if (typeDetail.Type == typeof(byte[]))
                {
                    if (jsonType != JsonObjectType.String)
                    {
                        throw new InvalidCastException();
                    }
                    if (valueString == null)
                    {
                        return(null);
                    }
                    return(Convert.FromBase64String(valueString));
                }
                else
                {
                    if (jsonType != JsonObjectType.Array)
                    {
                        throw new InvalidCastException();
                    }
                    var innerType = typeDetail.InnerTypeDetails[0].Type;
                    if (!typeDetail.Type.IsArray && typeDetail.IsIList)
                    {
                        var listType = TypeAnalyzer.GetGenericTypeDetail(plainListType, innerType);
                        var list     = (IList)listType.Creator();
                        foreach (var item in valueArray)
                        {
                            var value = item.Bind(innerType);
                            list.Add(value);
                        }
                        return(list);
                    }
                    else
                    {
                        var array = Array.CreateInstance(innerType, valueArray.Length);
                        for (var i = 0; i < valueArray.Length; i++)
                        {
                            var value = valueArray[i].Bind(innerType);
                            array.SetValue(value, i);
                        }
                        return(array);
                    }
                }
            }

            if (typeDetail.CoreType.HasValue)
            {
                if (jsonType != JsonObjectType.String && jsonType != JsonObjectType.Literal)
                {
                    throw new InvalidCastException();
                }
                return(typeDetail.CoreType.Value switch
                {
                    CoreType.Boolean => (bool)this,
                    CoreType.Byte => (byte)this,
                    CoreType.SByte => (sbyte)this,
                    CoreType.Int16 => (short)this,
                    CoreType.UInt16 => (ushort)this,
                    CoreType.Int32 => (int)this,
                    CoreType.UInt32 => (uint)this,
                    CoreType.Int64 => (long)this,
                    CoreType.UInt64 => (ulong)this,
                    CoreType.Single => (float)this,
                    CoreType.Double => (double)this,
                    CoreType.Decimal => (decimal)this,
                    CoreType.Char => (char)this,
                    CoreType.DateTime => (DateTime)this,
                    CoreType.DateTimeOffset => (DateTimeOffset)this,
                    CoreType.TimeSpan => (TimeSpan)this,
                    CoreType.Guid => (Guid)this,
                    CoreType.String => (string)this,
                    CoreType.BooleanNullable => (bool?)this,
                    CoreType.ByteNullable => (byte?)this,
                    CoreType.SByteNullable => (sbyte?)this,
                    CoreType.Int16Nullable => (short?)this,
                    CoreType.UInt16Nullable => (ushort?)this,
                    CoreType.Int32Nullable => (int?)this,
                    CoreType.UInt32Nullable => (uint?)this,
                    CoreType.Int64Nullable => (long?)this,
                    CoreType.UInt64Nullable => (ulong?)this,
                    CoreType.SingleNullable => (float?)this,
                    CoreType.DoubleNullable => (double?)this,
                    CoreType.DecimalNullable => (decimal?)this,
                    CoreType.CharNullable => (char?)this,
                    CoreType.DateTimeNullable => (DateTime?)this,
                    CoreType.DateTimeOffsetNullable => (DateTimeOffset?)this,
                    CoreType.TimeSpanNullable => (TimeSpan?)this,
                    CoreType.GuidNullable => (Guid?)this,
                    _ => throw new NotImplementedException(),
                });