示例#1
0
    protected internal override async ValueTask <byte[]> WriteParametersAsync(CancellationToken cancellationToken = default)
    {
        if (_parameters == null)
        {
            return(null);
        }

        using (var ms = new MemoryStream())
        {
            try
            {
                var xdr = new XdrReaderWriter(new DataProviderStreamWrapper(ms), _database.Charset);

                var bits = new BitArray(_parameters.Count);
                for (var i = 0; i < _parameters.Count; i++)
                {
                    var field = _parameters[i];
                    bits.Set(i, field.DbValue.IsDBNull());
                }
                var buffer = new byte[(int)Math.Ceiling(_parameters.Count / 8d)];
                for (var i = 0; i < buffer.Length * 8; i++)
                {
                    var index = i / 8;
                    // LSB
                    buffer[index] = (byte)((buffer[index] >> 1) | (bits.Length > i && bits[i] ? 1 << 7 : 0));
                }
                await xdr.WriteOpaqueAsync(buffer, cancellationToken).ConfigureAwait(false);

                for (var i = 0; i < _parameters.Count; i++)
                {
                    var field = _parameters[i];
                    if (field.DbValue.IsDBNull())
                    {
                        continue;
                    }
                    await WriteRawParameterAsync(xdr, field, cancellationToken).ConfigureAwait(false);
                }

                await xdr.FlushAsync(cancellationToken).ConfigureAwait(false);

                return(ms.ToArray());
            }
            catch (IOException ex)
            {
                throw IscException.ForIOException(ex);
            }
        }
    }
示例#2
0
    private async ValueTask <byte[]> EncodeSliceArrayAsync(Array sourceArray, CancellationToken cancellationToken = default)
    {
        var dbType  = DbDataType.Array;
        var charset = _database.Charset;
        var subType = (Descriptor.Scale < 0) ? 2 : 0;
        var type    = 0;

        using (var ms = new MemoryStream())
        {
            var xdr = new XdrReaderWriter(new DataProviderStreamWrapper(ms), _database.Charset);

            type   = TypeHelper.GetSqlTypeFromBlrType(Descriptor.DataType);
            dbType = TypeHelper.GetDbDataTypeFromBlrType(Descriptor.DataType, subType, Descriptor.Scale);

            foreach (var source in sourceArray)
            {
                switch (dbType)
                {
                case DbDataType.Char:
                    var buffer = charset.GetBytes(source.ToString());
                    await xdr.WriteOpaqueAsync(buffer, Descriptor.Length, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.VarChar:
                    await xdr.WriteAsync((string)source, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.SmallInt:
                    await xdr.WriteAsync((short)source, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.Integer:
                    await xdr.WriteAsync((int)source, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.BigInt:
                    await xdr.WriteAsync((long)source, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.Decimal:
                case DbDataType.Numeric:
                    await xdr.WriteAsync((decimal)source, type, Descriptor.Scale, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.Float:
                    await xdr.WriteAsync((float)source, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.Double:
                    await xdr.WriteAsync((double)source, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.Date:
                    await xdr.WriteDateAsync(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat), cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.Time:
                    await xdr.WriteTimeAsync((TimeSpan)source, cancellationToken).ConfigureAwait(false);

                    break;

                case DbDataType.TimeStamp:
                    await xdr.WriteAsync(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat), cancellationToken).ConfigureAwait(false);

                    break;

#warning New datatypes

                default:
                    throw TypeHelper.InvalidDataType((int)dbType);
                }
            }

            await xdr.FlushAsync(cancellationToken).ConfigureAwait(false);

            return(ms.ToArray());
        }
    }
示例#3
0
    private async ValueTask <byte[]> ReceiveSliceResponseAsync(ArrayDesc desc, CancellationToken cancellationToken = default)
    {
        try
        {
            var operation = await _database.ReadOperationAsync(cancellationToken).ConfigureAwait(false);

            if (operation == IscCodes.op_slice)
            {
                var isVariying = false;
                var elements   = 0;
                var length     = await _database.Xdr.ReadInt32Async(cancellationToken).ConfigureAwait(false);

                length = await _database.Xdr.ReadInt32Async(cancellationToken).ConfigureAwait(false);

                switch (desc.DataType)
                {
                case IscCodes.blr_text:
                case IscCodes.blr_text2:
                case IscCodes.blr_cstring:
                case IscCodes.blr_cstring2:
                    elements = length / desc.Length;
                    length  += elements * ((4 - desc.Length) & 3);
                    break;

                case IscCodes.blr_varying:
                case IscCodes.blr_varying2:
                    elements   = length / desc.Length;
                    isVariying = true;
                    break;

                case IscCodes.blr_short:
                    length = length * desc.Length;
                    break;
                }

                if (isVariying)
                {
                    using (var ms = new MemoryStream())
                    {
                        var xdr = new XdrReaderWriter(new DataProviderStreamWrapper(ms));
                        for (var i = 0; i < elements; i++)
                        {
                            var buffer = await _database.Xdr.ReadOpaqueAsync(await _database.Xdr.ReadInt32Async(cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false);

                            await xdr.WriteBufferAsync(buffer, buffer.Length, cancellationToken).ConfigureAwait(false);
                        }
                        await xdr.FlushAsync(cancellationToken).ConfigureAwait(false);

                        return(ms.ToArray());
                    }
                }
                else
                {
                    return(await _database.Xdr.ReadOpaqueAsync(length, cancellationToken).ConfigureAwait(false));
                }
            }
            else
            {
                await _database.ReadResponseAsync(operation, cancellationToken).ConfigureAwait(false);

                return(null);
            }
        }
        catch (IOException ex)
        {
            throw IscException.ForIOException(ex);
        }
    }