33using System . Linq ;
44using System . Net ;
55using System . Reflection . PortableExecutable ;
6+ using System . Runtime . CompilerServices ;
67using System . Text ;
78using System . Text . RegularExpressions ;
89using System . Threading . Tasks ;
910using BIE . DataPipeline ;
1011using Microsoft . VisualBasic . FileIO ;
1112
13+ [ assembly: InternalsVisibleTo ( "BIE.Tests" ) ]
1214namespace BIE . DataPipeline . Import
1315{
1416 internal class CsvImporter : IImporter
@@ -58,14 +60,22 @@ public CsvImporter(DataSourceDescription? dataSourceDescription)
5860 }
5961 }
6062
61- //tablename = name
63+ /// <summary>
64+ /// Returns the SQL table name for the data set given by the yaml file.
65+ /// </summary>
66+ /// <returns>The SQL table name.</returns>
6267 public string GetTableName ( )
6368 {
6469 return this . dataSourceDescription . table_name ;
6570 }
6671
72+ /// <summary>
73+ /// Creates a comma seperated sting with all column names for the SQL table.
74+ /// </summary>
75+ /// <returns>The SQL column name string.</returns>
6776 public string GetHeaderString ( )
6877 {
78+ //if the string is not empty the result can be returned instantly
6979 if ( ! headerString . Equals ( "" ) )
7080 {
7181 return headerString ;
@@ -82,6 +92,11 @@ public string GetHeaderString()
8292 return headerString ;
8393 }
8494
95+ /// <summary>
96+ /// Reads a line from the csv file.
97+ /// </summary>
98+ /// <param name="nextLine">An output parameter that returns a line or an empty string.</param>
99+ /// <returns>A boolean indicating if the end of the file has been reached.</returns>
85100 public bool ReadLine ( out string nextLine )
86101 {
87102 string [ ] ? line ;
@@ -90,13 +105,6 @@ public bool ReadLine(out string nextLine)
90105
91106 while ( builder . Length == 0 )
92107 {
93- // Console.Write($"trying to write");
94-
95- // if (parser.EndOfData)
96- // {
97- // return false;
98- // }
99-
100108 line = parser . ReadFields ( ) ;
101109 if ( line == null )
102110 {
@@ -106,7 +114,6 @@ public bool ReadLine(out string nextLine)
106114
107115 if ( line . Length == 0 )
108116 {
109- //TODO what to do with empty lines
110117 Console . WriteLine ( "Line is empty" ) ;
111118 //Read next line
112119 nextLine = "" ;
@@ -116,12 +123,21 @@ public bool ReadLine(out string nextLine)
116123
117124 foreach ( var ( i , yamlIndex ) in columnIndexes )
118125 {
126+ //checks if the line has not enougth content for the expected yaml columns.
127+ if ( i >= line . Length )
128+ {
129+ Console . WriteLine ( "Line does not match the number of expected columns" ) ;
130+ //Read next line
131+ builder . Clear ( ) ;
132+ break ;
133+ }
134+
119135 //check if the value can be empty
120136 if ( dataSourceDescription . table_cols [ yamlIndex ] . is_not_nullable && line [ i ] == "" )
121137 {
122138 Console . WriteLine ( "Line does not match not null criteria" ) ;
123139 //Read next line
124- nextLine = "" ;
140+ builder . Clear ( ) ;
125141 break ;
126142 }
127143
@@ -130,20 +146,16 @@ public bool ReadLine(out string nextLine)
130146
131147 if ( columnTypes [ i ] == typeof ( string ) )
132148 {
133- // nextLine += $"'{line[i]}',";
134149 builder . Append ( $ "'{ line [ i ] } ',") ;
135150 continue ;
136151 }
137152
138- // nextLine += string.Format("{0},", Convert.ChangeType(line[i], columnTypes[i]));
139- // nextLine += $"{line[i]},";
140153 builder . Append ( $ "{ line [ i ] } ,") ;
141154 }
142155 }
143156
144157 builder . Length -- ; // this removes the last comma
145158
146- // nextLine = RemoveLastComma(nextLine);
147159 nextLine = builder . ToString ( ) ;
148160
149161 return true ;
0 commit comments