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

Commit dbe649a

Browse files
committed
Handle if value from SQLite Reader is already a bool
1 parent 2977f7c commit dbe649a

3 files changed

Lines changed: 74 additions & 4 deletions

File tree

src/ServiceStack.OrmLite.Sqlite/Converters/SqliteBoolConverter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public override object ToDbValue(Type fieldType, object value)
2222

2323
public override object FromDbValue(Type fieldType, object value)
2424
{
25+
if (value is bool b)
26+
return b;
27+
2528
var intVal = int.Parse(value.ToString());
2629
return intVal != 0;
2730
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Threading.Tasks;
2+
using NUnit.Framework;
3+
using ServiceStack.DataAnnotations;
4+
using ServiceStack.Text;
5+
6+
namespace ServiceStack.OrmLite.Tests.Issues
7+
{
8+
[Alias("users")]
9+
public class User
10+
{
11+
[Alias("id"), PrimaryKey, Required, AutoIncrement]
12+
public int Id { get; set; }
13+
14+
[Alias("username")]
15+
public string Username { get; set; }
16+
17+
[Alias("password")]
18+
public string Password { get; set; }
19+
20+
[Alias("system_admin"), Required]
21+
public bool IsSystemAdmin { get; set; }
22+
23+
[Alias("system_user"), Required]
24+
public bool IsSystemUser { get; set; }
25+
26+
[Alias("is_admin"), Required]
27+
public bool IsAdmin { get; set; }
28+
29+
[Alias("notes")]
30+
public string Notes { get; set; }
31+
}
32+
33+
public class BooleanTests
34+
{
35+
[Test]
36+
public async Task Can_create_user_with_BOOLEAN_columns()
37+
{
38+
var factory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
39+
using var db = await factory.OpenAsync();
40+
OrmLiteUtils.PrintSql();
41+
42+
db.ExecuteSql(@"
43+
CREATE TABLE IF NOT EXISTS `users` (
44+
`id` INTEGER NOT NULL,
45+
`username` VARCHAR,
46+
`system_user` BOOLEAN,
47+
`system_admin` BOOLEAN,
48+
`is_admin` BOOLEAN,
49+
`password` VARCHAR,
50+
`notes` VARCHAR,
51+
PRIMARY KEY(`id`)
52+
);");
53+
54+
var row = new User {
55+
Notes = "notes",
56+
IsAdmin = true,
57+
Username = "user",
58+
Password = "pass"
59+
};
60+
61+
await db.SaveAsync(row);
62+
63+
var dbRow = await db.SingleByIdAsync<User>(row.Id);
64+
Assert.That(dbRow.Id, Is.EqualTo(row.Id));
65+
Assert.That(dbRow.Notes, Is.EqualTo(row.Notes));
66+
Assert.That(dbRow.IsAdmin, Is.EqualTo(row.IsAdmin));
67+
Assert.That(dbRow.Username, Is.EqualTo(row.Username));
68+
Assert.That(dbRow.Password, Is.EqualTo(row.Password));
69+
}
70+
}
71+
}

tests/ServiceStack.OrmLite.Tests.Setup/TestConfig.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Data;
5-
using System.Data.Common;
6-
using System.Data.SqlClient;
7-
using System.IO;
85
using System.Linq;
9-
using ServiceStack.Text;
106

117
namespace ServiceStack.OrmLite.Tests
128
{

0 commit comments

Comments
 (0)