Beispiel #1
0
        public void Init()
        {
            const string createKsCql =
                @"CREATE KEYSPACE Test WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';";
            const string createTableCql = @"create table Test.BasicFlow (id int primary key, value text);";
            const string truncateTableCql = @"truncate Test.BasicFlow;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                try
                {
                    var createKs = new CqlCommand(connection, createKsCql);
                    createKs.ExecuteNonQuery();
                }
                catch (AlreadyExistsException)
                {
                    //ignore
                }

                try
                {
                    var createTable = new CqlCommand(connection, createTableCql);
                    createTable.ExecuteNonQuery();
                }
                catch (AlreadyExistsException)
                {
                    var truncTable = new CqlCommand(connection, truncateTableCql);
                    truncTable.ExecuteNonQuery();
                }
            }
        }
Beispiel #2
0
        public static void ClassInit(TestContext context)
        {
            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();
                try
                {
                    var createKs = new CqlCommand(connection, CreateKsCql);
                    createKs.ExecuteNonQuery();
                }
                catch (AlreadyExistsException)
                {
                    //ignore
                }

                try
                {
                    var createTable = new CqlCommand(connection, CreateTableCql);
                    createTable.ExecuteNonQuery();
                }
                catch (AlreadyExistsException)
                {
                    //ignore
                }
            }
        }
Beispiel #3
0
        public static void ClassInit(TestContext context)
        {
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();
                try
                {
                    var createKs = new CqlCommand(connection, CreateKsCql);
                    createKs.ExecuteNonQuery();
                }
                catch(AlreadyExistsException)
                {
                    //ignore
                }

                try
                {
                    var createTable = new CqlCommand(connection, CreateTableCql);
                    createTable.ExecuteNonQuery();
                }
                catch(AlreadyExistsException)
                {
                    //ignore
                }
            }
        }
Beispiel #4
0
        public void Init()
        {
            const string createKsCql =
                @"CREATE KEYSPACE Test WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';";
            const string createTableCql   = @"create table Test.BasicFlow (id int primary key, value text);";
            const string truncateTableCql = @"truncate Test.BasicFlow;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                try
                {
                    var createKs = new CqlCommand(connection, createKsCql);
                    createKs.ExecuteNonQuery();
                }
                catch (AlreadyExistsException)
                {
                    //ignore
                }

                try
                {
                    var createTable = new CqlCommand(connection, createTableCql);
                    createTable.ExecuteNonQuery();
                }
                catch (AlreadyExistsException)
                {
                    var truncTable = new CqlCommand(connection, truncateTableCql);
                    truncTable.ExecuteNonQuery();
                }
            }
        }
Beispiel #5
0
        public void Init()
        {
            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var truncTable = new CqlCommand(connection, TruncateTableCql);
                truncTable.ExecuteNonQuery();
            }
        }
Beispiel #6
0
        public static void Init(TestContext context)
        {
            const string createConnection = "Server=localhost;username=cassandra;password=cassandra";

            const string createKsCql =
                @"CREATE KEYSPACE LinqTest WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';";
            const string createTableCql =
                @"create table linqtest.myvalue (id int primary key, value text, ignored text);";


            using (var connection = new CqlConnection(createConnection))
            {
                connection.SetConnectionTimeout(0);
                connection.Open();

                try
                {
                    var createKs = new CqlCommand(connection, createKsCql);
                    createKs.ExecuteNonQuery();

                    var createTable = new CqlCommand(connection, createTableCql);
                    createTable.ExecuteNonQuery();

                    using (var transaction = connection.BeginTransaction())
                    {
                        transaction.BatchType = CqlBatchType.Unlogged;

                        var insert = new CqlCommand(connection, "insert into linqtest.myvalue (id,value) values(?,?)");
                        insert.Transaction = transaction;
                        insert.Prepare();

                        for (int i = 0; i < 10000; i++)
                        {
                            insert.Parameters[0].Value = i;
                            insert.Parameters[1].Value = "Hallo " + i;
                            insert.ExecuteNonQuery();
                        }

                        transaction.Commit();
                    }
                }
                catch (AlreadyExistsException)
                {
                    //ignore
                }
            }

            CqlConnection.Shutdown(createConnection);
        }
        public static void Init(TestContext context)
        {
            const string createKsCql =
                @"CREATE KEYSPACE TestUDT WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';";

            const string createAddressType =
                @"create type TestUDT.TAddress (street text, number int);";

            const string createUserType =
                @"CREATE type TestUDT.TUser (name text, password blob, address frozen<TAddress>, phones list<text>);";

            const string createTableCql =
                @"create table TestUDT.Members (id int primary key, user frozen<TUser>, comment frozen<tuple<text,text>>);";

            const string createTable2Cql =
                @"create table TestUDT.Groups (id int primary key, members set<frozen<tuple<int, frozen<TUser>>>>);";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.SetConnectionTimeout(0);
                connection.Open();

                Cassandra210OrUp = string.CompareOrdinal(connection.ServerVersion, "2.1.0") >= 0;
                if(!Cassandra210OrUp)
                    return;

                try
                {
                    var createKs = new CqlCommand(connection, createKsCql);
                    createKs.ExecuteNonQuery();

                    var createAddress = new CqlCommand(connection, createAddressType);
                    createAddress.ExecuteNonQuery();

                    var createUser = new CqlCommand(connection, createUserType);
                    createUser.ExecuteNonQuery();

                    var createTable = new CqlCommand(connection, createTableCql);
                    createTable.ExecuteNonQuery();

                    var createTable2 = new CqlCommand(connection, createTable2Cql);
                    createTable2.ExecuteNonQuery();
                }
                catch(AlreadyExistsException)
                {
                }
            }
        }
Beispiel #8
0
        static void SetupDatabase()
        {

            const string createKsCql =
                @"CREATE KEYSPACE linqperftest WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';";

            const string createTableCql =
                @"create table linqperftest.myvalue (id int primary key, value text, ignored text);";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                try
                {
                    var createKs = new CqlCommand(connection, createKsCql);
                    createKs.ExecuteNonQuery();

                    var createTable = new CqlCommand(connection, createTableCql);
                    createTable.ExecuteNonQuery();

                    using (var transaction = connection.BeginTransaction())
                    {
                        transaction.BatchType = CqlBatchType.Unlogged;

                        var insert = new CqlCommand(connection, "insert into linqperftest.myvalue (id,value) values(?,?)");
                        insert.Transaction = transaction;
                        insert.Prepare();

                        for (int i = 0; i < 10000; i++)
                        {
                            insert.Parameters[0].Value = i;
                            insert.Parameters[1].Value = "Hallo " + i;
                            insert.ExecuteNonQuery();
                        }

                        transaction.Commit();
                    }

                }
                catch (AlreadyExistsException)
                {
                }
            }
        }
Beispiel #9
0
        public static void Cleanup()
        {
            const string dropCql = @"drop keyspace Test;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();
                try
                {
                    var drop = new CqlCommand(connection, dropCql);
                    drop.ExecuteNonQuery();
                }
                catch (InvalidException)
                {
                    //ignore
                }
            }
        }
