Пример #1
0
        protected virtual string GetSqlForDelete(ExecuteTask <EntityType> task)
        {
            var query = new StringBuilder();

            query.Append(string.Format(deleteTemplate
                                       , TableName
                                       , TableAlias));

            query.Append(Environment.NewLine);

            if (task.Joins.Count > 0)
            {
                query.Append(_joinBuilder.BuildJoins(task.Joins));

                query.Append(Environment.NewLine);
            }

            if (task.Queries.Count < 1)
            {
                throw new InvalidOperationException("Delete statement must contain a where clause");
            }

            query.Append(_queryBuilder.BuildQueryElements(task.Queries, task.Index));

            query.Append(Environment.NewLine);

            return(query.ToString());
        }
Пример #2
0
        protected virtual string GetSqlForUpdate(ExecuteTask <EntityType> task)
        {
            var query = new StringBuilder();

            query.Append(string.Format(updateTemplate
                                       , TableName
                                       , TableAlias
                                       , BuildSetParamaters(task)));

            query.Append(Environment.NewLine);

            if (task.Joins.Count > 0)
            {
                query.Append(BuildJoins(task.Joins));

                query.Append(Environment.NewLine);
            }

            if (task.Queries.Count > 0)
            {
                query.Append(BuildQueryElements(task.Queries, task.Index));

                query.Append(Environment.NewLine);
            }

            query.Append(Environment.NewLine);

            return(query.ToString());
        }
Пример #3
0
        protected virtual string GetSqlForInsert(ExecuteTask <EntityType> task)
        {
            var names = ColumnNames.Where(s => s.Value != IdColumn).Select(s => s.Value);

            var query = new StringBuilder();

            if (names.Count() > 0)
            {
                query.Append(string.Format(insertTemplate
                                           , TableName
                                           , "[" + String.Join("], [", names.ToArray()).Replace("_spc_", " ") + "]"
                                           , IdColumn
                                           , BuildInsertParameters(task.Index)));
            }
            else
            {
                query.Append(string.Format(insertNoFieldsTemplate
                                           , TableName
                                           , IdColumn));
            }

            query.Append(Environment.NewLine);

            return(query.ToString());
        }
Пример #4
0
        //public IEntityWriter<IdType, EntityType> Where(QueryElement query)
        //{
        //    if (_tasks.Count != 1)
        //        throw new InvalidOperationException("There was no current operation which to append this query");

        //    _tasks[0].Queries.Add(query);

        //    return this;
        //}

        public IEntityWriter <IdType, EntityType> Run(string script, IDictionary <string, object> parms)
        {
            var task = new ExecuteTask <EntityType>(_tasks.Count)
            {
                TaskType = ActionType.None,
                Script   = script,
                Params   = parms
            };

            _tasks.Add(task);

            return(this);
        }
Пример #5
0
        public IEntityWriter <IdType, EntityType> DeleteByQuery(IList <QueryElement> queries)
        {
            var task = new ExecuteTask <EntityType>(_tasks.Count)
            {
                TaskType = ActionType.Remove
            };

            foreach (var q in queries)
            {
                task.Queries.Add(q);
            }

            _tasks.Add(task);

            return(this);
        }
Пример #6
0
        protected virtual object BuildSetParamaters(ExecuteTask <EntityType> task)
        {
            int count     = 0;
            var fieldSets = ColumnNames
                            .Except(ColumnNames.Where(c => c.Value == IdColumn))
                            .OrderBy(f => f.Value)
                            .Select(field =>
                                    string.Format
                                        (setTemplate
                                        , field.Value.Replace("_spc_", " ")
                                        , GetParamName(field.Value, "u", task.Index, ref count)));

            var s = string.Join(", ", fieldSets);

            return("set " + s.TrimEnd(',', ' '));
        }
Пример #7
0
        public IEntityWriter <IdType, EntityType> Delete(IList <IdType> ids)
        {
            var task = new ExecuteTask <EntityType>(_tasks.Count)
            {
                TaskType = ActionType.Remove
            };

            task.Queries.Add(new QueryElement()
            {
                FieldName  = IdColumn,
                Operator   = Operator.In,
                TableAlias = TableAlias,
                ValueArray = ids.Cast <object>().ToArray()
            });

            _tasks.Add(task);

            return(this);
        }
Пример #8
0
        public IEntityWriter <IdType, EntityType> Delete(EntityType entity)
        {
            var task = new ExecuteTask <EntityType>(_tasks.Count)
            {
                TaskType = ActionType.Remove,
                Entity   = entity
            };

            task.Queries.Add(new QueryElement()
            {
                FieldName  = IdColumn,
                Operator   = Operator.Equals,
                TableAlias = TableAlias,
                Value      = entity.Id
            });

            _tasks.Add(task);

            return(this);
        }
Пример #9
0
        public IEntityWriter <IdType, EntityType> Update(IList <EntityType> entities)
        {
            foreach (var entity in entities)
            {
                var task = new ExecuteTask <EntityType>(_tasks.Count)
                {
                    TaskType = ActionType.Update,
                    Entity   = entity,
                };

                task.Queries.Add(
                    new QueryElement()
                {
                    FieldName  = IdColumn,
                    Operator   = Operator.Equals,
                    Value      = entity.Id,
                    TableAlias = TableAlias.ToString()
                });

                _tasks.Add(task);
            }

            return(this);
        }