public override void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
        {
            if (!_IsActive || _Fields == null || !e.IsSelect())
            {
                return;
            }

            PXDBOperation eOp = e.Operation & PXDBOperation.Option;
            bool          operationAllowsSelectForXml =
                eOp == PXDBOperation.External ||
                eOp == PXDBOperation.Internal ||
                eOp == PXDBOperation.Normal && e.Value == null ||
                eOp == PXDBOperation.GroupBy && sender.BqlSelect != null;                    // it's ok because the subselect will be used inside FROM (...) part without any aggregation
            Type tableToUse = eOp == PXDBOperation.External ? sender.GetItemType() : e.Table ?? _BqlTable;


            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                if (sender.Graph.Caches[_BqlTable].BqlSelect != null && operationAllowsSelectForXml)
                {
                    e.BqlTable  = _BqlTable;
                    e.FieldName = ((e.Operation & PXDBOperation.Option) == PXDBOperation.External ? sender.GetItemType().Name : _BqlTable.Name) + '.' + _DatabaseFieldName;
                }
                else
                {
                    PXCommandPreparingEventArgs.FieldDescription description;
                    sender.Graph.Caches[_BqlTable].RaiseCommandPreparing(_DatabaseFieldName, e.Row, e.Value, e.Operation, e.Table, out description);
                    if (description != null)
                    {
                        e.DataType  = description.DataType;
                        e.DataValue = description.DataValue;
                        e.BqlTable  = _BqlTable;
                        e.FieldName = description.FieldName;
                    }
                }
            }
            else
            {
                var dialect = e.SqlDialect;
                if (operationAllowsSelectForXml)
                {
                    if (aggregateAttributes && (e.Operation & PXDBOperation.Option) != PXDBOperation.External)
                    {
                        List <Type> init = new List <Type>()
                        {
                            tableToUse
                        };

                        StringBuilder bld = new StringBuilder();
                        dialect.prepareAttributesJoined(sender.Graph, bld, init, ((IBqlSearch)_SingleSelect).GetField(), _PureWhere);
                        e.FieldName = bld.ToString();

                        e.DataType = PXDbType.NVarChar;
                        return;
                    }
                    else
                    {
                        e.BqlTable  = _BqlTable;
                        e.FieldName = BqlCommand.SubSelect + _Fields.Length + ')';
                    }
                }
                else
                {
                    e.FieldName = BqlCommand.Null;
                }
            }
            e.DataType   = PXDbType.Int;
            e.DataLength = 4;
        }
        public override void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
        {
            if (!_IsActive || _Fields == null || !e.IsSelect())
            {
                return;
            }

            PXDBOperation eOp = e.Operation & PXDBOperation.Option;
            bool          operationAllowsSelectForXml =
                eOp == PXDBOperation.External ||
                eOp == PXDBOperation.Internal ||
                eOp == PXDBOperation.Normal && e.Value == null ||
                eOp == PXDBOperation.GroupBy && sender.BqlSelect != null;                    // it's ok because the subselect will be used inside FROM (...) part without any aggregation
            Type tableToUse = eOp == PXDBOperation.External ? sender.GetItemType() : e.Table ?? _BqlTable;

            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                if (sender.Graph.Caches[_BqlTable].BqlSelect != null && operationAllowsSelectForXml)
                {
                    e.BqlTable = _BqlTable;
                    e.Expr     = new Column(_DatabaseFieldName, (e.Operation & PXDBOperation.Option) == PXDBOperation.External ? sender.GetItemType() : _BqlTable);
                }
                else
                {
                    PXCommandPreparingEventArgs.FieldDescription description;
                    sender.Graph.Caches[_BqlTable].RaiseCommandPreparing(_DatabaseFieldName, e.Row, e.Value, e.Operation, e.Table, out description);
                    if (description != null)
                    {
                        e.DataType  = description.DataType;
                        e.DataValue = description.DataValue;
                        e.BqlTable  = _BqlTable;
                        e.Expr      = description.Expr;
                    }
                }
            }
            else
            {
                var dialect = e.SqlDialect;
                if (operationAllowsSelectForXml)
                {
                    if (aggregateAttributes && (e.Operation & PXDBOperation.Option) != PXDBOperation.External)
                    {
                        List <Type> init = new List <Type>()
                        {
                            tableToUse
                        };

                        e.BqlTable = tableToUse;
                        e.DataType = PXDbType.NVarChar;

                        e.Expr = new SQLTree.SubQuery(GetAttributesJoinedQuery(sender.Graph, init, ((IBqlSearch)_SingleSelect).GetField(), _PureWhere));
                        return;
                    }
                    else
                    {
                        e.BqlTable = _BqlTable;

                        Query q = new Query().Field(new SQLConst(_Fields.Length));
                        e.Expr = new SubQuery(q);
                    }
                }
                else
                {
                    e.Expr = SQLExpression.Null();
                }
            }
            e.DataType   = PXDbType.Int;
            e.DataLength = 4;
        }