Beispiel #10
0
        public void Cleanup()
        {
            const string dropCql = @"drop keyspace Test;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                try
                {
                    var drop = new CqlCommand(connection, dropCql);
                    drop.ExecuteNonQuery();
                }
                catch (InvalidException)
                {
                    //ignore
                }
            }
        }
Beispiel #11
0
        public void DefaultDeserializeTest()
        {
            const string insertCql = @"insert into Test.Types(aInt) values (1);";

            const string selectCql = "select * from Test.Types limit 1;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();

                var   selectCmd = new CqlCommand(connection, selectCql);
                Types result    = null;
                using (var reader = selectCmd.ExecuteReader <Types>())
                {
                    if (reader.Read())
                    {
                        result = reader.Current;
                    }
                }

                Assert.IsNotNull(result);

                Assert.AreEqual(result.aASCIIString, default(string));
                Assert.AreEqual(result.aVarcharString, default(string));
                Assert.AreEqual(result.aVarint, default(BigInteger));
                Assert.AreEqual(result.aTextString, default(string));
                Assert.AreEqual(result.aBool, default(bool));
                Assert.AreEqual(result.aDouble, default(double));
                Assert.AreEqual(result.aFloat, default(float));
                Assert.AreEqual(result.aInet, default(IPAddress));
                Assert.AreEqual(result.aLong, default(long));
                Assert.AreEqual(result.aTimeUUID, default(Guid));
                Assert.AreEqual(result.aUUID, default(Guid));
                Assert.AreEqual(result.aBlob, default(byte[]));
                Assert.AreEqual(result.aList, default(List <string>));
                Assert.AreEqual(result.aSet, default(HashSet <int>));
                Assert.AreEqual(result.aMap, default(Dictionary <long, string>));
            }
        }
Beispiel #12
0
        public void NullableDeserializeTest()
        {
            const string insertCql = @"insert into Test.Types(aInt) values (4);";

            const string selectCql = "select * from Test.Types limit 1;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();

                var           selectCmd = new CqlCommand(connection, selectCql);
                NullableTypes result    = null;
                using (var reader = selectCmd.ExecuteReader <NullableTypes>())
                {
                    if (reader.Read())
                    {
                        result = reader.Current;
                    }
                }

                Assert.IsNotNull(result);

                Assert.IsNull(result.aASCIIString);
                Assert.IsNull(result.aVarcharString);
                Assert.IsNull(result.aVarint);
                Assert.IsNull(result.aTextString);
                Assert.IsNull(result.aBool);
                Assert.IsNull(result.aDouble);
                Assert.IsNull(result.aFloat);
                Assert.IsNull(result.aInet);
                Assert.IsNull(result.aLong);
                Assert.IsNull(result.aTimeUUID);
                Assert.IsNull(result.aUUID);
                Assert.IsNull(result.aBlob);
                Assert.IsNull(result.aList);
                Assert.IsNull(result.aSet);
                Assert.IsNull(result.aMap);
            }
        }
Beispiel #13
0
        public void BasicFlowAdo()
        {
            //Assume
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);";
            const string retrieveCql = @"select id,value,ignored from Test.BasicFlow;";

            //Act

            using(IDbConnection connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                IDbCommand cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Parameters.Add(new CqlParameter("id", CqlType.Int));
                cmd.Parameters.Add(new CqlParameter("value", CqlType.Text));

                cmd.Prepare();

                ((IDbDataParameter)cmd.Parameters["id"]).Value = 456;
                ((IDbDataParameter)cmd.Parameters["value"]).Value = "Hallo 456";

                cmd.ExecuteNonQuery();

                IDbCommand selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One)
                {
                    EnableTracing = true
                };
                IDataReader reader = selectCmd.ExecuteReader();

                DataTable schema = reader.GetSchemaTable();
                Assert.AreEqual(3, schema.Rows.Count);
                Assert.IsTrue(
                    schema.Rows.Cast<DataRow>().Any(row => row[CqlSchemaTableColumnNames.ColumnName].Equals("ignored")));

                if(reader.Read())
                {
                    int id = reader.GetInt32(0);
                    string value = reader.GetString(1);
                    Assert.AreEqual(456, id);
                    Assert.IsTrue(reader.IsDBNull(2));
                    Assert.AreEqual("Hallo 456", value);
                }
                else
                    Assert.Fail("Read should have succeeded");
                }
            }
Beispiel #14
0
        public void SelectTypedMultiple()
        {
            //Assume
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);";
            const string retrieveCql = @"select * from Test.BasicFlow;";

            //Act
            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Prepare();

                for (int i = 0; i < 100; i++)
                {
                    cmd.Parameters[0].Value = i;
                    cmd.Parameters[1].Value = "Hello " + i;
                    cmd.ExecuteNonQuery();
                }

                //select data
                var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One);
                
                using (var reader = selectCmd.ExecuteReader<BasicFlowData>())
                {
                    Assert.AreEqual(100, reader.Count);

                    var results = new bool[100];
                    for (int i = 0; i < 100; i++)
                    {
                        if (reader.Read())
                        {
                            var current = reader.Current;
                            results[current.Id] = true;
                            Assert.AreEqual("Hello " + current.Id, current.Data);
                        }
                        else
                            Assert.Fail("Read should have succeeded");
                    }
                    Assert.IsFalse(reader.Read());
                    Assert.IsTrue(results.All(p => p), "Not all expected entries are returned");
                }
            }
        }
Beispiel #15
0
        public void SerializeNullableObjectOutDefaultsTest()
        {
            const string insertCql = @"insert into LinqTest.Types(aInt) values (1);";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();
            }

            using (var context = new SerializationContext())
            {
                var result = context.NullableTypes.FirstOrDefault();

                Assert.IsNotNull(result);

                Assert.IsNull(result.aASCIIString);
                Assert.IsNull(result.aVarcharString);
                Assert.IsNull(result.aVarint);
                Assert.IsNull(result.aTextString);
                Assert.IsNull(result.aBool);
                Assert.IsNull(result.aDecimal);
                Assert.IsNull(result.aDouble);
                Assert.IsNull(result.aFloat);
                Assert.IsNull(result.aInet);
                Assert.IsNull(result.aLong);
                Assert.IsNull(result.aTimeUUID);
                Assert.IsNull(result.aUUID);
                Assert.IsNull(result.aBlob);
                Assert.IsNull(result.aList);
                Assert.IsNull(result.aSet);
                Assert.IsNull(result.aMap);

            }
        }
Beispiel #16
0
        public void Init()
        {
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var truncTable = new CqlCommand(connection, TruncateTableCql);
                truncTable.ExecuteNonQuery();
            }
        }
