Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
finished changes to work with new CQL in Cassandra 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Berardi committed Jan 3, 2013
1 parent 13f5db5 commit bfa48ac
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 29 deletions.
16 changes: 8 additions & 8 deletions src/CassandraContext.cs
Expand Up @@ -318,20 +318,20 @@ public void SaveChanges(IFluentRecord record)
/// ///
/// </summary> /// </summary>
/// <param name="cqlQuery"></param> /// <param name="cqlQuery"></param>
public IEnumerable<ICqlRow> ExecuteQuery(UTF8Type cqlQuery) public IEnumerable<ICqlRow> ExecuteQuery(UTF8Type cqlQuery, string cqlVersion = CqlVersion.ConnectionDefault)
{ {
var op = new ExecuteCqlQuery(cqlQuery); var op = new ExecuteCqlQuery(cqlQuery, cqlVersion);
return ExecuteOperation(op); return ExecuteOperation(op);
} }


/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="cqlQuery"></param> /// <param name="cqlQuery"></param>
public void TryExecuteNonQuery(UTF8Type cqlQuery) public void TryExecuteNonQuery(UTF8Type cqlQuery, string cqlVersion = CqlVersion.ConnectionDefault)
{ {
try { try {
ExecuteNonQuery(cqlQuery); ExecuteNonQuery(cqlQuery, cqlVersion);
} catch (Exception exc) { } catch (Exception exc) {
Debug.WriteLine(exc); Debug.WriteLine(exc);
} }
Expand All @@ -341,19 +341,19 @@ public void TryExecuteNonQuery(UTF8Type cqlQuery)
/// ///
/// </summary> /// </summary>
/// <param name="cqlQuery"></param> /// <param name="cqlQuery"></param>
public void ExecuteNonQuery(UTF8Type cqlQuery) public void ExecuteNonQuery(UTF8Type cqlQuery, string cqlVersion = CqlVersion.ConnectionDefault)
{ {
var op = new ExecuteCqlNonQuery(cqlQuery); var op = new ExecuteCqlNonQuery(cqlQuery, cqlVersion);
ExecuteOperation(op); ExecuteOperation(op);
} }


/// <summary> /// <summary>
/// The last error that occured during the execution of an operation. /// The last error that occurred during the execution of an operation.
/// </summary> /// </summary>
public CassandraException LastError { get; private set; } public CassandraException LastError { get; private set; }


/// <summary> /// <summary>
/// Indicates if errors should be thrown when occuring on operation. /// Indicates if errors should be thrown when occurring on operation.
/// </summary> /// </summary>
public bool ThrowErrors { get; set; } public bool ThrowErrors { get; set; }


Expand Down
1 change: 1 addition & 0 deletions src/Connections/Connection.cs
Expand Up @@ -178,6 +178,7 @@ public void SetKeyspace(string keyspace)
/// ///
/// </summary> /// </summary>
/// <param name="cqlVersion"></param> /// <param name="cqlVersion"></param>
[Obsolete("This will be retired soon, please pass the CQL version through the Execute method.", error: false)]
public void SetCqlVersion(string cqlVersion) public void SetCqlVersion(string cqlVersion)
{ {
CheckWasDisposed(); CheckWasDisposed();
Expand Down
4 changes: 2 additions & 2 deletions src/Connections/ConnectionBuilder.cs
Expand Up @@ -37,7 +37,7 @@ public ConnectionBuilder(string keyspace, string host, int port = Server.Default
ConnectionString = GetConnectionString(); ConnectionString = GetConnectionString();
} }


public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int maxRetries = 0, int serverPollingInterval = 30, 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 = true, string username = null, string password = null) public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int maxRetries = 0, int serverPollingInterval = 30, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.Edge, bool compressCqlQueries = true, string username = null, string password = null)

This comment has been minimized.

Copy link
@eplowe

eplowe Jan 7, 2013

Contributor

@nberardi Seems like the first constructor for ConnectionBuilder is using:

string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault

That's why you saw the commit come over on my fork.

{ {
Keyspace = keyspace; Keyspace = keyspace;
Servers = new List<Server>() { server }; Servers = new List<Server>() { server };
Expand Down Expand Up @@ -311,7 +311,7 @@ private void InitializeConnectionString(string connectionString)


if (!pairs.ContainsKey("CQL Version")) if (!pairs.ContainsKey("CQL Version"))
{ {
CqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault; CqlVersion = FluentCassandra.Connections.CqlVersion.Edge;
} }
else else
{ {
Expand Down
8 changes: 5 additions & 3 deletions src/Connections/CqlVersion.cs
Expand Up @@ -3,12 +3,14 @@


namespace FluentCassandra.Connections namespace FluentCassandra.Connections
{ {
public sealed class CqlVersion public static class CqlVersion
{ {
public const string Cql2 = "2.0.0"; public const string Cql = "0.8.0";
public const string Cql3 = "3.0.0"; public const string Cql3 = "3.0.0";
public const string Edge = Cql3;
public const string ConnectionDefault = null;


[Obsolete("This is no longer supported, please use ConnectionDefault", error: true)]
public const string ServerDefault = null; public const string ServerDefault = null;
public const string Edge = Cql3;
} }
} }
2 changes: 2 additions & 0 deletions src/Connections/IConnection.cs
Expand Up @@ -12,6 +12,8 @@ public interface IConnection : IDisposable
Cassandra.Client Client { get; } Cassandra.Client Client { get; }


void SetKeyspace(string keyspace); void SetKeyspace(string keyspace);

[Obsolete("This will be retired soon, please pass the CQL version through the Execute method.", error: false)]
void SetCqlVersion(string cqlVersion); void SetCqlVersion(string cqlVersion);


void Open(); void Open();
Expand Down
32 changes: 24 additions & 8 deletions src/Operations/ExecuteCqlNonQuery.cs
Expand Up @@ -8,30 +8,46 @@ namespace FluentCassandra.Operations
public class ExecuteCqlNonQuery : Operation<Void> public class ExecuteCqlNonQuery : Operation<Void>
{ {
public UTF8Type CqlQuery { get; private set; } public UTF8Type CqlQuery { get; private set; }

public string CqlVersion { get; private set; }
public bool CompressCqlQuery { get; private set; }


public override Void Execute() public override Void Execute()
{ {
Debug.Write(CqlQuery.ToString(), "query"); Debug.Write(CqlQuery.ToString(), "query");
byte[] query = CqlQuery; byte[] query = CqlQuery;
bool isCqlQueryCompressed = query.Length > 200 && Session.ConnectionBuilder.CompressCqlQueries; bool isCqlQueryCompressed = query.Length > 200 && Session.ConnectionBuilder.CompressCqlQueries;


// it doesn't make sense to compress queryies that are really small // it doesn't make sense to compress queries that are really small
if (isCqlQueryCompressed) if (isCqlQueryCompressed)
query = Helper.ZlibCompress(query); query = Helper.ZlibCompress(query);


var result = Session.GetClient().execute_cql_query( if (CqlVersion == FluentCassandra.Connections.CqlVersion.ConnectionDefault)
query, CqlVersion = Session.ConnectionBuilder.CqlVersion;
isCqlQueryCompressed ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE
); switch(CqlVersion) {
case FluentCassandra.Connections.CqlVersion.Cql:
Session.GetClient().execute_cql_query(
query,
isCqlQueryCompressed ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE);
break;

case FluentCassandra.Connections.CqlVersion.Cql3:
Session.GetClient().execute_cql3_query(
query,
isCqlQueryCompressed ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE,
Session.WriteConsistency);
break;

default:
throw new FluentCassandraException(CqlVersion + " is not a valid CQL version.");
}


return new Void(); return new Void();
} }


public ExecuteCqlNonQuery(UTF8Type cqlQuery) public ExecuteCqlNonQuery(UTF8Type cqlQuery, string cqlVersion)
{ {
CqlQuery = cqlQuery; CqlQuery = cqlQuery;
CqlVersion = cqlVersion;
} }
} }
} }
33 changes: 27 additions & 6 deletions src/Operations/ExecuteCqlQuery.cs
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using FluentCassandra.Apache.Cassandra;
using FluentCassandra.Linq; using FluentCassandra.Linq;
using FluentCassandra.Types; using FluentCassandra.Types;


Expand All @@ -13,6 +14,7 @@ public class ExecuteCqlQuery : ColumnFamilyOperation<IEnumerable<ICqlRow>>
private static readonly Regex ColumnFamilyNameExpression = new Regex(@"FROM\s+(?<name>\w+)"); private static readonly Regex ColumnFamilyNameExpression = new Regex(@"FROM\s+(?<name>\w+)");


public UTF8Type CqlQuery { get; private set; } public UTF8Type CqlQuery { get; private set; }
public string CqlVersion { get; private set; }


private string TryGetFamilyName() private string TryGetFamilyName()
{ {
Expand All @@ -34,14 +36,32 @@ public override IEnumerable<ICqlRow> Execute()
byte[] query = CqlQuery; byte[] query = CqlQuery;
bool isCqlQueryCompressed = query.Length > 200 && Session.ConnectionBuilder.CompressCqlQueries; bool isCqlQueryCompressed = query.Length > 200 && Session.ConnectionBuilder.CompressCqlQueries;


// it doesn't make sense to compress queryies that are really small // it doesn't make sense to compress queries that are really small
if (isCqlQueryCompressed) if (isCqlQueryCompressed)
query = Helper.ZlibCompress(query); query = Helper.ZlibCompress(query);


var result = Session.GetClient().execute_cql_query( if (CqlVersion == FluentCassandra.Connections.CqlVersion.ConnectionDefault)
query, CqlVersion = Session.ConnectionBuilder.CqlVersion;
isCqlQueryCompressed ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE
); var result = (CqlResult)null;

switch (CqlVersion) {
case FluentCassandra.Connections.CqlVersion.Cql:
result = Session.GetClient().execute_cql_query(
query,
isCqlQueryCompressed ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE);
break;

case FluentCassandra.Connections.CqlVersion.Cql3:
result = Session.GetClient().execute_cql3_query(
query,
isCqlQueryCompressed ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE,
Session.ReadConsistency);
break;

default:
throw new FluentCassandraException(CqlVersion + " is not a valid CQL version.");
}


return GetRows(result); return GetRows(result);
} }
Expand Down Expand Up @@ -78,9 +98,10 @@ private IEnumerable<FluentColumn> GetColumns(Apache.Cassandra.CqlRow row, Cassan
return list; return list;
} }


public ExecuteCqlQuery(UTF8Type cqlQuery) public ExecuteCqlQuery(UTF8Type cqlQuery, string cqlVersion)
{ {
CqlQuery = cqlQuery; CqlQuery = cqlQuery;
CqlVersion = cqlVersion;
} }
} }
} }
4 changes: 2 additions & 2 deletions test/FluentCassandra.Tests/Bugs/Issue36KeyAliasSupport.cs
Expand Up @@ -21,10 +21,10 @@ public void Dispose()
} }


[Fact] [Fact]
public void Test_Cql2() public void Test_Cql()
{ {
var connBuilder = _db.ConnectionBuilder; var connBuilder = _db.ConnectionBuilder;
connBuilder = new ConnectionBuilder(connBuilder.Keyspace, connBuilder.Servers[0], cqlVersion: CqlVersion.Cql2); connBuilder = new ConnectionBuilder(connBuilder.Keyspace, connBuilder.Servers[0], cqlVersion: CqlVersion.Cql);
var db = new CassandraContext(connBuilder); var db = new CassandraContext(connBuilder);


// arrange // arrange
Expand Down

0 comments on commit bfa48ac

Please sign in to comment.