private static Column[] ParseColumns(OtpInputStream s)
        {
            int colNameCount = s.ReadListHead();

            string[] columnNames = new string[colNameCount];
            for (int i = 0; i < colNameCount; i++)
            {
                columnNames[i] = s.ReadBinaryAsString();
            }

            if (colNameCount > 0)
            {
                s.ReadNil();
            }

            int colTypeCount = s.ReadListHead();

            ColumnType[] columnTypes = new ColumnType[colTypeCount];
            for (int i = 0; i < colTypeCount; i++)
            {
                string a = s.ReadAtom();
                columnTypes[i] = (ColumnType)Enum.Parse(typeof(ColumnType), a, true);
            }

            if (colTypeCount > 0)
            {
                s.ReadNil();
            }

            return(columnNames.Zip(columnTypes, (cname, ctype) => new Column(cname, ctype)).ToArray());
        }
示例#2
0
        public void Read_Binary(string want)
        {
            byte[] buf = null;
            using (var os = new OtpOutputStream())
            {
                os.WriteStringAsBinary(want);
                buf = os.ToArray();
            }

            using (var s = new OtpInputStream(buf))
            {
                string got = s.ReadBinaryAsString();
                Assert.AreEqual(want, got);
            }
        }
        private static Cell ParseCell(OtpInputStream s, int i, Column[] cols)
        {
            string msg;
            Column col = cols[i];
            byte   tag = s.Peek();

            switch (tag)
            {
            case OtpExternal.NilTag:
                tag = s.Read1();     // NB: actually consume the byte
                return(new Cell());

            case OtpExternal.BinTag:
                if (col.Type == ColumnType.Varchar || col.Type == ColumnType.Blob)
                {
                    return(new Cell(s.ReadBinaryAsString()));
                }
                else
                {
                    throw OnBadTag(tag, col, ColumnType.Varchar, ColumnType.Blob);
                }

            case OtpExternal.SmallIntTag:
            case OtpExternal.IntTag:
            case OtpExternal.SmallBigTag:
            case OtpExternal.LargeBigTag:
                long val = s.ReadLong();
                switch (col.Type)
                {
                case ColumnType.SInt64:
                    return(new Cell(val, isUnixTimestamp: false));

                case ColumnType.Timestamp:
                    return(new Cell(val, isUnixTimestamp: true));

                default:
                    throw OnBadTag(tag, col, ColumnType.SInt64, ColumnType.Timestamp);
                }

            case OtpExternal.FloatTag:
            case OtpExternal.NewFloatTag:
                if (col.Type != ColumnType.Double)
                {
                    throw OnBadTag(tag, col, ColumnType.Double);
                }

                return(new Cell(s.ReadDouble()));

            case OtpExternal.AtomTag:
                if (col.Type != ColumnType.Boolean)
                {
                    throw OnBadTag(tag, col, ColumnType.Boolean);
                }

                return(new Cell(s.ReadBoolean()));

            case OtpExternal.ListTag:
                int arity = s.ReadNil();
                if (arity == 0)
                {
                    // null cell
                    return(new Cell());
                }
                else
                {
                    msg = string.Format(
                        "Expected nil list, got one with arity {0}",
                        arity);
                    throw new InvalidOperationException(msg);
                }

            default:
                msg = string.Format(
                    "Unknown cell type encountered, tag {0}, '{1}:{2}'",
                    tag,
                    col.Name,
                    col.Type);
                throw new InvalidOperationException(msg);
            }
        }
        private static Cell ParseCell(OtpInputStream s, int i, Column[] cols)
        {
            string msg;
            Column col = cols[i];
            byte tag = s.Peek();
            switch (tag)
            {
                case OtpExternal.NilTag:
                    tag = s.Read1(); // NB: actually consume the byte
                    return new Cell();

                case OtpExternal.BinTag:
                    if (col.Type == ColumnType.Varchar || col.Type == ColumnType.Blob)
                    {
                        return new Cell(s.ReadBinaryAsString());
                    }
                    else
                    {
                        throw OnBadTag(tag, col, ColumnType.Varchar, ColumnType.Blob);
                    }

                case OtpExternal.SmallIntTag:
                case OtpExternal.IntTag:
                case OtpExternal.SmallBigTag:
                case OtpExternal.LargeBigTag:
                    long val = s.ReadLong();
                    switch (col.Type)
                    {
                        case ColumnType.SInt64:
                            return new Cell(val, isUnixTimestamp: false);
                        case ColumnType.Timestamp:
                            return new Cell(val, isUnixTimestamp: true);
                        default:
                            throw OnBadTag(tag, col, ColumnType.SInt64, ColumnType.Timestamp);
                    }

                case OtpExternal.FloatTag:
                case OtpExternal.NewFloatTag:
                    if (col.Type != ColumnType.Double)
                    {
                        throw OnBadTag(tag, col, ColumnType.Double);
                    }

                    return new Cell(s.ReadDouble());

                case OtpExternal.AtomTag:
                    if (col.Type != ColumnType.Boolean)
                    {
                        throw OnBadTag(tag, col, ColumnType.Boolean);
                    }

                    return new Cell(s.ReadBoolean());

                case OtpExternal.ListTag:
                    int arity = s.ReadNil();
                    if (arity == 0)
                    {
                        // null cell
                        return new Cell();
                    }
                    else
                    {
                        msg = string.Format(
                            "Expected nil list, got one with arity {0}",
                            arity);
                        throw new InvalidOperationException(msg);
                    }

                default:
                    msg = string.Format(
                        "Unknown cell type encountered, tag {0}, '{1}:{2}'",
                        tag,
                        col.Name,
                        col.Type);
                    throw new InvalidOperationException(msg);
            }
        }
        private static Column[] ParseColumns(OtpInputStream s)
        {
            int colNameCount = s.ReadListHead();
            string[] columnNames = new string[colNameCount];
            for (int i = 0; i < colNameCount; i++)
            {
                columnNames[i] = s.ReadBinaryAsString(); 
            }

            if (colNameCount > 0)
            {
                s.ReadNil();
            }

            int colTypeCount = s.ReadListHead();
            ColumnType[] columnTypes = new ColumnType[colTypeCount];
            for (int i = 0; i < colTypeCount; i++)
            {
                string a = s.ReadAtom();
                columnTypes[i] = (ColumnType)Enum.Parse(typeof(ColumnType), a, true);
            }

            if (colTypeCount > 0)
            {
                s.ReadNil();
            }

            return columnNames.Zip(columnTypes, (cname, ctype) => new Column(cname, ctype)).ToArray();
        }