Beispiel #17
0
        public void SerializeObjectInOutNullTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(
                aInt,
                aLong ,
                aVarint ,
                aTextString ,
                aVarcharString ,
                aASCIIString ,
                aBlob ,
                aBool ,
                aDecimal ,
                aDouble  , 
                aFloat  , 
                aTimestamp ,
                aTimeUUID ,
                aUUID ,
                aInet ,
                aList,
                aSet,
                aMap) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var values = new NullableTypes
                {
                    aInt = 3,
                    aASCIIString = null,
                    aBlob = null,
                    aBool = null,
                    aDecimal = null,
                    aDouble = null,
                    aFloat = null,
                    aInet = null,
                    aLong = null,
                    aTextString = null,
                    aVarcharString = null,
                    aTimeUUID = null,
                    aUUID = null,
                    aTimestamp = null,
                    aVarint = null,
                    aList = null,
                    aSet = null,
                    aMap = null
                };

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.Prepare();
                insertCmd.Parameters.Set(values);
                insertCmd.ExecuteNonQuery();

                var selectCmd = new CqlCommand(connection, selectCql);
                NullableTypes result = null;
                using(var reader = selectCmd.ExecuteReader<NullableTypes>())
                {
                    if(reader.Read())
                        result = reader.Current;
                }

                Assert.IsNotNull(result);

                Assert.IsNull(result.aASCIIString);
                Assert.IsNull(result.aVarcharString);
                Assert.IsNull(result.aVarint);
                Assert.IsNull(result.aTextString);
                Assert.IsNull(result.aBool);
                Assert.IsNull(result.aDecimal);
                Assert.IsNull(result.aDouble);
                Assert.IsNull(result.aFloat);
                Assert.IsNull(result.aInet);
                Assert.IsNull(result.aLong);
                Assert.IsNull(result.aTimeUUID);
                Assert.IsNull(result.aUUID);
                Assert.IsNull(result.aBlob);
                Assert.IsNull(result.aList);
                Assert.IsNull(result.aSet);
                Assert.IsNull(result.aMap);
            }
        }
Beispiel #18
0
        public void SerializeObjectInOutDefaultsTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(
                aInt,
                aLong ,
                aVarint ,
                aTextString ,
                aVarcharString ,
                aASCIIString ,
                aBlob ,
                aBool ,
                aDecimal ,
                aDouble , 
                aFloat , 
                aTimestamp ,
                aTimeUUID ,
                aUUID ,
                aInet ,
                aList,
                aSet,
                aMap) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var values = new Types
                {
                    aInt = 2,
                    aASCIIString = default(string),
                    aBlob = default(byte[]),
                    aBool = default(bool),
                    aDecimal = default(decimal),
                    aDouble = default(double),
                    aFloat = default(float),
                    aInet = default(IPAddress),
                    aLong = default(long),
                    aTextString = default(string),
                    aVarcharString = default(string),
                    aTimeUUID = TimeGuid.Default,
                    aUUID = default(Guid),
                    aTimestamp = default(DateTime),
                    aVarint = default(BigInteger),
                    aList = default(List<string>),
                    aSet = default(HashSet<int>),
                    aMap = default(Dictionary<long, string>)
                };

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.Prepare();
                insertCmd.Parameters.Set(values);
                insertCmd.ExecuteNonQuery();

                var selectCmd = new CqlCommand(connection, selectCql);
                Types result = null;
                using(var reader = selectCmd.ExecuteReader<Types>())
                {
                    if(reader.Read())
                        result = reader.Current;
                }

                Assert.IsNotNull(result);

                Assert.AreEqual(result.aASCIIString, default(string));
                Assert.AreEqual(result.aVarcharString, default(string));
                Assert.AreEqual(result.aVarint, default(BigInteger));
                Assert.AreEqual(result.aTextString, default(string));
                Assert.AreEqual(result.aBool, default(bool));
                Assert.AreEqual(result.aDecimal, default(decimal));
                Assert.AreEqual(result.aDouble, default(double));
                Assert.AreEqual(result.aFloat, default(float));
                Assert.AreEqual(result.aInet, default(IPAddress));
                Assert.AreEqual(result.aLong, default(long));
                Assert.AreEqual(result.aTimeUUID, TimeGuid.Default);
                Assert.AreEqual(result.aUUID, default(Guid));
                Assert.AreEqual(result.aBlob, default(byte[]));
                Assert.AreEqual(result.aList, default(List<string>));
                Assert.AreEqual(result.aSet, default(HashSet<int>));
                Assert.AreEqual(result.aMap, default(Dictionary<long, string>));
            }
        }
Beispiel #19
0
        public void SerializeOutNullTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(aInt) values (1);";

            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                string aAsciiString;
                string aVarcharString;
                BigInteger? aVarint;
                string aTextString;
                bool? aBool;
                decimal? aDecimal;
                double? aDouble;
                float? aFloat;
                IPAddress aInet;
                long? aLong;
                Guid? aTimeUUID;
                Guid? aUUID;
                byte[] aBlob;
                List<string> aList;
                HashSet<int> aSet;
                Dictionary<long, string> aMap;

                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();

                var selectCmd = new CqlCommand(connection, selectCql);

                using(var reader = selectCmd.ExecuteReader())
                {
                    if(reader.Read())
                    {
                        aAsciiString = reader["aasciistring"] as string;
                        aVarcharString = reader["avarcharstring"] as string;
                        aVarint = reader["avarint"] as BigInteger?;
                        aTextString = reader["atextstring"] as string;
                        aBool = reader["abool"] as bool?;
                        aDecimal = reader["adecimal"] as decimal?;
                        aDouble = reader["adouble"] as double?;
                        aFloat = reader["afloat"] as float?;
                        aInet = reader["ainet"] as IPAddress;
                        aLong = reader["along"] as long?;
                        aTimeUUID = reader["atimeuuid"] as Guid?;
                        aUUID = reader["auuid"] as Guid?;
                        aBlob = reader["ablob"] as byte[];
                        aList = reader["alist"] as List<string>;
                        aSet = reader["aset"] as HashSet<int>;
                        aMap = reader["amap"] as Dictionary<long, string>;
                    }
                    else
                        throw new Exception("No row returned!");
                }

                Assert.AreEqual(aAsciiString, default(string));
                Assert.AreEqual(aVarcharString, default(string));
                Assert.AreEqual(aVarint, default(BigInteger?));
                Assert.AreEqual(aTextString, default(string));
                Assert.AreEqual(aBool, default(bool?));
                Assert.AreEqual(aDecimal, default(decimal?));
                Assert.AreEqual(aDouble, default(double?));
                Assert.AreEqual(aFloat, default(float?));
                Assert.AreEqual(aInet, default(IPAddress));
                Assert.AreEqual(aLong, default(long?));
                Assert.AreEqual(aTimeUUID, default(Guid?));
                Assert.AreEqual(aUUID, default(Guid?));
                Assert.AreEqual(aBlob, default(byte[]));
                Assert.AreEqual(aList, default(List<string>));
                Assert.AreEqual(aSet, default(HashSet<int>));
                Assert.AreEqual(aMap, default(Dictionary<long, string>));
            }
        }
