Exemple #1
0
 /// <summary>
 /// Add parameters for filtering a report to be used on an edit form.
 /// </summary>
 private void AddFauxRelationshipParameters(QuerySettings settings, IDbCommand selectCommand)
 {
     if (settings.TargetResource != 0)
     {
         selectCommand.AddParameterWithValue("@targetResource", settings.TargetResource);
     }
     if (settings.IncludeResources != null)
     {
         selectCommand.AddIdListParameter("@includeResources", settings.IncludeResources);
     }
     if (settings.ExcludeResources != null)
     {
         selectCommand.AddIdListParameter("@excludeResources", settings.ExcludeResources);
     }
 }
        /// <summary>
        ///     Gets the upgrade IDs of a multiple entities.
        /// </summary>
        /// <param name="entityIds">The Int64 ID of an entity.</param>
        public IDictionary <long, Guid> GetUpgradeIds(IEnumerable <long> entityIds)
        {
            // TODO: This is a low use method, however consider offering some form of cache.

            if (entityIds == null)
            {
                throw new ArgumentNullException(nameof(entityIds));
            }

            const string sql = @"select e.Id, e.UpgradeId from Entity e join @ids i on e.Id = i.Id and e.TenantId = @tenantId";

            var result = new Dictionary <long, Guid>();

            using (IDatabaseContext ctx = DatabaseProvider.GetContext())
                using (IDbCommand command = ctx.CreateCommand(sql))
                {
                    command.AddParameter("@tenantId", DbType.Int64, RequestContext.TenantId);
                    command.AddIdListParameter("@ids", entityIds);

                    using (IDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            long id        = reader.GetInt64(0);
                            Guid upgradeId = reader.GetGuid(1);
                            result.Add(id, upgradeId);
                        }
                    }
                }

            return(result);
        }
Exemple #3
0
        /// <summary>
        ///     Called after saving of the specified enumeration of entities has taken place.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <param name="state">The state.</param>
        public void OnAfterSave(IEnumerable <IEntity> entities, IDictionary <string, object> state)
        {
            object         duplicates;
            HashSet <long> entitiesToDeleteSet = new HashSet <long>();

            // Get any merged duplicates to delete
            if (state.TryGetValue(ResourceKeyHelper.MergedDuplicatesStateKey, out duplicates))
            {
                var duplicatesAsHashSet = duplicates as HashSet <long>;

                if (duplicatesAsHashSet != null && duplicatesAsHashSet.Count != 0)
                {
                    entitiesToDeleteSet.UnionWith(duplicatesAsHashSet.Select(id => EventTargetStateHelper.GetIdFromTemporaryId(state, id)));
                }
            }

            Dictionary <long, ResourceKeyDataHash> dataHashesToDelete = ResourceKeyHelper.GetResourceKeyDataHashToDeleteState(state);

            // Get any datahashes to delete
            if (dataHashesToDelete != null && dataHashesToDelete.Count != 0)
            {
                entitiesToDeleteSet.UnionWith(dataHashesToDelete.Keys);
            }

            if (entitiesToDeleteSet.Count != 0)
            {
                Entity.Delete(entitiesToDeleteSet);
            }

            object movedEntitiesObject;

            if (state.TryGetValue("movedEntities", out movedEntitiesObject))
            {
                Dictionary <long, ISet <long> > movedEntities = movedEntitiesObject as Dictionary <long, ISet <long> >;

                if (movedEntities != null)
                {
                    using (DatabaseContext context = DatabaseContext.GetContext( ))
                    {
                        foreach (KeyValuePair <long, ISet <long> > pair in movedEntities)
                        {
                            using (IDbCommand command = context.CreateCommand("spPostEntityMove", CommandType.StoredProcedure))
                            {
                                command.AddIdListParameter("@entityIds", pair.Value);
                                command.AddParameter("@tenantId", DbType.Int64, RequestContext.TenantId);
                                command.AddParameter("@solutionId", DbType.Int64, pair.Key);

                                command.ExecuteNonQuery( );
                            }
                        }
                    }
                }
            }

            // Save the resource key data hashes for any resources at the reverse end
            // of any key relationships.
            ResourceKeyHelper.SaveResourceKeyDataHashesForReverseRelationships(entities, state);
        }
