Пример #1
0
        internal static Async <Result <T> > InsertGoAsync <T>(Assembly client, DbRow row, bool identityInsert, ConnectBy connectBy,
                                                              Action <Result <T> > onCompleted)
            where T : DbRow
        {
            var name = Text.NotAvailable;

            try
            {
                var connectable = GetInsertBody <T>(client, row, identityInsert, connectBy, Text.Method.InsertGoAsync, ref name);

                return(connectable.GoAsync <T>(result =>
                {
                    if (result.RowCount > 0)
                    {
                        PropertyAccessor.SetValues(row, PropertyAccessor.GetValues(result.First()));
                        row.SetStatus(DbRowStatus.Loaded);
                    }

                    result.FinalizeCrud();
                    onCompleted?.Invoke(result);
                }));
            }
            catch (QueryTalkException)
            {
                throw;
            }
            catch (System.Exception ex)
            {
                throw Crud.ClrException(ex, name, Text.Method.InsertGoAsync);
            }
        }
Пример #2
0
        internal static Result <T> InsertGo <T>(Assembly client, DbRow row, bool identityInsert, ConnectBy connectBy)
            where T : DbRow
        {
            var name = Text.NotAvailable;

            try
            {
                var connectable = GetInsertBody <T>(client, row, identityInsert, connectBy, Text.Method.InsertGo, ref name);
                var result      = connectable.Go <T>();
                if (result.RowCount > 0)
                {
                    PropertyAccessor.SetValues(row, PropertyAccessor.GetValues(result.First()));
                    row.SetStatus(DbRowStatus.Loaded);

                    // has rowversion & it is not RK
                    if (row.HasNonRKRowversion)
                    {
                        Crud.ReloadGo <T>(client, row, false, connectBy, true);
                    }
                    else
                    {
                        // rowversion column is null:
                        var map = DbMapping.TryGetNodeMap(row);
                        if (map.HasRowversion && row.GetRowversionValue() == null)
                        {
                            row.SetStatus(DbRowStatus.Faulted);
                        }
                        else
                        {
                            row.SetStatus(DbRowStatus.Loaded);
                        }
                    }
                }

                result.FinalizeCrud();
                return(result);
            }
            catch (QueryTalkException)
            {
                throw;
            }
            catch (System.Exception ex)
            {
                throw Crud.ClrException(ex, name, Text.Method.InsertGo);
            }
        }
Пример #3
0
        internal static Async <Result <T> > DeleteGoAsync <T>(Assembly client, DbRow row, bool forceMirroring, ConnectBy connectBy,
                                                              Action <Result <T> > onCompleted)
            where T : DbRow
        {
            var     name = Text.NotAvailable;
            NodeMap map;

            try
            {
                var connectable = GetDeleteBody(client, row, forceMirroring, connectBy, Text.Method.DeleteGoAsync, ref name, out map);

                // ignore table T load
                connectable.IgnoreLoad = true;

                return(connectable.GoAsync <T>(result =>
                {
                    if (result.ReturnValue == 1)
                    {
                        row.SetStatus(DbRowStatus.Deleted);
                    }
                    // concurrency violation:
                    else
                    {
                        if (forceMirroring)
                        {
                            var arguments = map.BuildExceptionReport(row.GetOriginalRKValues(), row.GetOriginalRowversionValue());
                            throw new QueryTalkException("Crud.DeleteGoAsync", QueryTalkExceptionType.ConcurrencyViolation,
                                                         arguments, Text.Method.DeleteGo).SetObjectName(map.Name.Sql);
                        }
                    }

                    result.FinalizeCrud();
                    onCompleted?.Invoke(result);
                }));
            }
            catch (QueryTalkException ex)
            {
                Loader.TryThrowInvalidSqlOperationException(ex, name, Text.Method.DeleteGoAsync);
                throw;
            }
            catch (System.Exception ex)
            {
                var ex2 = Crud.ClrException(ex, name, Text.Method.DeleteGoAsync);
                Loader.TryThrowInvalidSqlOperationException(ex2, name);
                throw ex2;
            }
        }