Beispiel #20
0
        public void SerializeInOutTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(
                aInt,
                aLong ,
                aVarint ,
                aTextString ,
                aVarcharString ,
                aASCIIString ,
                aBlob ,
                aBool ,
                aDecimal ,
                aDouble  , 
                aFloat  , 
                aTimestamp ,
                aTimeUUID ,
                aUUID ,
                aInet ,
                aList,
                aSet,
                aMap) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();


                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.Prepare();

                insertCmd.Parameters["aasciistring"].Value = "hello world!";
                insertCmd.Parameters["ablob"].Value = new byte[] {1, 2, 3, 4};
                insertCmd.Parameters["abool"].Value = true;
                insertCmd.Parameters["adecimal"].Value = -1234567890.0987654321m;
                insertCmd.Parameters["adouble"].Value = 1.234;
                insertCmd.Parameters["afloat"].Value = 5.789f;
                insertCmd.Parameters["ainet"].Value = new IPAddress(new byte[] {127, 0, 0, 1});
                insertCmd.Parameters["aint"].Value = 10;
                insertCmd.Parameters["along"].Value = 56789012456;
                insertCmd.Parameters["atextstring"].Value = "some other text with \u005C unicode";
                insertCmd.Parameters["avarcharstring"].Value = "some other varchar with \u005C unicode";
                insertCmd.Parameters["atimeuuid"].Value = DateTime.Now.GenerateTimeBasedGuid();
                insertCmd.Parameters["auuid"].Value = Guid.NewGuid();
                insertCmd.Parameters["atimestamp"].Value = DateTime.Now;
                insertCmd.Parameters["avarint"].Value = new BigInteger(12345678901234);
                insertCmd.Parameters["alist"].Value = new List<string> {"string 1", "string 2"};
                insertCmd.Parameters["aset"].Value = new HashSet<int> {1, 3, 3};
                insertCmd.Parameters["amap"].Value =
                    new Dictionary<long, string> {{1, "value 1"}, {2, "value 2"}, {3, "value 3"}};

                insertCmd.ExecuteNonQuery();

                string aAsciiString;
                string aVarcharString;
                BigInteger aVarint;
                string aTextString;
                bool aBool;
                decimal aDecimal;
                double aDouble;
                float aFloat;
                IPAddress aInet;
                long aLong;
                Guid aTimeUUID;
                Guid aUUID;
                byte[] aBlob;
                List<string> aList;
                HashSet<int> aSet;
                Dictionary<long, string> aMap;

                var selectCmd = new CqlCommand(connection, selectCql);
                using(var reader = selectCmd.ExecuteReader())
                {
                    if(reader.Read())
                    {
                        aAsciiString = reader.GetString(reader.GetOrdinal("aasciistring"));
                        aVarcharString = reader.GetString(reader.GetOrdinal("avarcharstring"));
                        aVarint = reader.GetBigInteger(reader.GetOrdinal("avarint"));
                        aTextString = reader.GetString(reader.GetOrdinal("atextstring"));
                        aBool = reader.GetBoolean(reader.GetOrdinal("abool"));
                        aDecimal = reader.GetDecimal(reader.GetOrdinal("adecimal"));
                        aDouble = reader.GetDouble(reader.GetOrdinal("adouble"));
                        aFloat = reader.GetFloat(reader.GetOrdinal("afloat"));
                        aInet = reader.GetIPAddress(reader.GetOrdinal("ainet"));
                        aLong = reader.GetInt64(reader.GetOrdinal("along"));
                        aTimeUUID = reader.GetGuid(reader.GetOrdinal("atimeuuid"));
                        aUUID = reader.GetGuid(reader.GetOrdinal("auuid"));
                        aBlob = reader.GetBytes(reader.GetOrdinal("ablob"));
                        aList = reader.GetList<string>(reader.GetOrdinal("alist"));
                        aSet = reader.GetSet<int>(reader.GetOrdinal("aset"));
                        aMap = reader.GetDictionary<long, string>(reader.GetOrdinal("amap"));
                    }
                    else
                        throw new Exception("No row returned!");
                }

                Assert.AreEqual(insertCmd.Parameters["aasciistring"].Value, aAsciiString);
                Assert.AreEqual(insertCmd.Parameters["avarcharstring"].Value, aVarcharString);
                Assert.AreEqual(insertCmd.Parameters["avarint"].Value, aVarint);
                Assert.AreEqual(insertCmd.Parameters["atextstring"].Value, aTextString);
                Assert.AreEqual(insertCmd.Parameters["abool"].Value, aBool);
                Assert.AreEqual(insertCmd.Parameters["adecimal"].Value, aDecimal);
                Assert.AreEqual(insertCmd.Parameters["adouble"].Value, aDouble);
                Assert.AreEqual(insertCmd.Parameters["afloat"].Value, aFloat);
                Assert.AreEqual(insertCmd.Parameters["ainet"].Value, aInet);
                Assert.AreEqual(insertCmd.Parameters["along"].Value, aLong);
                Assert.AreEqual(insertCmd.Parameters["atimeuuid"].Value, aTimeUUID);
                Assert.AreEqual(insertCmd.Parameters["auuid"].Value, aUUID);
                Assert.IsTrue(((byte[])insertCmd.Parameters["ablob"].Value).SequenceEqual(aBlob));
                Assert.IsTrue(((List<string>)insertCmd.Parameters["alist"].Value).SequenceEqual(aList));
                Assert.IsTrue(((HashSet<int>)insertCmd.Parameters["aset"].Value).SequenceEqual(aSet));
                foreach(var entry in ((Dictionary<long, string>)insertCmd.Parameters["amap"].Value))
                {
                    var val = aMap[entry.Key];
                    Assert.AreEqual(entry.Value, val);
                }
            }
        }
Beispiel #21
0
        public void NullDeserializeParameterTest()
        {
            const string insertCql = @"insert into Test.Types(aInt) values (1);";

            const string selectCql = "select * from Test.Types limit 1;";



            using (var connection = new CqlConnection(ConnectionString))
            {
                string                    aASCIIString;
                string                    aVarcharString;
                BigInteger?               aVarint;
                string                    aTextString;
                bool?                     aBool;
                double?                   aDouble;
                float?                    aFloat;
                IPAddress                 aInet;
                long?                     aLong;
                Guid?                     aTimeUUID;
                Guid?                     aUUID;
                byte[]                    aBlob;
                List <string>             aList;
                HashSet <int>             aSet;
                Dictionary <long, string> aMap;

                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();

                var selectCmd = new CqlCommand(connection, selectCql);

                using (var reader = selectCmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        aASCIIString   = (string)reader["aasciistring"];
                        aVarcharString = (string)reader["avarcharstring"];
                        aVarint        = (BigInteger?)reader["avarint"];
                        aTextString    = (string)reader["atextstring"];
                        aBool          = (bool?)reader["abool"];
                        aDouble        = (double?)reader["adouble"];
                        aFloat         = (float?)reader["afloat"];
                        aInet          = (IPAddress)reader["ainet"];
                        aLong          = (long?)reader["along"];
                        aTimeUUID      = (Guid?)reader["atimeuuid"];
                        aUUID          = (Guid?)reader["auuid"];
                        aBlob          = (byte[])reader["ablob"];
                        aList          = (List <string>)reader["alist"];
                        aSet           = (HashSet <int>)reader["aset"];
                        aMap           = (Dictionary <long, string>)reader["amap"];
                    }
                    else
                    {
                        throw new Exception("No row returned!");
                    }
                }

                Assert.AreEqual(aASCIIString, default(string));
                Assert.AreEqual(aVarcharString, default(string));
                Assert.AreEqual(aVarint, default(BigInteger?));
                Assert.AreEqual(aTextString, default(string));
                Assert.AreEqual(aBool, default(bool?));
                Assert.AreEqual(aDouble, default(double?));
                Assert.AreEqual(aFloat, default(float?));
                Assert.AreEqual(aInet, default(IPAddress));
                Assert.AreEqual(aLong, default(long?));
                Assert.AreEqual(aTimeUUID, default(Guid?));
                Assert.AreEqual(aUUID, default(Guid?));
                Assert.AreEqual(aBlob, default(byte[]));
                Assert.AreEqual(aList, default(List <string>));
                Assert.AreEqual(aSet, default(HashSet <int>));
                Assert.AreEqual(aMap, default(Dictionary <long, string>));
            }
        }
