public void insertingSingleValue(Type tp)
        {
            string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(tp);
            string tableName             = "table" + Guid.NewGuid().ToString("N").ToLower();

            try
            {
                Session.WaitForSchemaAgreement(
                    QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}(
         tweet_id uuid PRIMARY KEY,
         value {1}
         );", tableName, cassandraDataTypeName)));
            }
            catch (AlreadyExistsException)
            {
            }

            var    toInsert = new List <object[]>(1);
            object val      = Randomm.RandomVal(tp);

            if (tp == typeof(string))
            {
                val = "'" + val.ToString().Replace("'", "''") + "'";
            }
            var row1 = new object[2] {
                Guid.NewGuid(), val
            };

            toInsert.Add(row1);

            bool isFloatingPoint = false;

            if (row1[1].GetType() == typeof(string) || row1[1].GetType() == typeof(byte[]))
            {
                QueryTools.ExecuteSyncNonQuery(Session,
                                               string.Format("INSERT INTO {0}(tweet_id,value) VALUES ({1}, {2});", tableName, toInsert[0][0],
                                                             row1[1].GetType() == typeof(byte[])
                                                                 ? "0x" + CqlQueryTools.ToHex((byte[])toInsert[0][1])
                                                                 : "'" + toInsert[0][1] + "'"), null);
            }
            // rndm.GetType().GetMethod("Next" + tp.Name).Invoke(rndm, new object[] { })
            else
            {
                if (tp == typeof(Single) || tp == typeof(Double))
                {
                    isFloatingPoint = true;
                }
                QueryTools.ExecuteSyncNonQuery(Session,
                                               string.Format("INSERT INTO {0}(tweet_id,value) VALUES ({1}, {2});", tableName, toInsert[0][0],
                                                             !isFloatingPoint
                                                                 ? toInsert[0][1]
                                                                 : toInsert[0][1].GetType()
                                                             .GetMethod("ToString", new[] { typeof(string) })
                                                             .Invoke(toInsert[0][1], new object[] { "r" })), null);
            }

            QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName),
                                        Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel(), toInsert);
            QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName));
        }
