示例#1
0
        /// <summary>
        /// Attempt to read existing results file into the query DataTable
        /// </summary>
        /// <param name="qm"></param>

        public void ReadBinaryResultsFile(string fileName)
        {
            QueryTable   qt;
            QueryColumn  qc;
            BinaryReader br = null;

            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                bool saveHandlersEnabled = Qm.DataTable.EnableDataChangedEventHandlers(false);                 // disable for faster load

                bool saveUpdateMaxRowsPerKey = UpdateMaxRowsPerKeyEnabled;
                UpdateMaxRowsPerKeyEnabled = false;                 // disable for faster load

                int id = Query.UserObject.Id;
                if (id <= 0)
                {
                    throw new Exception("Query not saved");
                }
                if (DataTableMx == null || DataTableMx.Columns.Count == 0)
                {
                    throw new Exception("DataTable not defined");
                }

                br = BinaryFile.OpenReader(fileName);
                string       sq  = br.ReadString();
                Query        q0  = Query.Deserialize(sq);         // deserialize the saved query
                QueryManager qm0 = new QueryManager();
                qm0.LinkMember(q0);
                ResultsFormat        rf0  = new ResultsFormat(qm0, OutputDest.WinForms);
                ResultsFormatFactory rff0 = new ResultsFormatFactory(qm0, OutputDest.WinForms);
                rff0.Build();                 // build format with vo positions

                // The cached query cols should match those of the current query: however,
                // we'll create a mapping just in case they don't

                int voArrayLen0 = br.ReadInt32();                            // cached vo array len
                int voArrayLen  = DataTableMx.Columns.Count - KeyValueVoPos; // current query vo array len

                List <int> q0VoMap = new List <int>();                       // vo position in cached query data
                List <int> qVoMap  = new List <int>();                       // vo position in current version of query

                q0VoMap.Add(0);                                              // first position is the common key value
                qVoMap.Add(0);

                foreach (QueryTable qt0 in q0.Tables)                 // scan each table in cached data
                {
                    foreach (QueryColumn qc0 in qt0.QueryColumns)     // and each column
                    {
                        if (qc0.VoPosition < 0)
                        {
                            continue;                                 // skip if not mapped to the vo in cached data
                        }
                        int q0VoPos = qc0.VoPosition - KeyValueVoPos; // where it is in cache

                        int qvoPos = -1;                              // where it will go
                        qt = Query.GetTableByName(qt0.MetaTable.Name);
                        if (qt != null)
                        {
                            qc = qt.GetQueryColumnByName(qc0.MetaColumn.Name);
                            if (qc != null)
                            {
                                qvoPos = qc.VoPosition - KeyValueVoPos;
                            }
                        }

                        q0VoMap.Add(q0VoPos);                       // where it is in saved data
                        qVoMap.Add(qvoPos);                         // where it will go (not including attributes & check cols)
                    }
                }

                if (q0VoMap.Count != voArrayLen0)
                {
                    throw new Exception("Cached Vo length doesn't match list of selected columns");
                }

                DataTableMx.Clear();                           // clear the rows
                CidList  cidList = new CidList();
                object[] voa     = new object[voArrayLen];     // array to fill

                while (!BinaryFile.ReaderEof(br))              // process each row
                {
                    for (int mi = 0; mi < q0VoMap.Count; mi++) // each col
                    {
                        object o = VoArray.ReadBinaryItem(br);
                        if (mi == 0 && o != null)                         // add to key list if key
                        {
                            cidList.Add(o.ToString(), false);
                        }

                        if (qVoMap[mi] >= 0)                         // save in new buf if mapped
                        {
                            voa[qVoMap[mi]] = o;
                        }
                    }

                    DataRowMx dr = AddDataRow(voa);
                }

                br.Close();
                Qm.DataTable.EnableDataChangedEventHandlers(saveHandlersEnabled);

                UpdateMaxRowsPerKeyEnabled = saveUpdateMaxRowsPerKey;
                InitializeRowAttributes(false);

                ResultsKeys = cidList.ToStringList();                 // include keys in DTM as well

                double ms = sw.Elapsed.TotalMilliseconds;
                return;
            }
            catch (Exception ex)
            {
                if (br != null)
                {
                    br.Close();
                }
                throw new Exception(ex.Message, ex);
            }
        }