c# - Reading in a complex text file to input into database -
i working on program read in text file , insert areas of text file different columns on database. text file set this:
"intro information" "more intro information" srvrmgr> "information system" srbrmgr> list parameters component *admbatchproc* "headers" *name of record* *alias of record* *value of record* the columns create table containing of setting information component. 1 of settings listed, file moves component , returns information component in new table. need read in component , information on tables without headers or other information. need able transfer data database. columns fixed width on each table within file.
any recommendations how approach welcome. have never read in file complex dont know how approach ignoring alot of information while trying other information ready database. component value trying gather follows word component on line starts "srvrmgr".
the '*' represents areas put datbase.
siebel enterprise applications siebel server manager, version 8.1.1.11 [23030] lang_independent copyright (c) 1994-2012, oracle. rights reserved. programs (which include both software , documentation) contain proprietary information; provided under license agreement containing restrictions on use , disclosure , protected copyright, patent, , other intellectual , industrial property laws. reverse engineering, disassembly, or decompilation of programs, except extent required obtain interoperability other independently created software or specified law, prohibited. oracle, jd edwards, peoplesoft, , siebel registered trademarks of oracle corporation and/or affiliates. other names may trademarks of respective owners. if have received software in error, please notify oracle corporation @ 1.800.oracle1. type "help" list of commands, "help <topic>" detailed connected 1 server(s) out of total of 1 server(s) in enterprise srvrmgr> configure list parameters show pa_name,pa_alias,pa_value srvrmgr> srvrmgr> list parameters component admbatchproc pa_name pa_alias pa_value ---------------------------------------------------------------------- ------------------------------------- -------------------------------------------------------------------------------------------------------------------- adm data type name admdatatype adm eai method name admeaimethod upsert adm deployment filter admfilter 213 rows returned. srvrmgr> list parameters component admobjmgr_enu pa_name pa_alias pa_value ---------------------------------------------------------------------- ------------------------------------- -------------------------------------------------------------------------------------------------------------------- accessibleenhanced accessibleenhanced false this beginning of text file. produced in system called siebel show of settings environment. need pull component name (there multiple on actual file ones shown here 'admbatchproc' , 'admobjmgr_enu'), , data shown on table below created siebel. rest of information irrelevant purpose of task need.
i recommend using test-driven development techniques in case. i'm guessing possible variations of input format near infinite.
try this:
1) create interface represent data operations or parsing logic expect application perform. example:
public interface iparserbehaviors { void startnextcomponent(); void settablename(string tablename); void definecolumns(ienumerable<string> columnnames); void loadnewdatarow(ienumerable<object> rowvalues); datatable producetableforcurrentcomponent(); // etc. } 2) gather many small examples of discrete inputs have well-defined behaviors possible.
3) inject behaviors handler parser. example:
public class parser { private const string component_marker = "srvrmgr"; private readonly iparserbehaviors _behaviors; public parser(iparserbehaviors behaviors) { _behaviors = behaviors; } public void readfile(string filename) { // bla bla foreach (string line in linesoffile) { // maintain state if (line.startswith(component_marker)) { datatable table = _behaviors.producetableforcurrentcomponent(); // save table database _behaviors.startnextcomponent(); } else if (/* condition */) { // parse text _behaviors.loadnewdatarow(values); } } } } 4) create tests around expected behaviors, using preferred mocking framework. example:
public void filewithtwocomponents_startstwonewcomponents() { string filename = "twocomponents.log"; mock<iparserbehaviors> mockbehaviors = new mock<iparserbehaviors>(); parser parser = new parser(mockbehaviors.object); parser.readfile(filename); mockbehaviors.verify(mock => mock.startnextcomponent(), times.exactly(2)); } this way, able integrate under controlled tests. when (not if) runs problem, can distill case wasn't covered, , add test surrounding behavior, after extracting case log being used. separating concerns way allows parsing logic independent data operation logic. needs of parsing specific behaviors seems central application, seems perfect fit creating domain-specific interfaces.
Comments
Post a Comment