/// <summary> /// Gets the upgrade IDs of a multiple entities. /// </summary> /// <param name="upgradeIds">The Int64 ID of an entity.</param> public IDictionary <Guid, long> GetIdsFromUpgradeIds(IEnumerable <Guid> upgradeIds) { // TODO: This is a low use method, however consider offering some form of cache. if (upgradeIds == null) { throw new ArgumentNullException(nameof(upgradeIds)); } const string sql = @"select e.UpgradeId, e.Id from Entity e join @ids i on e.UpgradeId = i.Id and e.TenantId = @tenantId"; var result = new Dictionary <Guid, long>( ); using (IDatabaseContext ctx = DatabaseProvider.GetContext( )) using (IDbCommand command = ctx.CreateCommand(sql)) { command.AddParameter("@tenantId", DbType.Int64, RequestContext.TenantId); command.AddListParameter("@ids", TableValuedParameterType.Guid, upgradeIds.Select(g => (object)g)); using (IDataReader reader = command.ExecuteReader( )) { while (reader.Read( )) { Guid upgradeId = reader.GetGuid(0); long id = reader.GetInt64(1); result.Add(upgradeId, id); } } } return(result); }
/// <summary> /// Find entities with a field of a particular value. /// </summary> /// <param name="fieldValues">The field values.</param> /// <returns> /// Dictionary matching field values to one or more entities that were found. N, or null if none were found. /// </returns> /// <exception cref="System.ArgumentNullException"> /// typeId /// or /// fieldId /// or /// fieldValues /// </exception> public ILookup <object, long> GetEntitiesByField(IReadOnlyCollection <object> fieldValues) { if (fieldValues == null) { throw new ArgumentNullException("fieldValues"); } // Get user long userId = RequestContext.GetContext( ).Identity.Id; string sql = _queryResult.Sql; // Run query var entities = new List <Tuple <long, object> >( ); using (DatabaseContext ctx = DatabaseContext.GetContext( )) { using (IDbCommand command = ctx.CreateCommand(sql)) { ctx.AddParameter(command, "@user", DbType.Int64, userId); ctx.AddParameter(command, "@tenant", DbType.Int64, RequestContext.TenantId); command.AddListParameter("@valueList", _fieldType, fieldValues); if (_queryResult.SharedParameters != null) { foreach (KeyValuePair <ParameterValue, string> parameter in _queryResult.SharedParameters) { ctx.AddParameter(command, parameter.Value, parameter.Key.Type, parameter.Key.Value); } } using (IDataReader reader = command.ExecuteReader( )) { while (reader.Read( )) { long entityId = reader.GetInt64(0); object fieldValue = reader.GetValue(1); Tuple <long, object> entry = new Tuple <long, object>(entityId, fieldValue); entities.Add(entry); } } } } var results = entities.ToLookup(t => t.Item2, t => t.Item1); return(results); }
/// <summary> /// Call database proc to gather relationship metadata from whichever package has the highest version number. /// </summary> /// <param name="uniqueTypes"></param> /// <returns></returns> private IDictionary <Guid, RelationshipTypeEntry> GetEntriesFromAppLibrary(IEnumerable <Guid> uniqueRelTypes) { Dictionary <Guid, RelationshipTypeEntry> results = new Dictionary <Guid, RelationshipTypeEntry>( ); using (IDatabaseContext ctx = DatabaseContext.GetContext( )) using (IDbCommand command = ctx.CreateCommand( )) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "spGetRelationshipMetadataFromAppLibraryLatest"; command.AddListParameter("@relTypeIds", TableValuedParameterType.Guid, uniqueRelTypes.Select(g => (object)g)); using (IDataReader reader = command.ExecuteReader( )) { int relTypeIdCol = reader.GetOrdinal("RelTypeId"); int aliasCol = reader.GetOrdinal("alias"); int reverseAliasCol = reader.GetOrdinal("reverseAlias"); int cardinalityCol = reader.GetOrdinal("cardinality"); int cloneActionCol = reader.GetOrdinal("cloneAction"); int reverseCloneActionCol = reader.GetOrdinal("reverseCloneAction"); while (reader.Read( )) { Guid relTypeId = reader.GetGuid(relTypeIdCol); var relTypeEntry = new RelationshipTypeEntry { TypeId = relTypeId, Alias = reader.IsDBNull(aliasCol) ? null : reader.GetString(aliasCol), ReverseAlias = reader.IsDBNull(reverseAliasCol) ? null : reader.GetString(reverseAliasCol), CloneAction = DecodeCloneAction(reader, cloneActionCol), ReverseCloneAction = DecodeCloneAction(reader, reverseCloneActionCol), }; results.Add(relTypeId, relTypeEntry); } } } return(results); }