Well, most probably the answer depends on the query language 🙂
Here is the task:
Problem 3 – A query language over a CSV file
A CSV files (comma separated values) can be represented as a table.
For example this CSV file:
1 2 3 4 |
id, name, course 1, Rado, Haskell 2, Ivo, Python |
Can be represented by this table:
id | name | course |
---|---|---|
1 | Rado | Haskell |
We are going to use CSV files as tables and make a simple query language for fetching data.
In a language of your choice, make a program that:
- Reads a CSV file.
- Gives the user an interactive input for queries.
- Answers the queries with data from the CSV file.
The queries, that should be supported are:
SELECT [columns] LIMIT X
– where you can SELECT without giving the LIMIT. Then this will fetch all rows.SUM [column]
– returns the sum of all integers in the given column.SHOW
– returns a list of all column names in your data.FIND X
– returns all rows, that hasX
in some of their cells (Match X with at least one of the columns). IfX
is a string, search for every string, that containsX
as a substring.
Here are examples of all queries.
Consider that we have loaded the following CSV:
1 2 3 4 5 6 7 |
id,name,hometown 1,Kiara,Lunel 2,Mona,Henley-on-Thames 3,Kiayada,Villers-aux-Tours 4,Karly,Hillsboro 5,Igor,Oranienburg |
Now, let’s make queries:
1 2 3 4 5 6 7 8 9 |
query>SELECT id |id| |--| |1 | |2 | |3 | |4 | |5 | |
1 2 3 4 5 6 7 8 9 |
query> SELECT id, name |id| name | |--|--------| |1 |Kiara | |2 |Mona | |3 |Kiayada | |4 |Karly | |5 |Igor | |
1 2 3 4 5 |
query> SELECT id, name LIMIT 1 |id| name | |--|--------| |1 |Kiara | |
1 2 3 |
query> SUM id 15 |
1 2 3 |
query> SHOW id, name, hometown |
1 2 3 4 5 6 |
query> FIND "-" |id| name | hometown | |--|--------| ------------------| |2 |Mona | Henley-on-Thames | |3 |Kiayada | Villers-aux-Tours | |
Features
- If the query is non-valid – say it. Don’t crash the program.
- As you see in the examples, the results should be displayed in visual, tabular way. This is up to you. You don’t have to follow the styles of the example.
What I did? Check by yourself:
And my favourite – here comes the code 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Text.RegularExpressions; class CSV_query_language { public static string[] sCommandActivate; public static string[] sCommandDetails; public const string SHOW = "show"; public const string SUM = "sum"; public const string FIND = "find"; public const string SELECT = "select"; public const string LIMIT = "limit"; static void Main() { try { string sCommand = Console.ReadLine(); sCommandActivate = sCommand.Split('>'); sCommandDetails = sCommandActivate[1].Split(' '); if (sCommandDetails.Contains(SHOW)) { ShowHeader(); } if (sCommandDetails.Contains(SUM)) { SumValues(); } if (sCommandDetails.Contains(FIND)) { FindTheResults(); } if (sCommandDetails.Contains(SELECT)) { ShowMeWhatYouGotGeneral(); } Console.WriteLine("\nThank you for using the custom query language!\nPress Y for a new query."); string sNewQuery = Console.ReadLine(); if (sNewQuery == "Y" || sNewQuery == "y") { Main(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("\nAn error has occurred. Try again!"); Console.WriteLine("Possible queries:\n"); Console.WriteLine("query> show"); Console.WriteLine("query> sum id"); Console.WriteLine("query> select id, name, age limit 5"); Console.WriteLine("query> select id, experience"); Console.WriteLine("query> find \"-\""); Console.WriteLine("Please, enter a query and enjoy the result:\n"); Main(); } } static void ShowMeWhatYouGotGeneral() { string file1 = @"New Text Document.csv"; string[] allLines = File.ReadAllLines(file1); if (sCommandDetails.Contains(LIMIT)) { SetALimit(); } else { ShowMeWhatYouGot(); } } static void FindTheResults() { string file1 = @"New Text Document.csv"; string[] allLines = File.ReadAllLines(file1); int iLastValue = sCommandDetails.Length - 1; string sStringToSearch = sCommandDetails[iLastValue]; sStringToSearch = sStringToSearch.Replace("\"", ""); string sBeautiful = ""; Boolean first = true; int iItem = 0; string[] display; string sDisplayResult; string[] getColumnName = allLines[0].Split(','); sBeautiful = new String('*', 20 * allLines.Length - 1); Console.WriteLine(sBeautiful); for (int i = 0; i < getColumnName.Length; i++) { sDisplayResult = getColumnName[i]; Console.Write(string.Format("| {0,-20}", sDisplayResult)); } Console.WriteLine(); Console.WriteLine(sBeautiful); first = true; Boolean newLineNeeded = false; foreach (var item in allLines) { if (first) { first = false; continue; } if (item.Contains(sStringToSearch)) { iItem = item.Split(',').Length; display = item.Split(','); for (int i = 0; i < iItem; i++) { sDisplayResult = display[i]; Console.Write(string.Format("| {0,-20}", sDisplayResult)); newLineNeeded = true; } if (newLineNeeded) { Console.WriteLine(); } newLineNeeded = false; } } Console.WriteLine(sBeautiful); } static void ShowMeWhatYouGot() { int iItem = 0; string sDisplayResult = ""; Boolean first = true; string sBeautiful = ""; string[] display; string Translator2 = ""; string file1 = @"New Text Document.csv"; string[] allLines = File.ReadAllLines(file1); string sArrDisplay2 = String.Join(",", sCommandDetails, 1, sCommandDetails.Length - 1); string[] sArrDisplay = sArrDisplay2.Split(','); int iCountColumns = sCommandDetails.Length - 2; string[] getColumnName = allLines[0].Split(','); foreach (string item in allLines) { iItem = item.Split(',').Length; display = item.Split(','); for (int i = 0; i < iItem; i++) { if (sArrDisplay.Contains(display[i])) { Translator2 = i + "," + Translator2; } } } //Removing the word SHOW from the Translator2 Translator2 = Translator2.Remove(Translator2.Length - 1); foreach (string item in allLines) { sBeautiful = new String('*', 20 * Translator2.Length - 1); if (first) { Console.WriteLine(sBeautiful); } iItem = item.Split(',').Length; display = item.Split(','); for (int i = 0; i < iItem; i++) { if (Translator2.Contains(i.ToString())) { sDisplayResult = display[i]; Console.Write(string.Format("| {0,-20}", sDisplayResult)); } } Console.WriteLine(); if (first) { Console.WriteLine(sBeautiful); first = false; } } Console.WriteLine(sBeautiful); } static void ShowHeader() { string file1 = @"New Text Document.csv"; StreamReader sReader = new StreamReader(file1, Encoding.GetEncoding("Windows-1251")); Console.WriteLine(sReader.ReadLine()); } static void SumValues() { string file1 = @"New Text Document.csv"; string[] allLines = File.ReadAllLines(file1); int iLastValue = sCommandDetails.Length - 1; int iResult = -1; int iCounter = -1; string sColumnToWork = sCommandDetails[iLastValue]; string[] getColumnName = allLines[0].Split(','); foreach (var item in getColumnName) { iCounter++; if (item == sColumnToWork) { iResult = iCounter; } } int iCountColumns = allLines[0].Split(',').Length; string sGetColumnThatWeAreTalkingAboutName = getColumnName[iResult]; string sFirstLine = ""; //Setting the header to look like: 0,0,0,0... etc, in order to be excluded from the sum: if (iCountColumns > 1) { for (int i = 0; i < iCountColumns - 1; i++) { sFirstLine = sFirstLine + "0,"; } sFirstLine = sFirstLine + "0"; } else { sFirstLine = "0"; } allLines[0] = sFirstLine; IEnumerable strs = allLines; var columnQuery = from line in strs let elements = line.Split(',') select Convert.ToInt32(elements[iResult]); var results = columnQuery.ToList(); double dSum = results.Sum(); Console.WriteLine("\nThe sum of column [{0}] is {1:0.##}!", sGetColumnThatWeAreTalkingAboutName, dSum); } static void SetALimit() { Boolean first = true; int iItem = 0; string sDisplayResult = ""; string sBeautiful = ""; string Translator2 = ""; int z = -2; string file1 = @"New Text Document.csv"; string[] allLines = File.ReadAllLines(file1); string sArrDisplay2 = String.Join(",", sCommandDetails, 1, sCommandDetails.Length - 1); string[] sArrDisplay = sArrDisplay2.Split(','); int Limit = int.Parse(sArrDisplay[sArrDisplay.Length - 1])-1; sArrDisplay = sArrDisplay.Reverse().Skip(2).Reverse().ToArray(); int iCountColumns = sCommandDetails.Length - 2; string[] getColumnName = allLines[0].Split(','); string[] display; foreach (string item in allLines) { iItem = item.Split(',').Length; display = item.Split(','); for (int i = 0; i < iItem; i++) { if (sArrDisplay.Contains(display[i])) { Translator2 = i + "," + Translator2; } } } //We remove the last comma from the string here: Translator2 = Translator2.Remove(Translator2.Length - 1); foreach (string item in allLines) { z++; if (z > Limit) { break; } sBeautiful = new String('*', 20 * Translator2.Length - 1); if (first) { Console.WriteLine(sBeautiful); } iItem = item.Split(',').Length; display = item.Split(','); for (int i = 0; i < iItem; i++) { if (Translator2.Contains(i.ToString())) { sDisplayResult = display[i]; Console.Write(string.Format("| {0,-20}", sDisplayResult)); } } Console.WriteLine(); if (first) { Console.WriteLine(sBeautiful); first = false; } } Console.WriteLine(sBeautiful); } } |
Enjoy it.