/// <summary> /// Initializes a new instance of the <see cref="Column" /> class. /// </summary> /// <param name="index"> The index. </param> /// <param name="keyspace"> The keyspace. </param> /// <param name="table"> The table. </param> /// <param name="name"> The name. </param> /// <param name="type"> the type of the column </param> public Column(int index, string keyspace, string table, string name, CqlType type) { Index = index; Keyspace = keyspace; Table = table; Name = name; Type = type; }
public CqlType CreateType(CqlType innerType) { return _types.GetOrAdd(innerType, type => (CqlType) Activator.CreateInstance( typeof(ListType<>).MakeGenericType(type.Type), type) ); }
/// <summary> /// Sets the partition key to the provided value /// </summary> /// <param name="type"> The type in which the value is represented in Cassandra. </param> /// <param name="value"> The value of the partition key column. </param> public void Set(CqlType type, Object value) { if (value == null) { throw new ArgumentNullException("value"); } _key = ValueSerialization.Serialize(type, value); }
private CqlType CreateType(CqlType keyType, CqlType valueType) { var tuple = new Tuple<CqlType, CqlType>(keyType, valueType); return _types.GetOrAdd(tuple, types => (CqlType)Activator.CreateInstance( typeof(MapType<,>).MakeGenericType(types.Item1.Type, types.Item2.Type), types.Item1, types.Item2)); }
public static Type ToType(this CqlType colType) { Type type; if (ColType2Type.TryGetValue(colType, out type)) { return(type); } throw new ArgumentException("Unsupported type"); }
/// <summary> /// Initializes a new instance of the <see cref="CqlColumn" /> class. /// </summary> /// <param name="index"> The index. </param> /// <param name="keyspace"> The keyspace. </param> /// <param name="table"> The table. </param> /// <param name="name"> The name. </param> /// <param name="cqlType"> Type of the column. </param> /// <param name="customData"> The custom data. </param> /// <param name="collectionKeyType"> Type of the collection key. </param> /// <param name="collectionValueType"> Type of the collection value. </param> internal CqlColumn(int index, string keyspace, string table, string name, CqlType cqlType, string customData, CqlType? collectionKeyType, CqlType? collectionValueType) { Index = index; Keyspace = keyspace; Table = table; Name = name; CqlType = cqlType; CustomData = customData; CollectionKeyType = collectionKeyType; CollectionValueType = collectionValueType; }
/// <summary> /// Initializes a new instance of the <see cref="CqlColumn" /> class. /// </summary> /// <param name="index"> The index. </param> /// <param name="keyspace"> The keyspace. </param> /// <param name="table"> The table. </param> /// <param name="name"> The name. </param> /// <param name="cqlType"> Type of the column. </param> /// <param name="customData"> The custom data. </param> /// <param name="collectionKeyType"> Type of the collection key. </param> /// <param name="collectionValueType"> Type of the collection value. </param> internal CqlColumn(int index, string keyspace, string table, string name, CqlType cqlType, string customData, CqlType?collectionKeyType, CqlType?collectionValueType) { Index = index; Keyspace = keyspace; Table = table; Name = name; CqlType = cqlType; CustomData = customData; CollectionKeyType = collectionKeyType; CollectionValueType = collectionValueType; }
/// <summary> /// Initializes a new instance of the <see cref="CqlParameter" /> class. /// </summary> /// <param name="name"> The name. </param> /// <param name="type"> The typeCode. </param> public CqlParameter(string name, CqlType type) : this() { SetParameterName(name); _column.Type = type; }
public static byte[] Serialize(CqlType colType, object data) { //null value check if (data == null) return null; byte[] rawData; switch (colType) { case CqlType.Ascii: rawData = Encoding.ASCII.GetBytes(Convert.ToString(data)); break; case CqlType.Text: case CqlType.Varchar: rawData = Encoding.UTF8.GetBytes(Convert.ToString(data)); break; case CqlType.Blob: rawData = (byte[])data; break; case CqlType.Double: rawData = BitConverter.GetBytes(Convert.ToDouble(data)); if (IsLittleEndian) Array.Reverse(rawData); break; case CqlType.Float: rawData = BitConverter.GetBytes(Convert.ToSingle(data)); if (IsLittleEndian) Array.Reverse(rawData); break; case CqlType.Timestamp: if (data is long) rawData = BitConverter.GetBytes((long)data); else rawData = BitConverter.GetBytes(Convert.ToDateTime(data).ToTimestamp()); if (IsLittleEndian) Array.Reverse(rawData); break; case CqlType.Bigint: case CqlType.Counter: rawData = BitConverter.GetBytes(Convert.ToInt64(data)); if (IsLittleEndian) Array.Reverse(rawData); break; case CqlType.Int: rawData = BitConverter.GetBytes(Convert.ToInt32(data)); if (IsLittleEndian) Array.Reverse(rawData); break; case CqlType.Varint: var dataString = data as string; if (dataString != null) rawData = BigInteger.Parse(dataString).ToByteArray(); else { var integer = (BigInteger)data; rawData = integer.ToByteArray(); } //to bigendian Array.Reverse(rawData); break; case CqlType.Boolean: rawData = BitConverter.GetBytes(Convert.ToBoolean(data)); break; case CqlType.Uuid: case CqlType.Timeuuid: var guid = (Guid)data; //return null if Guid is a nil Guid if (guid == default(Guid)) { rawData = null; } else { rawData = guid.ToByteArray(); if (IsLittleEndian) { Array.Reverse(rawData, 0, 4); Array.Reverse(rawData, 4, 2); Array.Reverse(rawData, 6, 2); } } break; case CqlType.Inet: rawData = ((IPAddress)data).GetAddressBytes(); break; default: throw new ArgumentException("Unsupported type"); } return rawData; }
private static object Deserialize(CqlType colType, byte[] rawData) { object data; switch (colType) { case CqlType.Ascii: data = Encoding.ASCII.GetString(rawData); break; case CqlType.Text: case CqlType.Varchar: data = Encoding.UTF8.GetString(rawData); break; case CqlType.Blob: data = rawData; break; case CqlType.Double: if (IsLittleEndian) Array.Reverse(rawData); data = BitConverter.ToDouble(rawData, 0); break; case CqlType.Float: if (IsLittleEndian) Array.Reverse(rawData); data = BitConverter.ToSingle(rawData, 0); break; case CqlType.Timestamp: data = rawData.ToLong().ToDateTime(); break; case CqlType.Bigint: case CqlType.Counter: data = rawData.ToLong(); break; case CqlType.Int: data = rawData.ToInt(); break; case CqlType.Varint: //to little endian Array.Reverse(rawData); data = new BigInteger(rawData); break; case CqlType.Boolean: data = BitConverter.ToBoolean(rawData, 0); break; case CqlType.Uuid: case CqlType.Timeuuid: return rawData.ToGuid(); case CqlType.Inet: data = new IPAddress(rawData); break; default: throw new ArgumentException("Unsupported type"); } return data; }
internal CqlListType(CqlType elementType) { ElementType = elementType; }
/// <summary> /// Translates the object to its Cql string representation. /// </summary> /// <param name="value"> The value. </param> /// <param name="type"> The type. </param> /// <returns> </returns> /// <exception cref="CqlLinqException">Unable to translate term to a string representation</exception> public static string ToStringValue(object value, CqlType type) { switch (type) { case CqlType.Text: case CqlType.Varchar: case CqlType.Ascii: var str = (string)value; return "'" + str.Replace("'", "''") + "'"; case CqlType.Inet: return "'" + value + "'"; case CqlType.Boolean: return ((bool)value) ? "true" : "false"; case CqlType.Decimal: return value.ToString(); case CqlType.Double: case CqlType.Float: var culture = CultureInfo.InvariantCulture; return string.Format(culture, "{0:E}", value); case CqlType.Counter: case CqlType.Bigint: case CqlType.Int: return string.Format("{0:D}", value); case CqlType.Timeuuid: case CqlType.Uuid: return ((Guid)value).ToString("D"); case CqlType.Varint: return ((BigInteger)value).ToString("D"); case CqlType.Timestamp: long timestamp = ((DateTime)value).ToTimestamp(); return string.Format("{0:D}", timestamp); case CqlType.Blob: return ((byte[])value).ToHex("0x"); case CqlType.List: { var listType = value.GetType().GetGenericArguments()[0].ToCqlType(); var builder = new StringBuilder(); builder.Append("["); bool first = true; foreach (var val in (IEnumerable)value) { if (!first) { builder.Append(","); } first = false; builder.Append(ToStringValue(val, listType)); } builder.Append("]"); return builder.ToString(); } case CqlType.Set: { var setType = value.GetType().GetGenericArguments()[0].ToCqlType(); var builder = new StringBuilder(); builder.Append("{"); bool first = true; foreach (var val in (IEnumerable)value) { if (!first) { builder.Append(","); } first = false; builder.Append(ToStringValue(val, setType)); } builder.Append("}"); return builder.ToString(); } case CqlType.Map: { var keyType = value.GetType().GetGenericArguments()[0].ToCqlType(); var valType = value.GetType().GetGenericArguments()[1].ToCqlType(); var builder = new StringBuilder(); builder.Append("{"); bool first = true; foreach (DictionaryEntry entry in (IDictionary)value) { if (!first) { builder.Append(","); } first = false; builder.Append(ToStringValue(entry.Key, keyType)); builder.Append(":"); builder.Append(ToStringValue(entry.Value, valType)); } builder.Append("}"); return builder.ToString(); } default: throw new CqlLinqException("Unable to translate term to a string representation"); } }
public static byte[] Serialize(CqlType colType, object data) { //null value check if (data == null) { return(null); } byte[] rawData; switch (colType) { case CqlType.Ascii: rawData = Encoding.ASCII.GetBytes(Convert.ToString(data)); break; case CqlType.Text: case CqlType.Varchar: rawData = Encoding.UTF8.GetBytes(Convert.ToString(data)); break; case CqlType.Blob: rawData = (byte[])data; break; case CqlType.Double: rawData = BitConverter.GetBytes(Convert.ToDouble(data)); if (IsLittleEndian) { Array.Reverse(rawData); } break; case CqlType.Float: rawData = BitConverter.GetBytes(Convert.ToSingle(data)); if (IsLittleEndian) { Array.Reverse(rawData); } break; case CqlType.Timestamp: if (data is long) { rawData = BitConverter.GetBytes((long)data); } else { rawData = BitConverter.GetBytes(Convert.ToDateTime(data).ToTimestamp()); } if (IsLittleEndian) { Array.Reverse(rawData); } break; case CqlType.Bigint: case CqlType.Counter: rawData = BitConverter.GetBytes(Convert.ToInt64(data)); if (IsLittleEndian) { Array.Reverse(rawData); } break; case CqlType.Int: rawData = BitConverter.GetBytes(Convert.ToInt32(data)); if (IsLittleEndian) { Array.Reverse(rawData); } break; case CqlType.Varint: var dataString = data as string; if (dataString != null) { rawData = BigInteger.Parse(dataString).ToByteArray(); } else { var integer = (BigInteger)data; rawData = integer.ToByteArray(); } //to bigendian Array.Reverse(rawData); break; case CqlType.Boolean: rawData = BitConverter.GetBytes(Convert.ToBoolean(data)); break; case CqlType.Uuid: case CqlType.Timeuuid: var guid = (Guid)data; //return null if Guid is a nil Guid if (guid == default(Guid)) { rawData = null; } else { rawData = guid.ToByteArray(); if (IsLittleEndian) { Array.Reverse(rawData, 0, 4); Array.Reverse(rawData, 4, 2); Array.Reverse(rawData, 6, 2); } } break; case CqlType.Inet: rawData = ((IPAddress)data).GetAddressBytes(); break; default: throw new ArgumentException("Unsupported type"); } return(rawData); }
private static object Deserialize(CqlType colType, byte[] rawData) { object data; switch (colType) { case CqlType.Ascii: data = Encoding.ASCII.GetString(rawData); break; case CqlType.Text: case CqlType.Varchar: data = Encoding.UTF8.GetString(rawData); break; case CqlType.Blob: data = rawData; break; case CqlType.Double: if (IsLittleEndian) { Array.Reverse(rawData); } data = BitConverter.ToDouble(rawData, 0); break; case CqlType.Float: if (IsLittleEndian) { Array.Reverse(rawData); } data = BitConverter.ToSingle(rawData, 0); break; case CqlType.Timestamp: data = rawData.ToLong().ToDateTime(); break; case CqlType.Bigint: case CqlType.Counter: data = rawData.ToLong(); break; case CqlType.Int: data = rawData.ToInt(); break; case CqlType.Varint: //to little endian Array.Reverse(rawData); data = new BigInteger(rawData); break; case CqlType.Boolean: data = BitConverter.ToBoolean(rawData, 0); break; case CqlType.Uuid: case CqlType.Timeuuid: return(rawData.ToGuid()); case CqlType.Inet: data = new IPAddress(rawData); break; default: throw new ArgumentException("Unsupported type"); } return(data); }
internal CqlFrozenType(CqlType elementType) { ElementType = elementType; }
/// <summary> /// Initializes a new instance of the <see cref="CqlParameter" /> class. /// </summary> /// <param name="table"> The table. </param> /// <param name="name"> The name. </param> /// <param name="type"> The typeCode. </param> public CqlParameter(string table, string name, CqlType type) : this() { _column.Name = name; _column.Table = table; _column.Type = type; }
public CqlTupleType(CqlType firstType, CqlType secondType) { FirstType = firstType; SecondType = secondType; }
internal CqlMapType(CqlType keyType, CqlType valueType) { KeyType = keyType; ValueType = valueType; }
internal CqlSetType(CqlType elementType) { ElementType = elementType; }