Beispiel #22
0
        public void PrepareTest()
        {
            const string truncateTableCql = @"truncate Test.BasicFlow;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();
                var truncTable = new CqlCommand(connection, truncateTableCql);
                truncTable.ExecuteNonQuery();
            }
        }
Beispiel #23
0
        /// <summary>
        /// Creates the database.
        /// </summary>
        public static void CreateDatabase()
        {
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var existCommand = new CqlCommand(connection,
                                                  "select * from system.schema_keyspaces where keyspace_name='cqlsharptest';");
                using(var reader = existCommand.ExecuteReader())
                {
                    //check if database exists, if so, use it, otherwise create it
                    if(!reader.Read())
                    {
                        //create the keyspace 
                        var createKs = new CqlCommand(connection,
                                                      @"CREATE KEYSPACE cqlsharptest WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};");
                        createKs.ExecuteNonQuery();

                        //create the table
                        var createTable = new CqlCommand(connection,
                                                         @"CREATE TABLE cqlsharptest.measurements (id int PRIMARY KEY, customer text, values map<text, int>);");
                        createTable.ExecuteNonQuery();

                        //fill table with dummy data
                        var inserts = new List<Task>(25000);
                        for(int i = 0; i < 25000; i++)
                        {
                            //create and prepare the insert command
                            var insertCommand = new CqlCommand(connection,
                                                               "insert into cqlsharptest.measurements (id, customer, values) values (?,?,?)");
                            insertCommand.Prepare();

                            //create new measurement
                            var measurement = new Measurement
                            {
                                Id = i,
                                Customer = Customers[Random.Next(0, Customers.Count)],
                                Values = new Dictionary<string, int>
                                {
                                    {"Temp", Random.Next(0, 101)},
                                    {"Humidity", Random.Next(0, 101)},
                                    {"Clouds", Random.Next(0, 101)},
                                    {"Pressure", Random.Next(0, 101)},
                                    {"Rain", Random.Next(0, 101)},
                                    {"Sunshine", Random.Next(0, 101)},
                                    {"Overall", Random.Next(0, 101)}
                                }
                            };

                            //set insert parameters
                            insertCommand.Parameters.Set(measurement);

                            //add the insert as task to list
                            inserts.Add(insertCommand.ExecuteNonQueryAsync());
                        }

                        //wait for all inserts to complete
                        Task.WaitAll(inserts.ToArray());
                    }
                }
            }
        }
Beispiel #24
0
        public void SerializeTest()
        {
            const string insertCql = @"insert into Test.Types(
                aInt,
                aLong ,
                aVarint ,
                aTextString ,
                aVarcharString ,
                aASCIIString ,
                aBlob ,
                aBool ,
                aDouble  , 
                aFloat  , 
                aTimestamp ,
                aTimeUUID ,
                aUUID ,
                aInet ,
                aList,
                aSet,
                aMap) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            const string selectCql = "select * from Test.Types limit 1;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var values = new Types
                {
                    aASCIIString   = "hello world!",
                    aBlob          = new byte[] { 1, 2, 3, 4 },
                    aBool          = true,
                    aDouble        = 1.234,
                    aFloat         = 5.789f,
                    aInet          = new IPAddress(new byte[] { 127, 0, 0, 1 }),
                    aInt           = 10,
                    aLong          = 56789012456,
                    aTextString    = "some other text with \u005C unicode",
                    aVarcharString = "some other varchar with \u005C unicode",
                    aTimeUUID      = DateTime.Now.GenerateTimeBasedGuid(),
                    aUUID          = Guid.NewGuid(),
                    aTimestamp     = DateTime.Now,
                    aVarint        = new BigInteger(12345678901234),
                    aList          = new List <string> {
                        "string 1", "string 2"
                    },
                    aSet = new HashSet <int> {
                        1, 3, 3
                    },
                    aMap =
                        new Dictionary <long, string> {
                        { 1, "value 1" }, { 2, "value 2" }, { 3, "value 3" }
                    },
                };

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.Prepare();
                insertCmd.Parameters.Set(values);
                insertCmd.ExecuteNonQuery();

                var   selectCmd = new CqlCommand(connection, selectCql);
                Types result    = null;
                using (var reader = selectCmd.ExecuteReader <Types>())
                {
                    if (reader.Read())
                    {
                        result = reader.Current;
                    }
                }

                Assert.IsNotNull(result);

                Assert.AreEqual(result.aASCIIString, values.aASCIIString);
                Assert.AreEqual(result.aVarcharString, values.aVarcharString);
                Assert.AreEqual(result.aVarint, values.aVarint);
                Assert.AreEqual(result.aTextString, values.aTextString);
                Assert.AreEqual(result.aBool, values.aBool);
                Assert.AreEqual(result.aDouble, values.aDouble);
                Assert.AreEqual(result.aFloat, values.aFloat);
                Assert.AreEqual(result.aInet, values.aInet);
                Assert.AreEqual(result.aInt, values.aInt);
                Assert.AreEqual(result.aLong, values.aLong);
                Assert.AreEqual(result.aTimeUUID, values.aTimeUUID);
                Assert.AreEqual(result.aUUID, values.aUUID);
                Assert.IsTrue(result.aBlob.SequenceEqual(values.aBlob));
                Assert.IsTrue(result.aList.SequenceEqual(values.aList));
                Assert.IsTrue(result.aSet.SequenceEqual(values.aSet));
            }
        }
