Skip to content

Commit 6ef8609

Browse files
committedMar 15, 2011
Began implementing TSqlGenerator
1 parent 369d9f7 commit 6ef8609

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed
 

‎SqlCommandVisualizer/TSqlGenerator.cs

+30-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,40 @@ namespace shr.Visualizers.SqlCommandVisualizer
2727

2828
public class TSqlGenerator
2929
{
30-
SqlCommand m_Command = null;
30+
protected SqlCommand m_Command = null;
31+
protected List<String> _Declarations = new List<string>();
32+
protected List<String> _Assignments = new List<string>();
33+
protected String _Translation;
3134

3235
public TSqlGenerator(SqlCommand Cmd)
3336
{
3437
m_Command = Cmd;
38+
39+
List<TSqlParameter> TSqlParams = new List<TSqlParameter>();
40+
foreach (SqlParameter param in Cmd.Parameters)
41+
{
42+
TSqlParameter TSql = new TSqlParameter(param);
43+
_Declarations.Add(String.Format("{0}", TSql.Declaration));
44+
45+
if(!String.IsNullOrEmpty(TSql.Assignment))
46+
_Assignments.Add(String.Format("{0}", TSql.Assignment));
47+
}
48+
49+
}
50+
51+
public List<String> Declarations
52+
{
53+
get { return _Declarations; }
54+
}
55+
56+
public List<String> Assignments
57+
{
58+
get { return _Assignments; }
59+
}
60+
61+
public String TextTranslation
62+
{
63+
get { return _Translation; }
3564
}
3665
}
3766
}

‎SqlCommandVisualizer/TSqlParameter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public TSqlParameter(SqlParameter param1)
5858

5959
if (_Parameter == null)
6060
_Assignment = "";
61-
else if (Assignors.ContainsKey(_Parameter.SqlDbType))
61+
else if (Assignors.ContainsKey(_Parameter.SqlDbType) && _Parameter.Value != null)
6262
_Assignment = Assignors[_Parameter.SqlDbType](_Parameter);
6363
else
6464
_Assignment = "";

‎VisualizerTests/TSqlGeneratorTests.cs

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/******************************************************************************
2+
Copyright (c) 2011 Seth H. Richards
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
THE SOFTWARE.
19+
******************************************************************************/
20+
21+
using System;
22+
using System.Text;
23+
using System.Collections.Generic;
24+
using System.Linq;
25+
using Microsoft.VisualStudio.TestTools.UnitTesting;
26+
using System.Data.SqlClient;
27+
using shr.Visualizers.SqlCommandVisualizer;
28+
using System.Data;
29+
30+
namespace VisualizerTests
31+
{
32+
/// <summary>
33+
/// Summary description for TSqlGeneratorTests
34+
/// </summary>
35+
[TestClass]
36+
public class TSqlGeneratorTests
37+
{
38+
public TSqlGeneratorTests()
39+
{
40+
//
41+
// TODO: Add constructor logic here
42+
//
43+
}
44+
45+
private TestContext testContextInstance;
46+
47+
/// <summary>
48+
///Gets or sets the test context which provides
49+
///information about and functionality for the current test run.
50+
///</summary>
51+
public TestContext TestContext
52+
{
53+
get
54+
{
55+
return testContextInstance;
56+
}
57+
set
58+
{
59+
testContextInstance = value;
60+
}
61+
}
62+
63+
#region Additional test attributes
64+
//
65+
// You can use the following additional attributes as you write your tests:
66+
//
67+
// Use ClassInitialize to run code before running the first test in the class
68+
// [ClassInitialize()]
69+
// public static void MyClassInitialize(TestContext testContext) { }
70+
//
71+
// Use ClassCleanup to run code after all tests in a class have run
72+
// [ClassCleanup()]
73+
// public static void MyClassCleanup() { }
74+
//
75+
// Use TestInitialize to run code before running each test
76+
// [TestInitialize()]
77+
// public void MyTestInitialize() { }
78+
//
79+
// Use TestCleanup to run code after each test has run
80+
// [TestCleanup()]
81+
// public void MyTestCleanup() { }
82+
//
83+
#endregion
84+
85+
[TestMethod]
86+
public void Can_Generate_SQL_For_Single_Parameter()
87+
{
88+
SqlCommand Cmd = new SqlCommand();
89+
Cmd.Parameters.AddWithValue("@IntParam", 33);
90+
TSqlGenerator Generator = new TSqlGenerator(Cmd);
91+
92+
Assert.AreEqual(1, Generator.Declarations.Count);
93+
Assert.AreEqual(1, Generator.Assignments.Count);
94+
}
95+
96+
[TestMethod]
97+
public void Param_Without_Value_Should_Only_Generate_Declaration()
98+
{
99+
SqlCommand Cmd = new SqlCommand();
100+
Cmd.Parameters.Add("@Param", SqlDbType.Int);
101+
102+
TSqlGenerator Generator = new TSqlGenerator(Cmd);
103+
Assert.AreEqual(1, Generator.Declarations.Count);
104+
Assert.AreEqual(0, Generator.Assignments.Count);
105+
}
106+
}
107+
}

‎VisualizerTests/TSqlParameterTests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public void Can_Generate_Int_Assignment()
119119
Assert.AreEqual("SET @Param1 = 42;", TSqlParam1.Assignment);
120120
}
121121

122+
[TestMethod]
123+
public void Should_Not_Generate_Assignment_For_Int_Without_Value()
124+
{
125+
SqlParameter Param1 = new SqlParameter("@Param1", System.Data.SqlDbType.Int);
126+
TSqlParameter TSqlParam1 = new TSqlParameter(Param1);
127+
Assert.AreEqual("", TSqlParam1.Assignment);
128+
}
129+
122130
[TestMethod]
123131
public void Can_Generate_VarChar_Assignment()
124132
{

‎VisualizerTests/VisualizerTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
</ItemGroup>
4848
<ItemGroup>
4949
<Compile Include="Properties\AssemblyInfo.cs" />
50+
<Compile Include="TSqlGeneratorTests.cs" />
5051
<Compile Include="TSqlParameterTests.cs" />
5152
</ItemGroup>
5253
<ItemGroup>

0 commit comments

Comments
 (0)