Пример #1
0
        /// <summary>
        /// Низкоуровневый метод обновления после запроса
        /// </summary>
        /// <param name="query"></param>
        /// <param name="options"></param>
        /// <param name="c"></param>
        /// <param name="allids"></param>
        /// <param name="cascade"></param>
        public List <long> UpdateSingleQuery(string query, ObjectDataCacheHints options, IDbConnection c, List <long> allids, bool cascade)
        {
            options = options ?? ObjectDataCacheHints.Empty;
            object eq = query;

            if (options.KeyQuery)
            {
                eq = options.Key;
                var external = GetByExternals(eq);
                if (null != external)
                {
                    Set(external);
                    var exids = new[] { ((IWithId)external).Id };
                    if (cascade)
                    {
                        AfterUpdateCache(exids, c, new ObjectDataCacheHints {
                            NoChildren = true
                        });
                    }
                    return(exids.ToList());
                }
            }
            else
            {
                var externals = FindByExternals(eq).ToArray();
                if (externals.Any())
                {
                    var exarray = externals.ToArray();
                    foreach (var e in externals.ToArray())
                    {
                        Set(e);
                    }
                    var exids = exarray.OfType <IWithId>().Select(_ => _.Id).ToArray();
                    if (cascade)
                    {
                        AfterUpdateCache(exids, c, new ObjectDataCacheHints {
                            NoChildren = true
                        });
                    }
                    return(exids.ToList());
                }
            }



            allids = allids ?? new List <long>();
            if (null == c)
            {
                return(allids);
            }
            var q = query;

            if (!q.Contains("from"))
            {
                q = "select Id from " + Adapter.GetTableName();
                if (!string.IsNullOrWhiteSpace(query))
                {
                    q += " where " + query;
                }
            }

            if (string.IsNullOrWhiteSpace(c.ConnectionString))
            {
                throw new Exception("bad connection string!!!");
            }
            c.WellOpen();
            var cmd = c.CreateCommand(q);
            var ids = new List <long>();

            SqlLog.Trace(q);
            using (var idsReader = cmd.ExecuteReader()){
                while (idsReader.Read())
                {
                    var id = Convert.ToInt64(idsReader.GetValue(0));
                    if (!_nativeCache.ContainsKey(id))
                    {
                        ids.Add(id);
                    }
                    if (!allids.Contains(id))
                    {
                        allids.Add(id);
                    }
                }
            }
            if (ids.Count != 0)
            {
                q = "(Id in (" + string.Join(",", ids) + "))";

                cmd = c.CreateCommand(Adapter.PrepareSelectQuery(q));
                SqlLog.Trace(cmd.CommandText);
                using (var reader = cmd.ExecuteReader()){
                    var items = Adapter.ProcessRecordSet(reader, true).ToArray();
                    foreach (var item in items)
                    {
                        Set(item);
                    }
                }
                if (cascade)
                {
                    AfterUpdateCache(ids, c, options);
                }
            }
            return(allids);
        }