Skip to content

Commit f8a4c8f

Browse files
authored
Merge pull request #2 from asherber/develop
Add more overloads for ForType, GenerateSelect
2 parents 4b5acfb + 7306076 commit f8a4c8f

File tree

5 files changed

+192
-14
lines changed

5 files changed

+192
-14
lines changed

PetaPoco.SqlKata.Tests/AutoGenerateTests.cs

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,154 @@ namespace PetaPoco.SqlKata.Tests
1212
public class AutoGenerateTests
1313
{
1414
[Fact]
15-
public void ForType_Simple_Class()
15+
public void ForType_T_Simple_Class()
1616
{
1717
var q = new Query().ForType<MyClass>();
1818
var expected = new Query("MyClass");
1919
q.Should().BeEquivalentTo(expected);
2020
}
2121

2222
[Fact]
23-
public void ForType_With_TableName()
23+
public void ForType_T_With_TableName()
2424
{
2525
var q = new Query().ForType<MyClassWithName>();
2626
var expected = new Query("TableName");
2727
q.Should().BeEquivalentTo(expected);
2828
}
2929

3030
[Fact]
31-
public void Generate_SimpleClass()
31+
public void ForType_Simple_Class()
32+
{
33+
var q = new Query().ForType(typeof(MyClass));
34+
var expected = new Query("MyClass");
35+
q.Should().BeEquivalentTo(expected);
36+
}
37+
38+
[Fact]
39+
public void ForType_With_TableName()
40+
{
41+
var q = new Query().ForType(typeof(MyClassWithName));
42+
var expected = new Query("TableName");
43+
q.Should().BeEquivalentTo(expected);
44+
}
45+
46+
[Fact]
47+
public void ForObject_Simple_Class()
48+
{
49+
var obj = new MyClass();
50+
var q = new Query().ForObject(obj);
51+
var expected = new Query("MyClass");
52+
q.Should().BeEquivalentTo(expected);
53+
}
54+
55+
[Fact]
56+
public void ForObject_With_TableName()
57+
{
58+
var obj = new MyClassWithName();
59+
var q = new Query().ForObject(obj);
60+
var expected = new Query("TableName");
61+
q.Should().BeEquivalentTo(expected);
62+
}
63+
64+
[Fact]
65+
public void Generate_T_SimpleClass()
3266
{
3367
var q = new Query().GenerateSelect<MyClass>();
3468
var expected = new Query("MyClass").Select("ID", "Name");
3569
q.Should().BeEquivalentTo(expected);
3670
}
3771

3872
[Fact]
39-
public void Generate_NoFields()
73+
public void Generate_T_NoFields()
4074
{
4175
var q = new Query().GenerateSelect<NoFields>();
4276
var expected = new Query("NoFields").SelectRaw("NULL");
4377
q.Should().BeEquivalentTo(expected);
4478
}
4579

4680
[Fact]
47-
public void Generate_With_TableName()
81+
public void Generate_T_With_TableName()
4882
{
4983
var q = new Query().GenerateSelect<MyClassWithName>();
5084
var expected = new Query("TableName").Select("ID", "Name");
5185
q.Should().BeEquivalentTo(expected);
5286
}
5387

5488
[Fact]
55-
public void Generate_With_ColumnNames()
89+
public void Generate_T_With_ColumnNames()
5690
{
5791
var q = new Query().GenerateSelect<MyClassWithColumnNames>();
5892
var expected = new Query("MyClassWithColumnNames").Select("ID_FIELD", "NAME_FIELD");
5993
q.Should().BeEquivalentTo(expected);
6094
}
95+
96+
[Fact]
97+
public void Generate_SimpleClass()
98+
{
99+
var q = new Query().GenerateSelect(typeof(MyClass));
100+
var expected = new Query("MyClass").Select("ID", "Name");
101+
q.Should().BeEquivalentTo(expected);
102+
}
103+
104+
[Fact]
105+
public void Generate_NoFields()
106+
{
107+
var q = new Query().GenerateSelect(typeof(NoFields));
108+
var expected = new Query("NoFields").SelectRaw("NULL");
109+
q.Should().BeEquivalentTo(expected);
110+
}
111+
112+
[Fact]
113+
public void Generate_With_TableName()
114+
{
115+
var q = new Query().GenerateSelect(typeof(MyClassWithName));
116+
var expected = new Query("TableName").Select("ID", "Name");
117+
q.Should().BeEquivalentTo(expected);
118+
}
119+
120+
[Fact]
121+
public void Generate_With_ColumnNames()
122+
{
123+
var q = new Query().GenerateSelect(typeof(MyClassWithColumnNames));
124+
var expected = new Query("MyClassWithColumnNames").Select("ID_FIELD", "NAME_FIELD");
125+
q.Should().BeEquivalentTo(expected);
126+
}
127+
128+
[Fact]
129+
public void Generate_SimpleObject()
130+
{
131+
var obj = new MyClass();
132+
var q = new Query().GenerateSelect(obj);
133+
var expected = new Query("MyClass").Select("ID", "Name");
134+
q.Should().BeEquivalentTo(expected);
135+
}
136+
137+
[Fact]
138+
public void Generate_NoFields_Object()
139+
{
140+
var obj = new NoFields();
141+
var q = new Query().GenerateSelect(obj);
142+
var expected = new Query("NoFields").SelectRaw("NULL");
143+
q.Should().BeEquivalentTo(expected);
144+
}
145+
146+
[Fact]
147+
public void Generate_With_TableName_Object()
148+
{
149+
var obj = new MyClassWithName();
150+
var q = new Query().GenerateSelect(obj);
151+
var expected = new Query("TableName").Select("ID", "Name");
152+
q.Should().BeEquivalentTo(expected);
153+
}
154+
155+
[Fact]
156+
public void Generate_With_ColumnNames_Object()
157+
{
158+
var obj = new MyClassWithColumnNames();
159+
var q = new Query().GenerateSelect(obj);
160+
var expected = new Query("MyClassWithColumnNames").Select("ID_FIELD", "NAME_FIELD");
161+
q.Should().BeEquivalentTo(expected);
162+
}
61163
}
62164

