/// <summary>Default Constructor</summary>
 public RevisionPasajeroInstance(ONContext onContext)
     : base(onContext, "RevisionPasajero", "Clas_1348178673664478_Alias")
 {
     Oid = new RevisionPasajeroOid();
     RevisionRoleTemp = null;
     RevisionRoleOidTemp = null;
     PasajeroAeronaveRoleTemp = null;
     PasajeroAeronaveRoleOidTemp = null;
     StateObjAttrTemp = null;
 }
        public override void Copy(ONInstance instance)
        {
            RevisionPasajeroInstance linstance = instance as RevisionPasajeroInstance;

            Oid = new RevisionPasajeroOid(linstance.Oid);

            RevisionRoleTemp = linstance.RevisionRoleTemp;
            if (linstance.RevisionRoleOidTemp != null)
                RevisionRoleOidTemp = new RevisionOid(linstance.RevisionRoleOidTemp);
            PasajeroAeronaveRoleTemp = linstance.PasajeroAeronaveRoleTemp;
            if (linstance.PasajeroAeronaveRoleOidTemp != null)
                PasajeroAeronaveRoleOidTemp = new PasajeroAeronaveOid(linstance.PasajeroAeronaveRoleOidTemp);
            StateObj = new ONString(linstance.StateObj);

            base.Copy(instance);
        }
        /// <summary>
        /// Executes the SQL statment over the Data Base connected
        /// </summary>
        /// <param name="onSql">This parameter has the current SQL statment</param>
        /// <param name="onFilterList">List of filters to check</param>
        /// <param name="comparer">This parameter has all the information refering to the order criteria to add to SQL statment</param>
        /// <param name="startRowOid">This parameter has the OID necessary to start the search</param>
        /// <param name="blockSize">This parameter represents the number of instances to be returned</param>
        public override ONCollection ExecuteSql(ONSql onSql, ONFilterList onFilterList, ONDisplaySet displaySet, ONOrderCriteria orderCriteria, ONOid startRowOid, int blockSize)
        {
            RevisionCollection lQuery = null;
            bool lWithStartRow = (startRowOid != null).TypedValue;
            long lCount = -1;
            if (!lWithStartRow)
                lCount = 0;

            IDataReader lDataReader = null;
            ONSQLConnection lOnSQLConnection = null;

            try
            {
                lDataReader = Execute(onSql) as IDataReader;

                RevisionInstance lInstance = null;
                RevisionInstance lAntInstance = null;
                if (lDataReader != null)
                {
                    object[] lColumns;
                    if(displaySet == null)
                        lColumns = new object[6];
                    else
                        lColumns = new object[displaySet.ElementsInData];

                    lQuery = new RevisionCollection(OnContext);
                    bool lFoundStartRow = false;
                    while (lDataReader.Read())
                    {
                        lAntInstance = lInstance;

                        // Read Columns
                        lDataReader.GetValues(lColumns);

                        // Read Instance
                        int lIndex = 0;
                        lInstance = LoadFacet(OnContext, displaySet, lColumns, ref lIndex);

                        // Read related attributes
                        if (displaySet != null)
                            LoadRelated(OnContext, displaySet, lColumns, lIndex, lInstance);

                        if (lCount >= 0) // Add the load instance
                        {
                            if ((onFilterList == null) || (!onFilterList.InMemory))
                            {
                                // Add to the Instance list
                                lQuery.Add(lInstance);
                                lCount++;
                            }
                            else
                            {
                                ONSQLConnection lSQLConnectionOld = (ONSQLConnection) lInstance.OnContext.SqlConnection;

                                // Set another connection because it is imposible to use
                                // the same connection that is used in the DataReader
                                if (lOnSQLConnection == null)
                                    lOnSQLConnection = GetConnection();
                                lInstance.OnContext.SqlConnection = lOnSQLConnection;

                                if (onFilterList.FilterInMemory(lInstance))
                                {
                                    // Add to the Instance list
                                    lQuery.Add(lInstance);
                                    lCount++;
                                }

                                lInstance.OnContext.SqlConnection = lSQLConnectionOld;
                            }
                        }
                        else
                        {
                            if ((orderCriteria != null) && (orderCriteria.InMemory)) // Need to load for ordering in memory after loading
                            {
                                if (lAntInstance != null)
                                {
                                    // Set another connection because it is imposible to use
                                    // the same connection that is used in the DataReader
                                    ONSQLConnection lOnSQLConnectionOld = lInstance.OnContext.SqlConnection as ONSQLConnection;
                                    if (lOnSQLConnection == null)
                                        lOnSQLConnection = GetConnection();
                                    lInstance.OnContext.SqlConnection = lOnSQLConnection;

                                    int lCompare = orderCriteria.CompareSql(lInstance, lAntInstance);
                                    if (lCompare != 0)
                                    {
                                        if (lFoundStartRow)
                                            lCount = 1;
                                        else
                                            lQuery.Clear();
                                    }

                                    // Restores the old connection
                                    lInstance.OnContext.SqlConnection = lOnSQLConnectionOld;
                                }

                                if ((onFilterList == null) || (!onFilterList.InMemory))
                                {
                                    // Add to the Instance list
                                    lQuery.Add(lInstance);
                                }
                                else
                                {
                                    ONSQLConnection lSQLConnectionOld = (ONSQLConnection)lInstance.OnContext.SqlConnection;

                                    // Set another connection because it is imposible to use
                                    // the same connection that is used in the DataReader
                                    if (lOnSQLConnection == null)
                                        lOnSQLConnection = GetConnection();
                                    lInstance.OnContext.SqlConnection = lOnSQLConnection;

                                    if (onFilterList.FilterInMemory(lInstance))
                                    {
                                        // Add to the Instance list
                                        lQuery.Add(lInstance);
                                    }
                                    else
                                        lCount--;

                                    lInstance.OnContext.SqlConnection = lSQLConnectionOld;
                                }

                                if (lInstance.Oid.Equals(startRowOid))
                                    lFoundStartRow = true;
                            }

                            else if (lInstance.Oid.Equals(startRowOid)) // Search the start row
                                lCount = 0;
                        }

                        // Stop loading
                        if ((blockSize != 0) && (lCount > blockSize))
                        {
                            if (orderCriteria == null)
                                break;
                            else
                            {
                                // Set another connection because it is imposible to use
                                // the same connection that is used in the DataReader
                                ONSQLConnection lOnSQLConnectionOld = lInstance.OnContext.SqlConnection as ONSQLConnection;
                                if (lOnSQLConnection == null)
                                    lOnSQLConnection = GetConnection();
                                lInstance.OnContext.SqlConnection = lOnSQLConnection;

                                int lCompare = orderCriteria.CompareSql(lInstance, lAntInstance);

                                // Restores the old connection
                                lInstance.OnContext.SqlConnection = lOnSQLConnectionOld;

                                if (lCompare > 0)
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                string ltraceItem = "Method: ExecuteSql, Component: RevisionData";
              			if (e is ONSystemException)
              			{
              				ONSystemException lException = e as ONSystemException;
              				lException.addTraceInformation(ltraceItem);
              				throw lException;
                }
              		throw new ONSystemException(e, ltraceItem);
              		}
            finally
            {
                if (lOnSQLConnection != null)
                    ONDBData.CloseConnection(lOnSQLConnection);
                if (lDataReader != null)
                {
                    if (mSqlCommand != null)
                        mSqlCommand.Cancel();
                    lDataReader.Close();
                }
                Close();
                if ((onFilterList != null) && (onFilterList.InMemory) && !lWithStartRow && (lCount <= blockSize))
                    lQuery.totalNumInstances = lQuery.Count;
            }

            return lQuery;
        }