Exemple #1
0
 public static object ConvertFromList(IColumnInfo type_info, byte[] value, Type cSharpType)
 {
     if (type_info is ListColumnInfo)
     {
         var    list_typecode = (type_info as ListColumnInfo).ValueTypeCode;
         var    list_typeinfo = (type_info as ListColumnInfo).ValueTypeInfo;
         var    value_type    = TypeInterpreter.GetDefaultTypeFromCqlType(list_typecode, list_typeinfo);
         int    count         = BytesToInt16(value, 0);
         int    idx           = 2;
         var    openType      = typeof(List <>);
         var    listType      = openType.MakeGenericType(value_type);
         object ret           = Activator.CreateInstance(listType);
         var    addM          = listType.GetMethod("Add");
         for (int i = 0; i < count; i++)
         {
             var val_buf_len = BytesToInt16(value, idx);
             idx += 2;
             byte[] val_buf = new byte[val_buf_len];
             Buffer.BlockCopy(value, idx, val_buf, 0, val_buf_len);
             idx += val_buf_len;
             addM.Invoke(ret, new object[] { TypeInterpreter.CqlConvert(val_buf, list_typecode, list_typeinfo) });
         }
         return(ret);
     }
     throw new DriverInternalError("Invalid ColumnInfo");
 }
Exemple #2
0
        internal RowSetMetadata(BEBinaryReader reader)
        {
            var coldat          = new List <ColumnDesc>();
            var flags           = (FlagBits)reader.ReadInt32();
            var numberOfcolumns = reader.ReadInt32();

            this._rawColumns = new ColumnDesc[numberOfcolumns];
            string gKsname    = null;
            string gTablename = null;

            if ((flags & FlagBits.GlobalTablesSpec) == FlagBits.GlobalTablesSpec)
            {
                gKsname    = reader.ReadString();
                gTablename = reader.ReadString();
            }
            for (int i = 0; i < numberOfcolumns; i++)
            {
                var col = new ColumnDesc();
                if ((flags & FlagBits.GlobalTablesSpec) != FlagBits.GlobalTablesSpec)
                {
                    col.Keyspace = reader.ReadString();
                    col.Table    = reader.ReadString();
                }
                else
                {
                    col.Keyspace = gKsname;
                    col.Table    = gTablename;
                }
                col.Name     = reader.ReadString();
                col.TypeCode = (ColumnTypeCode)reader.ReadUInt16();
                col.TypeInfo = GetColumnInfo(reader, col.TypeCode);
                coldat.Add(col);
            }
            _rawColumns = coldat.ToArray();

            _columns     = new CqlColumn[_rawColumns.Length];
            _columnIdxes = new Dictionary <string, int>();
            for (int i = 0; i < _rawColumns.Length; i++)
            {
                _columns[i] = new CqlColumn()
                {
                    Name     = _rawColumns[i].Name,
                    Keyspace = _rawColumns[i].Keyspace,
                    Table    = _rawColumns[i].Table,
                    Type     = TypeInterpreter.GetDefaultTypeFromCqlType(
                        _rawColumns[i].TypeCode,
                        _rawColumns[i].TypeInfo),
                    TypeCode = _rawColumns[i].TypeCode,
                    TypeInfo = _rawColumns[i].TypeInfo
                };
                //TODO: what with full long column names?
                if (!_columnIdxes.ContainsKey(_rawColumns[i].Name))
                {
                    _columnIdxes.Add(_rawColumns[i].Name, i);
                }
            }
        }
