Esempio n. 1
0
        protected static List <T> doGetMulti(DataBase dataBase, Where <T> where)
        {
            var stopwatch = Stopwatch.StartNew();

            try
            {
                using (var scope = new NrdoScope(dataBase))
                {
                    return(scope.ExecuteSql(where.SQLStatement, createFromResult, where.SetOnCmd, where.CommandType).ToList());
                }
            }
            finally
            {
                log(stopwatch, "db-hit-multi", where);
            }
        }
Esempio n. 2
0
        private static T doGetSingle(DataBase dataBase, Where <T> where)
        {
            var stopwatch = Stopwatch.StartNew();

            try
            {
                using (var scope = new NrdoScope(dataBase))
                {
                    return(single(scope.ExecuteSql(where.SQLStatement, createFromResult, where.SetOnCmd, where.CommandType)));
                }
            }
            finally
            {
                log(stopwatch, "db-hit-single", where);
            }
        }
Esempio n. 3
0
        protected static void getVoid(DataBase dataBase, Where <T> where)
        {
            var stopwatch = Stopwatch.StartNew();

            try
            {
                using (var scope = new NrdoScope(dataBase))
                {
                    scope.ExecuteSql(where.SQLStatement, where.SetOnCmd, where.CommandType);
                }
            }
            finally
            {
                stopwatch.Stop();
                NrdoStats.UpdateGlobalStats(stats => stats.WithModification(stopwatch.Elapsed));
                log(stopwatch, "db-hit-void", where);
            }
        }
Esempio n. 4
0
 protected abstract void getPkeyFromSeq(NrdoScope scope);
Esempio n. 5
0
        public virtual void Delete()
        {
            // If it's not in the database in the first place,
            // deleting is a no-op.
            if (IsNew)
            {
                return;
            }

            // Store the ModificationCount
            var modCount = DataModification.Count;

            // Store a pre-deletion copy. This will be used in the modification events so that the Id can be captured by code that isn't in this table.
            var preDelete = this.FieldwiseClone();

            var stopwatch = Stopwatch.StartNew();
            var succeeded = false;

            // Make the database change. If an exception is thrown, FullFlush then rethrow it.
            try
            {
                // Delete from the database.
                NrdoTransactedScope.MaybeBeginTransaction(DataBase);
                using (var scope = new NrdoScope(DataBase))
                {
                    scope.ExecuteSql(DeleteStatement, setPkeyOnCmd);
                }
                isNew     = true;
                succeeded = true;
            }
            catch
            {
                DataModification.RaiseFullFlush();
                throw;
            }
            finally
            {
                stopwatch.Stop();
                Nrdo.DebugLog(() => Nrdo.DebugArgs(stopwatch, "db-delete", typeof(T).FullName, "Delete", null));
                if (!succeeded)
                {
                    deletionCache.Value.HitInfo.updateStats(stats => stats.WithFailure(stopwatch.Elapsed), stats => stats.WithFailure(stopwatch.Elapsed));
                }
                else
                {
                    deletionCache.Value.HitInfo.updateStats(stats => stats.WithModification(stopwatch.Elapsed), stats => stats.WithSingleCacheNonHit(stopwatch.Elapsed, false));
                }
            }

            // Lock again. If the modification count is not equal to what it was, FullFlush. Otherwise raise
            // the relevant event.
            lock (Nrdo.LockObj)
            {
                if (DataModification.Count != modCount)
                {
                    DataModification.RaiseFullFlush();
                }
                else
                {
                    DataModification.RaiseDelete(preDelete);
                }
            }
        }
Esempio n. 6
0
        public virtual void UpdateThis()
        {
            // If we can skip it, skip it
            if (!IsNew && IsUnchanged)
            {
                Nrdo.DebugLog(() => Nrdo.DebugArgs("db-update-skipped", typeof(T).FullName, "Update", null));
                return;
            }

            // Store the ModificationCount
            var modCount = DataModification.Count;

            // Make the database change. If an exception is thrown, FullFlush then rethrow it.
            var  wasNew    = IsNew;
            var  stopwatch = Stopwatch.StartNew();
            bool succeeded = false;

            try
            {
                // Insert or update in the database.
                string cmdText = IsNew ? InsertStatement : UpdateStatement;
                if (cmdText != null)
                {
                    NrdoTransactedScope.MaybeBeginTransaction(DataBase);
                    using (var scope = new NrdoScope(DataBase))
                    {
                        scope.ExecuteSql(cmdText, cmd =>
                        {
                            setDataOnCmd(cmd);
                            setPkeyOnCmd(cmd);
                        });
                        if (IsNew)
                        {
                            getPkeyFromSeq(scope);
                        }
                        isNew     = false;
                        succeeded = true;
                    }
                }
            }
            catch
            {
                DataModification.RaiseFullFlush();
                throw;
            }
            finally
            {
                stopwatch.Stop();
                Nrdo.DebugLog(() => Nrdo.DebugArgs(stopwatch, wasNew ? "db-insert" : "db-update", typeof(T).FullName, "Update", null));

                var cache = wasNew ? insertionCache.Value : updateCache.Value;
                if (!succeeded)
                {
                    cache.HitInfo.updateStats(stats => stats.WithFailure(stopwatch.Elapsed), stats => stats.WithFailure(stopwatch.Elapsed));
                }
                else
                {
                    cache.HitInfo.updateStats(stats => stats.WithModification(stopwatch.Elapsed), stats => stats.WithSingleCacheNonHit(stopwatch.Elapsed, false));
                }
            }

            // Lock again. If the modification count is not equal to what it was, FullFlush. Otherwise raise
            // the relevant event.
            lock (Nrdo.LockObj)
            {
                if (DataModification.Count != modCount)
                {
                    DataModification.RaiseFullFlush();
                }
                else if (wasNew)
                {
                    DataModification.RaiseInsert((T)this);
                }
                else
                {
                    DataModification.RaiseUpdate((T)this);
                }
            }
        }