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
38 lines (34 loc) · 1.39 KB
/
BoolHandler.cs
File metadata and controls
38 lines (34 loc) · 1.39 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
using System.Data;
using Npgsql.BackendMessages;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
using Npgsql.TypeMapping;
using NpgsqlTypes;
namespace Npgsql.TypeHandlers
{
/// <summary>
/// A type handler for the PostgreSQL bool data type.
/// </summary>
/// <remarks>
/// See http://www.postgresql.org/docs/current/static/datatype-boolean.html.
///
/// The type handler API allows customizing Npgsql's behavior in powerful ways. However, although it is public, it
/// should be considered somewhat unstable, and may change in breaking ways, including in non-major releases.
/// Use it at your own risk.
/// </remarks>
[TypeMapping("boolean", NpgsqlDbType.Boolean, DbType.Boolean, typeof(bool))]
public class BoolHandler : NpgsqlSimpleTypeHandler<bool>
{
/// <inheritdoc />
public BoolHandler(PostgresType postgresType) : base(postgresType) {}
/// <inheritdoc />
public override bool Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> buf.ReadByte() != 0;
/// <inheritdoc />
public override int ValidateAndGetLength(bool value, NpgsqlParameter? parameter)
=> 1;
/// <inheritdoc />
public override void Write(bool value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
=> buf.WriteByte(value ? (byte)1 : (byte)0);
}
}