/
BoolConverter.cs
37 lines (31 loc) · 1.04 KB
/
BoolConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Data;
namespace ServiceStack.OrmLite.Converters
{
public class BoolConverter : NativeValueOrmLiteConverter
{
public override string ColumnDefinition => "BOOL";
public override DbType DbType => DbType.Boolean;
//Also support coercing 0 != int as Bool
public override object FromDbValue(Type fieldType, object value)
{
if (value is bool)
return value;
return 0 != (long)this.ConvertNumber(typeof(long), value);
}
}
public class BoolAsIntConverter : BoolConverter
{
public override object ToDbValue(Type fieldType, object value)
{
if (value is bool)
return value;
return 0 != (long)this.ConvertNumber(typeof(long), value);
}
public override string ToQuotedString(Type fieldType, object value)
{
var boolValue = (bool)value;
return base.DialectProvider.GetQuotedValue(boolValue ? 1 : 0, typeof(int));
}
}
}