Permalink
Browse files

added support for CQL 3 and selecting the CQL version

  • Loading branch information...
1 parent 798d161 commit ea9226f56cf4ff70a7bf07586e717b3ad95a25f5 @nberardi nberardi committed May 24, 2012
@@ -20,6 +20,8 @@ public CassandraColumnFamilySchema(CfDef def)
_def = def;
#endif
+ KeyspaceName = def.Keyspace;
+
var familyType = ColumnType.Standard;
Enum.TryParse<ColumnType>(def.Column_type, out familyType);
@@ -66,6 +68,8 @@ public CassandraColumnFamilySchema(string name = null, ColumnType type = ColumnT
Columns = new List<CassandraColumnSchema>();
}
+ internal string KeyspaceName { get; set; }
+
public ColumnType FamilyType { get; set; }
public string FamilyName { get; set; }
public string FamilyDescription { get; set; }
@@ -77,5 +81,32 @@ public CassandraColumnFamilySchema(string name = null, ColumnType type = ColumnT
public CassandraType DefaultColumnValueType { get; set; }
public IList<CassandraColumnSchema> Columns { get; set; }
+
+ public static implicit operator CfDef(CassandraColumnFamilySchema schema)
+ {
+ var def = new CfDef {
+ Keyspace = schema.KeyspaceName,
+ Name = schema.FamilyName,
+ Comment = schema.FamilyDescription,
+ Column_type = schema.FamilyType.ToString(),
+ Key_alias = schema.KeyName.ToBigEndian(),
+ Key_validation_class = schema.KeyType.DatabaseType,
+ Comparator_type = schema.ColumnNameType.DatabaseType,
+ Default_validation_class = schema.DefaultColumnValueType.DatabaseType
+ };
+
+ if (schema.FamilyType == ColumnType.Super)
+ {
+ def.Comparator_type = schema.SuperColumnNameType.DatabaseType;
+ def.Subcomparator_type = schema.ColumnNameType.DatabaseType;
+ }
+
+ return def;
+ }
+
+ public static implicit operator CassandraColumnFamilySchema(CfDef def)
+ {
+ return new CassandraColumnFamilySchema(def);
+ }
}
}
@@ -31,5 +31,18 @@ public CassandraObject Name
public CassandraType NameType { get; set; }
public CassandraType ValueType { get; set; }
+
+ public static implicit operator ColumnDef(CassandraColumnSchema schema)
+ {
+ return new ColumnDef {
+ Name = schema.Name.ToBigEndian(),
+ Validation_class = schema.ValueType.DatabaseType
+ };
+ }
+
+ public static implicit operator CassandraColumnSchema(ColumnDef def)
+ {
+ return new CassandraColumnSchema(def, CassandraType.BytesType);
+ }
}
}
View
@@ -59,12 +59,7 @@ public void TryCreateSelf()
try
{
- string result = _context.AddKeyspace(new KsDef {
- Name = schema.Name,
- Strategy_class = schema.Strategy,
- Replication_factor = schema.ReplicationFactor,
- Cf_defs = new List<CfDef>(0)
- });
+ string result = _context.AddKeyspace(schema);
Debug.WriteLine(result, "keyspace setup");
}
catch
@@ -77,24 +72,9 @@ public void TryCreateColumnFamily(CassandraColumnFamilySchema schema)
{
try
{
- var def = new CfDef {
- Keyspace = KeyspaceName,
- Name = schema.FamilyName,
- Comment = schema.FamilyDescription,
- Column_type = schema.FamilyType.ToString(),
- Key_alias = schema.KeyName.ToBigEndian(),
- Key_validation_class = schema.KeyType.DatabaseType,
- Comparator_type = schema.ColumnNameType.DatabaseType,
- Default_validation_class = schema.DefaultColumnValueType.DatabaseType
- };
-
- if (schema.FamilyType == ColumnType.Super)
- {
- def.Comparator_type = schema.SuperColumnNameType.DatabaseType;
- def.Subcomparator_type = schema.ColumnNameType.DatabaseType;
- }
-
- string result = _context.AddColumnFamily(def);
+ schema.KeyspaceName = KeyspaceName;
+
+ string result = _context.AddColumnFamily(schema);
Debug.WriteLine(result, "column family setup");
}
catch
@@ -31,5 +31,20 @@ public CassandraKeyspaceSchema(KsDef def)
public int ReplicationFactor { get; set; }
public IList<CassandraColumnFamilySchema> ColumnFamilies { get; set; }
+
+ public static implicit operator KsDef(CassandraKeyspaceSchema schema)
+ {
+ return new KsDef {
+ Name = schema.Name,
+ Strategy_class = schema.Strategy,
+ Replication_factor = schema.ReplicationFactor,
+ Cf_defs = new List<CfDef>(0)
+ };
+ }
+
+ public static implicit operator CassandraKeyspaceSchema(KsDef def)
+ {
+ return new CassandraKeyspaceSchema(def);
+ }
}
}
View
@@ -64,18 +64,23 @@ public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel
/// </summary>
/// <param name="setKeyspace"></param>
/// <returns></returns>
- internal CassandraClientWrapper GetClient(bool setKeyspace = true)
+ internal CassandraClientWrapper GetClient(bool setKeyspace = true, bool? setCqlVersion = null)
{
+ var builder = ConnectionProvider.Builder;
+ setCqlVersion = setCqlVersion ?? (builder.CqlVersion != null);
+
if (_connection == null)
_connection = ConnectionProvider.Open();
if (!_connection.IsOpen)
_connection.Open();
if (setKeyspace)
- _connection.SetKeyspace(ConnectionProvider.Builder.Keyspace);
+ _connection.SetKeyspace(builder.Keyspace);
+
+ if (setCqlVersion.Value)
+ _connection.SetCqlVersion(builder.CqlVersion);
- var builder = ConnectionProvider.Builder;
if (!String.IsNullOrWhiteSpace(builder.Username) && !String.IsNullOrWhiteSpace(builder.Password))
Login(builder.Username, builder.Password);
@@ -112,6 +112,10 @@ public void Close()
_transport.Close();
}
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="keyspace"></param>
public void SetKeyspace(string keyspace)
{
Client.set_keyspace(keyspace);
@@ -120,6 +124,15 @@ public void SetKeyspace(string keyspace)
/// <summary>
///
/// </summary>
+ /// <param name="cqlVersion"></param>
+ public void SetCqlVersion(string cqlVersion)
+ {
+ Client.set_cql_version(cqlVersion);
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
public Cassandra.Client Client
{
get { return _client; }
@@ -14,7 +14,7 @@ public class ConnectionBuilder
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="timeout"></param>
- public ConnectionBuilder(string keyspace, string host, int port = Server.DefaultPort, int connectionTimeout = Server.DefaultTimeout, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, bool compressCqlQueries = false, string username = null, string password = null)
+ public ConnectionBuilder(string keyspace, string host, int port = Server.DefaultPort, int connectionTimeout = Server.DefaultTimeout, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault, bool compressCqlQueries = false, string username = null, string password = null)
{
Keyspace = keyspace;
Servers = new List<Server>() { new Server(host, port) };
@@ -27,14 +27,15 @@ public ConnectionBuilder(string keyspace, string host, int port = Server.Default
BufferSize = bufferSize;
ReadConsistency = read;
WriteConsistency = write;
+ CqlVersion = cqlVersion;
CompressCqlQueries = compressCqlQueries;
Username = username;
Password = password;
ConnectionString = GetConnectionString();
}
- public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, bool compressCqlQueries = false, string username = null, string password = null)
+ public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault, bool compressCqlQueries = false, string username = null, string password = null)
{
Keyspace = keyspace;
Servers = new List<Server>() { server };
@@ -47,6 +48,7 @@ public ConnectionBuilder(string keyspace, Server server, bool pooling = false, i
BufferSize = bufferSize;
ReadConsistency = read;
WriteConsistency = write;
+ CqlVersion = cqlVersion;
CompressCqlQueries = compressCqlQueries;
Username = username;
Password = password;
@@ -297,6 +299,19 @@ private void InitializeConnectionString(string connectionString)
#endregion
+ #region CqlVersion
+
+ if (!pairs.ContainsKey("CQL Version"))
+ {
+ CqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault;
+ }
+ else
+ {
+ CqlVersion = pairs["CQL Version"];
+ }
+
+ #endregion
+
#region CompressCqlQueries
if (!pairs.ContainsKey("Compress CQL Queries"))
@@ -351,6 +366,7 @@ private string GetConnectionString()
b.AppendFormat(format, "Read", ReadConsistency);
b.AppendFormat(format, "Write", WriteConsistency);
+ b.AppendFormat(format, "CQL Version", CqlVersion);
b.AppendFormat(format, "Compress CQL Queries", CompressCqlQueries);
b.AppendFormat(format, "Username", Username);
@@ -417,6 +433,11 @@ private string GetConnectionString()
/// <summary>
///
/// </summary>
+ public string CqlVersion { get; private set; }
+
+ /// <summary>
+ ///
+ /// </summary>
public bool CompressCqlQueries { get; private set; }
/// <summary>
@@ -0,0 +1,15 @@
+using System;
+using System.Linq;
+
+namespace FluentCassandra.Connections
+{
+ public sealed class CqlVersion
+ {
+ public const string Cql1 = "1.0.0.0";
+ public const string Cql2 = "2.0.0.0";
+ public const string Cql3 = "3.0.0.0";
+
+ public const string ServerDefault = null;
+ public const string Edge = Cql3;
+ }
+}
@@ -12,6 +12,7 @@ public interface IConnection : IDisposable
Cassandra.Client Client { get; }
void SetKeyspace(string keyspace);
+ void SetCqlVersion(string cqlVersion);
void Open();
void Close();
@@ -110,6 +110,7 @@
<Compile Include="Connections\ConnectionProvider.cs" />
<Compile Include="Connections\ConnectionProviderFactory.cs" />
<Compile Include="Connections\ConnectionType.cs" />
+ <Compile Include="Connections\CqlVersion.cs" />
<Compile Include="Connections\NormalConnectionProvider.cs" />
<Compile Include="Connections\IConnection.cs" />
<Compile Include="Connections\IConnectionProvider.cs" />
@@ -7,7 +7,7 @@
namespace FluentCassandra.Linq
{
- public class CqlObjectQuery<T> : IQueryable, IQueryable<T>
+ public class CqlObjectQuery<T> : IQueryable, IQueryable<T>, IOrderedQueryable, IOrderedQueryable<T>
{
private readonly Expression _expression;
private readonly IQueryProvider _provider;
View
@@ -6,7 +6,7 @@
namespace FluentCassandra.Linq
{
- public class CqlQuery : IQueryable, IQueryable<ICqlRow>
+ public class CqlQuery : IQueryable, IQueryable<ICqlRow>, IOrderedQueryable, IOrderedQueryable<ICqlRow>
{
private readonly Expression _expression;
private readonly CassandraColumnFamily _provider;
Oops, something went wrong.

0 comments on commit ea9226f

Please sign in to comment.