private FlashCardItem DoCreateCardItem(Guid meaningId, String query, DGetText GetText, FlashCardItemType itemType, bool useExample) { FlashCardItem item = null; SqlCeCommand cmd; lock (_commands) { if (!_commands.TryGetValue(query, out cmd)) { cmd = new SqlCeCommand(query, _connVocabulary); cmd.Parameters.Add(new SqlCeParameter("MeaningId", SqlDbType.UniqueIdentifier)); AddCommand(cmd); } } lock (_connVocabulary) { cmd.Parameters["MeaningId"].Value = meaningId; using (SqlCeDataReader reader = cmd.ExecuteReader()) { StringBuilder sb = new StringBuilder(); String example = String.Empty; while (reader.Read()) { if (sb.Length > 0) sb.Append(", "); sb.Append(GetText(reader)); if (useExample) example = GetExampleText(reader); } if (sb.Length > 0) item = new FlashCardItem(meaningId, itemType, sb.ToString(), example); } } return item; }
private bool DoAddAnswers(FlashCard card, Guid meaningId, Guid typeId, String query, DGetText GetText, FlashCardItemType itemType) { Debug.Assert(card != null); SqlCeCommand cmd = null; lock (_commands) { if (!_commands.TryGetValue(query, out cmd)) { cmd = new SqlCeCommand(query, _connVocabulary); cmd.Parameters.Add(new SqlCeParameter("TypeId", SqlDbType.UniqueIdentifier)); cmd.Parameters.Add(new SqlCeParameter("SeedId", SqlDbType.UniqueIdentifier)); AddCommand(cmd); _commands[query] = cmd; } } lock (_connVocabulary) { cmd.Parameters["TypeId"].Value = typeId; for (int i = 0; i < 10 && card.InnerAnswers.Count < ANSWERS_NUMBER; ++i) { cmd.Parameters["SeedId"].Value = Guid.NewGuid(); using (SqlCeDataReader reader = cmd.ExecuteReader()) { StringBuilder sb = new StringBuilder(); Guid previousMeaningId = Guid.Empty; while (card.InnerAnswers.Count < ANSWERS_NUMBER && reader.Read()) { Guid currentMeaningId = reader.GetGuid(0); if (currentMeaningId != meaningId && card.InnerAnswers.Count<FlashCardItem>(x => x.Id == currentMeaningId) == 0) { if (currentMeaningId != previousMeaningId && previousMeaningId != Guid.Empty) { Debug.Assert(sb.Length > 0); FlashCardItem item = new FlashCardItem(previousMeaningId, itemType, sb.ToString()); card.InnerAnswers.Add(item); sb.Length = 0; } if (sb.Length > 0) sb.Append(", "); sb.Append(GetText(reader)); previousMeaningId = currentMeaningId; } } if (card.InnerAnswers.Count < ANSWERS_NUMBER && previousMeaningId != Guid.Empty && sb.Length > 0) { FlashCardItem item = new FlashCardItem(previousMeaningId, itemType, sb.ToString()); card.InnerAnswers.Add(item); } } } } return card.InnerAnswers.Count > 2; }