public TEntity EndExecute(IAsyncResult ar) { var rs = InternalEndExecute(ar); Row row = rs.GetRows().FirstOrDefault(); if (row == null) { if (((MethodCallExpression)Expression).Method.Name == "First") { throw new InvalidOperationException("Sequence contains no elements."); } else if (((MethodCallExpression)Expression).Method.Name == "FirstOrDefault") { return(default(TEntity)); } } CqlColumn[] cols = rs.Columns; var colToIdx = new Dictionary <string, int>(); for (int idx = 0; idx < cols.Length; idx++) { colToIdx.Add(cols[idx].Name, idx); } var tag = (CqlQueryTag)Session.GetTag(ar); return(CqlQueryTools.GetRowFromCqlRow <TEntity>(row, colToIdx, tag.Mappings, tag.Alter)); }
protected override string GetCql(out object[] values) { bool withValues = GetTable().GetSession().BinaryProtocolVersion > 1; return(CqlQueryTools.GetInsertCQLAndValues(_entity, (GetTable()).GetQuotedTableName(), out values, _ttl, _timestamp, _ifNotExists, withValues)); }
internal IEnumerable <TEntity> AdaptRows(IEnumerable <Row> rows, Dictionary <string, int> colToIdx, CqlExpressionVisitor visitor) { foreach (Row row in rows) { yield return(CqlQueryTools.GetRowFromCqlRow <TEntity>(row, colToIdx, visitor.Mappings, visitor.Alter)); } }
public void Create() { List <string> cqls = CqlQueryTools.GetCreateCQL(this, false); foreach (string cql in cqls) { _session.WaitForSchemaAgreement(_session.Execute(cql)); } }
public void Create() { var cqls = CqlQueryTools.GetCreateCQL(this); foreach (var cql in cqls) { _session.WaitForSchemaAgreement(_session.Execute(cql)); } }
public void Create(ConsistencyLevel consictencyLevel = ConsistencyLevel.Default) { var cqls = CqlQueryTools.GetCreateCQL(this); foreach (var cql in cqls) { _session.Cluster.WaitForSchemaAgreement(_session.Execute(cql, consictencyLevel).QueriedHost); } }
public bool AppendChangesToBatch(StringBuilder batchScript, string tablename) { bool enableTracing = false; foreach (var kv in _table) { if (!CqlEqualityComparer <TEntity> .Default.Equals(kv.Key, kv.Value.Entity)) { throw new InvalidOperationException(); } if (kv.Value.QueryTracingEnabled) { enableTracing = true; } var cql = ""; if (kv.Value.MutationType == MutationType.Add) { cql = CqlQueryTools.GetInsertCQL(kv.Value.Entity, tablename); } else if (kv.Value.MutationType == MutationType.Delete) { cql = CqlQueryTools.GetDeleteCQL(kv.Value.Entity, tablename); } else if (kv.Value.MutationType == MutationType.None) { if (kv.Value.CqlEntityUpdateMode == EntityUpdateMode.AllOrNone) { cql = CqlQueryTools.GetUpdateCQL(kv.Key, kv.Value.Entity, tablename, true); } else { cql = CqlQueryTools.GetUpdateCQL(kv.Key, kv.Value.Entity, tablename, false); } } else { continue; } if (cql != null) { batchScript.AppendLine(cql + ";"); } } return(enableTracing); }
protected override Expression VisitConstant(ConstantExpression node) { if (node.Value is ITable) { var table = (node.Value as ITable); QuotedTableName = table.GetQuotedTableName(); AllowFiltering = table.GetEntityType().GetCustomAttributes(typeof(AllowFilteringAttribute), false).Any(); var props = table.GetEntityType().GetPropertiesOrFields(); foreach (var prop in props) { var memName = CqlQueryTools.CalculateMemberName(prop); Alter[prop.Name] = memName; } return(node); } else if (phasePhase.get() == ParsePhase.Condition) { WhereClause.Append(node.Value.Encode()); return(node); } else if (phasePhase.get() == ParsePhase.SelectBinding) { if (Alter.ContainsKey(currentBindingName.get())) { Mappings[currentBindingName.get()] = Tuple.Create(currentBindingName.get(), node.Value, Mappings.Count); SelectFields.Add(currentBindingName.get()); } else { Mappings[currentBindingName.get()] = Tuple.Create <string, object, int>(null, node.Value, Mappings.Count); } return(node); } else if (phasePhase.get() == ParsePhase.Take) { Limit = (int)node.Value; return(node); } else if (phasePhase.get() == ParsePhase.OrderBy || phasePhase.get() == ParsePhase.OrderByDescending) { OrderBy.Add(Alter[(string)node.Value].QuoteIdentifier() + (phasePhase.get() == ParsePhase.OrderBy ? " ASC" : " DESC")); return(node); } throw new CqlLinqNotSupportedException(node, phasePhase.get()); }
public IEnumerable <TEntity> EndExecute(IAsyncResult ar) { using (var outp = InternalEndExecute(ar)) { QueryTrace = outp.Info.QueryTrace; var cols = outp.Columns; var colToIdx = new Dictionary <string, int>(); for (int idx = 0; idx < cols.Length; idx++) { colToIdx.Add(cols[idx].Name, idx); } var rows = outp.GetRows(); var tag = (CqlQueryTag)Session.GetTag(ar); foreach (var row in rows) { yield return(CqlQueryTools.GetRowFromCqlRow <TEntity>(row, colToIdx, tag.Mappings, tag.Alter)); } } }
public void CreateIfNotExists() { if (_session.BinaryProtocolVersion > 1) { List <string> cqls = CqlQueryTools.GetCreateCQL(this, true); foreach (string cql in cqls) { _session.WaitForSchemaAgreement(_session.Execute(cql)); } } else { try { Create(); } catch (AlreadyExistsException) { //do nothing } } }
public override string ToString() { object[] _; return(CqlQueryTools.GetInsertCQLAndValues(_entity, (GetTable()).GetQuotedTableName(), out _, _ttl, _timestamp, _ifNotExists, false)); }
protected override string GetCql() { return(CqlQueryTools.GetInsertCQL(_entity, (GetTable()).GetQuotedTableName(), _ttl, _timestamp)); }
public void SaveChangesOneByOne(Context context, string tablename, ConsistencyLevel consistencyLevel) { var commitActions = new List <Action>(); try { foreach (var kv in _table) { if (!CqlEqualityComparer <TEntity> .Default.Equals(kv.Key, kv.Value.Entity)) { throw new InvalidOperationException(); } var cql = ""; if (kv.Value.MutationType == MutationType.Add) { cql = CqlQueryTools.GetInsertCQL(kv.Value.Entity, tablename); } else if (kv.Value.MutationType == MutationType.Delete) { cql = CqlQueryTools.GetDeleteCQL(kv.Value.Entity, tablename); } else if (kv.Value.MutationType == MutationType.None) { cql = CqlQueryTools.GetUpdateCQL(kv.Key, kv.Value.Entity, tablename, kv.Value.CqlEntityUpdateMode == EntityUpdateMode.AllOrNone); } else { continue; } QueryTrace trace = null; if (cql != null) // null if nothing to update { var res = context.ExecuteWriteQuery(cql, consistencyLevel, kv.Value.QueryTracingEnabled); if (kv.Value.QueryTracingEnabled) { trace = res.Info.QueryTrace; } } var nkv = kv; commitActions.Add(() => { if (nkv.Value.QueryTracingEnabled) { if (trace != null) { lock (_traces) _traces[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 }); } }); } } finally { foreach (var act in commitActions) { act(); } } }
protected override string GetCql() { return(CqlQueryTools.GetInsertCQL(_entity, (GetTable()).GetTableName())); }