Пример #4
0
        internal static Result <T> DeleteGo <T>(Assembly client, DbRow row, bool forceMirroring, ConnectBy connectBy)
            where T : DbRow
        {
            var     name = Text.NotAvailable;
            NodeMap map;

            try
            {
                var connectable = GetDeleteBody(client, row, forceMirroring, connectBy, Text.Method.DeleteGo, ref name, out map);
                var result      = connectable.Go();
                if (result.ReturnValue == 1)
                {
                    row.SetStatus(DbRowStatus.Deleted);
                    return(new Result <T>(true, 1));
                }
                // concurrency violation:
                else
                {
                    if (forceMirroring)
                    {
                        var arguments = map.BuildExceptionReport(row.GetOriginalRKValues(), row.GetOriginalRowversionValue());
                        throw new QueryTalkException("Crud.DeleteGo", QueryTalkExceptionType.ConcurrencyViolation,
                                                     arguments, Text.Method.DeleteGo).SetObjectName(map.Name.Sql);
                    }
                    else
                    {
                        return(new Result <T>(true, 0));
                    }
                }
            }
            catch (QueryTalkException ex)
            {
                Loader.TryThrowInvalidSqlOperationException(ex, name, Text.Method.DeleteGo);
                throw;
            }
            catch (System.Exception ex)
            {
                var ex2 = Crud.ClrException(ex, name, Text.Method.DeleteGo);
                Loader.TryThrowInvalidSqlOperationException(ex2, name);
                throw ex2;
            }
        }
Пример #5
0
        // isSilent:
        //   if true, then this method is used by other CRUD method (.UpdateGo/.InsertGo) which requires special treatment.
        internal static Result <T> ReloadGo <T>(Assembly client, DbRow row, bool forceMirroring, ConnectBy connectBy,
                                                bool isSilent = false)
            where T : DbRow
        {
            var name = Text.NotAvailable;

            try
            {
                ColumnSelector selector = forceMirroring ? ColumnSelector.All : ColumnSelector.RK;
                if (isSilent)
                {
                    selector = ColumnSelector.RK;
                }

                if (row == null)
                {
                    throw new QueryTalkException("Crud.GoReload", QueryTalkExceptionType.ArgumentNull, "row = null", Text.Method.ReloadGo);
                }

                Crud.CheckTable(row, Text.Method.ReloadGo);

                List <ParameterArgument> args = new List <ParameterArgument>();

                var map = DbMapping.TryGetNodeMap(row);
                name = map.Name.Sql;

                if (forceMirroring)
                {
                    if (!row.GetStatus().IsUpdatable())
                    {
                        var arguments = map.BuildExceptionReport(row.GetOriginalRKValues(), row.GetOriginalRowversionValue());
                        throw new QueryTalkException("Crud.ReloadGo", QueryTalkExceptionType.InvalidMirroring,
                                                     arguments, Text.Method.ReloadGo).SetObjectName(map.Name.Sql);
                    }
                }

                object[] originalValues;

                if (selector == ColumnSelector.All)
                {
                    if (map.HasRowversion)
                    {
                        originalValues = new object[] { row.GetOriginalRowversionValue() };
                    }
                    else
                    {
                        originalValues = row.GetOriginalValues();
                    }
                }
                // RK selector
                else
                {
                    originalValues = row.GetOriginalRKValues();
                }

                for (int i = 0; i < originalValues.Length; ++i)
                {
                    args.Add(new ParameterArgument(new Value(originalValues[i])));
                }

                args.Add(map.Name);

                if (selector == ColumnSelector.All)
                {
                    args.Add(new ParameterArgument(map.BuildOptimisticPredicate(originalValues, 1)));
                }
                else
                {
                    args.Add(new ParameterArgument(map.BuildRKPredicate(originalValues, 1)));
                }

                var cpass = ReloadProc(row.NodeID, selector).Pass(args.ToArray());
                cpass.SetRootMap(row.NodeID);   // important!
                Connectable connectable = Reader.GetConnectable(client, row, cpass, connectBy);
                var         result      = connectable.Go <T>();

                // success
                if (result.RowCount > 0)
                {
                    PropertyAccessor.SetValues(row, result.First().GetOriginalValues());
                    row.SetStatus(DbRowStatus.Loaded);
                    return(new Result <T>(true, 0));
                }
                // not found
                else
                {
                    var arguments = map.BuildExceptionReport(row.GetOriginalRKValues(), row.GetOriginalRowversionValue());

                    if (!isSilent)
                    {
                        if (forceMirroring)
                        {
                            throw new QueryTalkException("Crud.ReloadGo", QueryTalkExceptionType.ConcurrencyViolation,
                                                         arguments, Text.Method.ReloadGo).SetObjectName(map.Name.Sql);
                        }
                        else
                        {
                            throw new QueryTalkException("Crud.ReloadGo", QueryTalkExceptionType.ReloadFailed,
                                                         arguments, Text.Method.ReloadGo).SetObjectName(map.Name.Sql);
                        }
                    }
                    else
                    {
                        row.SetStatus(DbRowStatus.Faulted);
                        return(new Result <T>(true, 0));
                    }
                }
            }
            catch (QueryTalkException ex)
            {
                Loader.TryThrowInvalidSqlOperationException(ex, name, Text.Method.ReloadGo);
                throw;
            }
            catch (System.Exception ex)
            {
                var ex2 = Crud.ClrException(ex, name, Text.Method.ReloadGo);
                Loader.TryThrowInvalidSqlOperationException(ex2, name);
                throw ex2;
            }
        }
