|
| 1 | +using System.Data; |
| 2 | +using NUnit.Framework; |
| 3 | +using ServiceStack.DataAnnotations; |
| 4 | + |
| 5 | +namespace ServiceStack.OrmLite.SqlServerTests |
| 6 | +{ |
| 7 | + public class LetterFrequency |
| 8 | + { |
| 9 | + [AutoIncrement] |
| 10 | + public int Id { get; set; } |
| 11 | + |
| 12 | + public string Letter { get; set; } |
| 13 | + } |
| 14 | + |
| 15 | + [TestFixture] |
| 16 | + public class CustomSqlTests : OrmLiteTestBase |
| 17 | + { |
| 18 | + private const string DropProcedureSql = @" |
| 19 | + IF OBJECT_ID('spSearchLetters') IS NOT NULL |
| 20 | + DROP PROCEDURE spSearchLetters"; |
| 21 | + |
| 22 | + private const string CreateProcedureSql = @" |
| 23 | + CREATE PROCEDURE spSearchLetters |
| 24 | + ( |
| 25 | + @pLetter varchar(10), |
| 26 | + @pTotal int OUT |
| 27 | + ) |
| 28 | + AS |
| 29 | + BEGIN |
| 30 | + SELECT @pTotal = COUNT(*) FROM LetterFrequency WHERE Letter = @pLetter |
| 31 | + SELECT * FROM LetterFrequency WHERE Letter = @pLetter |
| 32 | + END"; |
| 33 | + |
| 34 | + [Test] |
| 35 | + public void Can_execute_stored_procedure_using_SqlList_with_out_params() |
| 36 | + { |
| 37 | + using (var db = OpenDbConnection()) |
| 38 | + { |
| 39 | + db.DropAndCreateTable<LetterFrequency>(); |
| 40 | + |
| 41 | + var rows = "A,B,B,C,C,C,D,D,E".Split(',').Map(x => new LetterFrequency { Letter = x }); |
| 42 | + db.InsertAll(rows); |
| 43 | + |
| 44 | + db.ExecuteSql(DropProcedureSql); |
| 45 | + db.ExecuteSql(CreateProcedureSql); |
| 46 | + |
| 47 | + IDbDataParameter pTotal = null; |
| 48 | + var results = db.SqlList<LetterFrequency>("spSearchLetters", |
| 49 | + cmd => |
| 50 | + { |
| 51 | + cmd.CommandType = CommandType.StoredProcedure; |
| 52 | + cmd.AddParam("pLetter", "C"); |
| 53 | + pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output); |
| 54 | + }); |
| 55 | + |
| 56 | + Assert.That(results.Count, Is.EqualTo(3)); |
| 57 | + Assert.That(pTotal.Value, Is.EqualTo("3")); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public void Can_execute_stored_procedure_using_SqlProc_with_out_params() |
| 63 | + { |
| 64 | + using (var db = OpenDbConnection()) |
| 65 | + { |
| 66 | + db.DropAndCreateTable<LetterFrequency>(); |
| 67 | + |
| 68 | + var rows = "A,B,B,C,C,C,D,D,E".Split(',').Map(x => new LetterFrequency { Letter = x }); |
| 69 | + db.InsertAll(rows); |
| 70 | + |
| 71 | + db.ExecuteSql(DropProcedureSql); |
| 72 | + db.ExecuteSql(CreateProcedureSql); |
| 73 | + |
| 74 | + var cmd = db.SqlProc("spSearchLetters", new { pLetter = "C" }); |
| 75 | + var pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output); |
| 76 | + var results = cmd.ConvertToList<LetterFrequency>(); |
| 77 | + |
| 78 | + Assert.That(results.Count, Is.EqualTo(3)); |
| 79 | + Assert.That(pTotal.Value, Is.EqualTo("3")); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments