Exemplo n.º 1
0
        private DbCommand CreateRemoveCommand(string removeFrom, Expression <Func <ShadowRow, bool> > predicate = null)
        {
            var sb = new StringBuilder()
                     .Append("DELETE FROM ")
                     .Append(removeFrom);

            var store = new QueryParametersStore();

            if (predicate != null)
            {
                var w        = new WhereQueryTranslator(store, new[] { predicate });
                var whereSql = w.TranslateToSql(Expression.Empty());

                if (!string.IsNullOrWhiteSpace(whereSql))
                {
                    sb.Append(" WHERE ");
                    sb.Append(whereSql);
                }
            }

            var cmd = Connection.Value.CreateCommand();

            cmd.CommandText = sb.ToString();
            foreach (var param in store.GetParams())
            {
                var p = cmd.CreateParameter();
                p.ParameterName = param.Key;
                p.Value         = param.Value;
                cmd.Parameters.Add(p);
            }

            return(cmd);
        }
Exemplo n.º 2
0
        private DbCommand CreateUpdateCommand(string updateTarget,
                                              Dictionary <string, object> updateFields,
                                              Expression <Func <ShadowRow, bool> > predicate = null)
        {
            var sb = new StringBuilder()
                     .Append("UPDATE ")
                     .Append(updateTarget);

            var store         = new QueryParametersStore();
            var fieldPNameMap = new Dictionary <string, string>();

            foreach (var updateField in updateFields)
            {
                fieldPNameMap[updateField.Key] = store.Append(updateField.Value);
            }


            {
                var setQueryBuilder = new StringBuilder();
                var loopCounter     = 0;
                foreach (var field in fieldPNameMap.Keys)
                {
                    setQueryBuilder.Append($"{field}={fieldPNameMap[field]}");

                    var itIsLast = loopCounter == fieldPNameMap.Count - 1;
                    if (!itIsLast)
                    {
                        setQueryBuilder.Append(", ");
                    }

                    loopCounter++;
                }

                sb.Append(" SET ");
                sb.Append(setQueryBuilder);
            }

            if (predicate != null)
            {
                var w        = new WhereQueryTranslator(store, new[] { predicate });
                var whereSql = w.TranslateToSql(Expression.Empty());

                if (!string.IsNullOrWhiteSpace(whereSql))
                {
                    sb.Append(" WHERE ");
                    sb.Append(whereSql);
                }
            }


            var cmd = Connection.Value.CreateCommand();

            cmd.CommandText = sb.ToString();
            foreach (var param in store.GetParams())
            {
                var p = cmd.CreateParameter();
                p.ParameterName = param.Key;
                p.Value         = param.Value;
                cmd.Parameters.Add(p);
            }

            return(cmd);
        }