Beispiel #25
0
        public void NullableSerializeTest()
        {
            const string insertCql = @"insert into Test.Types(
                aInt,
                aLong ,
                aVarint ,
                aTextString ,
                aVarcharString ,
                aASCIIString ,
                aBlob ,
                aBool ,
                aDouble  , 
                aFloat  , 
                aTimestamp ,
                aTimeUUID ,
                aUUID ,
                aInet ,
                aList,
                aSet,
                aMap) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            const string selectCql = "select * from Test.Types limit 1;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var values = new NullableTypes
                {
                    aInt           = 3,
                    aASCIIString   = null,
                    aBlob          = null,
                    aBool          = null,
                    aDouble        = null,
                    aFloat         = null,
                    aInet          = null,
                    aLong          = null,
                    aTextString    = null,
                    aVarcharString = null,
                    aTimeUUID      = null,
                    aUUID          = null,
                    aTimestamp     = null,
                    aVarint        = null,
                    aList          = null,
                    aSet           = null,
                    aMap           = null
                };

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.Prepare();
                insertCmd.Parameters.Set(values);
                insertCmd.ExecuteNonQuery();

                var           selectCmd = new CqlCommand(connection, selectCql);
                NullableTypes result    = null;
                using (var reader = selectCmd.ExecuteReader <NullableTypes>())
                {
                    if (reader.Read())
                    {
                        result = reader.Current;
                    }
                }

                Assert.IsNotNull(result);

                Assert.IsNull(result.aASCIIString);
                Assert.IsNull(result.aVarcharString);
                Assert.IsNull(result.aVarint);
                Assert.IsNull(result.aTextString);
                Assert.IsNull(result.aBool);
                Assert.IsNull(result.aDouble);
                Assert.IsNull(result.aFloat);
                Assert.IsNull(result.aInet);
                Assert.IsNull(result.aLong);
                Assert.IsNull(result.aTimeUUID);
                Assert.IsNull(result.aUUID);
                Assert.IsNull(result.aBlob);
                Assert.IsNull(result.aList);
                Assert.IsNull(result.aSet);
                Assert.IsNull(result.aMap);
            }
        }
Beispiel #26
0
        public void DefaultSerializeTest()
        {
            const string insertCql = @"insert into Test.Types(
                aInt,
                aLong ,
                aVarint ,
                aTextString ,
                aVarcharString ,
                aASCIIString ,
                aBlob ,
                aBool ,
                aDouble  , 
                aFloat  , 
                aTimestamp ,
                aTimeUUID ,
                aUUID ,
                aInet ,
                aList,
                aSet,
                aMap) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            const string selectCql = "select * from Test.Types limit 1;";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var values = new Types
                {
                    aInt           = 2,
                    aASCIIString   = default(string),
                    aBlob          = default(byte[]),
                    aBool          = default(bool),
                    aDouble        = default(double),
                    aFloat         = default(float),
                    aInet          = default(IPAddress),
                    aLong          = default(long),
                    aTextString    = default(string),
                    aVarcharString = default(string),
                    aTimeUUID      = default(Guid),
                    aUUID          = default(Guid),
                    aTimestamp     = default(DateTime),
                    aVarint        = default(BigInteger),
                    aList          = default(List <string>),
                    aSet           = default(HashSet <int>),
                    aMap           = default(Dictionary <long, string>)
                };

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.Prepare();
                insertCmd.Parameters.Set(values);
                insertCmd.ExecuteNonQuery();

                var   selectCmd = new CqlCommand(connection, selectCql);
                Types result    = null;
                using (var reader = selectCmd.ExecuteReader <Types>())
                {
                    if (reader.Read())
                    {
                        result = reader.Current;
                    }
                }

                Assert.IsNotNull(result);

                Assert.AreEqual(result.aASCIIString, default(string));
                Assert.AreEqual(result.aVarcharString, default(string));
                Assert.AreEqual(result.aVarint, default(BigInteger));
                Assert.AreEqual(result.aTextString, default(string));
                Assert.AreEqual(result.aBool, default(bool));
                Assert.AreEqual(result.aDouble, default(double));
                Assert.AreEqual(result.aFloat, default(float));
                Assert.AreEqual(result.aInet, default(IPAddress));
                Assert.AreEqual(result.aLong, default(long));
                Assert.AreEqual(result.aTimeUUID, default(Guid));
                Assert.AreEqual(result.aUUID, default(Guid));
                Assert.AreEqual(result.aBlob, default(byte[]));
                Assert.AreEqual(result.aList, default(List <string>));
                Assert.AreEqual(result.aSet, default(HashSet <int>));
                Assert.AreEqual(result.aMap, default(Dictionary <long, string>));
            }
        }
Beispiel #27
0
        public static void Cleanup()
        {
            if (!Cassandra210OrUp)
                return;

            const string dropCql = @"drop keyspace TestUDT;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                try
                {
                    var drop = new CqlCommand(connection, dropCql);
                    drop.ExecuteNonQuery();
                }
                catch(InvalidException)
                {
                    //ignore
                }
            }

            CqlConnection.ShutdownAll();
        }
Beispiel #28
0
        public void TransactionAddAfterDisposeThrowsException()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (6000, 'Transaction 6000');";
            const string insertCql2 = @"insert into Test.BasicFlow (id,value) values (6001, 'Transaction 6001');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //skip if server version too low
                if(string.CompareOrdinal(connection.ServerVersion, "2.0.0") < 0)
                    throw new ObjectDisposedException("dummy"); //as expected for this test

                //create transaction
                var transaction = connection.BeginTransaction();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Transaction = transaction;
                cmd.ExecuteNonQuery();

                //commit
                transaction.Commit();

                //dispose
                transaction.Dispose();

                //add again
                var cmd2 = new CqlCommand(connection, insertCql2, CqlConsistency.One);
                cmd2.Transaction = transaction;
                cmd2.ExecuteNonQuery();
            }
        }
Beispiel #29
0
        public void SerializeObjectInOutTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(
                aInt,
                aLong ,
                aVarint ,
                aTextString ,
                aVarcharString ,
                aASCIIString ,
                aBlob ,
                aBool ,
                aDecimal ,
                aDouble  ,
                aFloat  , 
                aTimestamp ,
                aTimeUUID ,
                aUUID ,
                aInet ,
                aList,
                aSet,
                aMap) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var values = new Types
                {
                    aASCIIString = "hello world!",
                    aBlob = new byte[] {1, 2, 3, 4},
                    aBool = true,
                    aDecimal = decimal.MaxValue/2,
                    aDouble = 1.234,
                    aFloat = 5.789f,
                    aInet = new IPAddress(new byte[] {127, 0, 0, 1}),
                    aInt = 10,
                    aLong = 56789012456,
                    aTextString = "some other text with \u005C unicode",
                    aVarcharString = "some other varchar with \u005C unicode",
                    aTimeUUID = DateTime.Now.GenerateTimeBasedGuid(),
                    aUUID = Guid.NewGuid(),
                    aTimestamp = DateTime.Now,
                    aVarint = new BigInteger(12345678901234),
                    aList = new List<string> {"string 1", "string 2"},
                    aSet = new HashSet<int> {1, 3, 3},
                    aMap =
                        new Dictionary<long, string> {{1, "value 1"}, {2, "value 2"}, {3, "value 3"}},
                };

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.Prepare();
                insertCmd.Parameters.Set(values);
                insertCmd.ExecuteNonQuery();

                var selectCmd = new CqlCommand(connection, selectCql);
                Types result = null;
                using(var reader = selectCmd.ExecuteReader<Types>())
                {
                    if(reader.Read())
                        result = reader.Current;
                }

                Assert.IsNotNull(result);

                Assert.AreEqual(values.aASCIIString, result.aASCIIString);
                Assert.AreEqual(values.aVarcharString, result.aVarcharString);
                Assert.AreEqual(values.aVarint, result.aVarint);
                Assert.AreEqual(values.aTextString, result.aTextString);
                Assert.AreEqual(values.aBool, result.aBool);
                Assert.AreEqual(values.aDecimal, result.aDecimal);
                Assert.AreEqual(values.aDouble, result.aDouble);
                Assert.AreEqual(values.aFloat, result.aFloat);
                Assert.AreEqual(values.aInet, result.aInet);
                Assert.AreEqual(values.aInt, result.aInt);
                Assert.AreEqual(values.aLong, result.aLong);
                Assert.AreEqual(values.aTimeUUID, result.aTimeUUID);
                Assert.AreEqual(values.aUUID, result.aUUID);
                Assert.IsTrue(result.aBlob.SequenceEqual(values.aBlob));
                Assert.IsTrue(result.aList.SequenceEqual(values.aList));
                Assert.IsTrue(result.aSet.SequenceEqual(values.aSet));
            }
        }
