Exemplo n.º 1
0
        internal static CriteriaNode CreateCriteriaNode <T>(params Expression <Func <T, bool> >[] expressions) where T : new()
        {
            if (expressions.Length == 0)
            {
                return(new CriteriaNode(null));
            }

            var nodes = new List <CriteriaNode>();

            foreach (var exp in expressions)
            {
                var node = new CriteriaNode(null);
                VisitExpression <T>(exp, node);

                nodes.Add(node);
            }

            if (nodes.Count == 1)
            {
                return(nodes[0]);
            }

            var firstNode = nodes[0];

            nodes.RemoveAt(0);

            return(MergeCriteriaNodes(firstNode, nodes));
        }
Exemplo n.º 2
0
        private DataTable RetrieveTable(string commandText, CriteriaNode criteriaNode)
        {
            try
            {
                Command.Start();


                commandText = CreateSqlTextByCriteriaNode(commandText, criteriaNode);

                Command.CommandText = EnclosePropertyInQuotation(commandText);

                using (var reader = Command.ExecuteReader())
                {
                    var table = new DataTable();

                    table.Load(reader);

                    return(table);
                }
            }
            catch (Exception ex)
            {
                throw CreateEasylinkException(Command.CommandText, ex);
            }
        }
Exemplo n.º 3
0
        private void DeleteAll <T>(CriteriaNode criteriaNode) where T : new()
        {
            var deleteSql = ClassConfigContainer.FindDeleteSql(typeof(T));

            try
            {
                Command.Start();

                var criterias = criteriaNode.SearchAll();

                if (criterias.Count > 0)
                {
                    UpdateCriteria <T>(criterias);
                }

                if (Shared.IsAuditable <T>())
                {
                    var originalObjects = RetrieveAll <T>(criteriaNode);

                    var dict = new Dictionary <T, string>();

                    foreach (var original in originalObjects)
                    {
                        string updateSql = ClassConfigContainer.FindUpdateSql1(typeof(T));

                        var propertyParameters =
                            ParametersCreator.CreatePropertyParameters(ParameterPrefix,
                                                                       original, ref updateSql);

                        string auditText = Auditor.AuditDelete(original, propertyParameters);

                        dict[original] = auditText;
                    }

                    ExecuteNonQuery(deleteSql, criteriaNode);

                    foreach (var original in originalObjects)
                    {
                        InsertAuditRecord(original, DbOperation.Delete, dict[original]);
                    }
                }
                else
                {
                    ExecuteNonQuery(deleteSql, criteriaNode);
                }
            }

            catch (Exception ex)
            {
                throw CreateEasylinkException(deleteSql, ex);
            }
        }
Exemplo n.º 4
0
        private void ExecuteNonQuery(string commandText, CriteriaNode criteriaNode)
        {
            try
            {
                Command.Start();

                Command.CommandText = CreateSqlTextByCriteriaNode(commandText, criteriaNode);

                Command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw CreateEasylinkException(Command.CommandText, ex);
            }
        }
Exemplo n.º 5
0
        private List <T> RetrieveAll <T>(CriteriaNode criteriaNode) where T : new()
        {
            var selectSql = ClassConfigContainer.FindSelectSql <T>();

            var criterias = criteriaNode.SearchAll();

            if (criterias.Count > 0)
            {
                UpdateCriteria <T>(criterias);
            }

            var table = RetrieveTable(selectSql, criteriaNode);

            return(ObjectMapper.MapTableToList <T>(table));
        }
Exemplo n.º 6
0
        private string CreateSqlTextByCriteriaNode(string sql, CriteriaNode criteriaNode)
        {
            var sb = new StringBuilder();

            sb.AppendLine(sql);

            var whereClause = CreateWhereClause(criteriaNode);

            if (!String.IsNullOrEmpty(whereClause))
            {
                sb.AppendLine(" WHERE " + whereClause);
            }


            return(sb.ToString());
        }
Exemplo n.º 7
0
        private T RetrieveObject <T>(CriteriaNode criteriaNode) where T : new()
        {
            var list = RetrieveAll <T>(criteriaNode);

            if (list.Count == 0)
            {
                return(default(T));
            }

            if (list.Count > 1)
            {
                throw new ServerException("More than one objects matched with criteria are retrieved.");
            }

            return(list[0]);
        }
Exemplo n.º 8
0
        private static CriteriaNode MergeCriteriaNodes(CriteriaNode criteriaNode, IList <CriteriaNode> nodes)
        {
            if (nodes.Count == 0)
            {
                return(criteriaNode);
            }

            var mergedNode = new CriteriaNode();

            mergedNode.LeftChild  = criteriaNode;
            mergedNode.RightChild = nodes[0];
            mergedNode.NodeType   = CriteriaNodeType.AND;

            nodes.RemoveAt(0);

            return(MergeCriteriaNodes(mergedNode, nodes));
        }
Exemplo n.º 9
0
        private string CreateWhereClause(CriteriaNode criteriaNode)
        {
            var criterias = criteriaNode.SearchAll();

            if (criterias.Count == 0)
            {
                return(string.Empty);
            }

            if (criteriaNode.Criteria != null)
            {
                return(CreateCondition(criteriaNode.Criteria));
            }

            return(string.Format("({0} {1} {2})", CreateWhereClause(criteriaNode.LeftChild), criteriaNode.NodeType,
                                 CreateWhereClause(criteriaNode.RightChild)));
        }
Exemplo n.º 10
0
        private static void VisitExpression <T>(Expression expression, CriteriaNode criteriaNode)
        {
            if (expression is Expression <Func <T, bool> > )
            {
                var temp = expression as Expression <Func <T, bool> >;

                VisitExpression <T>(temp.Body, criteriaNode);

                return;
            }

            if (expression is BinaryExpression)
            {
                var binaryExpresion = expression as BinaryExpression;

                if (binaryExpresion.NodeType == ExpressionType.AndAlso ||
                    binaryExpresion.NodeType == ExpressionType.OrElse)
                {
                    criteriaNode.LeftExpression = binaryExpresion.Left;

                    criteriaNode.RightExpression = binaryExpresion.Right;

                    if (binaryExpresion.NodeType == ExpressionType.AndAlso)
                    {
                        criteriaNode.NodeType = CriteriaNodeType.AND;
                    }
                    else
                    {
                        criteriaNode.NodeType = CriteriaNodeType.OR;
                    }

                    criteriaNode.LeftChild = new CriteriaNode(criteriaNode);

                    criteriaNode.RightChild = new CriteriaNode(criteriaNode);

                    VisitExpression <T>(criteriaNode.LeftExpression, criteriaNode.LeftChild);

                    VisitExpression <T>(criteriaNode.RightExpression, criteriaNode.RightChild);

                    return;
                }
            }

            criteriaNode.Criteria = CreateCriteria <T>(expression);
        }
Exemplo n.º 11
0
 internal CriteriaNode(CriteriaNode parent)
 {
     Parent = parent;
 }
Exemplo n.º 12
0
 internal CriteriaNode(CriteriaNode parent)
 {
     Parent = parent;
 }