public string GetSelect(out object[] values, bool withValues = true) { var sb = new StringBuilder(); sb.Append("SELECT "); sb.Append(SelectFields.Count == 0 ? "*" : string.Join(", ", from f in SelectFields select Alter[f].QuoteIdentifier())); sb.Append(" FROM "); sb.Append(QuotedTableName); if (WhereClause.Length > 0) { sb.Append(" WHERE "); sb.Append(WhereClause); } if (OrderBy.Count > 0) { sb.Append(" ORDER BY "); sb.Append(string.Join(", ", OrderBy)); } if (Limit > 0) { sb.Append(" LIMIT "); sb.Append(Limit); } if (AllowFiltering) { sb.Append(" ALLOW FILTERING"); } if (withValues) { return(cqlTool.FillWithValues(sb.ToString(), out values)); } values = null; return(cqlTool.FillWithEncoded(sb.ToString())); }
public static string GetDeleteCQLAndValues(object row, string quotedtablename, out object[] values, bool withValues = true) { var cqlTool = new CqlStringTool(); Type rowType = row.GetType(); var sb = new StringBuilder(); sb.Append("DELETE FROM "); sb.Append(quotedtablename); sb.Append(" WHERE "); List <MemberInfo> props = rowType.GetPropertiesOrFields(); bool first = true; foreach (MemberInfo prop in props) { var pk = prop.GetCustomAttributes(typeof(PartitionKeyAttribute), true).FirstOrDefault() as PartitionKeyAttribute; if (pk == null) { var rk = prop.GetCustomAttributes(typeof(ClusteringKeyAttribute), true).FirstOrDefault() as ClusteringKeyAttribute; if (rk == null) { continue; } } object pv = prop.GetValueFromPropertyOrField(row); if (pv != null) { if (first) { first = false; } else { sb.Append(" AND "); } string memName = CalculateMemberName(prop); sb.Append(memName.QuoteIdentifier()); sb.Append(" = " + cqlTool.AddValue(pv) + " "); } } if (withValues) { return(cqlTool.FillWithValues(sb.ToString(), out values)); } else { values = null; return(cqlTool.FillWithEncoded(sb.ToString())); } }
public static string GetUpdateCQLAndValues(object row, object newRow, string quotedtablename, out object[] values, bool all = false, bool withValues = true) { var cqlTool = new CqlStringTool(); Type rowType = row.GetType(); var set = new StringBuilder(); var where = new StringBuilder(); List <MemberInfo> props = rowType.GetPropertiesOrFields(); bool firstSet = true; bool firstWhere = true; bool changeDetected = false; foreach (MemberInfo prop in props) { string memName = CalculateMemberName(prop); var pk = prop.GetCustomAttributes(typeof(PartitionKeyAttribute), true).FirstOrDefault() as PartitionKeyAttribute; if (pk == null) { var rk = prop.GetCustomAttributes(typeof(ClusteringKeyAttribute), true).FirstOrDefault() as ClusteringKeyAttribute; if (rk == null) { var counter = prop.GetCustomAttributes(typeof(CounterAttribute), true).FirstOrDefault() as CounterAttribute; if (counter != null) { long diff = (Int64)prop.GetValueFromPropertyOrField(newRow) - (Int64)prop.GetValueFromPropertyOrField(row); if (diff != 0 || (Int64)prop.GetValueFromPropertyOrField(newRow) == 0) { changeDetected = true; if (firstSet) { firstSet = false; } else { set.Append(", "); } set.Append(memName.QuoteIdentifier() + " = " + memName.QuoteIdentifier()); set.Append((diff >= 0) ? "+" + diff : diff.ToString(CultureInfo.InvariantCulture)); } continue; } else { object newVal = prop.GetValueFromPropertyOrField(newRow); if (newVal != null) { bool areDifferent = !newVal.Equals(prop.GetValueFromPropertyOrField(row)); if (all || (areDifferent)) { if (areDifferent) { changeDetected = true; } if (firstSet) { firstSet = false; } else { set.Append(", "); } set.Append(memName.QuoteIdentifier()); set.Append(" = " + cqlTool.AddValue(newVal) + " "); } } else { changeDetected = true; if (firstSet) { firstSet = false; } else { set.Append(", "); } set.Append(memName.QuoteIdentifier()); set.Append(" = NULL "); } continue; } } } object pv = prop.GetValueFromPropertyOrField(row); if (pv != null) { if (firstWhere) { firstWhere = false; } else { where.Append(" AND "); } where.Append(memName.QuoteIdentifier()); where.Append(" = " + cqlTool.AddValue(pv) + " "); } } values = null; if (!changeDetected) { return(null); } var sb = new StringBuilder(); sb.Append("UPDATE "); sb.Append(quotedtablename); sb.Append(" SET "); sb.Append(set); sb.Append(" WHERE "); sb.Append(where); if (withValues) { return(cqlTool.FillWithValues(sb.ToString(), out values)); } else { values = null; return(cqlTool.FillWithEncoded(sb.ToString())); } }
public static string GetInsertCQLAndValues(object row, string quotedtablename, out object[] values, int?ttl, DateTimeOffset?timestamp, bool ifNotExists, bool withValues = true) { var cqlTool = new CqlStringTool(); Type rowType = row.GetType(); var sb = new StringBuilder(); sb.Append("INSERT INTO "); sb.Append(quotedtablename); sb.Append("("); List <MemberInfo> props = rowType.GetPropertiesOrFields(); bool first = true; foreach (MemberInfo prop in props) { if (first) { first = false; } else { sb.Append(", "); } string memName = CalculateMemberName(prop); sb.Append(memName.QuoteIdentifier()); } sb.Append(") VALUES ("); first = true; foreach (MemberInfo prop in props) { object val = prop.GetValueFromPropertyOrField(row); if (first) { first = false; } else { sb.Append(", "); } sb.Append(cqlTool.AddValue(val)); } sb.Append(")"); if (ifNotExists) { sb.Append(" IF NOT EXISTS"); } if (ttl != null || timestamp != null) { sb.Append(" USING"); } if (ttl != null) { sb.Append(" TTL "); sb.Append(ttl.Value); if (timestamp != null) { sb.Append(" AND"); } } if (timestamp != null) { sb.Append(" TIMESTAMP "); sb.Append((timestamp.Value - CqlQueryTools.UnixStart).Ticks / 10); } if (withValues) { return(cqlTool.FillWithValues(sb.ToString(), out values)); } else { values = null; return(cqlTool.FillWithEncoded(sb.ToString())); } }