Beispiel #30
0
        public void TransactionRollbackAfterCommitThrowsException()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (8000, 'Transaction 8000');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //skip if server version too low
                if(string.CompareOrdinal(connection.ServerVersion, "2.0.0") < 0)
                    throw new InvalidOperationException("as expected by test");

                //create transaction
                using(var transaction = connection.BeginTransaction())
                {
                    //insert data
                    var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                    cmd.Transaction = transaction;
                    cmd.ExecuteNonQuery();

                    //commit
                    transaction.Commit();

                    //rollback -> throws error
                    transaction.Rollback();
                }
            }
        }
Beispiel #31
0
        public void SerializeObjectOutDefaultsTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(aInt) values (1);";

            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();

                var selectCmd = new CqlCommand(connection, selectCql);
                Types result = null;
                using(var reader = selectCmd.ExecuteReader<Types>())
                {
                    if(reader.Read())
                        result = reader.Current;
                }

                Assert.IsNotNull(result);

                Assert.AreEqual(result.aASCIIString, default(string));
                Assert.AreEqual(result.aVarcharString, default(string));
                Assert.AreEqual(result.aVarint, default(BigInteger));
                Assert.AreEqual(result.aTextString, default(string));
                Assert.AreEqual(result.aBool, default(bool));
                Assert.AreEqual(result.aDecimal, default(decimal));
                Assert.AreEqual(result.aDouble, default(double));
                Assert.AreEqual(result.aFloat, default(float));
                Assert.AreEqual(result.aInet, default(IPAddress));
                Assert.AreEqual(result.aLong, default(long));
                Assert.AreEqual(result.aTimeUUID, default(Guid));
                Assert.AreEqual(result.aUUID, default(Guid));
                Assert.AreEqual(result.aBlob, default(byte[]));
                Assert.AreEqual(result.aList, default(List<string>));
                Assert.AreEqual(result.aSet, default(HashSet<int>));
                Assert.AreEqual(result.aMap, default(Dictionary<long, string>));
            }
        }
Beispiel #32
0
        public void TransactionCommitAfterRollbackThrowsException()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (8000, 'Transaction 8000');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //create transaction
                using(var transaction = connection.BeginTransaction())
                {
                    //insert data
                    var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                    cmd.Transaction = transaction;
                    cmd.ExecuteNonQuery();

                    //rollback
                    transaction.Rollback();

                    //commit -> throws error
                    transaction.Commit();
                }
            }
        }
Beispiel #33
0
        public void SerializeOutDefaultTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(aInt) values (1);";
            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();

                string aAsciiString;
                string aVarcharString;
                BigInteger aVarint;
                string aTextString;
                bool aBool;
                decimal aDecimal;
                double aDouble;
                float aFloat;
                IPAddress aInet;
                long aLong;
                Guid aTimeUUID;
                Guid aUUID;
                byte[] aBlob;
                List<string> aList;
                HashSet<int> aSet;
                Dictionary<long, string> aMap;

                var selectCmd = new CqlCommand(connection, selectCql);

                using(var reader = selectCmd.ExecuteReader())
                {
                    if(reader.Read())
                    {
                        aAsciiString = reader.GetString(reader.GetOrdinal("aasciistring"));
                        aVarcharString = reader.GetString(reader.GetOrdinal("avarcharstring"));
                        aVarint = reader.GetBigInteger(reader.GetOrdinal("avarint"));
                        aTextString = reader.GetString(reader.GetOrdinal("atextstring"));
                        aBool = reader.GetBoolean(reader.GetOrdinal("abool"));
                        aDecimal = reader.GetDecimal(reader.GetOrdinal("adecimal"));
                        aDouble = reader.GetDouble(reader.GetOrdinal("adouble"));
                        aFloat = reader.GetFloat(reader.GetOrdinal("afloat"));
                        aInet = reader.GetIPAddress(reader.GetOrdinal("ainet"));
                        aLong = reader.GetInt64(reader.GetOrdinal("along"));
                        aTimeUUID = reader.GetGuid(reader.GetOrdinal("atimeuuid"));
                        aUUID = reader.GetGuid(reader.GetOrdinal("auuid"));
                        aBlob = reader.GetBytes(reader.GetOrdinal("ablob"));
                        aList = reader.GetList<string>(reader.GetOrdinal("alist"));
                        aSet = reader.GetSet<int>(reader.GetOrdinal("aset"));
                        aMap = reader.GetDictionary<long, string>(reader.GetOrdinal("amap"));
                    }
                    else
                        throw new Exception("No row returned!");
                }

                Assert.AreEqual(aAsciiString, default(string));
                Assert.AreEqual(aVarcharString, default(string));
                Assert.AreEqual(aVarint, default(BigInteger));
                Assert.AreEqual(aTextString, default(string));
                Assert.AreEqual(aBool, default(bool));
                Assert.AreEqual(aDecimal, default(decimal));
                Assert.AreEqual(aDouble, default(double));
                Assert.AreEqual(aFloat, default(float));
                Assert.AreEqual(aInet, default(IPAddress));
                Assert.AreEqual(aLong, default(long));
                Assert.AreEqual(aTimeUUID, default(Guid));
                Assert.AreEqual(aUUID, default(Guid));
                Assert.AreEqual(aBlob, default(byte[]));
                Assert.AreEqual(aList, default(List<string>));
                Assert.AreEqual(aSet, default(HashSet<int>));
                Assert.AreEqual(aMap, default(Dictionary<long, string>));
            }
        }
Beispiel #34
0
        public void BasicInsertSelectSynchronous()
        {
            //Assume
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (12367,'Hallo 12367');";
            const string retrieveCql = @"select * from Test.BasicFlow;";

            //Act
            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.ExecuteNonQuery();

                //select data
                var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One);
                selectCmd.CommandTimeout = Timeout.Infinite;
                selectCmd.Prepare();

                CqlDataReader reader = selectCmd.ExecuteReader();
                Assert.AreEqual(1, reader.Count);
                if (reader.Read())
                {
                    Assert.AreEqual(12367, reader["id"]);
                    Assert.AreEqual("Hallo 12367", reader["value"]);
                    Assert.AreEqual(DBNull.Value, reader["ignored"]);
                }
                else
                {
                    Assert.Fail("Read should have succeeded");
                }
            }
        }