Beispiel #2
0
        public void InsertingSingleValue(Type tp)
        {
            var tableName = TestUtils.GetUniqueTableName();

            var toInsert = new List <object[]>(1);
            var val      = Randomm.RandomVal(tp);

            if (tp == typeof(string))
            {
                val = "'" + val.ToString().Replace("'", "''") + "'";
            }

            var row1 = new object[2] {
                Guid.NewGuid(), val
            };

            toInsert.Add(row1);

            var isFloatingPoint = false;

            if (row1[1].GetType() == typeof(string) || row1[1].GetType() == typeof(byte[]))
            {
                var query =
                    $"INSERT INTO {tableName}(tweet_id,value) VALUES (" +
                    $"{toInsert[0][0]}, " +
                    $"{(row1[1].GetType() == typeof(byte[]) ? "0x" + CqlQueryTools.ToHex((byte[]) toInsert[0][1]) : "'" + toInsert[0][1] + "'")}" +
                    ");";
                QueryTools.ExecuteSyncNonQuery(Session, query, null);
                VerifyStatement(QueryType.Query, query, 1);
            }
            else
            {
                if (tp == typeof(Single) || tp == typeof(Double))
                {
                    isFloatingPoint = true;
                }

                var query =
                    $"INSERT INTO {tableName}(tweet_id,value) VALUES (" +
                    $"{toInsert[0][0]}, " +
                    $"{(!isFloatingPoint ? toInsert[0][1] : toInsert[0][1].GetType().GetMethod("ToString", new[] { typeof(string) }).Invoke(toInsert[0][1], new object[] { "r" }))}" +
                    ");";
                QueryTools.ExecuteSyncNonQuery(Session, query, null);
                VerifyStatement(QueryType.Query, query, 1);
            }

            TestCluster.PrimeFluent(
                b => b.WhenQuery($"SELECT * FROM {tableName};", when => when.WithConsistency(ConsistencyLevel.LocalOne))
                .ThenRowsSuccess(new[] { "tweet_id", "value" }, r => r.WithRow(toInsert[0][0], toInsert[0][1])));

            QueryTools.ExecuteSyncQuery(
                Session, $"SELECT * FROM {tableName};", Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel(), toInsert);
        }
        public bool AppendChangesToBatch(StringBuilder batchScript, string tablename)
        {
            bool enableTracing = false;

            foreach (KeyValuePair <TEntity, TableEntry> kv in _table)
            {
                if (!CqlEqualityComparer <TEntity> .Default.Equals(kv.Key, kv.Value.Entity))
                {
                    throw new InvalidOperationException();
                }

                if (kv.Value.QueryTracingEnabled)
                {
                    enableTracing = true;
                }

                object[] values;
                string   cql = "";
                if (kv.Value.MutationType == MutationType.Add)
                {
                    cql = CqlQueryTools.GetInsertCQLAndValues(kv.Value.Entity, tablename, out values, null, null, false, false);
                }
                else if (kv.Value.MutationType == MutationType.Delete)
                {
                    cql = CqlQueryTools.GetDeleteCQLAndValues(kv.Value.Entity, tablename, out values, false);
                }
                else if (kv.Value.MutationType == MutationType.None)
                {
                    if (kv.Value.CqlEntityUpdateMode == EntityUpdateMode.AllOrNone)
                    {
                        cql = CqlQueryTools.GetUpdateCQLAndValues(kv.Key, kv.Value.Entity, tablename, out values, true, false);
                    }
                    else
                    {
                        cql = CqlQueryTools.GetUpdateCQLAndValues(kv.Key, kv.Value.Entity, tablename, out values, false, false);
                    }
                }
                else
                {
                    continue;
                }
                if (cql != null)
                {
                    batchScript.AppendLine(cql + ";");
                }
            }
            return(enableTracing);
        }
        // Test a wide row consisting of a ByteBuffer
        private static void TestByteRows(ISession session, string tableName)
        {
            session.Execute(String.Format("CREATE TABLE {0} (k INT, i {1}, PRIMARY KEY(k,i))", tableName, "BLOB"));

            // Build small ByteBuffer sample
            var bw = new FrameWriter(new MemoryStream(), new Serializer(ProtocolVersion.V1));

            for (int i = 0; i < 56; i++)
            {
                bw.WriteByte(0);
            }
            bw.WriteUInt16(0xCAFE);
            var bb = new byte[58];

            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 1024; ++i)
            {
                session.Execute(string.Format("INSERT INTO {0}(k,i) values({1},0x{2})", tableName, Key, CqlQueryTools.ToHex(bb)),
                                ConsistencyLevel.Quorum);
            }

            // Read data
            var rs = session.Execute("SELECT i FROM " + tableName + " WHERE k = " + Key, ConsistencyLevel.Quorum);

            // Verify data
            foreach (var row in rs)
            {
                Assert.AreEqual((byte[])row["i"], bb);
            }
        }
        public void SaveChangesOneByOne(Context context, string tablename, ConsistencyLevel consistencyLevel)
        {
            var commitActions = new List <Action>();

            try
            {
                foreach (KeyValuePair <TEntity, TableEntry> kv in _table)
                {
                    if (!CqlEqualityComparer <TEntity> .Default.Equals(kv.Key, kv.Value.Entity))
                    {
                        throw new InvalidOperationException();
                    }
                    string   cql = "";
                    object[] values;
                    if (kv.Value.MutationType == MutationType.Add)
                    {
                        cql = CqlQueryTools.GetInsertCQLAndValues(kv.Value.Entity, tablename, out values, null, null, false);
                    }
                    else if (kv.Value.MutationType == MutationType.Delete)
                    {
                        cql = CqlQueryTools.GetDeleteCQLAndValues(kv.Value.Entity, tablename, out values);
                    }
                    else if (kv.Value.MutationType == MutationType.None)
                    {
                        cql = CqlQueryTools.GetUpdateCQLAndValues(kv.Key, kv.Value.Entity, tablename, out values,
                                                                  kv.Value.CqlEntityUpdateMode == EntityUpdateMode.AllOrNone);
                    }
                    else
                    {
                        continue;
                    }

                    QueryTrace trace = null;
                    if (cql != null) // null if nothing to update
                    {
                        RowSet res = context.ExecuteWriteQuery(cql, values, consistencyLevel, kv.Value.QueryTracingEnabled);
                        if (kv.Value.QueryTracingEnabled)
                        {
                            trace = res.Info.QueryTrace;
                        }
                    }

                    KeyValuePair <TEntity, TableEntry> nkv = kv;
                    commitActions.Add(() =>
                    {
                        if (nkv.Value.QueryTracingEnabled)
                        {
                            if (trace != null)
                            {
                                _traces.TryAdd(nkv.Key, trace);
                            }
                        }
                        _table.Remove(nkv.Key);
                        if (nkv.Value.MutationType != MutationType.Delete && nkv.Value.CqlEntityTrackingMode != EntityTrackingMode.DetachAfterSave)
                        {
                            _table.Add(Clone(nkv.Value.Entity),
                                       new TableEntry
                            {
                                Entity                = nkv.Value.Entity,
                                MutationType          = MutationType.None,
                                CqlEntityUpdateMode   = nkv.Value.CqlEntityUpdateMode,
                                CqlEntityTrackingMode = nkv.Value.CqlEntityTrackingMode
                            });
                        }
                    });
                }
            }
            finally
            {
                foreach (Action act in commitActions)
                {
                    act();
                }
            }
        }