Exemple #3
0
 public static Type GetDefaultTypeFromList(IColumnInfo type_info)
 {
     if (type_info is ListColumnInfo)
     {
         var list_typecode = (type_info as ListColumnInfo).ValueTypeCode;
         var list_typeinfo = (type_info as ListColumnInfo).ValueTypeInfo;
         var value_type    = TypeInterpreter.GetDefaultTypeFromCqlType(list_typecode, list_typeinfo);
         var openType      = typeof(IEnumerable <>);
         var listType      = openType.MakeGenericType(value_type);
         return(listType);
     }
     throw new DriverInternalError("Invalid ColumnInfo");
 }
        public static Type GetDefaultTypeFromMap(IColumnInfo type_info)
        {
            if (type_info is MapColumnInfo)
            {
                var key_typecode   = (type_info as MapColumnInfo).KeyTypeCode;
                var key_typeinfo   = (type_info as MapColumnInfo).KeyTypeInfo;
                var value_typecode = (type_info as MapColumnInfo).ValueTypeCode;
                var value_typeinfo = (type_info as MapColumnInfo).ValueTypeInfo;
                var key_type       = TypeInterpreter.GetDefaultTypeFromCqlType(key_typecode, key_typeinfo);
                var value_type     = TypeInterpreter.GetDefaultTypeFromCqlType(value_typecode, value_typeinfo);

                var openType = typeof(IDictionary <,>);
                var dicType  = openType.MakeGenericType(key_type, value_type);
                return(dicType);
            }
            throw new DriverInternalError("Invalid ColumnInfo");
        }
        public static object ConvertFromMap(IColumnInfo type_info, byte[] value, Type cSharpType)
        {
            if (type_info is MapColumnInfo)
            {
                var    key_typecode   = (type_info as MapColumnInfo).KeyTypeCode;
                var    key_typeinfo   = (type_info as MapColumnInfo).KeyTypeInfo;
                var    value_typecode = (type_info as MapColumnInfo).ValueTypeCode;
                var    value_typeinfo = (type_info as MapColumnInfo).ValueTypeInfo;
                var    key_type       = TypeInterpreter.GetDefaultTypeFromCqlType(key_typecode, key_typeinfo);
                var    value_type     = TypeInterpreter.GetDefaultTypeFromCqlType(value_typecode, value_typeinfo);
                int    count          = BytesToUInt16(value, 0);
                int    idx            = 2;
                var    openType       = typeof(SortedDictionary <,>);
                var    dicType        = openType.MakeGenericType(key_type, value_type);
                object ret            = Activator.CreateInstance(dicType);
                var    addM           = dicType.GetMethod("Add");
                for (int i = 0; i < count; i++)
                {
                    var key_buf_len = BytesToUInt16(value, idx);
                    idx += 2;
                    byte[] key_buf = new byte[key_buf_len];
                    Buffer.BlockCopy(value, idx, key_buf, 0, key_buf_len);
                    idx += key_buf_len;

                    var value_buf_len = BytesToUInt16(value, idx);
                    idx += 2;
                    byte[] value_buf = new byte[value_buf_len];
                    Buffer.BlockCopy(value, idx, value_buf, 0, value_buf_len);
                    idx += value_buf_len;

                    addM.Invoke(ret, new object[] {
                        TypeInterpreter.CqlConvert(key_buf, key_typecode, key_typeinfo),
                        TypeInterpreter.CqlConvert(value_buf, value_typecode, value_typeinfo)
                    });
                }
                return(ret);
            }
            throw new DriverInternalError("Invalid ColumnInfo");
        }
        public static byte[] InvConvertFromMap(IColumnInfo type_info, object value)
        {
            var dicType = GetDefaultTypeFromMap(type_info);

            CheckArgument(dicType, value);
            var key_typecode   = (type_info as MapColumnInfo).KeyTypeCode;
            var key_typeinfo   = (type_info as MapColumnInfo).KeyTypeInfo;
            var value_typecode = (type_info as MapColumnInfo).ValueTypeCode;
            var value_typeinfo = (type_info as MapColumnInfo).ValueTypeInfo;
            var key_type       = TypeInterpreter.GetDefaultTypeFromCqlType(key_typecode, key_typeinfo);
            var value_type     = TypeInterpreter.GetDefaultTypeFromCqlType(value_typecode, value_typeinfo);

            List <byte[]> kbufs = new List <byte[]>();
            List <byte[]> vbufs = new List <byte[]>();
            int           cnt   = 0;
            int           bsize = 2;

            var key_prop   = dicType.GetProperty("Keys");
            var value_prop = dicType.GetProperty("Values");

            foreach (var obj in key_prop.GetValue(value, new object[] { }) as IEnumerable)
            {
                var buf = TypeInterpreter.InvCqlConvert(obj, key_typecode, key_typeinfo);
                kbufs.Add(buf);
                bsize += 2; //size of key
                bsize += buf.Length;
                cnt++;
            }

            foreach (var obj in value_prop.GetValue(value, new object[] { }) as IEnumerable)
            {
                var buf = TypeInterpreter.InvCqlConvert(obj, value_typecode, value_typeinfo);
                vbufs.Add(buf);
                bsize += 2; //size of value
                bsize += buf.Length;
            }

            var ret = new byte[bsize];

            var cntbuf = Int16ToBytes((short)cnt); // short or ushort ?

            int idx = 0;

            Buffer.BlockCopy(cntbuf, 0, ret, 0, 2);
            idx += 2;
            for (int i = 0; i < cnt; i++)
            {
                {
                    var buf             = kbufs[i];
                    var keyval_buf_size = Int16ToBytes((short)buf.Length);
                    Buffer.BlockCopy(keyval_buf_size, 0, ret, idx, 2);
                    idx += 2;
                    Buffer.BlockCopy(buf, 0, ret, idx, buf.Length);
                    idx += buf.Length;
                }
                {
                    var buf             = vbufs[i];
                    var keyval_buf_size = Int16ToBytes((short)buf.Length);
                    Buffer.BlockCopy(keyval_buf_size, 0, ret, idx, 2);
                    idx += 2;
                    Buffer.BlockCopy(buf, 0, ret, idx, buf.Length);
                    idx += buf.Length;
                }
            }

            return(ret);
        }