This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback when reading batch row with GetValues() to read fields i…
…ndividually
- Loading branch information
Showing
2 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/ServiceStack.OrmLite.PostgreSQL.Tests/Issues/CustomSqlIssue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Data; | ||
using NUnit.Framework; | ||
using ServiceStack.DataAnnotations; | ||
using ServiceStack.OrmLite.Tests; | ||
using System.Collections.Generic; | ||
using ServiceStack.Text; | ||
|
||
namespace ServiceStack.OrmLite.PostgreSQL.Tests.Issues | ||
{ | ||
[Alias("color")] | ||
public class ColorModel | ||
{ | ||
public string Color { get; set; } | ||
public string Value { get; set; } | ||
} | ||
|
||
public class ColorJsonModel | ||
{ | ||
public int Id { get; set; } | ||
public string ColorJson { get; set; } | ||
} | ||
|
||
public class OrmLiteModelArrayTests : OrmLiteTestBase | ||
{ | ||
public OrmLiteModelArrayTests() : base(Dialect.PostgreSql) {} | ||
|
||
[Test] | ||
public void test_model_with_array_to_json() | ||
{ | ||
using (var db = OpenDbConnection()) | ||
{ | ||
db.DropAndCreateTable<ColorModel>(); | ||
|
||
db.Insert(new ColorModel { Color = "red", Value = "#f00" }); | ||
db.Insert(new ColorModel { Color = "green", Value = "#0f0" }); | ||
db.Insert(new ColorModel { Color = "blue", Value = "#00f" }); | ||
db.Insert(new ColorModel { Color = "cyan", Value = "#0ff" }); | ||
db.Insert(new ColorModel { Color = "magenta", Value = "#f0f" }); | ||
db.Insert(new ColorModel { Color = "yellow", Value = "#ff0" }); | ||
db.Insert(new ColorModel { Color = "black", Value = "#000" }); | ||
|
||
const string sql = @"SELECT 1::integer AS id | ||
, json_agg(color.*) AS color_json | ||
FROM color;"; | ||
|
||
var results = db.Select<ColorJsonModel>(sql); | ||
|
||
//results.PrintDump(); | ||
|
||
Assert.That(results.Count, Is.EqualTo(1)); | ||
|
||
foreach (var result in results) | ||
{ | ||
Assert.That(result.Id, Is.EqualTo(1)); | ||
Assert.That(result.ColorJson, Is.Not.Null); | ||
} | ||
|
||
} | ||
} | ||
|
||
[Test] | ||
public void test_model_with_array_and_json() | ||
{ | ||
//OrmLiteConfig.DeoptimizeReader = true; | ||
|
||
using (var db = OpenDbConnection()) | ||
{ | ||
db.DropAndCreateTable<ColorModel>(); | ||
|
||
db.Insert(new ColorModel { Color = "red", Value = "#f00" }); | ||
db.Insert(new ColorModel { Color = "green", Value = "#0f0" }); | ||
db.Insert(new ColorModel { Color = "blue", Value = "#00f" }); | ||
db.Insert(new ColorModel { Color = "cyan", Value = "#0ff" }); | ||
db.Insert(new ColorModel { Color = "magenta", Value = "#f0f" }); | ||
db.Insert(new ColorModel { Color = "yellow", Value = "#ff0" }); | ||
db.Insert(new ColorModel { Color = "black", Value = "#000" }); | ||
|
||
// SQL contains array and json aggs. | ||
// We usually have ARRAY fields defined in the db, but when | ||
// retrieved we json-ize them. In otherwords the array exists in the tables/views. | ||
// We use SELECT.* which would contain the ARRAY field. | ||
// Array fields are not used in any of our models and should not cause the other | ||
// fields in the model to not be populated. | ||
const string sql = @"SELECT 1::integer AS id | ||
, json_agg(color.*) AS color_json | ||
, array_agg(color.*) AS color_array | ||
FROM color;"; | ||
|
||
var results = db.Select<ColorJsonModel>(sql); | ||
|
||
Assert.That(results.Count, Is.EqualTo(1)); | ||
|
||
foreach (var result in results) | ||
{ | ||
result.ColorJson.Print(); | ||
Assert.That(result.Id, Is.EqualTo(1)); | ||
Assert.That(result.ColorJson, Is.Not.Null); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters