Example #1
0
        /// <summary>
        /// Convert from string representation.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static object ConvertString(string source, ColumnType type)
        {
            if (source == null)
            {
                return(null);
            }

            switch (type)
            {
            case ColumnType.Null:
                return(null);

            case ColumnType.Integer:
                return(int.Parse(source, CultureInfo.InvariantCulture));

            case ColumnType.Float:
            case ColumnType.Real:
                return(Single.Parse(source, CultureInfo.InvariantCulture));

            case ColumnType.DbDouble:
                return(Double.Parse(source, CultureInfo.InvariantCulture));

            case ColumnType.VarCharIgnoreCase:
            case ColumnType.VarChar:
            case ColumnType.Char:
            case ColumnType.LongVarChar:
                return(source);

            case ColumnType.Date:
            case ColumnType.Time:
            case ColumnType.Timestamp:
                return(DateTime.Parse(source, CultureInfo.InvariantCulture));

            case ColumnType.Numeric:
            case ColumnType.DbDecimal:
                return(Decimal.Parse(source, CultureInfo.InvariantCulture));

            case ColumnType.Bit:
                return(Boolean.Parse(source));

            case ColumnType.TinyInt:
                return(Byte.Parse(source, CultureInfo.InvariantCulture));

            case ColumnType.SmallInt:
                return(Int16.Parse(source, CultureInfo.InvariantCulture));

            case ColumnType.BigInt:
                return(Int64.Parse(source));

            case ColumnType.Binary:
            case ColumnType.VarBinary:
            case ColumnType.LongVarBinary:
                return(new ByteArray(source).Value);

            case ColumnType.Other:
                return(ByteArray.Deserialize(new ByteArray(source).Value));

            case ColumnType.UniqueIdentifier:
                return(new Guid(source));

            default:
                throw Trace.Error(Trace.FUNCTION_NOT_SUPPORTED, (int)type);
            }
        }
