#region License
// The PostgreSQL License
//
// Copyright (C) 2017 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Npgsql.BackendMessages;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandlers;
namespace Npgsql.TypeHandling
{
///
/// Base class for all type handlers, which read and write CLR types into their PostgreSQL
/// binary representation.
/// Type handler writers shouldn't inherit from this class, inherit
/// or instead.
///
public abstract class NpgsqlTypeHandler
{
///
/// The PostgreSQL type handled by this type handler. Injected by .
///
internal PostgresType PostgresType { get; set; }
#region Read
///
/// Reads a value of type with the given length from the provided buffer,
/// using either sync or async I/O.
///
/// The buffer from which to read.
/// The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed.
/// If I/O is required to read the full length of the value, whether it should be performed synchronously or asynchronously.
/// Additional PostgreSQL information about the type, such as the length in varchar(30).
/// The fully-read value.
protected internal abstract ValueTask Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null);
///
/// Reads a value of type with the given length from the provided buffer,
/// with the assumption that it is entirely present in the provided memory buffer and no I/O will be
/// required. This can save the overhead of async functions and improves performance.
///
/// The buffer from which to read.
/// The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed.
/// Additional PostgreSQL information about the type, such as the length in varchar(30).
/// The fully-read value.
internal abstract T Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null);
///
/// Reads a column as the type handler's default read type, assuming that it is already entirely
/// in memory (i.e. no I/O is necessary). Called by , which
/// buffers entire rows in memory.
///
internal abstract object ReadAsObject(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null);
///
/// Reads a column as the type handler's default read type. If it is not already entirely in
/// memory, sync or async I/O will be performed as specified by .
///
internal abstract ValueTask