63165

PetaPoco.SqlKata.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters&gt;&lt;Filter ModuleMask="PetaPoco.SqlKata.Tests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /&gt;&lt;Filter ModuleMask="PetaPoco.SqlKata" ModuleVersionMask="*" ClassMask="ThisAssembly" FunctionMask="*" IsEnabled="True" /&gt;&lt;/ExcludeFilters&gt;&lt;/data&gt;</s:String></wpf:ResourceDictionary>

PetaPoco.SqlKata/PetaPoco.SqlKata.cs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,47 @@ public static Sql ToSql(this Query query, CompilerType compilerType)
6868
/// <param name="query"></param>
6969
/// <param name="mapper"></param>
7070
/// <returns></returns>
71-
public static Query ForType<T>(this Query query, IMapper mapper)
71+
public static Query ForType<T>(this Query query, IMapper mapper) => query.ForType(typeof(T), mapper);
72+
73+
/// <summary>
74+
/// Sets the table name for the <seealso cref="Query"/> based on the <seealso cref="PocoData"/> for the given type, using a default mapper.
75+
/// </summary>
76+
/// <param name="query"></param>
77+
/// <param name="type"></param>
78+
/// <returns></returns>
79+
public static Query ForType(this Query query, Type type) => query.ForType(type, _mapper);
80+
81+
/// <summary>
82+
/// Sets the table name for the <seealso cref="Query"/> based on the <seealso cref="PocoData"/> for the given type and mapper.
83+
/// </summary>
84+
/// <param name="query"></param>
85+
/// <param name="type"></param>
86+
/// <param name="mapper"></param>
87+
/// <returns></returns>
88+
public static Query ForType(this Query query, Type type, IMapper mapper)
7289
{
73-
var tableInfo = mapper.GetTableInfo(typeof(T));
90+
var tableInfo = mapper.GetTableInfo(type);
7491
return query.From(tableInfo.TableName);
7592
}
7693

