public List<AncestorPivot> GetAncestorPivots(string[] nonPivots)
 {
     string nonPivotGroup = string.Join(",", nonPivots);
     _session.Command.CommandText = string.Format(
         "SELECT DISTINCT AncestorFactId, AncestorRoleId, PivotId FROM Message " +
             "WHERE FactId IN ({0})",
         nonPivotGroup);
     using (var loader = new Loader(_session.Command.ExecuteReader()))
     {
         _session.Command.Parameters.Clear();
         return loader.LoadAncestorPivots().ToList();
     }
 }
 public List<WindowsPhoneSubscription> GetWindowsPhoneSubscriptionsByPivot(
     IEnumerable<FactID> pivotIds, int clientId)
 {
     string pivotIdGroup = string.Join(",", pivotIds
         .Select(id => id.key.ToString())
         .ToArray());
     _session.Command.CommandText = string.Format(
         "SELECT PivotFactId, DeviceUri " +
         "FROM WindowsPhoneSubscription " +
         "WHERE PivotFactId IN ({0}) " +
         "AND ClientId != @ClientId",
         pivotIdGroup);
     AddParameter("@ClientId", clientId);
     using (var loader = new Loader(_session.Command.ExecuteReader()))
     {
         return loader.LoadWindowsPhoneSubscriptions().ToList();
     }
 }
 public IdentifiedFactMemento GetIdentifiedMemento(FactID factId)
 {
     // Get the fact.
     _session.Command.CommandText = HEAD_SELECT +
         "WHERE f.FactID = @FactID " +
         TAIL_JOIN +
         "ORDER BY p.PredecessorID";
     AddParameter("@FactID", factId.key);
     using (var loader = new Loader(_session.Command.ExecuteReader()))
     {
         _session.Command.Parameters.Clear();
         return loader.LoadMementos().FirstOrDefault();
     }
 }
        public List<FactID> GetRecentMessages(FactID pivotId, TimestampID timestamp, int clientId)
        {
            _session.Command.CommandText =
                "SELECT TOP (20) FactId " +
                "FROM Message " +
                "WHERE PivotId = @PivotId " +
                "AND FactId > @Timestamp " +
                "AND ClientId != @ClientId " +
                "ORDER BY FactId";
            AddParameter("@PivotId", pivotId.key);
            AddParameter("@Timestamp", timestamp.Key);
            AddParameter("@ClientId", clientId);
            using (var loader = new Loader(_session.Command.ExecuteReader()))
            {
                _session.Command.Parameters.Clear();

                return loader.LoadIDs().ToList();
            }
        }
        public List<IdentifiedFactMemento> GetEqualFactsByHashCode(FactMemento memento, bool readCommitted, int typeId)
        {
            _session.Command.CommandText = HEAD_SELECT +
                (readCommitted ? "" : "WITH (NOLOCK) ") +
                "WHERE f.FKTypeID = @TypeID AND f.Hashcode = @Hashcode " +
                TAIL_JOIN +
                "ORDER BY ff.FactID, p.PredecessorID";
            AddParameter("@TypeID", typeId);
            AddParameter("@Hashcode", memento.GetHashCode());
            using (var loader = new Loader(_session.Command.ExecuteReader()))
            {
                _session.Command.Parameters.Clear();

                return loader.LoadMementos()
                    .Where(im => im.Memento.Equals(memento))
                    .ToList();
            }
        }