Esempio n. 1
0
        /// <summary>
        /// Returns a dataset with one more datatables
        /// </summary>
        /// <param name="tableNames"></param>
        /// <returns></returns>
        public DataSet GetTableData(TableSchemaCollection tableSchemas)
        {
            DataSet retSet = new DataSet();

            if (tableSchemas.Count < 1)
            {
                return(retSet);                //return empty set if we aren't actually searching for anything
            }
            //create a database for each of the schemas
            Dictionary <int, DataTable> dataTables = new Dictionary <int, DataTable>();

            foreach (TableSchema schema in tableSchemas.Values)
            {
                DataTable dt = schema.BuildEmptyDataTable();
                dataTables.Add(schema.TableID, dt);
            }

            try{
                //1. Fetch all of the TPSRecords from the TPS file
                foreach (TPSPage page in _tpsPages)
                {
                    List <TPSRecord> pageRecords = page.GetRecords();
                    foreach (TPSRecord record in pageRecords)
                    {
                        //go through each record to see if we need it
                        if (record.RecordType == TPSRecord.TYPE_DATA)
                        {
                            //Check if this record belongs to a table that we are searchin for
                            //Notice that we need to generate the table ID before we can check
                            //This part is normally in the TableDataRecord file, but we want to know before
                            //parsing the whole record in the name of efficiency
                            byte[] recordTableIDBA = new byte[4];

                            if (record.RecordData.Length < 4)
                            {
                                continue;
                            }

                            //Get the tableID ( it is backwards )
                            recordTableIDBA[0] = record.RecordData[3];
                            recordTableIDBA[1] = record.RecordData[2];
                            recordTableIDBA[2] = record.RecordData[1];
                            recordTableIDBA[3] = record.RecordData[0];


                            int recordTableID = BitConverter.ToInt32(recordTableIDBA, 0);


                            if (tableSchemas.ContainsKey(recordTableID))
                            {
                                //we generate the record. This will actually create a datarow within the TDR
                                Record.TableDataRecord tdr = new Record.TableDataRecord(record, tableSchemas[recordTableID]);

                                dataTables[recordTableID].Rows.Add(tdr.TableDataRow.ItemArray);
                            }
                        }
                    }
                }
            }catch (Exception ex) {
                throw new Exception("Error searching for data: ", ex);
            }
            foreach (DataTable dt in dataTables.Values)
            {
                retSet.Tables.Add(dt);
            }

            return(retSet);
        }
