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

Commit

Permalink
Add fallback when reading batch row with GetValues() to read fields i…
Browse files Browse the repository at this point in the history
…ndividually
  • Loading branch information
mythz committed Mar 23, 2017
1 parent 69cffea commit 1d22996
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
104 changes: 104 additions & 0 deletions src/ServiceStack.OrmLite.PostgreSQL.Tests/Issues/CustomSqlIssue.cs
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);
}
}
}
}

}
9 changes: 8 additions & 1 deletion src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,14 @@ public static int GetColumnIndex(this IDataReader reader, IOrmLiteDialectProvide
if (values == null)
values = new object[reader.FieldCount];

dialectProvider.GetValues(reader, values);
try
{
dialectProvider.GetValues(reader, values);
}
catch (Exception ex)
{
Log.Warn("Error trying to use GetValues() from DataReader. Falling back to individual field reads...", ex);
}
}
else
{
Expand Down

0 comments on commit 1d22996

Please sign in to comment.