forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolHandler.cs
More file actions
executable file
·42 lines (39 loc) · 1.3 KB
/
BoolHandler.cs
File metadata and controls
executable file
·42 lines (39 loc) · 1.3 KB
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
38
39
40
41
42
using System;
using Npgsql.BackendMessages;
using NpgsqlTypes;
using System.Data;
namespace Npgsql.TypeHandlers
{
/// <remarks>
/// http://www.postgresql.org/docs/current/static/datatype-boolean.html
/// </remarks>
[TypeMapping("bool", NpgsqlDbType.Boolean, DbType.Boolean, typeof(bool))]
internal class BoolHandler : TypeHandler<bool>,
ISimpleTypeReader<bool>, ISimpleTypeWriter
{
public bool Read(NpgsqlBuffer buf, int len, FieldDescription fieldDescription)
{
return buf.ReadByte() != 0;
}
public int ValidateAndGetLength(object value, NpgsqlParameter parameter)
{
if (!(value is bool))
{
var converted = Convert.ToBoolean(value);
if (parameter == null)
{
throw CreateConversionButNoParamException(value.GetType());
}
parameter.ConvertedValue = converted;
}
return 1;
}
public void Write(object value, NpgsqlBuffer buf, NpgsqlParameter parameter)
{
if (parameter != null && parameter.ConvertedValue != null) {
value = parameter.ConvertedValue;
}
buf.WriteByte(((bool)value) ? (byte)1 : (byte)0);
}
}
}