Example #2
0
        /// <summary>
        /// Write column data to file.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="length"></param>
        /// <param name="type"></param>
        /// <param name="data"></param>
        internal static void WriteData(BinaryWriter writer, int length, ColumnType[] type, object[] data)
        {
            for (int i = 0; i < length; i++)
            {
                object o = data[i];

                if (o == null)
                {
                    writer.Write((int)ColumnType.Null);
                }
                else
                {
                    ColumnType t = type[i];

                    writer.Write((int)t);

                    switch (t)
                    {
                    case ColumnType.Null:
                        o = null;
                        break;

                    case ColumnType.Float:
                    case ColumnType.Real:
                        writer.Write((float)o);
                        break;

                    case ColumnType.DbDouble:
                        writer.Write((double)o);
                        break;

                    case ColumnType.Bit:
                        writer.Write((bool)o);
                        break;

                    case ColumnType.TinyInt:
                        writer.Write((byte)o);
                        break;

                    case ColumnType.SmallInt:
                        writer.Write((short)o);
                        break;

                    case ColumnType.DbDecimal:
                    case ColumnType.Numeric:
                        writer.Write((Decimal)o);
                        break;

                    case ColumnType.Integer:
                        writer.Write((int)o);
                        break;

                    case ColumnType.BigInt:
                        writer.Write((long)o);
                        break;

                    case ColumnType.Date:
                    case ColumnType.Time:
                    case ColumnType.Timestamp:
                        writer.Write(((DateTime)o).Ticks);
                        break;

                    case ColumnType.Binary:
                    case ColumnType.VarBinary:
                    case ColumnType.LongVarBinary:
                        byte[] b = (byte[])o;
                        writer.Write(b.Length);
                        writer.Write(b);
                        break;

                    case ColumnType.Other:
                        byte[] other = ByteArray.Serialize(o);
                        writer.Write(other.Length);
                        writer.Write(other);
                        break;

                    case ColumnType.UniqueIdentifier:
                        writer.Write(((Guid)o).ToString());
                        break;

                    default:
                        writer.Write(o.ToString());
                        break;
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Gets the size of the stored data.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="l"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        unsafe private static int GetSize(object[] data, int l, ColumnType[] type)
        {
            int s = 0;

            for (int i = 0; i < l; i++)
            {
                object o = data[i];

                s += 4;                    // type

                if (o != null)
                {
                    switch (type[i])
                    {
                    case ColumnType.Char:
                    case ColumnType.LongVarChar:
                    case ColumnType.VarChar:
                    case ColumnType.VarCharIgnoreCase:
                        s += System.Text.Encoding.Unicode.GetByteCount(o.ToString());
                        s += 1;
                        break;

                    case ColumnType.Float:
                    case ColumnType.Real:
                        s += sizeof(float);
                        break;

                    case ColumnType.DbDouble:
                        s += sizeof(double);
                        break;

                    case ColumnType.Bit:
                        s += sizeof(bool);
                        break;

                    case ColumnType.TinyInt:
                        s += sizeof(byte);
                        break;

                    case ColumnType.SmallInt:
                        s += sizeof(short);
                        break;

                    case ColumnType.Integer:
                        s += sizeof(int);
                        break;

                    case ColumnType.DbDecimal:
                    case ColumnType.Numeric:
                        s += sizeof(Decimal);
                        break;

                    case ColumnType.BigInt:
                        s += sizeof(long);
                        break;

                    case ColumnType.Date:
                    case ColumnType.Time:
                    case ColumnType.Timestamp:
                        s += sizeof(long);
                        break;

                    case ColumnType.Binary:
                    case ColumnType.VarBinary:
                    case ColumnType.LongVarBinary:
                        s += 4;
                        s += ((byte[])o).Length;
                        break;

                    case ColumnType.Other:
                        s += 4;
                        s += ByteArray.Serialize(o).Length;
                        break;

                    case ColumnType.UniqueIdentifier:
                        s += sizeof(Guid);
                        break;

                    default:
                        s += System.Text.Encoding.Unicode.GetByteCount(o.ToString());
                        s += 1;
                        break;
                    }
                }
            }

            return(s);
        }
Example #4
0
        /// <summary>
        /// Read column data from file.
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        internal static object[] ReadData(BinaryReader reader, int length)
        {
            object[] data = new object[length];

            for (int i = 0; i < length; i++)
            {
                ColumnType type = (ColumnType)Enum.Parse(typeof(ColumnType), reader.ReadInt32().ToString());

                object o = null;
                switch (type)
                {
                case ColumnType.Null:
                    o = null;
                    break;

                case ColumnType.Float:
                case ColumnType.Real:
                    o = reader.ReadSingle();
                    break;

                case ColumnType.DbDouble:
                    o = reader.ReadDouble();
                    break;

                case ColumnType.VarCharIgnoreCase:
                case ColumnType.VarChar:
                case ColumnType.Char:
                case ColumnType.LongVarChar:
                    o = reader.ReadString();
                    break;

                case ColumnType.Date:
                case ColumnType.Time:
                case ColumnType.Timestamp:
                    o = new DateTime(reader.ReadInt64());
                    break;

                case ColumnType.Numeric:
                case ColumnType.DbDecimal:
                    o = reader.ReadDecimal();
                    break;

                case ColumnType.Bit:
                    o = reader.ReadBoolean();
                    break;

                case ColumnType.TinyInt:
                    o = reader.ReadByte();
                    break;

                case ColumnType.SmallInt:
                    o = reader.ReadInt16();
                    break;

                case ColumnType.Integer:
                    o = reader.ReadInt32();
                    break;

                case ColumnType.BigInt:
                    o = reader.ReadInt64();
                    break;

                case ColumnType.Binary:
                case ColumnType.VarBinary:
                case ColumnType.LongVarBinary:
                    int len = reader.ReadInt32();
                    o = reader.ReadBytes(len);
                    break;

                case ColumnType.Other:
                    int other = reader.ReadInt32();
                    o = ByteArray.Deserialize(reader.ReadBytes(other));
                    break;

                case ColumnType.UniqueIdentifier:
                    o = new Guid(reader.ReadString());
                    break;

                default:
                    throw Trace.Error(Trace.WRONG_DATA_TYPE, (int)type);
                }

                data[i] = o;
            }

            return(data);
        }