94+
/// <summary>
95+
/// Sets the table name for the <seealso cref="Query"/> based on the <seealso cref="PocoData"/> for the given object, using a default mapper.
96+
/// </summary>
97+
/// <param name="query"></param>
98+
/// <param name="poco"></param>
99+
/// <returns></returns>
100+
public static Query ForObject(this Query query, object poco) => query.ForObject(poco, _mapper);
101+
102+
/// <summary>
103+
/// Sets the table name for the <seealso cref="Query"/> based on the <seealso cref="PocoData"/> for the given object and mapper.
104+
/// </summary>
105+
/// <param name="query"></param>
106+
/// <param name="poco"></param>
107+
/// <param name="mapper"></param>
108+
/// <returns></returns>
109+
public static Query ForObject(this Query query, object poco, IMapper mapper) => query.ForType(poco.GetType(), mapper);
110+
111+
77112
/// <summary>
78113
/// Generates a SELECT query based on the <seealso cref="PocoData"/> for the given type, using a default mapper.
79114
/// </summary>
@@ -89,11 +124,46 @@ public static Query ForType<T>(this Query query, IMapper mapper)
89124
/// <param name="query"></param>
90125
/// <param name="mapper"></param>
91126
/// <returns></returns>
92-
public static Query GenerateSelect<T>(this Query query, IMapper mapper)
127+
public static Query GenerateSelect<T>(this Query query, IMapper mapper) => query.GenerateSelect(typeof(T), mapper);
128+
129+
/// <summary>
130+
/// Generates a SELECT query based on the <seealso cref="PocoData"/> for the given object, using a default mapper.
131+
/// </summary>
132+
/// <param name="query"></param>
133+
/// <param name="poco"></param>
134+
/// <returns></returns>
135+
public static Query GenerateSelect(this Query query, object poco) => query.GenerateSelect(poco, _mapper);
136+
137+
/// <summary>
138+
/// Generates a SELECT query based on the <seealso cref="PocoData"/> for the given object and mapper.
139+
/// </summary>
140+
/// <param name="query"></param>
141+
/// <param name="poco"></param>
142+
/// <param name="mapper"></param>
143+
/// <returns></returns>
144+
public static Query GenerateSelect(this Query query, object poco, IMapper mapper)
145+
=> query.GenerateSelect(poco.GetType(), mapper);
146+
147+
/// <summary>
148+
/// Generates a SELECT query based on the <seealso cref="PocoData"/> for the given type, using a default mapper.
149+
/// </summary>
150+
/// <param name="query"></param>
151+
/// <param name="type"></param>
152+
/// <returns></returns>
153+
public static Query GenerateSelect(this Query query, Type type) => query.GenerateSelect(type, _mapper);
154+
155+
/// <summary>
156+
/// Generates a SELECT query based on the <seealso cref="PocoData"/> for the given type and mapper.
157+
/// </summary>
158+
/// <param name="query"></param>
159+
/// <param name="type"></param>
160+
/// <param name="mapper"></param>
161+
/// <returns></returns>
162+
public static Query GenerateSelect(this Query query, Type type, IMapper mapper)
93163
{
94-
var pd = PocoData.ForType(typeof(T), mapper);
95-
query = query.From(pd.TableInfo.TableName);
164+
var pd = PocoData.ForType(type, mapper);
96165

166+
query = query.From(pd.TableInfo.TableName);
97167
query = pd.Columns.Any() ? query.Select(pd.QueryColumns) : query.SelectRaw("NULL");
98168
return query;
99169
}

PetaPoco.SqlKata/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.1",
2+
"version": "1.2",
33
"publicReleaseRefSpec": [
44
"^refs/heads/master$",
55
"^refs/heads/v\\d+(?:\\.\\d+)?$"

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ public class MyClass
3535
property string Name { get; set; }
3636
}
3737

38-
// This is equivalent to new Query("MyClass")
38+
// These are all equivalent to new Query("MyClass")
3939
// If the class has a TableName property, that will be used instead.
4040
var query = new Query().ForType<MyClass>();
41+
var query = new Query().ForType(typeof(MyClass));
42+
var query = new Query().ForObject(new MyClass());
4143

4244
// SELECT [ID], [NAME_FIELD] FROM [MyClass]
4345
var query = new Query().GenerateSelect<MyClass>();
46+
var query = new Query().GenerateSelect(typeof(MyClass));
47+
var query = new Query().GenerateSelect(new MyClass());
4448

4549
```
4650

47-
Both of these methods also have overloads that can take an `IMapper` instance, if you don't want to use a default `ConventionMapper`.
51+
These methods all use a default `ConventionMapper`. They also have overloads that let you pass in your own `IMapper` instance.
4852

4953
### Compilers
5054

0 commit comments

Comments
 (0)