Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 8859b78

Browse files
committed
reset values after fallback
1 parent e9af073 commit 8859b78

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/ServiceStack.OrmLite.PostgreSQL.Tests/Issues/CustomSqlIssue.cs

+97
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,103 @@ public void test_model_with_array_and_json()
9999
}
100100
}
101101
}
102+
103+
[Alias("my_table")]
104+
public class MyModel
105+
{
106+
public int MyModelId { get; set; }
107+
public string Name { get; set; }
108+
public string Type { get; set; }
109+
public string NewField { get; set; }
110+
}
111+
112+
public class MyNewModel
113+
{
114+
public int MyModelId { get; set; }
115+
public string Name { get; set; }
116+
public string Type { get; set; }
117+
public string NewField { get; set; }
118+
public int RenamedId { get; set; }
119+
}
120+
121+
[Test]
122+
public void test_model_with_simple_array_and_duplicate_fields()
123+
{
124+
using (var db = OpenDbConnection())
125+
{
126+
127+
db.DropAndCreateTable<MyModel>();
128+
129+
db.Insert(new MyModel { MyModelId = 100, Name = "Test Name", NewField = "New Field", Type = "My Type" });
130+
db.Insert(new MyModel { MyModelId = 200, Name = "Tester Name 2", NewField = "New Field 2", Type = "My Type 2" });
131+
132+
const string sql = @"
133+
SELECT *, t2.my_model_id AS renamed_id
134+
FROM (
135+
SELECT *
136+
FROM my_table
137+
CROSS JOIN (SELECT ARRAY[1,2,3,4] AS int_array) AS c
138+
) AS t1
139+
INNER JOIN my_table AS t2 ON t1.my_model_id = t2.my_model_id;";
140+
141+
var results = db.Select<MyNewModel>(sql);
142+
143+
Assert.That(results.Count, Is.GreaterThan(1));
144+
145+
foreach (var result in results)
146+
{
147+
Console.WriteLine("{0} - {1} - {2}".Fmt(result.MyModelId, result.Name, result.RenamedId));
148+
Assert.That(result.MyModelId, Is.Not.EqualTo(0));
149+
Assert.That(result.RenamedId, Is.Not.EqualTo(0));
150+
Assert.That(result.Name, Is.Not.Empty);
151+
}
152+
153+
}
154+
}
155+
156+
[Test]
157+
public void test_model_with_complex_array_and_duplicate_fields()
158+
{
159+
using (var db = OpenDbConnection())
160+
{
161+
db.DropAndCreateTable<ColorModel>();
162+
163+
db.Insert(new ColorModel { Color = "red", Value = "#f00" });
164+
db.Insert(new ColorModel { Color = "green", Value = "#0f0" });
165+
db.Insert(new ColorModel { Color = "blue", Value = "#00f" });
166+
db.Insert(new ColorModel { Color = "cyan", Value = "#0ff" });
167+
db.Insert(new ColorModel { Color = "magenta", Value = "#f0f" });
168+
db.Insert(new ColorModel { Color = "yellow", Value = "#ff0" });
169+
db.Insert(new ColorModel { Color = "black", Value = "#000" });
170+
171+
db.DropAndCreateTable<MyModel>();
172+
173+
db.Insert(new MyModel { MyModelId = 100, Name = "Test Name", NewField = "New Field", Type = "My Type" });
174+
db.Insert(new MyModel { MyModelId = 200, Name = "Tester Name 2", NewField = "New Field 2", Type = "My Type 2" });
175+
176+
const string sql = @"
177+
SELECT *, t2.my_model_id AS renamed_id
178+
FROM (
179+
SELECT *
180+
FROM my_table
181+
CROSS JOIN (SELECT array_agg(color.*) AS color_array FROM color) AS c
182+
) AS t1
183+
INNER JOIN my_table AS t2 ON t1.my_model_id = t2.my_model_id;";
184+
185+
var results = db.Select<MyNewModel>(sql);
186+
187+
Assert.That(results.Count, Is.GreaterThan(1));
188+
189+
foreach (var result in results)
190+
{
191+
Console.WriteLine("{0} - {1} - {2}".Fmt(result.MyModelId, result.Name, result.RenamedId));
192+
Assert.That(result.MyModelId, Is.Not.EqualTo(0));
193+
Assert.That(result.RenamedId, Is.Not.EqualTo(0));
194+
Assert.That(result.Name, Is.Not.Empty);
195+
}
196+
}
197+
}
198+
102199
}
103200

104201
}

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ public static T PopulateWithSqlReader<T>(this T objWithProperties,
300300
}
301301
catch (Exception ex)
302302
{
303+
values = null;
303304
Log.Warn("Error trying to use GetValues() from DataReader. Falling back to individual field reads...", ex);
304305
}
305306
}

0 commit comments

Comments
 (0)