Esempio n. 2
0
        /// <summary>
        /// Will export data from all tables in the collection to a CSV
        /// </summary>
        /// <param name="tables"></param>
        /// <returns></returns>
        public bool ExportDataToCSV(TableSchemaCollection tableSchemas, string outputFolder)
        {
            if (tableSchemas.Count < 1)
            {
                return(true);                //return empty set if we aren't actually searching for anything
            }
            //create a database for each of the schemas
            Dictionary <int, DataTable>    dataTables  = new Dictionary <int, DataTable>();
            Dictionary <int, StreamWriter> outputFiles = new Dictionary <int, StreamWriter>();

            foreach (TableSchema schema in tableSchemas.Values)
            {
                if (outputFiles.ContainsKey(schema.TableID))
                {
                    continue;
                }

                string   newFile = outputFolder + "\\";
                FileInfo fi      = new FileInfo(_filename);
                if (fi.Extension.Length > 0)
                {
                    newFile += fi.Name.Replace(fi.Extension, "-" + schema.TableName + ".CSV");
                }
                else
                {
                    newFile += fi.Name + "-" + schema.TableName + ".CSV";
                }

                StreamWriter sw = new StreamWriter(newFile);
                outputFiles.Add(schema.TableID, sw);

                DataTable dt          = schema.BuildEmptyDataTable();
                string[]  columnNames = dt.Columns.Cast <DataColumn>().
                                        Select(column => "\"" + column.ColumnName.Replace("\"", "\"\"") + "\"").
                                        ToArray();
                outputFiles[schema.TableID].WriteLine(string.Join(",", columnNames));
                dataTables.Add(schema.TableID, dt);
            }
            try{
                //1. Fetch all of the TPSRecords from the TPS file
                foreach (TPSPage page in _tpsPages)
                {
                    List <TPSRecord> pageRecords = page.GetRecords();
                    foreach (TPSRecord record in pageRecords)
                    {
                        //go through each record to see if we need it
                        if (record.RecordType == TPSRecord.TYPE_DATA)
                        {
                            //Check if this record belongs to a table that we are searchin for
                            //Notice that we need to generate the table ID before we can check
                            //This part is normally in the TableDataRecord file, but we want to know before
                            //parsing the whole record in the name of efficiency
                            byte[] recordTableIDBA = new byte[4];

                            if (record.RecordData.Length < 4)
                            {
                                continue;
                            }

                            //Get the tableID ( it is backwards )
                            recordTableIDBA[0] = record.RecordData[3];
                            recordTableIDBA[1] = record.RecordData[2];
                            recordTableIDBA[2] = record.RecordData[1];
                            recordTableIDBA[3] = record.RecordData[0];


                            int recordTableID = BitConverter.ToInt32(recordTableIDBA, 0);


                            if (tableSchemas.ContainsKey(recordTableID))
                            {
                                //we generate the record. This will actually create a datarow within the TDR
                                Record.TableDataRecord tdr = new Record.TableDataRecord(record, tableSchemas[recordTableID]);

                                //dataTables[recordTableID].Rows.Add(tdr.TableDataRow.ItemArray);
                                string[] fields = tdr.TableDataRow.ItemArray.Select(field => "\"" + field.ToString().Replace("\"", "\"\"") + "\"").
                                                  ToArray();

                                outputFiles[recordTableID].WriteLine(string.Join(",", fields));
                            }
                        }
                    }
                }
            }catch (Exception ex) {
                throw new Exception("Error exporting data: ", ex);
            }

            //attempt to close any open files
            foreach (StreamWriter sw in outputFiles.Values)
            {
                try{
                    sw.Flush();
                    sw.Close();
                }catch (Exception) {}
            }
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Will read the next TPSRecord.
        /// True or false depending if there is another one in the queue
        /// </summary>
        /// <returns></returns>
        public bool Read()
        {
            //if there is anything on our temporary stack, just send the first one back to the user and remove it from thelist
            if (_returnDataStack.Count > 0)
            {
                _currentRow = _returnDataStack[0];
                _returnDataStack.RemoveAt(0);
                return(true);
            }


            //read page after page until either there are no more pages or we have something on our stack to return

            while (_tpsPages.Count > 0 && _returnDataStack.Count < 1)
            {
                TPSPage page = _tpsPages[0];
                _tpsPages.RemoveAt(0);                 //pop the page off the stack

                List <TPSRecord> pageRecords = page.GetRecords();
                foreach (TPSRecord record in pageRecords)
                {
                    //go through each record to see if we need it
                    if (record.RecordType == TPSRecord.TYPE_DATA)
                    {
                        //Check if this record belongs to a table that we are searchin for
                        //Notice that we need to generate the table ID before we can check
                        //This part is normally in the TableDataRecord file, but we want to know before
                        //parsing the whole record in the name of efficiency
                        byte[] recordTableIDBA = new byte[4];
                        if (record.RecordData.Length < 4)
                        {
                            continue;
                        }

                        //Get the tableID ( it is backwards )
                        recordTableIDBA[0] = record.RecordData[3];
                        recordTableIDBA[1] = record.RecordData[2];
                        recordTableIDBA[2] = record.RecordData[1];
                        recordTableIDBA[3] = record.RecordData[0];
                        int recordTableID = BitConverter.ToInt32(recordTableIDBA, 0);

                        if (_schema.TableID == recordTableID)
                        {
                            //Generate the record
                            Record.TableDataRecord      tdr    = new Record.TableDataRecord(record, _schema);
                            Dictionary <string, string> newRow = new Dictionary <string, string>();
                            for (int i = 0; i < tdr.TableDataRow.Table.Columns.Count; i++)
                            {
                                newRow.Add(tdr.TableDataRow.Table.Columns[i].ColumnName, tdr.TableDataRow[tdr.TableDataRow.Table.Columns[i].ColumnName].ToString());
                            }

                            _returnDataStack.Add(newRow);
                        }
                    }
                }
            }

            //Now send back a record. If not, we're out and send false
            if (_returnDataStack.Count > 0)
            {
                _currentRow = _returnDataStack[0];
                _returnDataStack.RemoveAt(0);
                return(true);
            }
            else
            {
                return(false);
            }

            return(false);
        }