Beispiel #35
0
        public void InsertWithTimestamp2()
        {
            if (!Cassandra210OrUp)
                return;

            using (var connection = new CqlConnection(ConnectionString))
            {
               connection.Open();

               var command = new CqlCommand(connection, "insert into testudt.members (id, comment) values (3,('hi','there3'));");
               command.ExecuteNonQuery();

                var select = new CqlCommand(connection, "select WRITETIME(comment) from testudt.members where id=3;");
                using (var reader = select.ExecuteReader())
                {
                    Assert.AreEqual(1, reader.Count);
                    if (reader.Read())
                    {
                        DateTime writeTime = ((long)(reader.GetDouble(0)/1000)).ToDateTime();
                        Assert.IsTrue(writeTime - DateTime.UtcNow < TimeSpan.FromSeconds(1));
                    }
                    else
                    {
                        Assert.Fail("Read failed.");
                    }
                }
            }
        }
Beispiel #36
0
        public void BasicInsertSelectSynchronousTimeout()
        {
            //Assume
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (13244,'Hallo 54235');";
            const string retrieveCql = @"select * from Test.BasicFlow;";

            //Act
            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.ExecuteNonQuery();


                //select data
                var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One);
                selectCmd.CommandTimeout = Timeout.Infinite;
                selectCmd.Prepare();

                //get connection, and cancel the select as soon as it registers
                EventHandler<Network.LoadChangeEvent> delayHandler = (src, ev) => Thread.Sleep(1200);
                Connection networkConnection = connection.GetConnection();
                networkConnection.OnLoadChange += delayHandler;

                try
                {
                    //set command timeout as low as possible (1 second)
                    selectCmd.CommandTimeout = 1;
                    CqlDataReader reader = selectCmd.ExecuteReader();
                }
                finally
                {
                    networkConnection.OnLoadChange -= delayHandler;
                }
               
            }
        }
Beispiel #37
0
        public void SerializeObjectOutNullTest()
        {
            const string insertCql = @"insert into SerializationTest.Types(aInt) values (4);";

            const string selectCql = "select * from SerializationTest.Types limit 1;";

            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();

                var selectCmd = new CqlCommand(connection, selectCql);
                NullableTypes result = null;
                using(var reader = selectCmd.ExecuteReader<NullableTypes>())
                {
                    if(reader.Read())
                        result = reader.Current;
                }

                Assert.IsNotNull(result);

                Assert.IsNull(result.aASCIIString);
                Assert.IsNull(result.aVarcharString);
                Assert.IsNull(result.aVarint);
                Assert.IsNull(result.aTextString);
                Assert.IsNull(result.aBool);
                Assert.IsNull(result.aDecimal);
                Assert.IsNull(result.aDouble);
                Assert.IsNull(result.aFloat);
                Assert.IsNull(result.aInet);
                Assert.IsNull(result.aLong);
                Assert.IsNull(result.aTimeUUID);
                Assert.IsNull(result.aUUID);
                Assert.IsNull(result.aBlob);
                Assert.IsNull(result.aList);
                Assert.IsNull(result.aSet);
                Assert.IsNull(result.aMap);
            }
        }
Beispiel #38
0
        public void SelectWithPaging()
        {
            //Assume
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);";
            const string retrieveCql = @"select * from Test.BasicFlow;";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Prepare();

                for(int i = 0; i < 100; i++)
                {
                    cmd.Parameters[0].Value = i;
                    cmd.Parameters[1].Value = "Hello " + i;
                    cmd.ExecuteNonQuery();
                }

                //select data
                var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One);
                selectCmd.PageSize = 10;

                using(var reader = selectCmd.ExecuteReader())
                {
                    //no paging when version < 2.0.0 is used...
                    var expectedCount = String.Compare(connection.ServerVersion, "2.0.0", StringComparison.Ordinal) < 0
                                            ? 100
                                            : 10;

                    Assert.AreEqual(expectedCount, reader.Count);

                    var results = new bool[100];
                    for(int i = 0; i < 100; i++)
                    {
                        if(reader.Read())
                        {
                            results[(int)reader["id"]] = true;
                            Assert.AreEqual("Hello " + reader["id"], reader["value"]);
                        }
                        else
                            Assert.Fail("Read should have succeeded");
                        }
                    Assert.IsFalse(reader.Read());
                    Assert.IsTrue(results.All(p => p));
                }
            }
        }
Beispiel #39
0
        public void TransactionResetAfterDispose()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (7000, 'Transaction 7000');";
            const string insertCql2 = @"insert into Test.BasicFlow (id,value) values (7001, 'Transaction 7001');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //skip if server version too low
                if(string.CompareOrdinal(connection.ServerVersion, "2.0.0") < 0)
                    return;

                //create transaction
                var transaction = connection.BeginTransaction();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Transaction = transaction;
                cmd.ExecuteNonQuery();

                //commit
                transaction.Commit();

                //dispose
                transaction.Dispose();

                //reset transaction to empty state, such that it can be reused
                transaction.Reset();

                //add again
                var cmd2 = new CqlCommand(connection, insertCql2, CqlConsistency.One);
                cmd2.Transaction = transaction;
                cmd2.ExecuteNonQuery();

                transaction.Commit();

                transaction.Dispose();
            }
        }
Beispiel #40
0
        public void SerializeObjectOutDefaultsTest()
        {
            const string insertCql = @"insert into LinqTest.Types(aInt) values (1);";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                var insertCmd = new CqlCommand(connection, insertCql);
                insertCmd.ExecuteNonQuery();
            }

            using (var context = new SerializationContext())
            {
                var result = context.Types.FirstOrDefault();

                Assert.IsNotNull(result);

                Assert.AreEqual(result.aASCIIString, default(string));
                Assert.AreEqual(result.aVarcharString, default(string));
                Assert.AreEqual(result.aVarint, default(BigInteger));
                Assert.AreEqual(result.aTextString, default(string));
                Assert.AreEqual(result.aBool, default(bool));
                Assert.AreEqual(result.aDecimal, default(decimal));
                Assert.AreEqual(result.aDouble, default(double));
                Assert.AreEqual(result.aFloat, default(float));
                Assert.AreEqual(result.aInet, default(IPAddress));
                Assert.AreEqual(result.aLong, default(long));
                Assert.AreEqual(result.aTimeUUID, default(Guid));
                Assert.AreEqual(result.aUUID, default(Guid));
                Assert.AreEqual(result.aBlob, default(byte[]));
                Assert.AreEqual(result.aList, default(List<string>));
                Assert.AreEqual(result.aSet, default(HashSet<int>));
                Assert.AreEqual(result.aMap, default(Dictionary<long, string>));

            }
        }