protected virtual ArraySegment <byte> WriteParameters() { if (_parameters == null) { return(new ArraySegment <byte>()); } using (var stream = new MemoryStream()) { var xdr = new XdrBinaryWriter(stream, _database.Charset); for (var i = 0; i < _parameters.Count; i++) { var field = _parameters[i]; try { WriteRawParameter(xdr, field); xdr.Write(field.NullFlag); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex); } } return(stream.ToArraySegment()); } }
protected override ArraySegment <byte> WriteParameters() { if (_parameters == null) { return(new ArraySegment <byte>()); } using (var stream = new MemoryStream()) { var xdr = new XdrBinaryWriter(stream, _database.Charset); try { var bits = new BitArray(_parameters.Count); for (int 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 (int 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)); } xdr.WriteOpaque(buffer); for (var i = 0; i < _parameters.Count; i++) { var field = _parameters[i]; if (field.DbValue.IsDBNull()) { continue; } WriteRawParameter(xdr, field); } return(stream.ToArraySegment()); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex); } } }
protected void WriteRawParameter(XdrBinaryWriter xdr, DbField field) { if (field.DbDataType != DbDataType.Null) { field.FixNull(); switch (field.DbDataType) { case DbDataType.Char: if (field.Charset.IsOctetsCharset) { xdr.WriteOpaque(field.DbValue.GetBinary(), field.Length); } else { var svalue = field.DbValue.GetString(); if ((field.Length % field.Charset.BytesPerCharacter) == 0 && svalue.Length > field.CharCount) { throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation }); } xdr.WriteOpaque(field.Charset.GetBytes(svalue), field.Length); } break; case DbDataType.VarChar: if (field.Charset.IsOctetsCharset) { xdr.WriteBuffer(field.DbValue.GetBinary()); } else { var svalue = field.DbValue.GetString(); if ((field.Length % field.Charset.BytesPerCharacter) == 0 && svalue.Length > field.CharCount) { throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation }); } xdr.WriteBuffer(field.Charset.GetBytes(svalue)); } break; case DbDataType.SmallInt: xdr.Write(field.DbValue.GetInt16()); break; case DbDataType.Integer: xdr.Write(field.DbValue.GetInt32()); break; case DbDataType.BigInt: case DbDataType.Array: case DbDataType.Binary: case DbDataType.Text: xdr.Write(field.DbValue.GetInt64()); break; case DbDataType.Decimal: case DbDataType.Numeric: xdr.Write(field.DbValue.GetDecimal(), field.DataType, field.NumericScale); break; case DbDataType.Float: xdr.Write(field.DbValue.GetFloat()); break; case DbDataType.Guid: xdr.WriteOpaque(field.DbValue.GetGuid().ToByteArray()); break; case DbDataType.Double: xdr.Write(field.DbValue.GetDouble()); break; case DbDataType.Date: xdr.Write(field.DbValue.GetDate()); break; case DbDataType.Time: xdr.Write(field.DbValue.GetTime()); break; case DbDataType.TimeStamp: xdr.Write(field.DbValue.GetDate()); xdr.Write(field.DbValue.GetTime()); break; case DbDataType.Boolean: xdr.Write(Convert.ToBoolean(field.Value)); break; default: throw IscException.ForStrParam($"Unknown SQL data type: {field.DataType}."); } } }
private byte[] EncodeSliceArray(Array sourceArray) { DbDataType dbType = DbDataType.Array; Charset charset = _database.Charset; int subType = (Descriptor.Scale < 0) ? 2 : 0; int type = 0; using (var stream = new MemoryStream()) { var xdr = new XdrBinaryWriter(stream, _database.Charset); type = TypeHelper.GetSqlTypeFromBlrType(Descriptor.DataType); dbType = TypeHelper.GetDbDataTypeFromBlrType(Descriptor.DataType, subType, Descriptor.Scale); foreach (object source in sourceArray) { switch (dbType) { case DbDataType.Char: byte[] buffer = charset.GetBytes(source.ToString()); xdr.WriteOpaque(buffer, Descriptor.Length); break; case DbDataType.VarChar: xdr.Write((string)source); break; case DbDataType.SmallInt: xdr.Write((short)source); break; case DbDataType.Integer: xdr.Write((int)source); break; case DbDataType.BigInt: xdr.Write((long)source); break; case DbDataType.Decimal: case DbDataType.Numeric: xdr.Write((decimal)source, type, Descriptor.Scale); break; case DbDataType.Float: xdr.Write((float)source); break; case DbDataType.Double: xdr.Write((double)source); break; case DbDataType.Date: xdr.WriteDate(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)); break; case DbDataType.Time: xdr.WriteTime((TimeSpan)source); break; case DbDataType.TimeStamp: xdr.Write(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)); break; default: throw TypeHelper.InvalidDataType((int)dbType); } } return(stream.ToArray()); } }
private byte[] ReceiveSliceResponse(ArrayDesc desc) { try { int operation = _database.ReadOperation(); if (operation == IscCodes.op_slice) { bool isVariying = false; int elements = 0; int length = _database.XdrStream.ReadInt32(); length = _database.XdrStream.ReadInt32(); 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 stream = new MemoryStream()) { var writer = new XdrBinaryWriter(stream); for (int i = 0; i < elements; i++) { byte[] buffer = _database.XdrStream.ReadOpaque(_database.XdrStream.ReadInt32()); writer.WriteBuffer(buffer, buffer.Length); } return(stream.ToArray()); } } else { return(_database.XdrStream.ReadOpaque(length)); } } else { _database.SetOperation(operation); _database.ReadResponse(); return(null); } } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex); } }