Ejemplo n.º 1
0
        void Write <T>([CanBeNull] T value, NpgsqlParameter param)
        {
            CheckReady();
            if (_column == -1)
            {
                throw new InvalidOperationException("A row hasn't been started");
            }

            if (value == null || value is DBNull)
            {
                WriteNull();
                return;
            }

            if (typeof(T) == typeof(object))
            {
                param.Value = value;
            }
            else
            {
                if (!(param is NpgsqlParameter <T> typedParam))
                {
                    _params[_column]        = typedParam = new NpgsqlParameter <T>();
                    typedParam.NpgsqlDbType = param.NpgsqlDbType;
                }
                typedParam.TypedValue = value;
            }
            param.ResolveHandler(_connector.TypeMapper);
            param.ValidateAndGetLength();
            param.LengthCache?.Rewind();
            param.WriteWithLength(_buf, false);
            param.LengthCache?.Clear();
            _column++;
        }
Ejemplo n.º 2
0
        async Task Write <T>([AllowNull] T value, NpgsqlParameter param, bool async, CancellationToken cancellationToken = default)
        {
            CheckReady();
            if (_column == -1)
            {
                throw new InvalidOperationException("A row hasn't been started");
            }

            if (value == null || value is DBNull)
            {
                await WriteNull(async, cancellationToken);

                return;
            }

            try
            {
                if (typeof(T) == typeof(object))
                {
                    param.Value = value;
                }
                else
                {
                    if (param is not NpgsqlParameter <T> typedParam)
                    {
                        _params[_column]        = typedParam = new NpgsqlParameter <T>();
                        typedParam.NpgsqlDbType = param.NpgsqlDbType;
                        param = typedParam;
                    }
                    typedParam.TypedValue = value;
                }
                param.ResolveHandler(_connector.TypeMapper);
                param.ValidateAndGetLength();
                param.LengthCache?.Rewind();
                await param.WriteWithLength(_buf, async, cancellationToken);

                param.LengthCache?.Clear();
                _column++;
            }
            catch
            {
                // An exception here will have already broken the connection etc.
                Cleanup();
                throw;
            }
        }