Beispiel #6
0
        public void InsertingSingleValue(Type tp)
        {
            var cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(tp);
            var tableName             = TestUtils.GetUniqueTableName();

            try
            {
                var query = $@"CREATE TABLE {tableName}(tweet_id uuid PRIMARY KEY, value {cassandraDataTypeName});";
                QueryTools.ExecuteSyncNonQuery(Session, query);
            }
            catch (AlreadyExistsException)
            {
            }

            var toInsert = new List <object[]>(1);
            var val      = Randomm.RandomVal(tp);

            if (tp == typeof(string))
            {
                val = "'" + val.ToString().Replace("'", "''") + "'";
            }
            var row1 = new object[2] {
                Guid.NewGuid(), val
            };

            toInsert.Add(row1);

            var isFloatingPoint = false;

            if (row1[1].GetType() == typeof(string) || row1[1].GetType() == typeof(byte[]))
            {
                QueryTools.ExecuteSyncNonQuery(Session,
                                               $"INSERT INTO {tableName}(tweet_id,value) VALUES ({toInsert[0][0]}, {(row1[1].GetType() == typeof(byte[]) ? "0x" + CqlQueryTools.ToHex((byte[]) toInsert[0][1]) : "'" + toInsert[0][1] + "'")});", null);
            }
            else
            {
                if (tp == typeof(Single) || tp == typeof(Double))
                {
                    isFloatingPoint = true;
                }
                QueryTools.ExecuteSyncNonQuery(Session,
                                               $"INSERT INTO {tableName}(tweet_id,value) VALUES ({toInsert[0][0]}, {(!isFloatingPoint ? toInsert[0][1] : toInsert[0][1].GetType().GetMethod("ToString", new[] {typeof(string)}).Invoke(toInsert[0][1], new object[] {"r"}))});", null);
            }

            QueryTools.ExecuteSyncQuery(Session, $"SELECT * FROM {tableName};",
                                        Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel(), toInsert);
        }
Beispiel #7
0
        /*
         * Test a wide row consisting of a ByteBuffer
         */

        private void testByteRows()
        {
            // Build small ByteBuffer sample
            var bw = new BEBinaryWriter();

            for (int i = 0; i < 56; i++)
            {
                bw.WriteByte(0);
            }
            bw.WriteUInt16(0xCAFE);
            var bb = new byte[58];

            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 1024; ++i)
            {
                Session.Execute(string.Format("INSERT INTO wide_byte_rows(k,i) values({0},0x{1})", key, CqlQueryTools.ToHex(bb)),
                                ConsistencyLevel.Quorum);
            }

            // Read data
            var rs = Session.Execute("SELECT i FROM wide_byte_rows WHERE k = " + key, ConsistencyLevel.Quorum);

            // Verify data
            foreach (var row in rs)
            {
                Assert.AreEqual((byte[])row["i"], bb);
            }
        }