sapiens / SqlFu

Fast and versatile .net core data mapper/micro-orm
Other
229 stars 50 forks source link

Added new Fetch methods for multiple resultsets and... #18

Closed ventaur closed 11 years ago

ventaur commented 11 years ago

...increased visibility of CreateAndSetupCommand so users can create other extension methods to suit their needs.

These methods are described in the discussion for issue: https://github.com/sapiens/SqlFu/issues/6.

If this isn't suitable, I encourage at least increasing CreateAndSetupCommand's visibility to public for the sake of doing something like this in users' own code. Also, tests are an issue with the SQLite database used in the existing tests. However, I've also been using this code in production for over a month now with great results. Here's an example:

public IList<QuizInfo> GetQuizzesForIds(IEnumerable<Guid> quizIds)
{
    var args = new { QuizIds = quizIds };
    string test = "select * from Quiz where Id in (@QuizIds); " +
                  "select * from Question where QuizId in (@QuizIds); " +
                  "select * from Answer A inner join Question Q on A.QuestionId = Q.Id where Q.QuizId in (@QuizIds);";

    var result = Fetch<QuizInfo, QuizQuestionInfo, QuizAnswerInfo>(sql, args);
    var allQuizzes = result.Item1;
    var allQuestions = result.Item2;
    var allAnswers = result.Item3;

    foreach (QuizInfo quiz in allQuizzes) {
        Guid quizId = quiz.Id;
        var questions = allQuestions.Where(q => q.QuizId == quizId).ToList();
        quiz.Questions = questions;

        foreach (QuizQuestionInfo question in questions) {
            Guid questionId = question.Id;
            var answers = allAnswers.Where(a => a.QuestionId == questionId).ToList();
            question.Answers = answers;
        }
    }

    return allQuizzes;
}
sapiens commented 11 years ago

Merged with devel