Ejemplo n.º 1
0
        /// <summary>
        /// Selections the clause.
        /// </summary>
        /// <param name="selectionCriterion">The selectionCriterion.</param>
        /// <returns></returns>
        private string SelectionClause(SelectionCriterion selectionCriterion, bool forParameters)
        {
            string result = string.Empty;

            if (selectionCriterion.Expression == Expression.None)
            {
                if (selectionCriterion == null)
                {
                    throw new NullReferenceException("SelectionCriterion cannot be null!");
                }
                if (!m_Types.Contains(selectionCriterion.Type))
                {
                    m_Types.Add(selectionCriterion.Type);
                }
                selectionCriterion.PropertyAlias = AppInfo.GetTableName(selectionCriterion.Type) + ".";
                if (selectionCriterion.OriginalPropertyName != "ObjectKey")
                {
                    selectionCriterion.PropertyAlias += selectionCriterion.OriginalPropertyName;
                }
                else
                {
                    selectionCriterion.PropertyAlias += AppInfo.GetTableName(selectionCriterion.Type) + "Key";
                }
                string expression = string.Empty;
                if (m_PreviousSelectionCriterion != null && m_PreviousSelectionCriterion.Expression == Expression.Begin)
                {
                    expression = "(";
                }
                if (forParameters)
                {
                    object value = selectionCriterion.Value;
                    if (selectionCriterion.ComparisonMethod == ComparisonMethod.In || selectionCriterion.ComparisonMethod == ComparisonMethod.NotIn)
                    {
                        if (value != null)
                        {
                            object   obj   = value;
                            Array    array = obj as Array;
                            object[] objs  = new object[array.Length];
                            int      i     = 0;
                            foreach (var item in array)
                            {
                                objs[i++] = item;
                            }
                            value = objs;
                        }
                        else
                        {
                            throw new InvalidOperationException("ComparisonMethod.In/ComparisonMethod.NotIn cannot have a null object value");
                        }
                    }
                    string text = ComparisonTextForParameters(m_DataObjectBroker, selectionCriterion.ComparisonMethod, ref value, m_Parameters.Count);
                    if (value != null)
                    {
                        if (selectionCriterion.ComparisonMethod != ComparisonMethod.In && selectionCriterion.ComparisonMethod != ComparisonMethod.NotIn)
                        {
                            m_Parameters.Add(value);
                        }
                        else
                        {
                            for (int i = 0; i < ((object[])value).Length; i++)
                            {
                                m_Parameters.Add(((object[])value)[i]);
                            }
                        }
                    }
                    result = OperandText(selectionCriterion.OperandType) + " " + expression + ColumnAlias(selectionCriterion) + " " + text + " ";
                }
                else
                {
                    result = OperandText(selectionCriterion.OperandType) + " " + expression +
                             ColumnAlias(selectionCriterion) + " " +
                             ComparisonText(m_DataObjectBroker, selectionCriterion.ComparisonMethod, selectionCriterion.Value, selectionCriterion.Property.PropertyType) + " ";
                }
            }
            else if (selectionCriterion.Expression == Expression.Begin)
            {
                result = string.Empty;
            }
            else if (selectionCriterion.Expression == Expression.End)
            {
                result = ") ";
            }
            else
            {
                throw new ApplicationException("Could not generate where clause!");
            }
            m_PreviousSelectionCriterion = selectionCriterion;
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the order by clause.
        /// </summary>
        private void SetOrderByClause()
        {
            int index = -1;

            OrderBy = string.Empty;

            foreach (OrderCriterion orderCriterion in base.OrderCriteriaList)
            {
                string persistentTypeName = AppInfo.GetTableName(orderCriterion.Type);

                index++;

                if (index > 0)
                {
                    OrderBy += ", ";
                }

                if (orderCriterion.OriginalPropertyName != "ObjectKey")
                {
                    if (orderCriterion.XmlPath == string.Empty)
                    {
                        string pTypeName = PersistentTypeName != null && !String.Empty.Equals(PersistentTypeName) ? PersistentTypeName : persistentTypeName;
                        string field     = string.Format("{0}." + "{1}", pTypeName, orderCriterion.OriginalPropertyName);

                        if (orderCriterion.AggregateFunction == AggregateFunction.None)
                        {
                            OrderBy += field;
                        }
                        else
                        {
                            OrderBy += string.Format("{0}({1})", orderCriterion.AggregateFunction, field);
                        }
                    }
                    else
                    {
                        char[]   delimiterChars = { '.' };
                        string[] xmlColumn      = orderCriterion.OriginalPropertyName.Split(delimiterChars);
                        OrderBy += m_DataObjectBroker.ConnectionInfo.GetXmlOrder(persistentTypeName, xmlColumn, orderCriterion.XmlPath, GetDataType(orderCriterion));
                    }
                }
                else
                {
                    string field = persistentTypeName + "." + persistentTypeName + "Key";

                    if (orderCriterion.AggregateFunction == AggregateFunction.None)
                    {
                        OrderBy += field;
                    }
                    else
                    {
                        OrderBy += string.Format("{0}({1})", orderCriterion.AggregateFunction, field);
                    }
                }

                OrderBy += " ";

                if (m_Reverse)
                {
                    if (orderCriterion.SortOrder.ToUpper().Equals("ASC"))
                    {
                        orderCriterion.SortOrder = "DESC";
                    }
                    else
                    {
                        orderCriterion.SortOrder = "ASC";
                    }
                }

                OrderBy += orderCriterion.SortOrder;
            }

            if (OrderBy.Length > 0)
            {
                OrderBy = "ORDER BY " + OrderBy;
            }
        }