Пример #1
0
        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
        private static Row[] ParseRows(OtpInputStream s, Column[] cols)
        {
            int rowCount = s.ReadListHead();

            Row[] rows = new Row[rowCount];
            for (int i = 0; i < rowCount; i++)
            {
                rows[i] = ParseRow(s, cols);
            }

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

            return(rows);
        }
Пример #3
0
        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);
            }
        }
Пример #4
0
        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);
            }
        }
Пример #5
0
        private static Row[] ParseRows(OtpInputStream s, Column[] cols)
        {
            int rowCount = s.ReadListHead();
            Row[] rows = new Row[rowCount];
            for (int i = 0; i < rowCount; i++)
            {
                rows[i] = ParseRow(s, cols);
            }

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

            return rows;
        }
Пример #6
0
        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();
        }