Пример #6
0
        internal static Async <Result <T> > UpdateGoAsync <T>(Assembly client, DbRow row, bool forceMirroring, ConnectBy connectBy,
                                                              Action <Result <T> > onCompleted)
            where T : DbRow
        {
            var     name = Text.NotAvailable;
            NodeMap map;

            Crud.CheckTable(row, Text.Method.UpdateGo);

            try
            {
                var connectable = GetUpdateBody <T>(client, row, forceMirroring, connectBy, Text.Method.UpdateGo, ref name, out map);

                // no columns to modify?
                if (connectable == null)
                {
                    return(Async <Result <T> > .CreateDefault <T>());
                }

                return(connectable.GoAsync <T>(result =>
                {
                    try
                    {
                        // success
                        if (result.ReturnValue == 1)
                        {
                            PropertyAccessor.SetValues(row, PropertyAccessor.GetValues(result.First()));
                            row.SetStatus(DbRowStatus.Loaded);
                        }
                        // concurrency violation
                        else
                        {
                            if (forceMirroring)
                            {
                                var arguments = map.BuildExceptionReport(row.GetOriginalRKValues(), row.GetOriginalRowversionValue());
                                throw new QueryTalkException("Crud.UpdateGoAsync", QueryTalkExceptionType.ConcurrencyViolation,
                                                             arguments, Text.Method.UpdateGoAsync).SetObjectName(map.Name.Sql);
                            }
                        }

                        result.FinalizeCrud();
                        onCompleted?.Invoke(result);
                    }
                    catch (QueryTalkException)
                    {
                        throw;
                    }
                    catch (System.Exception ex)
                    {
                        throw Crud.ClrException(ex, name, Text.Method.InsertGoAsync);
                    }
                }));
            }
            catch (QueryTalkException ex)
            {
                Loader.TryThrowInvalidSqlOperationException(ex, name, Text.Method.UpdateGo);
                throw;
            }
            catch (System.Exception ex)
            {
                var ex2 = Crud.ClrException(ex, name, Text.Method.UpdateGo);
                Loader.TryThrowInvalidSqlOperationException(ex2, name);
                throw ex2;
            }
        }
Пример #7
0
        internal static Result <T> UpdateGo <T>(Assembly client, DbRow row, bool forceMirroring, ConnectBy connectBy)
            where T : DbRow
        {
            var     name = Text.NotAvailable;
            NodeMap map;

            try
            {
                var connectable = GetUpdateBody <T>(client, row, forceMirroring, connectBy, Text.Method.UpdateGo, ref name, out map);

                // no columns to modify?
                if (connectable == null)
                {
                    return(new Result <T>(false, 0));
                }

                var result = connectable.Go <T>();

                // success
                if (result.ReturnValue == 1)
                {
                    PropertyAccessor.SetValues(row, PropertyAccessor.GetValues(result.First()));

                    // has rowversion & it is not RK
                    if (row.HasNonRKRowversion)
                    {
                        row.SetStatus(DbRowStatus.Loaded);
                        Crud.ReloadGo <T>(client, row, true, connectBy, true);
                    }
                    else
                    {
                        // rowversion column is null:
                        if (map.HasRowversion && row.GetRowversionValue() == null)
                        {
                            row.SetStatus(DbRowStatus.Faulted);
                        }
                        else
                        {
                            row.SetStatus(DbRowStatus.Loaded);
                        }
                    }

                    result.FinalizeCrud();
                    return(result);
                }
                // optimistic concurrency violation
                else
                {
                    if (forceMirroring)
                    {
                        var arguments = map.BuildExceptionReport(row.GetOriginalRKValues(), row.GetOriginalRowversionValue());
                        throw new QueryTalkException("Crud.UpdateGo", QueryTalkExceptionType.ConcurrencyViolation,
                                                     arguments, Text.Method.UpdateGo).SetObjectName(map.Name.Sql);
                    }
                    else
                    {
                        result.FinalizeCrud();
                        return(result);
                    }
                }
            }
            catch (QueryTalkException ex)
            {
                Loader.TryThrowInvalidSqlOperationException(ex, name, Text.Method.UpdateGo);
                throw;
            }
            catch (System.Exception ex)
            {
                var ex2 = Crud.ClrException(ex, name, Text.Method.UpdateGo);
                Loader.TryThrowInvalidSqlOperationException(ex2, name);
                throw ex2;
            }
        }