Exemple #4
0
        public void AddIdListParameter( )
        {
            using (DatabaseContext context = DatabaseContext.GetContext( ))
            {
                List <long> ids = new List <long>();
                ids.Add(1);
                ids.Add(2);

                IDbCommand cmd = context.CreateCommand("select Id from @ids order by Id");
                cmd.AddIdListParameter("@ids", ids);

                IDataReader reader = cmd.ExecuteReader( );
                Assert.IsTrue(reader.Read( ));
                Assert.AreEqual(1, reader.GetInt64(0));
                Assert.IsTrue(reader.Read( ));
                Assert.AreEqual(2, reader.GetInt64(0));
                Assert.IsFalse(reader.Read( ));
            }
        }
Exemple #5
0
 /// <summary>
 /// Add root ID filter information.
 /// </summary>
 private void AddRootIdFilterParameter(QuerySettings settings, IDbCommand selectCommand)
 {
     if (settings.RootIdFilterList != null)
     {
         if (!settings.SupportRootIdFilter)
         {
             throw new Exception("Cannot apply rood ID filter because SupportRootIdFilter was not turned on during query evaluation.");
         }
         selectCommand.AddIdListParameter(EntityListParameterName, settings.RootIdFilterList);
     }
     else
     {
         if (settings.SupportRootIdFilter)
         {
             throw new Exception("Query expects RoodIdFilterList but none was provided.");
         }
         if (selectCommand.CommandText.Contains(EntityListParameterName))
         {
             throw new Exception("Query expects EntityList but none was provided.");
         }
     }
 }
Exemple #6
0
        /// <summary>
        /// Execute the query held in the BulkSqlQuery object for the specified entities, and capture the results in a fairly raw format.
        /// </summary>
        /// <param name="query">The query object.</param>
        /// <param name="entities">IDs of root-level entities to load. Must not contain duplicates.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// query
        /// or
        /// entities
        /// </exception>
        /// <exception cref="System.ArgumentException">No entities were loaded;entities</exception>
        public static BulkRequestResult RunQuery(BulkSqlQuery query, IEnumerable <long> entities)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }

            var entitiesList = entities.ToList( );

            if (entitiesList.Count <= 0)
            {
                throw new ArgumentException("No entities were loaded", "entities");
            }

            var result = new BulkRequestResult();

            result.BulkSqlQuery = query;

            /////
            // HACK:TODO: 'LastLogin' is handled differently to all other fields on an entity. See UserAccountValidator
            /////
            long lastLogonId = WellKnownAliases.CurrentTenant.LastLogon;

            if (query.FieldTypes.ContainsKey(lastLogonId))
            {
                using (CacheContext cacheContext = new CacheContext( ))
                {
                    cacheContext.Entities.Add(lastLogonId);
                }
            }

            using (DatabaseContext ctx = DatabaseContext.GetContext())
                using (IDbCommand command = ctx.CreateCommand())
                {
                    // If single entity, then pass via parameter (to allow SQL to cache execution plan)
                    if (entitiesList.Count == 1)
                    {
                        ctx.AddParameter(command, "@entityId", DbType.Int64, entitiesList[0]);
                    }
                    else
                    {
                        command.AddIdListParameter("@entityIds", entitiesList);
                    }

                    ctx.AddParameter(command, "@tenantId", DbType.Int64, RequestContext.TenantId);

                    foreach (KeyValuePair <string, DataTable> tvp in query.TableValuedParameters)
                    {
                        command.AddTableValuedParameter(tvp.Key, tvp.Value);
                    }

                    command.CommandText = "dbo.spExecBulkRequest";
                    command.CommandType = CommandType.StoredProcedure;

                    using (IDataReader reader = command.ExecuteReader())
                    {
                        if (reader != null)
                        {
                            ReadRelationships(reader, query, result);
                            while (reader.NextResult())
                            {
                                ReadFieldValues(reader, query, result);
                            }
                        }
                    }
                }

            return(result);
        }