Esempio n. 1
0
        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 string GetUpdate(out object[] values, Type tableType, int?ttl, DateTimeOffset?timestamp, bool withValues = true)
        {
            var sb = new StringBuilder();

            sb.Append("UPDATE ");
            sb.Append(QuotedTableName);
            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);
                sb.Append(" ");
            }
            sb.Append(" SET ");

            var setStatements = new List <string>();

            foreach (KeyValuePair <string, Tuple <string, object, int> > mapping in Mappings)
            {
                object o = mapping.Value.Item2;
                if (o != null)
                {
                    var        val          = (object)null;
                    MemberInfo propsOrField = o.GetType().GetPropertiesOrFields().SingleOrDefault(pf => pf.Name == mapping.Value.Item1);
                    var        oriProps     = tableType.GetPropertiesOrFields().SingleOrDefault(pf => pf.Name == mapping.Key);
                    var        counter      = oriProps != null?oriProps.GetCustomAttributes(typeof(CounterAttribute), true).FirstOrDefault() as CounterAttribute : null;

                    if (o.GetType().IsPrimitive || propsOrField == null)
                    {
                        val = o;
                    }
                    else
                    {
                        val = propsOrField.GetValueFromPropertyOrField(o);
                    }

                    if (!Alter.ContainsKey(mapping.Key))
                    {
                        throw new CqlArgumentException("Unknown column: " + mapping.Key);
                    }
                    if (counter != null)
                    {
                        setStatements.Add(Alter[mapping.Key].QuoteIdentifier() + " = " + Alter[mapping.Key].QuoteIdentifier() + " + " + cqlTool.AddValue(val));
                    }
                    else
                    {
                        setStatements.Add(Alter[mapping.Key].QuoteIdentifier() + " = " + cqlTool.AddValue(val));
                    }
                }
                else
                {
                    if (!Alter.ContainsKey(mapping.Key))
                    {
                        throw new CqlArgumentException("Unknown column: " + mapping.Key);
                    }
                    setStatements.Add(Alter[mapping.Key].QuoteIdentifier() + " = NULL");
                }
            }

            if (setStatements.Count == 0)
            {
                throw new CqlArgumentException("Nothing to update");
            }
            sb.Append(String.Join(", ", setStatements));

            if (WhereClause.Length > 0)
            {
                sb.Append(" WHERE ");
                sb.Append(WhereClause);
            }

            if (UpdateIfClause.Length > 0)
            {
                sb.Append(" IF ");
                sb.Append(UpdateIfClause);
            }

            if (withValues)
            {
                return(cqlTool.FillWithValues(sb.ToString(), out values));
            }
            values = null;
            return(cqlTool.FillWithEncoded(sb.ToString()));
        }
Esempio n. 3
0
        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()));
            }
        }
Esempio n. 4
0
        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()));
            }
        }